#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    32
    Rep Power
    11

    Trouble with multiple Inheritance


    I'm trying to teach myself about inheritance and multiple inheritance. I've heard good and bad stuff about it, but i'd still like to learn how to do it.

    Anyway, I've almost figured it out. But for some reason, when the program is run, it doesn't run how I would expect.

    I have no clue what part of the code to post so, i'll post it all.

    Here's my .h file called "person.h"
    PHP Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <stdlib.h>

    using namespace std;

    class 
    fname
    {
    protected:
        
        
    string fname[15];
        
    string lname[15];
        
    int age;
        
    string language[15];
    public:
        
    void getInfo();
        
    void displayinfo();
    };

    void fname::getInfo()
    {
    int counter 0;
    for(
    counter =0counter 2counter++)
    {
        
    cout << "Enter first name: ";
        
    cin >> fname[counter];
    }
    };

    void fname::displayinfo()
    {
    int counter 0;
    for(
    counter 0counter 2counter++)
    {
        
    cout << "Here it is:" << endl;
        
    cout << "First name: " << fname[counter] << endl;
    }
    }

    class 
    lname:public fname
    {
    private:
        
    string lname[15];
    public:
        
    void getInfo();
        
    void displayLname();
    };

    void lname::getInfo()
    {
    int counter 0;
    for(
    counter 0counter 2counter++)
        {
        
    cout << "Enter last name: ";
        
    cin >> lname[counter];
        }
     };

    void lname::displayLname()
    {
    int counter 0;
    for(
    counter =0counter 2counter++)
        {
        
    cout << "Last Name: " << lname << endl;
        }
    }


    class 
    age:public fname
    {
    private:
        
    string age[15];
    public:
        
    void getInfo();
        
    void displayAge();
    };

    void age::getInfo()
    {
    int counter 0;
    for(
    counter 0counter 2counter++)
        {
        
    cout << "Enter age: ";
        
    cin >> age[counter];
        }
     };

    void age::displayAge()
    {
    int counter 0;
    for(
    counter =0counter 2counter++)
        {
        
    cout << "Age: " << age << endl;
        }
    }


    class 
    language:public fname
    {
    private:
        
    string language[15];
    public:
        
    void getInfo();
        
    void displayLanguage();
    };

    void language::getInfo()
    {
    int counter 0;
    for(
    counter 0counter 2counter++)
        {
        
    cout << "Enter language: ";
        
    cin >> language[counter];
        }
    };

    void language::displayLanguage()
    {
    int counter 0;
    for(
    counter =0counter 2counter++)
        {
        
    cout << "language: " << language << endl;
        }

    And here's my main cpp file. I'm trying to keep it as small as possible and use the .h file as much as possible.
    PHP Code:
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include <person.h>

    using namespace std;


    int main()
    {
      
    fname mfname;
      
    lname mlname;
      
    age mage;
      
    language mlanguage;
      
    mfname.getInfo();
      
    mlname.getInfo();
      
    mage.getInfo();
      
    mlanguage.getInfo();
      
      
    mfname.displayinfo();
      
    mlname.displayLname();
      
    mage.displayAge();
      
    mlanguage.displayLanguage();
      return 
    1;

    Now, when I run it, it asks for first name, then it asks first name again. Then it asks for last name, then last name again. then age, then age again...you get the idea. It's asking for each part twice and then prints it out like this.

    First name: Bob
    First name: James
    Last name: Newheart
    Last name: Hetfield
    age: 70
    age: 50
    language: English
    language: English

    But I want it to print out like this

    First name: Bob
    Last name: Newheart
    age: 70
    language: English

    First name: James
    Last name: Hetfield
    age: 50
    language: English

    Any help anyone?

    Thanks in advance.
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    Code:
    for(counter =0; counter < 2; counter++)
    
    {
    
        cout << "Enter first name: ";
    
        cin >> fname[counter];
    
    }
    see the problem? all of your loops do that.

IMN logo majestic logo threadwatch logo seochat tools logo