Create a class named Person, which contains
• A pure virtual function named print()
• Two data fields i.e. personName and age
A class named Student inherits Person class, which contains
• Two data fields i.e. Std_id and Cgpa
• Overridden function print() to display all details relevant to a patient
A class named Regular inherited from class Student which holds
• A data field representing the name of School
• A data filed representing the Fee
• Overridden function print() to display all details relevant to a Regular student
A class named Private inherited from class Student which holds
• A data field representing the address
• A data filed representing the Fee
• Overridden function print() to display all details relevant to a private Student.
In the main function, create instances of derived classes to access respective print() function using dynamic binding.
#include <iostream>
#include <string>
using namespace std;
class Person{
protected:
string personName;
int age;
public:
Person(string n, int ag): personName(n), age(ag){}
virtual void print(){}
};
class Student: public Person{
protected:
int std_id;
float cgpa;
public:
Student(string n, int ag, int id, float gpa):
Person(n, ag), std_id(id), cgpa(gpa){}
void print(){
cout<<"Name: "<<personName<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Student ID: "<<std_id<<endl;
cout<<"CGPA: "<<cgpa<<endl;
}
};
class Private: public Student{
string address;
float fee;
public:
Private(string n, int ag, int id, float gpa, string addr, float f):
Student(n, ag, id, gpa), address(addr), fee(f){}
void print(){
cout<<endl;
Student::print();
cout<<"Address: "<<address<<endl;
cout<<"Fee: "<<fee<<endl;
}
};
int main(){
Student Mark("Mark", 18, 23242, 4.8);
Mark.print();
Private John("John", 21, 1232, 3.2, "818 Milk Street", 3000);
John.print();
return 0;
}
Comments
Leave a comment