0

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.

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;

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; };
};
gmiley
  • 6,531
  • 1
  • 13
  • 25
Tobymac208
  • 27
  • 5
  • If Microsoft is teaching you to write `Person *pPerson = new Person`, then *shame on them*. There is absolutely no reason for dynamic allocation here. This is not Java. – Cody Gray - on strike Nov 30 '16 at 00:50
  • 1
    Anyway, the obvious issue is that you are incorrectly initializing the members inside of the `Person` constructor. Looks like a simple typo. Instead of setting the `firstName` member to the `fName` parameter, you're doing the opposite. Do consider using an [initializer list](http://stackoverflow.com/questions/926752/why-should-i-prefer-to-use-member-initialization-list). – Cody Gray - on strike Nov 30 '16 at 00:52
  • I just noticed that Cody! Thank you. – Tobymac208 Nov 30 '16 at 00:53
  • Microsoft is simply showing me that I can use this method for memory management to avoid memory leaks. This was just a practice program for a unit I am finishing up. – Tobymac208 Nov 30 '16 at 00:54
  • Well, you wouldn't have a memory management problem is you just created the object on the stack. :-) And while it's good to know that manual memory management exists, I certainly hope that either they are teaching you or you already know about smart pointers, in the unlikely case that you *do* need to deal with raw dynamically allocated memory. – Cody Gray - on strike Nov 30 '16 at 00:58
  • @Tobymac208 in your contructor use `this->age` to get or set the member field. The parameter `age` from your constructor override the cope of your member fields. !Same for age,width and height. – al-eax Nov 30 '16 at 03:18
  • @CodyGray I was unaware of smart pointers, but am very intrigued by them now that I've looked them up! Thanks again! – Tobymac208 Nov 30 '16 at 04:12

1 Answers1

0

The order in your constructor is not correct: fName = firstName; should have been firstName = fName; and so on...

koskot77
  • 36
  • 4