Answer to Question #207459 in C++ for osama

Question #207459

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. 


1
Expert's answer
2021-06-15T15:37:03-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS