Create three classes Person Student Teacher Have Teacher and Student inherent Pe
ID: 3557114 • Letter: C
Question
- Create three classes
- Person
- Student
- Teacher
- Have Teacher and Student inherent Person
- Any attributes/methods that Teacher and Person will share should be in Person and have the appropriate modifiers(private,protected,public) and access modifiers(gets/sets)
- Each class MUST override the ToString method
- Teacher and Student MUST call the Person's contrutor using "base"
- Teacher and Student MUST override at least one of Person's methods other then ToString.
- Create at least one instance of each class to highlight the differences and show how the classes are making use of inherentance.
Explanation / Answer
//simple code inhrtitance can be observed by the messages that the diffrent objects prints through method //overriding
#include<iostream>
using namespace std;
class person
{
public : char name[10]; //each type of acess specifier is taken
protected :char message[10];
private : int registration ;
public: void getname()
{
cout<<" enter your name :";
gets(name);
}
public:
person()
{
cout<<" << inside base class person constructor >> ";
// this message wiil be repaeted because on //each craetion of child class parent constructors are called implictly"
//just to show the functioning of //constructor it is not necesary
}
void setname()
{
cout<<" person name is : "<<name<<" ";
}
void getmessage()
{
cout<<" enter your message :";
//the message is taken as input in the form of character arry
//so the it will be used as a string in tosting method
gets(message);
}
void tostring()
{
cout<<" << this is persons base class message:"<<message<<">>";
}
};
class student:public person //public inheritance
{
public:
student()
{
// super();
cout<<" << inside the subclass student constructor >> ";
}
void getname()
{
cout<<"enter your name :";
gets(name);
}
public:
void setname()
{
cout<<" << this method is overridded >> ";
cout<<"student name is : "<<name<<" ";
}
void getmessage()
{
cout<<" enter your message :";
gets(message);
}
void tostring()
{
cout<<" << this is student subclass message :"<<message<<">>";
}
};
class teacher:public person //public inheritance
{
public:
teacher()
{
cout<<" << inside subclass teacher constructor >> ";
}
void getname()
{
cout<<"enter your name :";
gets(name);
}
public:
void setname()
{
cout<<" << this method is overridded >> ";
cout<<"teacher name is : "<<name<<" ";
}
void getmessage()
{
cout<<"enter your message :";
gets(message);
}
void tostring()
{
cout<<"this is teacher subclass message "<<message;
}
};
void main()
{
person p;
p.getname();
p.getmessage();
student s;
s.getname();
s.getmessage();
teacher t;
t.getname();
t.getmessage();
p.setname();
p.tostring();
s.setname();
s.tostring();
t.setname();
t.tostring();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.