Using the following classes: class students{ private: string name; string phoneN
ID: 641245 • Letter: U
Question
Using the following classes:
class students{
private:
string name;
string phoneNumber;
int studentNumber;
double gpa;
public:
students()
{
name = "student";
phoneNumber = "0000000000";
studentNumber = 0;
gpa = 0.00;
}
students(string n, string pn, int sn, double g){
name = n;
phoneNumber = pn;
studentNumber = sn;
gpa = g;
}
void displayValues()
{
cout << "Name: " << name << endl;
cout << "Phone Number: " << phoneNumber << endl;
cout << "Student Number: " << studentNumber << endl;
cout<<std::fixed<<"GPA: "<<std::setprecision(2)<<gpa<<endl;
}
};
int main(){
students student1;
students student2("Sample", "123456789", 1, 4.0 );
cout << "Student 1: " << endl;
student1.displayValues();
cout << endl << "Student 2: " << endl;
student2.displayValues();
}
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.
Explanation / Answer
class Person{
private:
string firstname;
string lastname;
int age;
public:
Person()
{
firstname ="FirstName";
lastname = "LastName";
age = 0;
}
Person(string fn, string ln, int a)
{
firstname = fn;
lastname = ln;
age = a;
}
void display()
{
cout << "Person class Details :" <<endl;
cout<<"FirstName"<<firstname<<endl;
cout<<"LastName"<<lastname<<endl;
cout<<"Age"<<age<<endl;
return 0;
}
};
class Professor: public Person{
private:
string department;
public:
Professor():Person() { }
Professor(string firstname, string lastname, int age, string dept): Person(firstname, lastname, age)
{
department = dept;
}
void display()
{
cout << "Professor class Details :" <<endl;
cout<<"FirstName"<<firstname<<endl;
cout<<"LastName"<<lastname<<endl;
cout<<"Age"<<age<<endl;
cout<<"Department"<<department<<endl;
return 0;
}
};
class students{
private:
string firstname;
string lastname;
int age;
string phoneNumber;
int studentNumber;
double gpa;
public:
Student():Person()
{
firstname = "student";
lastname ="";
age = 0;
phoneNumber = "0000000000";
studentNumber = 0;
gpa = 0.00;
}
students(string fname,string lname, int age, string pn, int sn, double g): Person(fname, lname,age){
phoneNumber = pn;
studentNumber = sn;
gpa = g;
}
void displayValues()
{
cout << "Name: " << firstname << endl;
cout << "Phone Number: " << phoneNumber << endl;
cout << "Student Number: " << studentNumber << endl;
cout<<std::fixed<<"GPA: "<<std::setprecision(2)<<gpa<<endl;
}
void display()
{
cout << "Student class Details :" <<endl;
cout<<"FirstName"<<firstname<<endl;
cout<<"LastName"<<lastname<<endl;
cout<<"Age"<<age<<endl;
cout << "Phone Number: " << phoneNumber << endl;
cout << "Student Number: " << studentNumber << endl;
cout<<std::fixed<<"GPA: "<<std::setprecision(2)<<gpa<<endl;
}
};
int main(){
Person *pIndividual;
Professor prof("John", "Kennedy",40,"CS");
Student stu("James","Weickie",20,123432143,1,4.8);
// store the address of Professor
pIndividual= &prof;
// call display() of Professor class.
pIndividual->display();
// store the address of Student Class
pIndividual = &stu;
// call display() of Student class.
pIndividual->display();
}
Expected Output:
Person Class Details:
FirstName: John
LastName:Kennedy
Age: 40
Person Class Details
FirstName: James
LastName:Weickei
Age:20
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.