0

My class extPersonType is inherited from 3 other classes. The program compiles with no errors, but for some reason the string relation and phoneNumber do not show up. All the other information I ask for does. Where is my problem?

class extPersonType: public personType, public dateType, public addressType
{
public:
extPersonType(string relation = "", string phoneNumber = "", string address = "", string city = "", string state = "", int zipCode = 55555, string first = "", string last = "", 
    int month = 1, int day = 1, int year = 0001)
    : addressType(address, city, state, zipCode),  personType(first, last), dateType (month, day, year)
{
}
void print() const;

private:
string relation; 
string phoneNumber;
};

void extPersonType::print() const
{
cout << "Relationship: " << relation << endl;
cout << "Phone Number: " << phoneNumber << endl;
addressType::print();
personType::print();
dateType::printDate();
}



/*******
MAIN PROGRAM
*******/

int main()
{
extPersonType my_home("Friend", "555-4567", "5142 Wyatt Road", "North Pole", "AK", 99705, "Jesse", "Alford", 5, 24, 1988);
my_home .extPersonType::print();
      return 0;
}
1
  • 2
    Just as an aside, using multiple inheritance is probably not how you want to model the relationship Commented Oct 9, 2012 at 6:49

3 Answers 3

3

That's because you don't initilize them anywhere

    extPersonType(string relation = "", string phoneNumber = "", string address = "", string city = "", string state = "", int zipCode = 55555, string first = "", string last = "", int month = 1, int day = 1, int year = 0001)
        : relation (relation), phoneNumber (phoneNumber)// <<<<<<<<<<<< this is missing
           addressType(address, city, state, zipCode),  personType(first, last), dateType (month, day, year)
{
}

You should not forget to assign/initilize your variables in the constructor

Also, this is recommandation but I don't really think inheritance is necessary here. You should use composition.

class extPersonType
{
 private:
   string relation; 
   string phoneNumber;

   addressType address;
   personType person_name;
   dateType date; // birthday ?
}
Sign up to request clarification or add additional context in comments.

2 Comments

That was my problem! Thank you!!!! I did in the other classes... but not this one for some reason. Now I feel dumb. haha. Thanks!
@JesseAlford Feeling dumb is a good sign, seek it out everywhere, and try to understand it.
1

You should call it as

my_home.print();

You are probably confused by the way it is declared:

void extPersonType::print(){ <..> }

Here the extPersonType:: part just tells the compiler that the funciton is a part of the class. When you call the function, you already call it for a specific object of the class (in your case, my_home), so you shouldn't use the class name.

1 Comment

ok. The only reason I was using the class name was because the super classes or base classes have the exact same function within them. I thought I had to explicitly call this classes print funtion so it would run this print function and not a different one. Thanks.
1

You aren't actually initializing your class member variables. You need to do something like the following to initialize the relation and phoneNumber members:

extPersonType(string relation = "", string phoneNumber = "", string address = "", 
    string city = "", string state = "", int zipCode = 55555, string first = "", string last = "", 
    int month = 1, int day = 1, int year = 0001)
    : addressType(address, city, state, zipCode),  personType(first, last), dateType (month, day, year),
      relation(relation), phoneNumber(phoneNumber)  // <== init mmebers
{
}

I suspect that you may need to do something similar with the addressType, personType, and dateType base class constructors as well.

1 Comment

Amazing! That was exactly my problem. Thank you! All the other class member variables I initialized. For some reason this slipped past me. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.