In the Module 06 Programming Assignment (Inheritance Proved) you created a progr
ID: 3766403 • Letter: I
Question
In the Module 06 Programming Assignment (Inheritance Proved) you created a program for a class Person and a class Student. Reuse those two classes for this assignment and build upon them as follows:
Add another class Professional that inherits the Person class and overrides the display() method.
Enhance all three classes to use Polymorphism on the display() method.
In the main() method, use a polymorphic pointer variable pIndividual of type Person to call the corresponding display()method of all the three classes.
Code so far is:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
//inheritence example
class Person
{//parent class
public://data member with public acces specifier
string name;
string address;
string city;
string state;
int zip;
int phonenumber;
public:
Person()//default constructor
{
name = "john";
address = "1234 street";
city = "Minneapolis";
state = "Minnesota";
zip = 12345;
phonenumber = 1234567890;
}
public:
void display()
{
//default function display
cout << "default display function in person" << endl;
cout << name << endl;
cout << address << endl;
cout << city << endl;
cout << state << endl;
cout << zip << endl;
cout << phonenumber << endl;
}
void display1(string name, string address, string city, string state, int zip, int phonenumber)
{
//function with arguments
cout << " display function with parameters in person" << endl;
cout << name << endl;
cout << address << endl;
cout << city << endl;
cout << state << endl;
cout << zip << endl;
cout << phonenumber << endl;
}
};
/* Studnet class is derived from base class Person. */
class Student : public Person
{
public:
string grade;
string course;
float gpa;
public:
Student()
{//default constructor
grade = "A";
course = "Class";
gpa = 4.0;
}
public:
void display()
{//default constructor
cout << "default display function in student" << endl;
cout << name << endl;
cout << address << endl;
cout << city << endl;
cout << state << endl;
cout << zip << endl;
cout << phonenumber << endl;
cout << grade << endl;
cout << course << endl;
cout << gpa << endl;
}
void display1(string grade, string course, float gpa)
{//constructor with arguments
cout << " display function with argument in student" << endl;
cout << name << endl;
cout << address << endl;
cout << city << endl;
cout << state << endl;
cout << zip << endl;
cout << phonenumber << endl;
cout << grade << endl;
cout << course << endl;
cout << gpa << endl;
}
};
int main()
{
Person p;
p.display();
p.display1("Matt", "4114 James Ave", "New Hope", "Minnesota", 133444, 14444445);
Student s;
s.display();
s.display1("B", "Programming", 3.0);
system("pause");
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
//inheritence example
class Person
{//parent class
public://data member with public acces specifier
string name;
string address;
string city;
string state;
int zip;
int phonenumber;
public:
Person()//default constructor
{
name = "john";
address = "1234 street";
city = "Minneapolis";
state = "Minnesota";
zip = 12345;
phonenumber = 1234567890;
}
public:
virtual void display()
{
//default function display
cout << "default display function in person" << endl;
cout << name << endl;
cout << address << endl;
cout << city << endl;
cout << state << endl;
cout << zip << endl;
cout << phonenumber << endl;
}
/* void display1(string name, string address, string city, string state, int zip, int phonenumber)
{
//function with arguments
cout << " display function with parameters in person" << endl;
cout << name << endl;
cout << address << endl;
cout << city << endl;
cout << state << endl;
cout << zip << endl;
cout << phonenumber << endl;
}*/
};
/*Professional class is derived from base class Person*/
class Professional : public Person
{
public:
string designation;
float salary;
public:
Professional()
{//default constructor
designation = "Professor";
salary = 3000.00;
}
void display()
{//default constructor
cout<<"default display function in Professional" << endl;
cout<<name<<endl;
cout<<address<<endl;
cout<<city<<endl;
cout<<state<<endl;
cout<<zip<<endl;
cout<<phonenumber<<endl;
cout<<designation<<endl;
cout<<salary<<endl;
}
/* void display1(string designation, float salary)
{
//constructor with arguments
cout << " display function with argument in Professional" << endl;
cout << name << endl;
cout << address << endl;
cout << city << endl;
cout << state << endl;
cout << zip << endl;
cout << phonenumber << endl;
cout << designation << endl;
cout << salary << endl;
} */
};
/* Studnet class is derived from base class Person. */
class Student : public Person
{
public:
string grade;
string course;
float gpa;
public:
Student()
{//default constructor
grade = "A";
course = "Class";
gpa = 4.0;
}
public:
void display()
{//default constructor
cout << "default display function in student" << endl;
cout << name << endl;
cout << address << endl;
cout << city << endl;
cout << state << endl;
cout << zip << endl;
cout << phonenumber << endl;
cout << grade << endl;
cout << course << endl;
cout << gpa << endl;
}
/*void display1(string grade, string course, float gpa)
{//constructor with arguments
cout << " display function with argument in student" << endl;
cout << name << endl;
cout << address << endl;
cout << city << endl;
cout << state << endl;
cout << zip << endl;
cout << phonenumber << endl;
cout << grade << endl;
cout << course << endl;
cout << gpa << endl;
}*/
};
int main()
{
Person *pIndividual;
Person pr;
Student st;
Professional pf;
pIndividual = ≺
cout<<"Person object: "<<endl;
pIndividual->display();
cout<<endl<<endl;
pIndividual = &st;
cout<<"Student object: "<<endl;
pIndividual->display();
cout<<endl<<endl;
pIndividual = &pf;
cout<<"Professional object: "<<endl;
pIndividual->display();
cout<<endl<<endl;
return 0;
}