How to Define Derive Class in Inheritance

First Define a base class as shown below:

class Base_Class_Name
{

}

Once the base class is defined you can define a derived class using following syntax:

class Derived_Class_Name: Visibility_Mode Base_Class_Name
{

}
Here,
Derived_Class_Name
is the name of the class that you want to derive.
Visibility_Mode indicates the mode in which you want to derive a new class. It can be public, private or protected.
Base_Class_Name is the name of the class from which you want to derive a new class.

In the above syntax visibility mode determines how the data members of the base class are inherited in to derived class. Visibility mode can be public, private or protected. If you don’t specify visibility mode then by default it is private.

     

The effect of visibility mode is explained below:
(1) public:
• When a class is derived publicly public member of the base class becomes public member of the derived class.
• When a class is derived publicly protected member of the base class becomes protected member of the derived class.
• Private members of the base class can not be inherited.
(2) private:
• When a class is derived privately public member of the base class becomes private member of the derived class.
• When a class is derived privately protected member of the base class becomes private member of the derived class.
• Private members of the base class can not be inherited.
(3) protected:
• When a class is derived in protected mode public member of the base class becomes protected member of the derived class.
• When a class is derived in protected mode protected member of the base class becomes protected member of the derived class.
• Private members of the base class can not be inherited.

Download Projects


Download Programs