Inheritance in CPP (C plus plus)
Inheritance in CPP (C Plus Plus)
Today, in this fresh and new article, we will cover the following topics;
- What is inheritance?
- Program of inheritance in C Plus Plus
What is inheritance?
Inheritance is an object-oriented technique in which we can create a new derived class from an existing base class. We can say that base class is the parent class and derived class is the child class.
The child class inherits all the features from the parent class and child class can also have some additional features of its own.
Program on inheritance in C++ (OOP )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | <span style="font-size: 14pt; font-family: arial, helvetica, sans-serif;">#include <iostream> using namespace std; class person { public: int age; string profession; person(): profession("Unemployed"), age(14) { } void show() { cout << "My Profession is: " << profession <<endl; cout << "My Age is: " << age <<endl; w(); t(); } void w() { cout << "I can walk." <<endl ;} void t() { cout << "I can talk." <<endl ;} }; class mathteacher : public person { public: void teachmath() { cout << "I can teach Mathematics." <<endl<< endl; } }; class footballer : public person { public: void play_football() { cout << "I can play Football." << endl; } }; int main() { mathteacher teacher; teacher.profession = "Teacher"; teacher.age = 23; teacher.show(); teacher.teachmath(); footballer footballer; footballer.profession = "Footballer"; footballer.age = 19; footballer.show(); footballer.play_football(); return 0; }</span> |
Output:
Topic Covered
Inheritance in CPP (C plus plus).