C++ Polymorphism need assistance -


i had take inheritance between class person , class student , write test program using polymorphic pointer pindividual. program compiles isn't listing student1 stats me.

here code:

    #include <iostream>     #include <string>      using namespace std;      class person     {     public:          string m_name, m_address, m_city, m_state;          int m_zip, m_phone_number;           void virtual list_stats();     };       void person::list_stats()      {            cout << "this function show_stats() in class person show   person1's " << endl;           cout << "information:" << endl << endl;           cout << "name: " << m_name << endl << "address: " << m_address << endl << "city: " << m_city << endl;            cout << "state: " << m_state << endl << "zip: " << m_zip << endl << "phone number: " << m_phone_number << endl << endl;       }        class student : public person       {       public:             char m_grade;             string m_course;             float m_gpa;             void virtual list_stats();             student(float gpa = 4.0);       };        student::student(float gpa)       {            m_gpa = gpa;       }        void student::list_stats()        {           cout << "this function show_stats() in class student show student1's " << endl;           cout << "information using pointer pindividual:" << endl << endl;           cout << "name: " << m_name << endl << "address: " << m_address << endl << "city: " << m_city << endl;           cout << "state: " << m_state << endl << "zip: " << m_zip << endl << "phone number: " << m_phone_number << endl << endl;    }      int main()       {           person person1;           person1.m_name = "sarah";           person1.m_address = "abc blvd.";           person1.m_city = "sunnytown";           person1.m_state = "fl";           person1.m_zip = 34555;           person1.m_phone_number = 1234567;            person1.list_stats();            student student1(4.0);           student1.m_name = "todd";           student1.m_address = "123 4 dr.";           student1.m_city = "anytown";           student1.m_state = "tx";           student1.m_zip = 12345;           student1.m_phone_number = 7654321;           student1.m_grade = 'a';           student1.m_course = "programming";             person* pindividual = new student;           pindividual->list_stats();            system("pause");           return exit_success;      } 

because creating another instance of student new. default constructed instance not have data set. need:

person* pindividual = &student1; 

to pointer student1 created , see data when calling list_stats().


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -