C++ Class Inheritance

Introduction

We can build a class from an existing class.

It is said that a class can be derived from an existing class.

This is known as inheritance and is one of the pillars of object-oriented programming, abbreviated as OOP.

To derive a class from an existing class, we write:

class MyDerivedClass : public MyBaseClass {}; 

A simple example would be:

class MyBaseClass 
{ 

}; /*  w ww  . j a va  2 s  .  c  o  m*/

class MyDerivedClass : public MyBaseClass 
{ 

}; 

int main() 
{ 

} 

In this example, MyDerivedClass inherits the MyBaseClass.

It is said that MyDerivedClass is derived from MyBaseClass, or MyBaseClass is a base class for MyDerivedClass.

It is also said that MyDerivedClass is MyBaseClass.

They all mean the same thing.

Now the two classes have some sort of relationship.

This relationship can be expressed through different naming conventions, but the most important one is inheritance.




PreviousNext

Related