Consider that a school "ABC" hired you as a programmer to maintain the record of its Students and Teachers.
You need to develop a system in C++ to enter the data and show it as and when required. Keeping in the view above scenario, you are required to write a C++ program that can display the information of any specific Teacher or the Student. It will contain following three classes: 1. Person 2. Teacher 3. Student 4. where a Person should be the base class and Teacher and Student should be its derived classes. You are required to use the concept of Polymorphism to generate the sample output. Consider following class table for above scenario. Class Data Members Member Functions Person • Name • Age • Gender • Address • Phone • Default Constructor • Setter and getter functions for all the data member. • A function to save information of a person • A function to display information of a person Teacher • Qualification • Salary • Default Constructor • Setter and getter functions for all the data members • A function to save information of a Teacher • A function to display information of a Teacher. Student • Previous Education • Obtained marks • Total Marks • Default Constructor • Setter and getter functions for all the data members • A function to save information of a Student. • A function to display information of a Student. Solution Guidelines: Ø Create three classes: Person as a base class and Teacher and Student as derived classes. Ø You have to create the default constructor for each class. Ø You have to create the setter and getter functions for each attribute of Person class. Ø You have to create Save_Information function for each class as mentioned in the table. Ø You have to create Display_Information function for each class as mentioned in the table. Ø You have to use concept of Polymorphism to generate the sample output. Sample Output:
Answer
C++ code
#include <iostream>
#include <fstream>
using namespace std;
// Base class
class Person
{
private:
string name;
int age;
string gender;
string address ;
string phone ;
public:
Person(){
}
Person(string n,int a,
string g, string add,string ph){
name = n;
age = a;
gender = g;
address = add;
phone = ph;
}
// Setter
void setname(string nam) {
name = nam;
}
void setage(int a) {
age = a;
}
void setgender(string gen) {
gender = gen;
}
void
setaddress(string add) {
address = add;
}
void
setphone(string p ) {
phone = p;
}
// Getter
string getname() {
return name ;
}
int getage() {
return age ;
}
string getgender() {
return gender ;
}
string
getaddress() {
return address ;
}
string
getphone( ) {
return phone ;
}
void Display(){
cout
<<"Displaying Patient information:\n" ;
cout <<"Name:
" << getname()<< "\n";
cout <<"age :
" << getage() << "\n";
cout <<"gender :
" <<getgender() << "\n";
cout <<"address :
" << getaddress() << "\n";
cout <<"phone :
" << getphone() << "\n";
}
void Save(){
ofstream
MyFile("Person.txt");
// Write to the file
MyFile
<<"Displaying Patient information:\n" ;
MyFile
<<"Name: " << getname()<<
"\n";
MyFile <<"age :
" << getage() << "\n";
MyFile <<"gender
: " <<getgender() << "\n";
MyFile <<"address
: " << getaddress() << "\n";
MyFile <<"phone :
" << getphone() << "\n";
// Close the file
MyFile.close();
}
};
// Sub class inheriting from Base
Class(Parent) Teacher 3. Student 4. where
class Teacher : public Person
{
private:
string Qualification ;
double salary;
public:
Teacher(){
}
Teacher(string n,int a, string g,
string add,string ph ,string Qualif, double sal): Person(n,
a, g, add, ph) {
Qualification
= Qualif;
salary = sal;
}
// Setter
void setQualification(string
Qualif) {
Qualification =
Qualif;
}
void setsalary(int s) {
salary = s;
}
// Getter
string getQualification() {
return
Qualification ;
}
int getsalary() {
return salary ;
}
void Display(){
cout
<<"Displaying Patient information:\n" ;
cout <<"Teacher
Name: " << getname()<< "\n";
cout <<"Teacher
age : " << getage() << "\n";
cout <<"Teacher
gender : " <<getgender() << "\n";
cout <<"Teacher
address : " << getaddress() << "\n";
cout <<"Teacher
phone : " << getphone() << "\n";
cout <<"Teacher
Qualification : " << getQualification() <<
"\n";
cout <<"Teacher
salary : " << getsalary() << "\n";
}
void Save(){
ofstream
MyFile("Teacher.txt");
MyFile <<"Displaying
Patient information:\n" ;
MyFile <<"Teacher
Name: " << getname()<< "\n";
MyFile <<"Teacher
age : " << getage() << "\n";
MyFile <<"Teacher
gender : " <<getgender() << "\n";
MyFile <<"Teacher
address : " << getaddress() << "\n";
MyFile <<"Teacher
phone : " << getphone() << "\n";
MyFile <<"Teacher
Qualification : " << getQualification() <<
"\n";
MyFile <<"Teacher
salary : " << getsalary() << "\n";
// Close the file
MyFile.close();
}
};
class Student : public Person
{
private:
string previousEduction ;
int obtainedmarks;
int totalmarks;
public:
Student(){
}
Student(string n,int a, string g,
string add,string ph ,string previ, int obtainedma , int totalma ):
Person(n, a, g, add, ph) {
previousEduction
= previ;
obtainedmarks
= obtainedma;
totalmarks =
totalma;
}
// Setter
void
setpreviousEduction(string previ) {
previousEduction
= previ;
}
void setobtainedmarks(int o)
{
obtainedmarks =
o;
}
void settotalmarks(int t) {
totalmarks = t;
}
// Getter
string getpreviousEduction() {
return
previousEduction ;
}
int getobtainedmarks() {
return obtainedmarks ;
}
int gettotalmarks() {
return totalmarks ;
}
void Display(){
cout
<<"Displaying Patient information:\n" ;
cout <<"Student
Name: " << getname()<< "\n";
cout <<"Student
age : " << getage() << "\n";
cout <<"Student
gender : " <<getgender() << "\n";
cout <<"Student
address : " << getaddress() << "\n";
cout <<"Student
phone : " << getphone() << "\n";
cout <<"Student
previous Eduction : " << getpreviousEduction() <<
"\n";
cout <<"Student
obtained marks : " << getobtainedmarks() << "\n";
cout <<"Student
total marks : " << gettotalmarks() << "\n";
}
void Save(){
ofstream
MyFile("Student.txt");
MyFile <<"Displaying
Patient information:\n" ;
MyFile <<"Student
Name: " << getname()<< "\n";
MyFile <<"Student
age : " << getage() << "\n";
MyFile <<"Student
gender : " <<getgender() << "\n";
MyFile <<"Student
address : " << getaddress() << "\n";
MyFile <<"Student
phone : " << getphone() << "\n";
MyFile <<"Student
previous Eduction : " << getpreviousEduction() <<
"\n";
MyFile <<"Student
obtained marks : " << getobtainedmarks() << "\n";
MyFile <<"Student
total marks : " << gettotalmarks() << "\n";
// Close the file
MyFile.close();
}
};
int main() {
// variables
string name;
int age;
string gender;
string address ;
string phone ;
// for teacher
string Qualification ;
double salary;
// for student
string previousEduction ;
int obtainedmarks;
int totalmarks;
string options ;
string YesorNo ;
while(true){
cout <<"Enter Choice : T for
Teacher , S for Student :\n" ;
cin >> options ;
if (options == "T"){
cout <<"Enter following Data
for Teacher :\n";
cout <<"Enter Name : " ;
cin >> name ;
cout <<"Enter Gender: "
;
cin >> gender ;
cout <<"Enter Age : " ;
cin >> age ;
cout <<"Enter Address : "
;
cin >> address ;
cout <<"Enter Phone number : "
;
cin >> phone ;
cout <<"Enter Qualification :
" ;
cin >> Qualification ;
cout <<"Enter Salary : "
;
cin >> salary ;
Teacher teacher(name ,age ,gender , address
,phone ,Qualification , salary );
cout <<"Do you want to enter more
data< Y for Yes , N for no ): " ;
cin >> YesorNo;
if( YesorNo == "Y")
teacher.Display();
else
break;
}
if(options == "S"){
cout
<<"Enter following Data for Student :\n";
cout <<"Enter Name : " ;
cin >> name ;
cout <<"Enter Gender: "
;
cin >> gender ;
cout <<"Enter Age : " ;
cin >> age ;
cout <<"Enter Address : "
;
cin >> address ;
cout <<"Enter Phone number : "
;
cin >> phone ;
cout <<"Enter Previous Eduction :
" ;
cin >> previousEduction ;
cout <<"Enter Obtained Marks :
" ;
cin >> obtainedmarks ;
cout <<"Enter Total marks : "
;
cin >> totalmarks ;
Student student(name ,age ,gender ,
address ,phone ,previousEduction , obtainedmarks , totalmarks );
cout <<"Do you want to enter
more data< Y for Yes , N for no ): " ;
cin >> YesorNo;
if( YesorNo == "Y")
student.Display();
else
break;
}
}// end loop
return 0;
}