Sie sind auf Seite 1von 31

INHERITANCE

By

Prof. Manikandan

Dept of Computer Application

QMC, Chennai.

manisankar27@gmail.com

DEFINITION

The mechanism of deriving a new class from


an old one is called inheritance (or derivation).

The old class is referred to as the base class


and the new one is called the derived class or
subclass.

The reuse of a class that has already been


tested, debugged and used many times can
the effort of developing and testing the same
again.

DEFINING BASE & DERIVED CLASSES


Base
The

class:

parent class of a derived class.


Classes may be used to crate other classes.
Its super class.

Derived class:

can be defined by specifying its relationship with the


base class in addition own details.

Having

own properties as well as Base class property.

SYNTAX
c1ass derived class-name : visibility-mode base-c1assname
{
//
// members of derived class
//
};
The colon indicates that the derived-class-name is
derived from the base-class-name.
visibility-mode is optional and, if present, may be
either private or public.
The default visibility, mode is private.

EXAMPLE
class ABC: private XYZ // private derivation
{
members of ABC
};
class ABC: public XYZ // public derivation
{
members of ABC
};
class ABC: XYZ // private derivation by default
{
members of ABC
};

TYPES OF INHERITANCE
1.
2.
3.
4.
5.

Single Inheritance
Multilevel Inheritance
Multiple Inheritance
Hierarchical Inheritance
Hybrid Inheritance

SINGLE INHERITANCE

Single Inheritance is method in which a derived


class has only one base class.

include<iostream.h>

#include<conio.h>
class emp
{

public:

int eno;

char name[20],des[20];
void get()
{

cout<<"Enter the employee number:";


cin>>eno;

cout<<"Enter the employee name:";


cin>>name;

cout<<"Enter the designation:";

};

cin>>des;

class salary:public emp //Single derivation


{
float bp,hra,da,pf,np;
public:
void get1()
{
cout<<"Enter the basic pay:";
cin>>bp;
cout<<"Enter the Humen Resource
Allowance:";
cin>>hra;
cout<<"Enter the Dearness Allowance :";
cin>>da;
cout<<"Enter the Profitablity Fund:";
cin>>pf;
}

void calculate()
{
np=bp+hra+da-pf;
}
void display()
{

};

cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<b
p<<"\t"<<hra<<"\t"<<da<<"\t"<<pf<<"\t"<<np
<<"\n";
}

void main()
{
int i,n;
char ch;
salary s[10];
clrscr();
cout<<"Enter the number of employee:";
cin>>n;
for(i=0;i<n;i++)
{
s[i].get();
s[i].get1();
s[i].calculate();
}
cout<<"\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";
for(i=0;i<n;i++)
{
s[i].display();
}
getch();
}

MULTILEVEL INHERITANCE

Where one can inherit from a derived class,


thereby making this derived class the base class
for the new class.

SYNTAX

A derived class with multilevel 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.

# include <iostream.h>
class student
{
protected:
int roll_number;
public:
void get_number(int);
void put_number(void);
};
void student :: get_number(int a)
{
roll_number = a;
}
void student :: put_number(int a)
{
cout << Roll number: << roll_number << \n;
}

class test : public student // First level derivation


{
protected:
float sub1;
float sub2;
public:
void get_marks(float, float);
void put_marks(void);
};
void test :: get_marks(float x, float y)
{
sub1 = x;
sub2 = y;
}
void test :: put_marks()
{
cout << Marks in SUB1 = << sub1 << \n;
cout << Marks in SUB2 = << sub2 << \n;
}

class result : public test // Second level derivation


{
float total; // private by default
public:
void display(void);
};
void result :: display(void)
{
total = sub1 + sub2;
put_number();
put_marks();
cout << Total = << total << \n;
}
int main()
{
result student1; // student1 created
student1.get_number(111);
student1.get_marks(75.0, 59.5);
student1.display();
return 0;
}

//OUTPUT
Roll Number: 111
Marks in SUB1 = 75
Marks in SUB2 = 59.5
Total = 134.5

MULTILPLE INHERITANCE

A class can inherit the attributes of two or more


classes 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


classes is as follows:

where, visibility may be either public or private.


The base classes are separated by commas.

#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks :";
cin>>m1>>m2;
}
};
class sports
{
protected:
int sm;
// sm = Sports mark
public:
void getsm()
{
cout<<"\nEnter the sports mark :";
cin>>sm;
}
};
class statement:public student,public sports
{
int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal
cout<<"\n\tAverage : "<<avg;
}
};
void main()
{
clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();
getch();
}

: "<<tot;

HIERARCHICAL INHERITANCE

Hierarchical Inheritance is a method of inheritance


where one or more derived classes are derived from
common base class.
Application of inheritance is to use it as a support to
the hierarchical design of a program.
Many programming problem can be cast into a
hierarchy where certain features of one level are
shared by many others below that level.

As an example, following Fig. shows a hierarchical


classification of students in a university

#include<iostream.h>
class side
{
protected:
int l;
public:
void set_value(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_value(10);
cout<<s.sq();
cube c;
c.set_value(20);
cout<<c.cub();
return 0;
}

HYBRID INHERITANCE
Hybrid Inheritance is a method where one or more
types of inheritance are combined together and used.

SYNTAX
class sports: public test, public sports
{

};
Where test itself is a derived class from student.
That is
class test : pub1ic student
{

};

// Example Program for Hybrid Inheritance


#include <iostream>
using namespace std;
class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number = a;
}
void put_number(void)
{
cout << Roll No: << roll_number << \n;
}
};
class test : public student
{
protected:
f1oat part1, part2;
public:
void get marks (float x, float y)
{
partl = x;
part2 = y;
}

void put marks(void)


{
cout << "Marks obtained: " << "\n"
<< Part1 = << part1 << "\n"
<< Part2 = << part2 << "\n";
}
};
class sports : public student
{
protected:
float score;
public:
void get_score (float s)
{
score = s;
}
void put score(void)
{
cout << "Sports wt: " << score << "\n\n";
}
};

class result : public test, public sports


{
float tota1;
public:
void display(void);
};
void result :: display(void)
{
total = part1 + part2 + score;
put_number();
put_marks();
put_score();
cout << "Total Score: " << total << "\n;
}
int main()
{
resu1t student _1 ;
student_l.get score(7.0);
student_1.display();
return 0;
}

ADVANTAGES OF INHERITANCE
Inheritance supports reusability.
Easy to modifying existing classes.
New class derived form base class and add new
features.
Inheritance is transitive in nature.

Thank You

Das könnte Ihnen auch gefallen