Advertisement



< Prev
Next >



C++ Inheritance





The C++ language gives us a powerful OOP feature known as inheritance. Inheritance allows a class to use/inherit the features of another existing class. The class that wants to use the features of another class is called subclass or derived class, whereas the class whose features are to be used/inherited is referred to as base class. Hence, inheritance gives C++ the power of reusability.




What features a subclass inherits from its base class, through inheritance?



Note - Base class members marked with private visibility mode are never inherited.




Syntax of Inheritance


Let us take a look at a general syntax of performing inheritance, using which we could define a derived class which is inheriting a base class.


class derived-class-name : visibility-mode base-class-name
{
//members of derived class
}







Publicly inheriting a class

Let us understand the syntax used to publicly inherit the feature of a class by another class.

class Y
{
public:
int a;
protected:
int b;
private:
int c;
}


class X : public Y
{
}

In the example above, the class named Y is publicly inherited another class named Y, which means that -


Advertisement




Privately inheriting a class


Let us understand the syntax used to privately inherit the feature of a class by another class.

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement