130 likes | 692 Views
INHERITANCE IN C++. Topics to be discussed. Inheritance Define a Class Hierarchy Visibility Mode Access Rights of Derived Classes Access Control to different Members of a Class. Inheritance. Inheritance is a mechanism of deriving a new class(derived class) from an old one(base class).
E N D
Topics to be discussed • Inheritance • Define a Class Hierarchy • Visibility Mode • Access Rights of Derived Classes • Access Control to different Members of a Class
Inheritance • Inheritance is a mechanism of deriving a new class(derived class) from an old one(base class). • For example,an organization consists of various types of employees like managers,engineers,technicians and so on.In this case,employee is the base class and engineer is the derived class.
Employee Engineer Putting Them Together • Employee is the base class • Engineer is the derived class • The derived class can • inherit all members from the base class, except the constructor • access all public and protected members of the base class • define its private data member • provide its own constructor • define its public member functions BACK
Define a Class Hierarchy • Syntax: classDerivedClassName : visibility-modeBaseClassName { ………; // members of derived class ………; } The Colon Symbol(: ) shows the relationship of a derived class to its base class where • Visibility-mode specifies the type of derivation • private by default, or public • Any class can serve as a base class • Thus a derived class can also be a base class BACK
Visibility Mode • Then all public members of a base class will be considered as public menbers of the derived class When visibility mode is Public When visibility mode is Private Then public members of a base class will be considered as private menbers of the derived class
Protected Access Specifier • The private members of the base class can not be inherited and it is not available for the derived class. • One way to solve this problem is by changing the visibility mode of the private member by making it public. • When a member declared as Protected is accesible by the member functions within the class and any class immediately derived from it.
class class_name { private: … //By default … //visible to member functions … of its own protected: … //visible to member functions … of its own and derived class … public: … // visible to all … … }; BACK
Access Rights of Derived Classes Type of Inheritance • The type of inheritance defines the access level for the members of derived classthat are inherited from the base class Access Control for Members BACK