[pastacode lang=”cpp” manual=”class%20Person%20%0A%7B%0A%20%20…%20..%20…%0A%7D%3B%0Aclass%20MathsTeacher%20%3A%20public%20Person%20%0A%7B%0A%20%20…%20..%20…%0A%7D%3B%0Aclass%20Footballer%20%3A%20public%20Person%0A%7B%0A%20%20….%20..%20…%0A%7D%3B” message=”” highlight=”” provider=”manual”/]
Example: Inheritance in C++ Programming
- Create game characters using the concept of inheritance.
[pastacode lang=”cpp” manual=”%23include%20%3Ciostream%3E%0Ausing%20namespace%20std%3B%0A%0Aclass%20Person%0A%7B%0A%20%20%20%20%20public%3A%0A%20%20%20%20%20%20%20%20string%20profession%3B%0A%20%20%20%20%20%20%20%20int%20age%3B%0A%0A%20%20%20%20%20%20%20%20Person()%3A%20profession(%22unemployed%22)%2C%20age(16)%20%7B%20%7D%0A%20%20%20%20%20%20%20%20void%20display()%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22My%20profession%20is%3A%20%22%20%3C%3C%20profession%20%3C%3C%20endl%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22My%20age%20is%3A%20%22%20%3C%3C%20age%20%3C%3C%20endl%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20walk()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20talk()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20void%20walk()%20%7B%20cout%20%3C%3C%20%22I%20can%20walk.%22%20%3C%3C%20endl%3B%20%7D%0A%20%20%20%20%20%20%20%20void%20talk()%20%7B%20cout%20%3C%3C%20%22I%20can%20talk.%22%20%3C%3C%20endl%3B%20%7D%0A%7D%3B%0A%0A%2F%2F%20MathsTeacher%20class%20is%20derived%20from%20base%20class%20Person.%0Aclass%20MathsTeacher%20%3A%20public%20Person%0A%7B%0A%20%20%20%20public%3A%0A%20%20%20%20%20%20%20void%20teachMaths()%20%7B%20cout%20%3C%3C%20%22I%20can%20teach%20Maths.%22%20%3C%3C%20endl%3B%20%7D%0A%7D%3B%0A%0A%2F%2F%20Footballer%20class%20is%20derived%20from%20base%20class%20Person.%0Aclass%20Footballer%20%3A%20public%20Person%0A%7B%0A%20%20%20%20public%3A%0A%20%20%20%20%20%20%20void%20playFootball()%20%7B%20cout%20%3C%3C%20%22I%20can%20play%20Football.%22%20%3C%3C%20endl%3B%20%7D%0A%7D%3B%0A%0Aint%20main()%0A%7B%0A%20%20%20%20%20MathsTeacher%20teacher%3B%0A%20%20%20%20%20teacher.profession%20%3D%20%22Teacher%22%3B%0A%20%20%20%20%20teacher.age%20%3D%2023%3B%0A%20%20%20%20%20teacher.display()%3B%0A%20%20%20%20%20teacher.teachMaths()%3B%0A%0A%20%20%20%20%20Footballer%20footballer%3B%0A%20%20%20%20%20footballer.profession%20%3D%20%22Footballer%22%3B%0A%20%20%20%20%20footballer.age%20%3D%2019%3B%0A%20%20%20%20%20footballer.display()%3B%0A%20%20%20%20%20footballer.playFootball()%3B%0A%0A%20%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]
Output
[pastacode lang=”cpp” manual=”My%20profession%20is%3A%20Teacher%0AMy%20age%20is%3A%2023%0AI%20can%20walk.%0AI%20can%20talk.%0AI%20can%20teach%20Maths.%0AMy%20profession%20is%3A%20Footballer%0AMy%20age%20is%3A%2019%0AI%20can%20walk.%0AI%20can%20talk.%0AI%20can%20play%20Football.” message=”” highlight=”” provider=”manual”/]
- In this program, Person is a base class, while MathsTeacher and Footballer are derived from Person
- Person class has two data members – profession and age. It also has two member functions – walk() and talk().
- Both MathsTeacher and Footballer can access all data members and member functions of Person.
- However, MathsTeacher and Footballer have their own member functions as well:teachMaths() and playFootball() respectively. These functions are only accessed by their own class.
- In the main() function, a new MathsTeacher object teacher is created.
- Since, it has access to Person’s data members, profession and age of teacher is set. This data is displayed using the display() function defined in the Person class. Also, the teachMaths() function is called, defined in the MathsTeacher class.
- Likewise, a new Footballer object footballer is also created. It has access to Person’s data members as well, which is displayed by invoking the display() function. The playFootball() function only accessible by the footballer is called then after.
Access specifiers in Inheritance
- When creating a derived class from a base class, you can use different access specifiers to inherit the data members of the base class.
- These can be public, protected or private.
- In the above example, the base class Person has been inherited public-ly by MathsTeacherand Footballer.
Member Function Overriding in Inheritance
- Suppose, base class and derived class have member functions with same name and arguments.
- If you create an object of the derived class and try to access that member function, the member function in derived class is only invoked.
- The member function of derived class overrides the member function of base class.