Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 5

Single Level Inheritance

#include<iostream> class marks : public student


{
using namespace std; private:
class student int m1,m2,m3;
{ public:
void getmarks()
protected: { // getdata();
int rno; cout<<"Enter Marks 1 :- \t";
char name[20]; cin>>m1;
cout<<"Enter Marks 2 :- \t";
public: cin>>m2;
void getdata() cout<<"Enter Marks 2 :- \t";
{ cin>>m3; }
void display()
cout<<"Enter RollNo :- \t";
{ cout<<rno<<"\t"<<name<<"\
cin>>rno; t"<<m1<<"\t"<<m2<<"\t"<<m3; }
cout<<"Enter Name :- \t"; };
cin>>name; main(){ marks std; std.getdata();
std.getmarks(); std.display();
SATHIYA
} RR
Multiple Inheritance
#include<iostream.h> class statement:public student,public sports
using namespace std; {
int tot,avg;
class student
public:
{ protected: int rno,m1,m2;
void display()
public: {
void get(){ tot=(m1+m2+sm);
cout<<"Enter the Roll no :"; avg=tot/3;
cin>>rno; cout<<"\n\n\tRoll No : <rno<<"\n\tTotal: <<tot;
cout<<"Enter the two marks :"; cout<<"\n\tAverage : "<<avg;
cin>>m1>>m2; }}; }
class sports };
main()
{ protected:
{
int sm; // sm = Sports mark
statement obj;
public: void getsm() { obj.get();
cout<<"\nEnter the sports mark :"; obj.getsm();
cin>>sm; }}; obj.display();
SATHIYA RR
}
Hierarchical Inheritance
#include<iostream> class Triangle: public Shape
{
using namespace std;
public:
class Shape float area ()
{ {
protected: return (width * height / 2);
float width, height; }
public: };
void set_data (float a, float b)
main ()
{ width = a; {
height = b; Rectangle rect;
} Triangle tri;
}; rect.set_data (5,3);
class Rectangle: public Shape tri.set_data (2,5);
cout << rect.area() << endl;
{
cout << tri.area() << endl;
public:
float area () }
{ return (width * height);
}
}; SATHIYA RR
Multilevel Inheritance
#include<iostream> void student:: display()
{ cout <<"Roll NO:"<<rl<<endl;
#include<bits/stdc++.h>
cout<<"name : "<<nm<<endl; }
using namespace std; void marks ::getmarks()
class student // base class { cout<<"enter three subject marks "<<endl;
{ cin>>s1>>s2>>s3; }
protected: int rl; char nm[20]; void marks ::putmarks()
public: void read(); void display(); }; { cout <<"subject 1:"<<s1<<endl;
cout<<" subject 2 : "<<s2<<endl;
class marks : public student
cout <<"subject 3:"<<s3<<endl; }
{ void result::process()
protected: int s1, s2, s3; {t= s1+s2+s3;
public: p = t/3.0;
void getmarks(); void putmarks(); }; p>=60?strcpy(div,"first"):p>=50?strcpy(div, "second"):
class result : public marks strcpy(div,"third");
}
{
void result::printresult()
private: int t; float p; char div[10]; {cout<<"total = "<<t<<endl;
public: void process(); void printresult(); }; cout<<"per = "<<p<<endl;
void student::read() { cout<<"div = "<<div<<endl; }
cout<<"enter Roll no and Name "<<endl; main()
cin>>rl>>nm; } { result x; x.read(); x.getmarks(); x.process();
SATHIYA RR
x.display(); x.putmarks(); x.printresult(); }
Hybrid Inheritance
#include<iostream> class negative
using namespace std; {protected:
class arithmetic int n1,n2,diff;
{protected: public:
int num1, num2; void sub()
public: {cout<<"\nFor Subtraction:";
void getdata() cout<<"\nEnter the first number: ";
{cout<<"For Addition:"; cin>>n1;
cout<<"\nEnter the first number: "; cout<<"\nEnter the second number: ";
cin>>num1; cin>>n2;
cout<<"\nEnter the second number: "; diff=n1-n2; } };
cin>>num2; }}; class result:public positive, public negative
class positive:public arithmetic {public:
{protected: void display(){
int sum; cout<<"\nSum of "<<num1<<" and "<<num2<<"=
public: "<<sum;
void add() cout<<"\nDifference of "<<n1<<" and "<<n2<<"=
{getdata(); "<<diff;} };
sum=num1+num2; } }; main(){result z;z.add();z.sub();z.display();}
SATHIYA RR

You might also like