Concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++

Understand the concepts of Inheritance, Pointers, Polymorphism and Virtual Functions in C++ programming language

Introduction to Inheritance in C++

Let me introduce you to the concept of Inheritance in C++. Reusability is yet another important feature of C++. It is always nice if we could reuse something that already exists rather than trying to create the same all over again. It would not only save the time and money but also reduce frustration and increase readability. For instance, the reuse of a class that has already been tested, debugged and used many time can save us the effort of developing and testing the same again. Fortunately, C++ strongly supports the concept of reusability.The C++ classes can be reused in several ways. Once a class has been written and tested, it can be adapted by other programmers to suit their requirements. This is basically done by creating new classes which reuse the properties of the existing ones. The mechanism of deriving a new class from an old one is called...

Multiple Inheritance in C++

Inheritance in which a derived class is derived from several base class is known as multiple inheritance. A class can inherit the attributes of two or more classes as shown in fig. below. This is known as multiple inheritance. Multiple inheritance allows us to combine the features of several existing classes as a starting point for defining new classes. It is like a child inheriting the physical features of one parent and the intelligence of another. The syntax of a derived class with multiple base class is as follows: class D : visibility B-1, visibility B-2… { … … (Body of D) … }; where visibility may be either public or private. The base classes are separated by commas. Example: class P : public M, public N { public: void display(void); }; Classes M and N have been specified as follows: class M { protected: int m; public: void get_m(int);...

Multi-Level Inheritance in C++

The mechanism of deriving a class from another derived class is known as multi-level inheritance in C++ It is not uncommon that a class is derived from another derived class as shown below: The class A serves as a base class for the derived class B which in turn serves as a base class for the derived class C. The class B is known as intermediate base class since it provides a link for the inheritance between A and C. The chain A -> B -> C is known as inheritance path. A derived class with multi-level inheritance is declared as follows: class A //Base Class { … }; class B : public A //B derived from A { … }; class C : public B //C derived from B { … }; This process can be extended to any number of levels. Let us consider the below example: Assume...

Protected Inheritance in C++

Understanding the concept of Protected Inheritance in C++. A special mention of visibility label protected: We have just seen how to increase the capabilities of an existing class without modifying it. We have also seen that a private member of a base class cannot be inherited and therefore its is not available for the derived class directly. What do we do if the private data needs to be inherited by a derived class? This can be accomplished by modifying the visibility limit of the private member by making it public. But, this would make it accessible to all the other functions of the program, thus, eliminating the advantage of data hiding. C++ provides a third visibility modifier, protected which serves a limited purpose in inheritance. A member declared as protected is accessible by the member functions within its class and any class immediately derived from it. It cannot be accessed...

Single Inheritance – Private Inheritance in C++

Let us understand what does private inheritance mean… Consider a simple example to illustrate the single inheritance. The Program given below shows a base class B and a derived class D. The class B contains one private data member, one public data member and three public member functions. The class D contains one private data member and two public member functions. #include<iostream.h> class B { private: int a; //private; not inheritable public: int b; //public; ready for inheritance void get_ab(); int get_a(); void show_a(); }; class D : private B //private derivation { private: int c; public: void mul(); void display(); }; In private derivation, the public members of the base class becomes private members of the derived class. Therefore, the objects of the derived class D cannot have direct access to the public member functions of the base class B. Hence, if we create an object of the derived...

Single Inheritance – Public Inheritance in C++

Inheritance in which the derived class is derived from only one base class is called single inheritance. Let us consider a simple example to illustrate the single inheritance. Program given below shows a base class B and a derived class D. The class B contains one private data member, one public data member and three public member functions. The class D contains one private data member and two public member functions. #include<iostream.h> class B { private: int a; //private; not inheritable public: int b; //public; ready for inheritance void get_ab(); int get_a(); void show_a(); }; class D : public B //public derivation { private: int c; public: void mul(); void display(); }; /*…Defining of functions…*/ void B :: get_ab() { a = 5; b = 10; } int B :: get_a() { return a; } void B :: show_a() { cout << “a = ” << a <<endl; } void...