Inheritance in C++ Language

Ratings:
(4)
Views: 965
Banner-Img
Share this blog:

Inheritance

Inheritance is a process of acquiring the properties and behavior of one class inside another class. Inheritance is process of using data member functions of one class inside another class. Deriving a class from existing class is called as inheritance.  

Advantages of Inheritance in C++

Generalization/reusability Specialization /extensibility  

Types of Inheritance supported by C++

Single Level Inheritance Multi level Inheritance Multiple inheritance Hierarchal inheritance Hybrid Inheritance  

Example of Inheritance in C++

Screenshot_23     How to derive a class Syntax class derived –classname:<visibility Mode>base-classname, base-class-name,. . . . . . . . { data members; Member functions; };

What is visibility mode?

Visibility Mode defines the visibility or accessibility of base class members with in derived class. This visibility mode can be

  1. Private
  2. Public
  3. Protected

Default visibility mode is private. When the visibility mode is private, public members of private members of derived class. The public members of the base class are accessibility by the member function of the derived class only. They are not accessible to the derived class object In both cases private members of the base class are not accessible to the derived class of member functions. They are accessible to the derived class objects. Screenshot_24 When the visibility Mode is public, public members of the base class becomes the public members of derived class.  

Single level inheritance in C++

A derived class with a only one base  class is called single level inheritance. Example Class base { . . . . . . .  . . . .  . }; class derived:public base { . . . . . . .  . . . .  . }; Example   # include <iostream.h> class person { char name[10]; public; void read_name() { cout<<”\n Input name”; cin>> name; } void print_name() { cout<<”\n Name”<<name; } }; class employee :public person { float salary; public: void read_salary() { cout<<”\n Input salary”; cin>>salary; } void print _salary() { cout<<”\n Salary is “<<salary; }}; void  main() {employee emp1; cout <<”\n << size of (emp1); emp1.read_name (); emp1.read_salary(); emp1.print_name(); emp1.print_salary(); } O/p : Input name a aa Input Salary 5000 Name aaa Salary is 5000 On creation of derived class object .Memory is allocated for data members of base class and derived class. Private Members of base class are not accessible derived class if base class provide public member functions it can access it.

Protected Member in C++

A protected member is accessible by the members functions class is immediately derived from it. It cannot be accessed by the functions outside these two classes.   Screenshot_26   Example # include <iostream.h> class employee { protected : int empno; char name[10]; }; class salaried employee :public employee { float salary; public: void read() { cout<<”\n Input empno”; cin>> empno; cout<<”\n Input ename”; cin>> ename; cout<<”\n Input salary”; cin >>salary; } void print() { cout<<”\n Employee No”<<empno; cout<<”\n Employee Name”<< name; cout<<”\n Salary”<< salary; }}; void main() { salaried _employee emp1; emp1.read(); emp1.printf(); }  

You liked the article?

Like: 0

Vote for difficulty

Current difficulty (Avg): Medium

EasyMediumHardDifficultExpert
IMPROVE ARTICLEReport Issue

About Author

Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.

Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox