Answer to Question #283796 in C++ for ibrahim

Question #283796

Write a Person class that has attributes of id, name and address. It has a constructor to initialize, a member function to input and a member function to display data members. Create second class Student that inherits Person class. It has additional data members of roll number and marks. It also has member functions to input and display its data members. Create another class Scholarship that inherits Students class. It has additional attributes of scholarship name and amount. It also has member functions to input and display its data members.


1
Expert's answer
2021-12-31T08:59:46-0500
#include <iostream>
#include <string>
class Person
{
protected:
    int id;
    std::string name;
    std::string address;
public:
    Person()
    {
        std::cout<<"Person c"<<std::endl;
    }
    Person(int i,const std::string& n,const std::string& a) : id(i), name(n), address(a){}
    void input()
    {
        std::cin>>id>>name;
        std::cin.ignore();
        std::getline(std::cin, address);
    }
    void display()
    {
        std::cout<<id<<'\t'<<name<<'\n'<<address<<'\n';
    }
};
class Student : public Person
{
protected:
    int roll_number;
    int mark;
public:
    Student()
    {
        std::cout<<"Student c"<<std::endl;
    }
    Student(int i, const std::string& n, const std::string& a, int r_n, int m): Person(i, n, a)
    {
        this->roll_number = r_n;
        this->mark = m;
    }
    void input()
    {
        Person::input();
        std::cin>>roll_number>>mark;
    }
    void display()
    {
        Person::display();
        std::cout<<roll_number<<'\t'<<mark<<std::endl;
    }
};
class Scholarship: public Student
{
protected:
    std::string scholarship_name;
    int amount;
public:
    Scholarship(int i, const std::string& n, const std::string& a, int r_n, int m, const std::string& sch_n, int am): Student(i, n, a, r_n, m)
    {
        this->amount = am;
        this->scholarship_name = sch_n;
    }
    
    void input()
    {
        Student::input();
        std::cin>>scholarship_name>>amount;
    }
    void display()
    {
        Student::display();
        std::cout<<scholarship_name<<'\t'<<amount<<std::endl;
    }
};


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