Inheritance

 Inheritance

Definition-

  • Inheritance in Object Oriented Programming can be described as a process of creating new classes from existing classes. New classes inherit some of the properties and behavior of the existing classes. An existing class that is "parent" of a new class is called a base class. ... Inheritance is a technique of code reuse.


Base and derived classes-

  • A class can be used as the base class for a derived new class. The derived class inherits all of the properties of the base class. The derived class can add new members or change base class members. ... In a C++ program the OOP paradigm is centered around your class definitions.

Type of inheritance and their implementation-

  • In C++, we have 5 different types of Inheritance-
        1)Single Inheritance
        2)Multiple Inheritance
        3)Hierarchical Inheritance
        4)Multilevel Inheritance
        5)Hybrid Inheritance (also known as Virtual Inheritance)





Virtual base classes-

  • Virtual base classes (C++ only) Suppose you have two derived classes B and C that have a common base class A , and you also have another class D that inherits from B and C . You can declare the base class A as virtual to ensure that B and C share the same subobject of A .
                        class A
                        {
                            public:
                                int i;
                        };

                        class B : virtual public A
                        {
                            public:
                                int j;
                        };

                        class C: virtual public A
                        {
                            public:
                                int k;
                        };

                        class D: public B, public C
                        {
                            public:
                               int sum;
                        };

                        int main()
                        {
                            D ob;
                            ob.i = 10; //unambiguous since only one copy of i is
                        inherited.
                            ob.j = 20;
                            ob.k = 30;
                            ob.sum = ob.i + ob.j + ob.k;
                            cout << “Value of i is : ”<< ob.i<<”\n”;
                            cout << “Value of j is : ”<< ob.j<<”\n”; cout << “Value
                        of k is :”<< ob.k<<”\n”;
                           cout << “Sum is : ”<< ob.sum <<”\n”;
                        return 0;
                        }

Abstract class-

  •  Abstract class is declared in useof base class and contained at least one pure vartual classes.

  •  An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier ( = 0 ) in the declaration of a virtual member function in the class declaration.

Post a Comment

Previous Post Next Post