0% found this document useful (0 votes)
5 views7 pages

Inheritance_Program

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

Inheritance_Program

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

#include <iostream>

using namespace std;


class B
{
int a;
public:
int b;
void set_ab();
int get_a(void);
void show_a(void);
};
class D : public B
{
int c;
public:
void mul(void);
void display(void);
};
void B :: set_ab(void)
{
a = 5; b = 10;
}
int B :: get_a()
{
return a;
}
void B :: show_a()
{
cout <<"a =" <<a <<"\n";
}
void D :: mul()
{
c = b * get_a();
}
void D :: display()
{
cout <<"a =" <<get_a() <<"\n";
cout <<"b =" <<b <<"\n";
cout <<"c =" <<c <<"\n";
}
int main()
{
D d;
d.set_ab();
d.mul();
d.show_a();
d.display();
d.b = 20;
d.mul();
d.display();
return 0;
}
O/P
a =5
a =5
b =10
c =50
a =5
b =20
c =100

Private Derivation
#include <iostream>
using namespace std;
class B
{
int a; // private; not inheritable
public:
int b; // public; ready for inheritance
void get_ab();
int get_a(void);
void show_a(void);
};
class D : private B // private derivation
{
int c;
public:
void mul(void);
void display(void);
};
//————————————————————————————————
void B :: get_ab(void)
{
cout << "Enter values for a and b";
cin >> a >> b;
}
int B :: get_a()
{
return a;
}
void B :: show_a()
{
cout <<"a =" <<a <<"\n";
}
void D :: mul()
{
get_ab();
c = b * get_a(); // 'a' cannot be used directly
}
void D :: display()
{
show_a(); // outputs value of 'a'
cout << "b =" << b << "\n";
cout << "c = " << c << "\n";
}
//———————————————————————————————
int main()
{
D d;
// d.get_ab(); WON’T WORK
d.mul();
// d.get_a();WON’T WORK
d.display();
// d.b = 20; WON’T WORK; b has become private
d.mul();
d.display();
return 0;
}

O/P
Enter values for a and b5
10
a =5
b =10
c = 50
Enter values for a and b
Multilevel Inheritance:
#include <iostream>
using namespace std;
class student
{
protected:
int roll_number;
public:
void getnumber(int);
void put_number(void);
};
void student :: getnumber(int a)
{
roll_number = a;
}
void student :: put_number()
{
cout << "Roll Number: " << roll_number << "\n";
}
class test : public student // First level derivation
{
protected:
float sub1;
float sub2;
public:
void get_marks(float, float);
void put_marks(void);
};
void test :: get_marks(float x, float y)
{
sub1 = x;

sub2 = y;
}
void test :: put_marks()
{
cout << "Marks in SUB1 = " << sub1 << "\n";
cout << "Marks in SUB2 = " << sub2 << "\n";
}
class result : public test // Second level derivation
{
float total; // private by default
public:
void display(void);
};
void result :: display(void)
{
total = sub1 + sub2;
put_number();
put_marks();
cout << "Total = " << total << "\n";
}
int main()
{
result student1; // student 1 created
student1.getnumber(111);
student1.get_marks(75.0, 59.5);
student1.display();
return 0;
}

O/P
Roll Number: 111
Marks in SUB1 = 75
Marks in SUB2 = 59.5
Total = 134.5
Multiple Inheritances
#include <iostream>
using namespace std;
class M
{
protected:
int m;
public:
void get_m(int);
};
class N
{
protected:
int n;
public:
void get_n(int);
};
class P : public M, public N
{
public:
void display(void);
};
void M :: get_m(int x)
{
m = x;
}
void N :: get_n(int y)
{
n = y;
}
void P :: display(void)
{
cout << "m = " << m << "\n";
cout << "n = " << n << "\n";
cout << "m*n = " << m*n << "\n";
}
int main()
{
P p;
p.get_m(10);
p.get_n(20);
p.display();
}

O/P
m = 10
n = 20
m*n = 200

You might also like