Write a class Person 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 another class Student that inherits Person class. It has additional attributes of
rollnumber and marks. It also has member function to input and display its data
members.
#include<iostream>
using namespace std;
class Person
{
double ID;
string name;
string address;
public:
Person():ID(0),name(""),address(""){}
Person(double _ID, string _name, string _address)
:ID(_ID), name(_name), address(_address){}
void SetPerson()
{
cout<<"\nPlease, enter an ID for new person:";
cin>>ID;
cout<<"Please, enter a name for new person:";
cin>>name;
cout<<"Please, enter an address of new person:";
cin.ignore(256,'\n');
getline(cin,address,'\n');
}
void PrintPerson()
{
cout<<"\nData of the person:"
<<"\nID: "<<ID
<<"\nName: "<<name
<<"\nAddress: "<<address;
}
};
class Student:public Person
{
int rollnumber;
string marks;
public:
Student():Person(),rollnumber(0),marks(""){}
Student(double _ID, string _name, string _address, int _rollnumber, string _marks)
:Person(_ID, _name,_address),rollnumber(_rollnumber),marks(_marks){}
void SetStudent()
{
cout<<"\nPlease, enter a rollnumber for new student:";
cin>>rollnumber;
cout<<"Please, enter marks for new student:";
cin>>marks;
}
void PrintStudent()
{
cout<<"\nData of the student:"
<<"\nRollnumber: "<<rollnumber
<<"\nMarks: "<<marks;
}
};
int main()
{
Student a(123,"Alex","5 street",4,"A,B,C,D,E");
a.PrintPerson();
a.PrintStudent();
Student b;
b.SetPerson();
b.SetStudent();
b.PrintPerson();
b.PrintStudent();
}
Comments
Leave a comment