Class : BCA 3rd Semester Course Code: BCA-S3-03 Course Title: Object Oriented Programming Concepts in C++ Unit III Inheritance The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance. Inheritance lets you create new classes from existing class. Any new class that you create from an existing class is called derived class; existing class is called base class. The inheritance relationship enables a derived class to inherit features from its base class. Furthermore, the derived class can add new features of its own. Therefore, rather than create completely new classes from scratch, you can take advantage of inheritance and reduce software complexity. Forms of Inheritance Single Inheritance: It is the inheritance hierarchy wherein one derived class inherits from one base class. Multiple Inheritance: It is the inheritance hierarchy wherein one derived class inherits from multiple base class(es) Hierarchical Inheritance: It is the inheritance hierarchy wherein multiple subclasses inherit from one base class. Multilevel Inheritance: It is the inheritance hierarchy wherein subclass acts as a base class for other classes. Hybrid Inheritance: The inheritance hierarchy that reflects any legal combination of other four types of inheritance.
In order to derive a class from another, we use a colon (:) in the declaration of the derived class using the following format : class derived_class: memberaccessspecifier base_class... ; Where derived_class is the name of the derived class and base_class is the name of the class on which it is based. The member Access Specifier may be public, protected or private. This accesss specifier describes the access level for the members that are inherited from the base class. Member Access Specifier Private How Members of the Base Class Appear in the Derived Class Private members of the base class are inaccessible to the class. derived Protected members of the base class become private members of the derived class. Public members of the base class become private members of the derived class. Protected Private members of the base class are inaccessible to the derived class.
Protected members of the base class become protected members of the derived class. Public members of the base class become protected members of the derived class. Public Private members of the base class are inaccessible to the class. derived Protected members of the base class become protected members of the derived class. Public members of the base class become public members of the derived class. In principle, a derived class inherits every member of a base class except constructor and destructor. It means private members are also become members of derived class. But they are inaccessible by the members of derived class. Following example further explains concept of inheritance : class Shape protected: float width, height; void set_data (float a, float b) width = a; height = b; ; class Rectangle: public Shape float area ()
; return (width * height); class Triangle: public Shape float area () return (width * height / 2); ; int main () Rectangle rect; Triangle tri; rect.set_data (5,3); tri.set_data (2,5); cout << rect.area() << endl; cout << tri.area() << endl; return 0; output : 15 5 The object of the class Rectangle contains : width, height inherited from Shape becomes the protected member of Rectangle. set_data() inherited from Shape becomes the public member of Rectangle area is Rectangle s own public member The object of the class Triangle contains : width, height inherited from Shape becomes the protected member of Triangle. set_data() inherited from Shape becomes the public member of Triangle area is Triangle s own public member set_data () and area() are public members of derived class and can be accessed from outside class i.e. from main()
Constructor and inheritance The compiler automatically calls a base class constructor before executing the derived class constructor. The compiler s default action is to call the default constructor in the base class. You can specify which of several base class constructors should be called during the creation of a derived class object. This is done by specifying the arguments to the selected base class constructor in the definition of the derived class constructor. class Rectangle private : float length; float width; Rectangle () length = 0; width = 0; Rectangle (float len, float wid) length = len; width = wid; ; float area() return length * width ; class Box : public Rectangle private : float height; Box ()
height = 0; Box (float len, float wid, float ht) : Rectangle(len, wid) height = ht; ; float volume() return area() * height; int main () Box bx; Box cx(4,8,5); cout << bx.volume() << endl; cout << cx.volume() << endl; return 0; output : 0 160 Overriding Base Class Functions A derived class can override a member function of its base class by defining a derived class member function with the same name and parameter list. It is often useful for a derived class to define its own version of a member function inherited from its base class. This may be done to specialize the member function to the needs of the derived class. When this happens, the base class member function is said to be overridden by the derived class. class mother void display () cout << "mother: display function\n"; ; class daughter : public mother
void display () cout << "daughter: display function\n\n"; ; int main () daughter rita; rita.display(); return 0; output: daughter: display function Gaining Access to an Overridden Function It is occasionally useful to be able to call the overridden version. This is done by using the scope resolution operator to specify the class of the overridden member function being accessed. class daughter : public mother void display () cout << "daughter: display function\n\n"; mother::display(); ; output: daughter: display function mother: display function Virtual Base Class Multipath inheritance may lead to duplication of inherited members from a grandparent base class. This
may be avoided by making the common base class a virtual base class. When a class is made a virtual base class, C++ takes necessary care to see that only one copy of that class is inherited. class A ; class B1 : virtual public A ; class B2 : virtual public A ; class C : public B1, public B2 // only one copy of A // will be inherited ; Ambiguity in multiple inheritance Ambiguity in multiple inheritance: In multiple inheritance, there may be possibility that a class may inherit member functions with same name from two or more base classes and the derived class may not have functions with same name as those of its base classes. If the object of the derivedd class need to
access one of the same named member function of the base classes then it result in ambiguity as it is not clear to the compiler which base s class member function should be invoked. The ambiguity simply means the state when the compiler gets confused. Example: class a private: void abc() ; class b private: void abc() ; class c : public a, public b private: ; void main() c obj; obj.abc();//error. In this example, the two base classes a and b have function of same name abc() which are inherited to the derived class c. When the object of the class c is created and calls the function abc() then the compiler is confused which base class function to call.
Solution of ambiguity in multiple inheritance: The ambiguity can be resolved by using the scope resolution operator to specify the class in which the member function lies as given below: obj.a :: abc(); This statement invoke the function name abc() which are lies in base class a.