Lab 10 Oops SOHAN

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

[Nehal Anwar(22DCS032)]

[Sohini;22DCS057]

Program No – 10

//Write programs in C++ to implement all type of inheritance.


//Single Inheritance

#include<iostream.h>

#include<conio.h>

class A { int a,b;

public: int mul()

cout<<”Enter the value of a & b : ”;

cin>>a>>b; return a*b;

};

Class B : public A

public:

void display()

{ int result = mul();

cout<<”\nMultiplication = ”<<result;

} };

int main()
{
clrscr();

B b;

b.display();

getch();

return 0;

Program No - 10
[Nehal Anwar(22DCS032)]
[Sohini;22DCS057]

/*Output
Enter the value of a & b : 23

Multiplication = 69

*/

Program No – 10
[Nehal [Sohini;22DCS057]
Anwar(22DCS032)]

//Multiple Inheritance

#include<iostream.h>

#include<conio.h>

class A { public: int

x; void getx()

{ cout<<”Enter the value of x

:-“; cin>>x;

} };

class B {

public:

int y;

void gety()

{ cout<<”Enter the value of y

:-“; cin>>y;

} }; class

{ public:

int z; void

getz()

{ cout<<”Enter the value of z

:-“; cin>>z;

} };

class D : public A , public B , public C

public:

void sum()

cout<<:Sum = “<<x+y+z;

Program No - 10
[Sohini;22DCS057]
[Nehal Anwar(22DCS032)]

} };

intmain()

{
clrscr();

D d;

d.getx();

d.gety();

d.getz();

d.sum();

getch(); return

0;

/*Output
Enter the value of x :- 40

Enter the value of y :- 20

Enter the value of z :- 10

Sum = 70

*/

Program No – 10
[Nehal Anwar(22DCS032)]
[Sohini;22DCS057]

//Hierarchical Inheritance

#include<iostream.h>

#include<conio.h>

class A { public: int

x,y; void getdata()

cout<<”Enter the value of x and y :- “;

cin>>x>>y;

} };

class B : public A

{ public:

void product()

cout<<”Product = “<<x * y;

} }; class C :

public A

{ public:

void sum()

cout<<”Sum = “<<x + y;

} }; class D :

public A

{ public:

void subtract()

cout<<”Subtraction = “<<x - y;

} }; int main() {

Program No - 10
[Sohini;22DCS057]

[Nehal Anwar(22DCS032)]

clrscr();

B b;

C c;

D d;

b.getdata();

b.product();

c.getdata();

c.sum();

d.getdata();

d.subtract();

getch(); return

0;

/* Output
Enter the value of x and y :- 5

Product = 30

Enter the value of x and y :- 40

50

Sum = 90

Enter the value of x and y :- 75

50

Subtraction = 25

*/

Program No – 10
[Sohini;22DCS057]

[Nehal Anwar(22DCS032)]

//Multilevel Inheritance

#include<iostream.h>

#include<conio.h>

class A { public:

int x;

void getdata()

{ cout<<”Enter the value of x

:- “; cin>>x;

} }; class B :

public A

{ public: int

y; void

readdata()

{ cout<<”Enter the value of y

:- “; cin>>y;

} }; class C :

public B

{ public:

int z; void

indata()

{ cout<<”Enter the value of z

:- “; cin>>z;

void product()

cout<<”Product = “<<x*y*z;

}};

Program No – 10
[Sohini;22DCS057]

[Nehal Anwar(22DCS032)]

Program No – 10
[Nehal[Sohini;22DCS057]
Anwar(22DCS032)]

int main()

clrscr();

C c;

c.getdata();

c.readdata();

c.indata();

c.product();

getch(); return

0;

/* Output
Enter the value of x :- 7

Enter the value of y :- 4

Enter the value of z :- 2

Product = 56

*/

Program No – 10
[Sohini;22DCS057]
[Nehal Anwar(22DCS032)]

//Hybrid Inheritance

#include<iostream.h>

#include<conio.h>

class A { public: int

x;

};
class B : public A

public :

B() {

x = 10;

} };

class C {

public:

int y;

C() {

y = 4;

} }; class D : public B,

public C

{ public:

void sum()

cout<<”Sum = ”<<x+y;

} }; int

main() {

clrscr();

D d;

Program No 10
[Nehal Anwar(22DCS032)]

d.sum();

getch();

return 0; }

/*Output
Sum = 14

*/

Program No – 10

You might also like