programing C++ Your programming assignment in this Module has three parts. Follo
ID: 3763095 • Letter: P
Question
programing C++
Your programming assignment in this Module has three parts. Follow the instructions below:
Create a class Person, which has a name, address, city, state, zip, and phone number. Create a default constructor to initialize the variables to some default values. DO NOT get them as an input from the user. If you prefer non-default constructors, then you can have them, but that is optional. Have a "display()" function to display the values.
Create a class Student, which inherits from Person, and has additionally grade, course, and GPA. Create a default constructor to initialize the three variables to some default values. DO NOT get them as an input from the user. If you prefer non-default constructors, then you can have them, but that is optional. Have a "display()" function to display the values. From the Student "display()" function call the Person "display()" function.
In the "main()", create an object of Person. Display its values. Then, create an object of Student. Display its values. This would display all the variables from both Person and Student.
Add plenty of narrative comments.
Your program must be compilable and executable.
Remember, all these are to prove that your inheritance is working. So, make sure your Inheritance is working.
Explanation / Answer
Solution.cpp
#include <iostream>
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="mike";
address="fridaynights";
city="NewYork";
state="london";
zip=23333;
phonenumber=4432223;
}
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="IT";
gpa=9.8;
}
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("john","CROME","LOSANGELS","WASHINGTON",133444,14444445);
Student s;
s.display();
s.display1("B","EEE",9.1);
return 0;
}
output
default display function in person
mike
fridaynights
NewYork
london
23333
4432223
display function with parameters in person
john
CROME
LOSANGELS
WASHINGTON
133444
14444445
default display function in student
mike
fridaynights
NewYork
london
23333
4432223
A
IT
9.8
display function with argument in student
mike
fridaynights
NewYork
london
23333
4432223
B
EEE
9.1
Exit code: 0 (normal program termination)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.