mardi 29 novembre 2016

Trying to print out values from an instantiated class in C++. Getting a random value as a result

So I'm doing a lab for one of Microsoft's courses on EDX, and they wanted me to create a class constructor called, "Person", and then instantiate an object using the constructor and print out the values. The issue I'm having, is that when I try to to print out the values, I'm getting the common, "random memory", value that you get when you haven't given a variable any data, and can't figure out why I'm getting this issue. PLEASE help!

Here's the code from my main() function, instantiating and printing out the values.

<pre>
      Person *pPerson = new Person("Bob", "Schwimmer", 49, 57, 201);
      cout << "His name is: " << pPerson->GetFirstName() << " " << pPerson->GetLastName() << endl;
      cout << "He is " << pPerson->GetAge() << endl;
      cout << "He weighs (in lbs) " << pPerson->GetWeight() << endl;
      cout << "He is " << pPerson->GetHeight() << " feet tall." << endl;
<code>

And here's my class constructor:

    class Person
{
private:
    string firstName;
    string lastName;
    int age;
    int height;
    int weight;


public: Person(string fName, string lName, int age, int height, int weight) { fName = firstName; lName = lastName; age = age; height = height; weight = weight; } ~Person() { cout << "Deconstructor has been called" << endl; } string GetFirstName() { return this->firstName; }; string GetLastName() { return this->lastName; }; int GetAge() { return this->age; }; int GetHeight() { return this->height; }; int GetWeight() { return this->weight; }; void SetFirstName(string fName) { fName = this->firstName; };

};

Aucun commentaire:

Enregistrer un commentaire