• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Inheritance in C++ Language

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();

}

 

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Inheritance in C++ Language"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.