Let’s remember that, Object Oriented Programming (OOP) is a way to integrate with objects which can contain data in the form (attributes or properties of objects), and code blocks in the form of procedures (methods, functions of objects). These attributes and methods are variables and functions that belong to the class, they are generally referred to as class members. In C++, classes have members (attributes, methods) and we should protect each member inside this class.
The Inheritance is one of the most important concept in object-oriented C++ programming as in other features of Classes. Inheritance allows us to define a class in terms of another class, and it makes easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time. Inheritance implements the relationship between classes. For example, a rectangle is a kind of shape and ellipse is a kind of shape etc.
Multiple Inheritance is another feature of C++ that a class can inherit from more than one classes. For example a derived class can be inherited from more than one base classes or derived classes.
Here is a derived class example, inherited public from base1 and inherited public from base2 classes,
1 2 3 |
class classname : public basen1, public base2 { }; |
Here is a full Multiple Inheritance example ,
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 |
#include <iostream> #include <string> class employee { public: std::string name; }; class engineer { public: std::string profession; }; class staff: public employee, public engineer { public: std::string department; void info() { std::cout << "Name: " << name << '\n'; std::cout << "Profession: " << profession << '\n'; std::cout << "Department: " << department << '\n'; } }; int main() { staff staff1; staff1.name = "Kate Smith"; // Inherited from employee class staff1.profession = "Computer Engineer"; // Inherited from engineer class staff1.department = "Backend Development Department"; // from staff class staff1.info(); // information function from staff class getchar(); return 0; } |
Here; name property inherited from employee class and profession property inherited from engineer class, department property is from the staff class finally info() method is called from staff class.
Get started building powerful apps with C++Builder!
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition