Types of Inheritances:
There are three kinds of inheritance
- Public Inheritance
- Private Inheritance
- Protected Inheritance
Public Inheritance:
In Public Inheritance, the public members of the base class become the public members of the derived class. Thus the objects of the derived class can access public members (both data and functions) of the base class. Similarly, the protected data members of the base class also become the protected members of derived class.
The general syntax for deriving a public class from base class is:
class sub_class_name : public base_class_name
{
—————–
—————–
—————–
};
where
public specifies the public inheritance.
sub_class_name represents name of the derived class.
base_class_name represents name of the base class.
Private Inheritance:
In Private Inheritance, the objects of the derived class cannot access the public members of the base class. Its objects can only access the protected data members of the base class.
The general syntax for deriving a private class from base class is:
class sub_class_name : private base class name
{
——————
——————
—————–
};
where
private specifies private inheritance.
sub_class_name represents name of the derived class.
base_class_name represents name of the base class.
Protected Inheritance:
The object of the class that is derived as protected can access only the protected member of the base class.
The general syntax for deriving a protected class from base class is:
class sub_class name : protected base_class_name
{
——————
——————
——————
};
where
protected specifies protected inheritance.
sub_class_name represents name of the derived class.
base_class_name represents name of the base class.
comment closed