- C++ Programming Basics
- C++ Tutorial
- C++ Environment Setup
- C++ Character Set
- C++ Keywords
- C++ Identifiers
- C++ Constants
- C++ Punctuators
- C++ Program Structure
- C++ Basic Syntax
- C++ Comments
- C++ Basic Programs
- C++ Input Output Operator
- C++ Input Output Stream
- C++ Type & Variable
- C++ Data Types
- C++ Data Type Modifiers
- C++ Variables
- C++ Variable Types
- C++ Variable Scope
- C++ Storage Classes
- C++ Formatting Output
- C++ Operators
- C++ Operators
- C++ Type Conversion
- C++ Numbers
- C++ Assignment Operator
- C++ Shorthand
- C++ Flow of Control
- C++ Statements
- C++ Flow Control
- C++ Decision Making
- C++ if if-else if-else-if switch
- C++ Loops
- C++ for while do-while Loop
- C++ break continue goto
- C++ Standard Library
- C++ Standard Library
- C++ Header Files
- C++ Character String
- C++ Mathematical Functions
- C++ Functions
- C++ Functions
- C++ Function Types
- C++ Function Prototype
- C++ Function Call
- C++ Function Return
- C++ Friend Function
- C++ Scope Rules
- C++ Arrays & Strings
- C++ Arrays
- C++ One Dimensional Arrays
- C++ Two Dimensional Arrays
- C++ Strings
- C++ Data Structure
- C++ Data Structure
- C++ Access Structure Member
- C++ Nested Data Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ typedef
- C++ #define
- C++ Programming Pointers
- C++ Pointers
- C++ Memory Map
- C++ Free Store
- C++ Declare Initialize Pointers
- C++ Dynamic Memory Allocation
- C++ Pointers & Arrays
- C++ Pointers & Const
- C++ Pointers & Functions
- C++ Pointers & Structures
- C++ Objects as Function Arguments
- C++ Pointers & Objects
- C++ References
- C++ File Handling
- C++ File Handling
- C++ File Streams
- C++ Data Files
- C++ Opening & Closing Files
- C++ Steps to Process Files
- C++ Change Stream Behaviour
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Object Oriented
- C++ Object Oriented
- C++ Function Overloading
- C++ Classes & Objects
- C++ Constructors & Destructors
- C++ Inheritance
- C++ Encapsulation
- C++ Polymorphism
- C++ Data Abstraction
- C++ Interfaces
- C++ Programming Advance
- C++ Linked Lists
- C++ Stacks
- C++ Queues
- C++ Date Time
- C++ Preprocessors
- C++ Exception Handling
- C++ Namespaces
- C++ Dynamic Memory
- C++ Multithreading
- C++ Templates
- C++ Signal Handling
- C++ Web Programming
- C++ Programming Examples
- C++ Programming Examples
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ Inheritance
Inheritance is the capability of one class to inherit the properties from another class. The most important advantage of inheritance is code reusability. Once a base class is written and debugged, it can be used in various situations without having to redefine it or rewrite it. Already tested and saved previous code can be reused.
Reusing existing code saves times, money, and efforts also, and increases program's reliability. Without redefining the old class, you can add new properties to the derived class, and you even can redefine an inherited class member functions.
Need for Inheritance
Inheritance is a splendid concept of object-oriented languages. Here are several reasons why inheritance was introduced:
- The capability to express the inheritance relationship which ensures the closeness with the real-world models.
- The idea of reusability. The advantages of reusability are: faster development time, easier maintenance, and easy to extend.
- Transitive nature of inheritance.
Different Forms of Inheritance
Inheritance may take place in many forms which are :
- Single Inheritance - When a sub class inherits only from one base class, known as single inheritance
- Multiple Inheritance - When a sub class inherits from multiple base classes, known as multiple inheritance
- Hierarchical Inheritance - When many sub classes inherit from a single base class, known as hierarchical inheritance
- Multilevel Inheritance - The transitive nature of inheritance is reflected by this form of inheritance. When a sub class inherits from a class that itself inherits from another class, known as multilevel inheritance
- Hybrid Inheritance - Hybrid inheritance combines two or more forms of inheritance
Visibility Modes
Here, the following table specifies the role of visibility-modes:
Visibility Mode is | Inheritable public member becomes (in derived class) | Inheritable protected member becomes (in derived class) | Private Member of base class are not directly accessible to derived class |
---|---|---|---|
public | public | protected | |
protected | protected | protected | |
private | private | private |
Inheritance and Access Control
When a class inherits some members from another class, there must be a way to control the access of inherited members. As you know that we can derive a class publicly or privately. Let's take a look at these.
Access Control in Publicly Derived Class
Here is an example program illustrating the access control in public derivation of a class
/* C++ Inheritance - Example Program */ #include<iostream.h> #include<stdio.h> #include<conio.h> class EMPLOYEE { private: char name[30]; unsigned long enumb; public: void getdata() { cout<<"Enter name: "; gets(name); cout<<"Enter Employee Number: "; cin>>enumb; } void putdata() { cout<<"Name: "<<name<<"\t"; cout<<"Emp. No: "<<enumb<<"\t"; cout<<"Basic Salary: "<<basic; } protected: float basic; void getbasic() { cout<<"Enter Basic: "; cin>>basic; } }; class MANAGER:public EMPLOYEE { private: char title[30]; public: void getdata() { EMPLOYEE::getdata(); getbasic(); cout<<"Enter Title: "; gets(title); } void putdata() { EMPLOYEE::putdata(); cout<<"\tTitle: "<<title<<"\n"; } }; void main() { clrscr(); MANAGER m1, m2; cout<<"Manager 1\n"; m1.getdata(); cout<<"\nManager 2\n"; m2.getdata(); cout<<"\n\t\tManager 1 Details\n"; m1.putdata(); cout<<"\n\t\tManager 2 Details\n"; m2.putdata(); getch(); }
Here is the sample run of the above C++ program:

Access Control in Privately Derived Class
Here is an example program illustrating access control of inherited members in the privately derived class
/* C++ Inheritance - Example Program */ #include<iostream.h> #include<stdio.h> #include<conio.h> class EMPLOYEE { private: char name[30]; unsigned long enumb; public: void getdata() { cout<<"Enter name: "; gets(name); cout<<"Enter Employee Number: "; cin>>enumb; } void putdata() { cout<<"Name: "<<name<<"\t"; cout<<"Emp. No: "<<enumb<<"\t"; cout<<"Basic Salary: "<<basic; } protected: float basic; void getbasic() { cout<<"Enter Basic: "; cin>>basic; } }; class MANAGER:private EMPLOYEE { private: char title[30]; public: void getdata() { EMPLOYEE::getdata(); getbasic(); cout<<"Enter Title: "; gets(title); } void putdata() { EMPLOYEE::putdata(); cout<<"\tTitle: "<<title<<"\n"; } }; void main() { clrscr(); MANAGER mng1, mng2; cout<<"Manager 1\n"; mng1.getdata(); cout<<"\nManager 2\n"; mng2.getdata(); cout<<"\n\t\tManager 1 Details\n"; mng1.putdata(); cout<<"\n\t\tManager 2 Details\n"; mng2.putdata(); getch(); }
Here is the sample output of this C++ program:

The derived class inherits all the members of the base class. However, the derived class has the direct access privilege only to the non-private members of the base class. Let's take an example program.
/* C++ Inheritance - Example Program */ #include<iostream.h> #include<conio.h> class BASE { int a; public: int b; void getdata() { cin>>a; } void putdata() { cout<<a; } }; class DERIVED:public BASE { int p; public: int q; }; void main() { clrscr(); cout<<"Size of BASE Class: "<<sizeof(BASE)<<"\n"; cout<<"Size of DERIVED Class: "<<sizeof(DERIVED)<<"\n"; BASE bobj; DERIVED dobj; cout<<"\nSize of BASE Class Object: "<<sizeof(bobj)<<"\n"; cout<<"Size of DERIVED Class Object: "<<sizeof(dobj); getch(); }
Below is the sample output of the above C++ program:

Constructors in Multiple Inheritance
Here is an example program illustrate the working of constructors and destructors in multiple inheritance
/* C++ Inheritance - Example Program */ #include<iostream.h> #include<conio.h> class BASE1 { protected: int a; public: BASE1(int x) { a=x; cout<<"Constructing BASE1\n"; } ~BASE1() { cout<<"Destructing BASE1\n"; } }; class BASE2 { protected: int b; public: BASE2(int y) { b=y; cout<<"Constructing BASE2\n"; } ~BASE2() { cout<<"Destructing BASE2\n"; } }; class DERIVED:public BASE2, public BASE1 { int c; public: DERIVED(int i, int j, int k):BASE2(i),BASE1(j) { c=k; cout<<"Constructing DERIVED\n"; } ~DERIVED() { cout<<"Destructing DERIVED\n"; } void show() { cout<<"1."<<a<<"\t2."<<b<<"\t3."<<c<<"\n"; } }; void main() { clrscr(); DERIVED obj(10,11,12); obj.show(); getch(); }
Here is the sample output of the above C++ program:

Virtual Base Classes
Below is an example program to show the working of virtual base classes
/* C++ Inheritance - Example Program */ #include<iostream.h> #include<conio.h> class BASE { public: int a; }; class DERIVED1:virtual public BASE { public: int b; }; class DERIVED2:virtual public BASE { public: int c; }; class DERIVED3:public DERIVED1, public DERIVED2 { public: int total; }; void main() { clrscr(); DERIVED3 obj; obj.a=15; obj.b=20; obj.c=25; obj.total=obj.a+obj.b+obj.c; cout<<obj.a<<"\t"<<obj.b<<"\t"<<obj.c<<"\t"<<obj.total<<"\n"; getch(); }
Here is the sample run of this C++ program:

« Previous Tutorial Next Tutorial »