A Polymorphic Pointer

Hello. I have posted here once before, but for those who may not know, my name is Adam and I am a Programming student. I am having issues with understanding why my code isn't working the way I need it to. I have read my text and read through the tutorial on this site, but still feel like I am missing something. I am not looking for the answer, but some help in the right direction.

I need to write a simple program with a class of both person and student, as well as a polymorphic pointer labeled pIndividual. I understand class and inheritance, but I am having trouble with using a pointer in this manner.

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#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 is the function show_stats() that is in class Person to 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 is the function show_stats() that is in class Student to show student1's " << endl;
     cout << "information by 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 = "Joe";
    person1.m_Address = "ABC Blvd.";
    person1.m_City = "Sunny Town";
    person1.m_State = "XX";
    person1.m_Zip = 34555;
    person1.m_Phone_Number = 1234567;
    
    person1.list_stats();
    
    Student student1(4.0);
    student1.m_Name = "Mac";
    student1.m_Address = "123 Four Drive.";
    student1.m_City = "Holiday";
    student1.m_State = "XX";
    student1.m_Zip = 12345;
    student1.m_Phone_Number = 7654321;
    student1.m_Grade = 'A';
    student1.m_Course = "Programming II";
    
    Person* pIndividual = new Student;
    pIndividual->list_stats();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


Everything compiles, but the output for student1 either doesn't show up or it is wrong. *sigh*

Thanks in advance for any guidance and/or insight.
Of course the output for student1 isn't showing up. You never ask it to show up (see lines 68-74). pIndividual is also not going to list anything, as it's empty. ;).

-Albatross
Yeah well, you're listing pIndividual's stats and not those of student1, so you shouldn't be surprised.
Instead of creating a new student, you probably should just assign &student1 to the pointer.
The Student object that pIndividual points to was never initialized. pIndividual is not pointing to student1 because you never told it to.
Albatross, Athar and filipe, thank you for your help. My code works now. I understand why the pointer wasn't pointing to what I wanted it to and why my syntax was wrong.
Topic archived. No new replies allowed.