inheritance in cpp
Hello From Iqbal;
welcome to My Blog;in the program below i am going to use famous Object Oriented Programming Properties i.e.
feel free to comment if you have any question(s).
#include <iostream>
using namespace std;
class Person{ // person class , the base class from whihc further classes will inherit
private :
// encapsulated private members(data fields)
string profession;
int age;
public:
Person(){
}
void setAge(int a){ //setters ( mutators)
this->age=a;
}
void setProfession(string prof){
this->profession = prof;
}
int getAge() { //getters (accessors)
return age;
}
string getProfession() {
return profession;
}
void display(){
cout<<"my Profession is"<<profession<<"\n";
cout<<"my age is: "<<age<<"\n";
walk();
talk();
}
void walk(){
cout<<" i can walk"<<"\n";
}
void talk(){
cout<<"i can talk"<<"\n";
}
};
class MathsTeacher : public Person{ //inherited from person base class
public :
MathsTeacher(){
setAge(23);
setProfession("Teacher");
}
void display(){ //method overriding from super class
cout<<"my Profession is:"<<getProfession()<<"\n";
cout<<"my age is: "<<getAge()<<"\n";
walk();
talk();
teachMaths();
}
void teachMaths(){
cout<<"i can teach Maths"<<"\n";
}
};
class Footballer : public Person{
public :
Footballer(){
setAge(19);
setProfession("FootBaller");
}
void display(){ //method overriding from super class
cout<<"my Profession is: "<<getProfession()<<"\n";
cout<<"my age is: "<<getAge()<<"\n";
walk();
talk();
playFootball();
}
void playFootball(){
cout<<"i can Play Football"<<"\n";
}
};
int main(int argc, char** argv) {
MathsTeacher teacher;
teacher.display();
Footballer footballer;
footballer.display();
return 0;
}
OutPut of the Program:
welcome to My Blog;in the program below i am going to use famous Object Oriented Programming Properties i.e.
- Inheritance,
- Encapsulation
- Method Overriding
feel free to comment if you have any question(s).
#include <iostream>
using namespace std;
class Person{ // person class , the base class from whihc further classes will inherit
private :
// encapsulated private members(data fields)
string profession;
int age;
public:
Person(){
}
void setAge(int a){ //setters ( mutators)
this->age=a;
}
void setProfession(string prof){
this->profession = prof;
}
int getAge() { //getters (accessors)
return age;
}
string getProfession() {
return profession;
}
void display(){
cout<<"my Profession is"<<profession<<"\n";
cout<<"my age is: "<<age<<"\n";
walk();
talk();
}
void walk(){
cout<<" i can walk"<<"\n";
}
void talk(){
cout<<"i can talk"<<"\n";
}
};
class MathsTeacher : public Person{ //inherited from person base class
public :
MathsTeacher(){
setAge(23);
setProfession("Teacher");
}
void display(){ //method overriding from super class
cout<<"my Profession is:"<<getProfession()<<"\n";
cout<<"my age is: "<<getAge()<<"\n";
walk();
talk();
teachMaths();
}
void teachMaths(){
cout<<"i can teach Maths"<<"\n";
}
};
class Footballer : public Person{
public :
Footballer(){
setAge(19);
setProfession("FootBaller");
}
void display(){ //method overriding from super class
cout<<"my Profession is: "<<getProfession()<<"\n";
cout<<"my age is: "<<getAge()<<"\n";
walk();
talk();
playFootball();
}
void playFootball(){
cout<<"i can Play Football"<<"\n";
}
};
int main(int argc, char** argv) {
MathsTeacher teacher;
teacher.display();
Footballer footballer;
footballer.display();
return 0;
}
OutPut of the Program:
Comments
Post a Comment