Beruflich Dokumente
Kultur Dokumente
By
Prof. Manikandan
QMC, Chennai.
manisankar27@gmail.com
DEFINITION
class:
Derived class:
Having
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
include<iostream.h>
#include<conio.h>
class emp
{
public:
int eno;
char name[20],des[20];
void get()
{
};
cin>>des;
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
SYNTAX
class A
{
}; // Base class
class B: public A
{
}; // B derived from A
class C: public B
{
}; // C derived from B
# 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;
}
//OUTPUT
Roll Number: 111
Marks in SUB1 = 75
Marks in SUB2 = 59.5
Total = 134.5
MULTILPLE INHERITANCE
#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
#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
{
};
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