inheretence

This is a discussion on inheretence within the C++ Programming forums, part of the General Programming Boards category; Hi everyone! I have the following program: #include<iostream> using namespace std; using std::cout; using std::cin; using std::endl; class student { ...

  1. #1
    Unregistered
    Guest

    Unhappy inheretence

    Hi everyone!
    I have the following program:
    #include<iostream>
    using namespace std;
    using std::cout;
    using std::cin;
    using std::endl;


    class student
    {

    private:
    char name[100];
    char address[100];
    char city[100];
    char state[100];
    char zip[100];
    public:
    student(){};
    student( char name,char address,char city,char state,char zip);
    void getdata()
    {
    cout<<"Enter name:"; cin.getline(name,100,'\n');
    cout<<"Enter address :"; cin.getline(address,100,'\n');
    cout<<"Enter city:"; cin.getline(city,100,'\n');
    cout<<"Enter state:"; cin.getline(state,100,'\n');
    cout<<"Enter zip:"; cin.getline(zip,100,'\n');
    }
    void showdata()
    {
    cout<<"NAME IS:"<<name<<endl;
    cout<<"ADDRESS IS:"<<address<<endl;
    cout<<"CITY IS:" <<city<<endl;
    cout<<"STATE IS :"<<state<<endl;
    cout<<"ZIP IS :"<<zip<<endl;
    }
    };

    int main()

    {
    student Student;

    Student. getdata();
    Student. showdata();



    return 0;
    };

    now if I have
    class Person
    {
    public:
    Person();
    ~Person();
    private:
    char name[100];
    char address[100];
    char city[100];
    char state[3];
    char zip[20];
    };

    how would we write a student class that publicly inherits from Person class?
    class Student : public Person
    {
    public:
    Student();
    ~Student();
    private:
    charcampus[100];
    char creditcardnum[100];
    char creditcardtype[100];
    };

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Yep, you had it right. To declare a class that public intherits from Person is as follows:
    Code:
    class Student : public Person
    {
      public:
        Student();
        ~Student();
      private:
        charcampus[100];
        char creditcardnum[100];
        char creditcardtype[100];
    };
    Also, in your person class, you may want to make any data you want to be accessed through student protected instead of private. For example all of your character arrays in Person should be declared as protected: if you need them in the student class.

  3. #3
    Unregistered
    Guest

    Thumbs up

    Thanks ! Iwill post the completed program please coment when it is posted.

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    please use code tags. if in question, read the top posts of this thread, the one with sticky before them.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21