Home | | Programming and Data Structure II | Inheritance and its Type

Chapter: Programming and Data structures : Object Oriented Programming Concepts

Inheritance and its Type

Type of Inheritance: · Single Inheritance,· Multiple Inheritance,· Multilevel Inheritance, · Hybrid Inheritance, · Hierarchical Inheritance.

INHERITANCE:

 

One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.

 

When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.

 

The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.

 

 

Base & Derived Classes:

 

A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes. To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form:

 

class derived-class: access-specifier base-class

 

Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default.

 

Type of Inheritance:

 

·        Single Inheritance

 

·        Multiple Inheritance

 

·        Multilevel Inheritance

 

·        Hybrid Inheritance

 

·        Hierarchical Inheritance

 

 

1. Single Inheritance:

 

 


 

Example:

 

#include<iostream.h>

 

#include<conio.h>

class student

{

 

public:

 

char name[10],collname[10],dept[5];

void getfun()

{

 

cout<<"Enter Name,College,Department\n";

cin>>name>>collname>>dept;

}

 

};

 

 

class cse:public student

 

{

 

public:

 

 

char sub1[5],sub2[5],sub3[5];

void getdet()

 

{

 

getfun();

 

cout<<"Enter subject 1,2,3";

cin>>sub1>>sub2>>sub3;

}

 

void putdet()

 

{

 

cout<<"Name\t"<<name<<"College name\n"<<collname<<"Department\t"<<dept <<"Subject 1,2,3\t"<<sub1<<sub2<<sub3;

}

 

};

 

 

void main()

 

{

 

clrscr();

cse c1;

c1.getdet();

c1.putdet();

getch();

 

}

 

 

Output:

 

Enter Name,College,Department paul

 

fx cse

 

Enter subject 1,2,3 maths

 

pds evs

 

Name paul College name fx Department cse subject 1,2,3 maths pds        evs

 

2. Multiple Inheritance


A derived class with several base classes is called multiple inheritance.

 

 

Example:

 

#include<iostream.h>

 

#include<conio.h>

class student

{

 

public:

 

char name[10],collname[10],dept[5];

void getfun()

 

{

 

cout<<"Enter name,college name,department\n";

cin>>name>>collname>>dept;

}

 

};

 

class internal

 

{

 

public:

 

char sub1[5],sub2[5],sub3[5];

int im1,im2,im3;

 

void getdet()

 

{

 

cout<<"Enter subject 1,2,3\n";

cin>>sub1>>sub2>>sub3;

 

cout<<"Enter internal marks for subject1,2,3\n";

 

cin>>im1>>im2>>im3;

 

}

 

};

 

 

class cse:public internal,public student

 

{

 

public:

 

int ex1,ex2,ex3,t1,t2,t3;

void calc()

{

 

getfun();

 

getdet();

 

cout<<"Enter external marks for subject1,2,3\n";

cin>>ex1>>ex2>>ex3;

 

t1=ex1+im1;

 

t2=ex2+im2;

 

t3=ex3+im3;

 

}

 

void putdet()

 

{

 

cout<<"\nname\t"<<name<<"collname\t"<<collname<<"dept\t"<<dept<<"\n" <<"sub1 Total\t"<<t1<<"sub2\t"<<t2<<"sub3\t"<<t3<<"\n";

 

}

 

};

 

 

void main()

 

{

 

clrscr();

cse c1;

c1.calc();

c1.putdet();

getch();

 

}

 

Output:

Enter name,college name,department

paul

Fx

cse

Enter subject 1,2,3

maths

pds

dbms

Enter internal marks for subject1,2,3

50

50

50

Enter external marks for subject1,2,3

50

50

50

name paul collname fx dept cse

sub1 Total 100 sub2 100 sub3 100

 

 

3. Multilevel Inheritance


The mechanism of deriving a class from another derived class is known as multilevel inheritance.


#include<iostream.h>

 

#include<conio.h>

 

#include<iomanip.h>

class student

{

 

public:

 

char name[10],clgname[10];

 

int age,year;

void get()

{

 

cout<<"Enter the Name,College Name,Age, Year:\n";

cin>>name>>clgname>>age>>year;

 

}

 

void put()

 

{

 

cout<<"Name:\t"<<"College Name:\t"<<"Age:\t" <<"Year:\t"<<"\n";

 

cout<<name<<"\t"<<clgname<<"\t"<<age<<"\t"<<year<<"\n";

 

}

 

};

 

class test:public student

 

{

 

public:

 

int m1,m2,m3;

void getmarks()

 

{

 

cout<<"Enter mark1,2,3";

cin>>m1>>m2>>m3;

}

 

void putmarks()

 

{

 

cout<<"Mark1:\t"<<"Mark2\t"<<"Mark3\t"<<"\n";

 

cout<<m1<<"\t"<<m2<<"\t"<<m3<<"\n";

 

}

 

};

 

class result:public test

 

{

 

public:

 

int i1,i2,i3,t1,t2,t3;

 

 

void getinternal()

 

{

 

cout<<"Enter the internal marks:";

cin>>i1>>i2>>i3;

 

}

 

void finalmark()

 

{

 

t1=i1+m1;

 

t2=i2+m2;

 

t3=i3+m3;

 

cout<<"Result M1,M2,M3:";

cout<<"\t"<<t1<<"\t"<<t2<<"\t"<<t3<<"\n";

}

 

};

 

void main()

 

{

 

clrscr();

result r1;

r1.get();

 

r1.getmarks();

 

r1.getinternal();

 

r1.put();

 

r1.putmarks();

 

r1.finalmark();

 

getch();

 

}

 

In this example the student class is derived into test class and then the test class further derived into result class. So the result class inherits the properties of both student and test class.

 

4. Hierarchical Inheritance

 

Hierarchical Inheritance is a method of inheritance where one or more derived classes are derived from common base class.


Example:

 

#include <iostream.h>

class Side

{

 

public: int l;

void set_values (int x)

 

{

 

l=x;

 

}

 

};

 

class Square: public Side

 

{

 

public: int sq()

{

 

return (l *l);

 

}

 

};

 

class Cube:public Side

 

{

 

public: int cub()

{

 

return (l *l*l);

 

}

 

};

 

int main ()

 

{

 

Square s;

 

s.set_values (10);

 

cout << "The square value is::" << s.sq() << endl;

Cube c;

 

c.set_values (20);

 

cout << "The cube value is::" << c.cub() << endl;

return 0;

}

 

 

 

Output:

 

The square value is:: 100

 

The cube value is::8000

 

 

In the above example the two derived classes "Square", "Cube" uses a single base class "Side". Thus two classes are inherited from a single class. This is the hierarchical inheritance OOP's concept in C++.

5. Hybrid Inheritance

 

Hybrid inheritance is combination of two or more inheritances such as single, multiple, multilevel.

 


 

Example:

 

#include <iostream.h>

class mm

{

 

protected: int rollno;

public:

 

void get_num(int a)

 

{

 

rollno = a;

 

}

 

void put_num()

 

{

 

cout << "Roll Number Is:"<< rollno << "\n";

 

}

 

};

 

class marks : public mm

 

{

 

protected: int sub1;

int sub2; public:

 

void get_marks(int x,int y)

 

{

 

sub1 = x;

sub2 = y;

 

}

 

void put_marks(void)

 

{

 

cout << "Subject 1:";

cout<< sub1 ;

cout<< "\n";

 

cout << "Subject 2:";

cout << sub2;

 

cout << "\n";

 

}

 

};

 

class extra

 

{

 

protected: float e;

public:

 

void get_extra(float s)

 

{

 

e=s;

 

}

 

void put_extra(void)

 

{

 

cout << "Extra Score::" << e; cout<< "\n";

 

}

 

};

 

class res : public marks, public extra

 

{

 

protected: float tot;

 

 

public:

 

void disp(void)

 

{

 

tot = sub1+sub2+e;

put_num();

put_marks();

put_extra();

cout << "Total:"<< tot;

 

}

 

};

 

int main()

 

{

 

res std1;

std1.get_num(10);

std1.get_marks(10,20);

std1.get_extra(33.12);

std1.disp();

return 0;

 

}

 

 

Output:

 

Roll Number Is: 10

 

Subject 1: 10

 

Subject 2: 20

 

Extra score:33.12

 

Total: 63.12

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Programming and Data structures : Object Oriented Programming Concepts : Inheritance and its Type |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.