How To Implement Inheritance In C++

How To Implement Inheritance In C++

How To Implement Inheritance In C++



How To Implement Inheritance In C++



How To Implement Inheritance In C++

It has 5 types
1. Single inheritance
2. Multi level inheritance
3. Multiple inheritance
4. Hybrid inheritance
5. Hierachical inheritance

Single inheritance

#include<iostream.h>

#include<conio.h>
class arun
{
public:
char name [20];
int age;
void getdata()
{
cout<<"Enter the name and age is:"<<endl;
cin>>name>>age;
}
void putdata()
{
cout<<"The given name is:"<<name<<endl;
cout<<"The given age is:"<<age<<endl;
}
};
class kumar;public arun
{
public:
int mark1,int mark2;
void getmark()
{
cout<<"Enter the mark1 and mark2 is:"<<endl;
cin>>mark1>>mark2;
}
void putmark()
{
cout<<"The given mark1 is:"<<mark1<<endl;
cout<<"The given mark2 is:"<<mark2<<endl;
}
};
void main()
{
kumar a;
clrscr();
a.getdata();
a.putdata();
a.getmark();
a.putmark();
getch();
}

Multi level inheritance

#include<iostream.h>
#include<conio.h>
class arun
{
public:
char name [20];
int age;
void getdata()
{
cout<<"Enter the name and age is:"<<endl;
cin>>name>>age;
}
void putdata()
{
cout<<"The given name is:"<<name<<endl;
cout<<"The given age is:"<<age<<endl;
}
};
class kumar:public arun
{
public:
int mark1,int mark2;
void getmark()
{
cout<<"Enter the name and age is:"<<endl;
cin>>mark1>>mark2;
}
void putmark()
{
cout<<"The given mark1 is:"<<mark1<<endl;
cout<<"The given mark2 is:"<<mark2<<endl;
}
};
class selva:public kumar
{
public:
int total,average;
void display()
{
total=mark1+mark2;
average=total/2;
cout<<"The given total is:"<<total<<endl;
cout<<"The given sum is:"<<average<<endl;
}
};
void main()
{
selva a;
clrscr();
a.getdata();
a.putdata();
a.getmark();
a.putmark();
a.display();
getch();
}

Multiple inheritance

#include<iostream.h>
#include<conio.h>
class arun
{
public:
char name [20];
int age;
void getdata()
{
cout<<"Enter the name and age is:"<<endl;
cin>>name>>age;
}
void putdata()
{
cout<<"The given name is:"<<name<<endl;
cout<<"The given age is:"<<age<<endl;
}
};
class kumar
{
public:
int mark1,mark2;
void getmark()
{
cout<<"Enter the mark1 and mark2 is:"<<endl;
cin>>mark1>>mark2;
}
void putmark()
{
cout<<"The given mark1 is:"<<mark1<<endl;
cout<<"The given mark2 is:"<<mark2<<endl;
}
};
class selva:public arun,public kumar
{
public:
int total,average;
void display()
{
total=mark1+mark2;
average=total/2;
cout<<"The given total is:"<<total<<endl;
cout<<"The given average is:"<<average<<endl;
}
};
void main()
{
selva a;
clrscr();
a.getdata();
a.putdata();
a.getmark();
a.putmark();
a.display();
getch();
}

Hybrid inheritance

#include<iostream.h>
#include<conio.h>
class arun
{
public:
char name [20];
int age;
void getdata()
{
cout<<"Enter the name and age is:"<<endl;
cin>>name>>age;
}
void putdata()
{
cout<<"The given name is:"<<name<<endl;
cout<<"The given age is:"<<age<<endl;
}
};
class kumar:public arun
{
public:
int mark1,int mark2;
void getmark()
{
cout<<"Enter the mark1 and mark2 is:"<<endl;
cin>>mark1>>mark2;
}
void putmark()
{
cout<<"The given mark1 is:"<<mark1<<endl;
cout<<"The given mark2 is:"<<mark2<<endl;
}
};
class sport
{
public:
int sport;
void getsport()
{
cout<<"Enter the sport mark1 is :"<<endl;
cin>>sport;
}
void putsport()
{
cout<<"The given sport mark is:"<<sport<<endl;
}
};
class selva:public kumar,public sport
{
public:
int total,average;
void display()
{
total=mark1+mark2+sport;
average=total/3;
cout<<"The given total is:"<<total<<endl;
cout<<"The given average is:"<<average<<endl;
}
};
void main()
{
selva a;
clrscr();
a.getdata();
a.putdata();
a.getmark();
a.putmark();
a.getsport();
a.putsport();
a.display();
getch();
}

Hierachical inheritance

#include<iostream.h>
#include<conio.h>
class college
{
public:
char name [20];
int age;
void getdata()
{
cout<<"Enter the name and age is:"<<endl;
cin>>name>>age;
}
void pudata()
{
cout<<"The given name is:"<<name<<endl;
cout<<"The given age is:"<<age<<endl;
}
};
class arts:public college
{
public:
int mark1,int mark2;
void getamark()
{
cout<<"Enter the arts mark1 and mark2 is:"<<endl;
cin>>mark1>>mark2;
}
void putmark()
{
cout<<"The given arts mark1 is:"<<mark1<<endl;
cout<<"The given art mark2 is:"<<mark2<<endl;
}
};
class science:public college
{
public:
int mark1,mark2;
void getsmark()
{
cout<<"Enter the science mark1 and mark2 is:"<<endl;
cin>>mark1>>mark2;
}
void putsmark()
{
cout<<"The given science mark1 is:"<<mark1<<endl;
cout<<"The given science mark2 is:"<<mark2<<endl;
}
};
class commerce:public college
{
public:
int mark1,mark2;
void getcmark()
{
cout<<"Enter the commerce mark1 and mark2 is:"<<endl;
cin>>mark1>>mark2;
}
void putmark()
cout<<"The given commerce mark1 is:"<<mark1<<endl;
cout<<"The given commerce mark2 is:"<<mark2<<endl;
}
};
void main()
{
arts a;
clrscr();
a.getdata();
a.putdata();
a.getamark();
a.putamark();
science s;
s.getsmark();
s.putsmark();
commerce c;
c.getcmark();
c.putcmark();
getch();

}

Post a Comment

0 Comments