0% found this document useful (0 votes)
30 views10 pages

Hierachical, Multiple, Multileve, Hybrid Inheritance

The document provides examples of various types of inheritance in C++ programming, including single, hierarchical, multiple, multilevel, and hybrid inheritance. Each section contains code snippets demonstrating how classes can inherit properties and methods from base classes. The examples illustrate the creation of derived classes and the use of methods to manipulate and display data.

Uploaded by

saadkhan73212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views10 pages

Hierachical, Multiple, Multileve, Hybrid Inheritance

The document provides examples of various types of inheritance in C++ programming, including single, hierarchical, multiple, multilevel, and hybrid inheritance. Each section contains code snippets demonstrating how classes can inherit properties and methods from base classes. The examples illustrate the creation of derived classes and the use of methods to manipulate and display data.

Uploaded by

saadkhan73212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

channel link : h ps://www.youtube.

com/c/ComputerScienceAcademy7

#include<iostream.h>
#include<conio.h>

// 1. Single inheritance

/* class number //base class


{
protected:
int a;
public:
void getdata(int x)
{
a = x;
}

void display()
{
cout<<"Number = "<<a<<endl;

}
};

class square : public number


{
private:
int sq;
public:
void getsquare()
{
sq = a * a;

void displaysq()
{
cout<<"Square = "<<sq<<endl;

}
};
void main()
{
clrscr();

square s;
s.getdata(25);
s.display();
s.getsquare();
s.displaysq();

getch();

*/

// 2. heirachical inheritance
/*
number

square cube */

/*
class number //base class
{
protected:
int a;
public:
void getdata(int x)
{
a = x;
}

void display()
{
cout<<"Number = "<<a<<endl;

}
};
class square : public number //derived class 1
{
private:
int sq;
public:
void getsquare()
{
sq = a * a;

void displaysq()
{
cout<<"Square = "<<sq<<endl;

}
};

class cube : public number //derived class


{
private:
int cb;
public:
void getcube()
{
cb = a * a * a;

void displaycb()
{
cout<<"Cube = "<<cb<<endl;

}
};

void main()
{
clrscr();
square s;
s.getdata(25);
s.display();
s.getsquare();
s.displaysq();

cube c;
c.getdata(5);
c.display();
c.getcube();
c.displaycb();

getch();

*/

// 3. Mul ple inheritance


/*
length breadth
rectangle
*/

/* class length //base class 1


{
protected: float l;

public:
void getlen()
{
cout<<"Enter length of rectangle: ";
cin>>l;
}
};

class breadth //base class 2


{
protected: float b;

public:
void getbre()
{
cout<<"Enter breadth of rectangle: ";
cin>>b;
}
};

class rectangle : public length, public breadth


{
private:
float area;

public:
void calcula on()
{
area = l * b;
}

void display()
{
cout<<"Area of rectangle = "<<area;
}
};

void main()
{
clrscr();

rectangle R;
R.getlen();
R.getbre();

R.calcula on();
R.display();

getch();

}
*/
// 4. Mul level inheritance

/* student

marks

result
*/

/* class student //base class


{
protected: int rn;

public:
void getrn()
{
cout<<"Enter your roll number: ";
cin>>rn;
}
void displayrn()
{
cout<<"Your Roll number = "<<rn<<endl;
}
};

class marks : public student //derived class / base class for next derived class
{
protected: int phy, chem;

public:
void getmarks()
{
cout<<"Enter marks obtained in phy & chem = ";
cin>>phy>>chem;
}

void displaymarks()
{
cout<<"marks in phy = "<<phy<<endl;
cout<<"marks in chem = "<<chem<<endl;
}
};

class result : public marks // derived class from other derived class
{
private:
int totalmarks;

public:
void display()
{
displayrn();
displaymarks();

totalmarks = phy + chem;

cout<<"Total marks = "<<totalmarks;


}

};

void main()
{
clrscr();

result R;
R.getrn();
R.getmarks();
R.display();

getch();

}
*/

// 5. Hybrid inheritance

/* student

marks sports
result
*/

class student //base class


{
protected: int rn;

public:
void getrn()
{
cout<<"Enter your roll number: ";
cin>>rn;
}
void displayrn()
{
cout<<"Your Roll number = "<<rn<<endl;
}
};

class marks : public student //derived class / base class for next derived class
{
protected: int phy, chem;

public:
void getmarks()
{
cout<<"Enter marks obtained in phy & chem = ";
cin>>phy>>chem;
}

void displaymarks()
{
cout<<"marks in phy = "<<phy<<endl;
cout<<"marks in chem = "<<chem<<endl;

}
};

class sports
{
protected: int sp;
public:
void getsp()
{
cout<<"Enter sports marks = ";
cin>>sp;

void displaysp()
{
cout<<"Sports marks = "<<sp<<endl;

};

class result : public marks, public sports // derived class from other derived class
{
private:
int totalmarks;

public:
void display()
{
displayrn();
displaymarks();

totalmarks = phy + chem + sp;


displaysp();

cout<<"Total marks = "<<totalmarks;


}

};

void main()
{
clrscr();

result R;
R.getrn();
R.getmarks();
R.getsp();
R.display();

getch();

You might also like