Write a C++ program with different class related through multiple inheritance and demonstrate the use of different access specifiers by means of member variables and member functions.





Implementation of the above problem:
#include<iostream.h>
#include<conio.h>
class base1
{
protected:
int z;
private:
int y;
public:
int x;
void set_value(int xx,int yy, int zz)
{
x=xx;
y=yy;
z=zz;
}
void display1()
{
cout<<"\nX="<<x;
cout<<"\ny="<<y;
cout<<"\nZ="<<z;
}
};
class base2
{
protected:
int a;

private:
int b;
public:
int c;



void set_values(int aa,int bb, int cc)
{
a=aa;
b=bb;
c=cc;
}
void display2()
{
cout<<"\nA="<<a;
cout<<"\nB="<<b;
cout<<"\nC="<<c;
}
};
class derived:public base1,private base2
{
public:
void display()
{
a=100;
c=200;
cout<<"\nValue of X in derived class="<<x;
//cout<<"\nValue of Y in derived class="<<y; base1::y not accessible
cout<<"\nValue of Z in derived class="<<z;
cout<<"\nValue of A in derived class="<<a;
//cout<<"\nValue of B in derived class="<<b;base2::b not accessible
cout<<"\nValue of C in derived class="<<c;
}
};
void main()
{
clrscr();

derived d;
d.set_value(10,20,30);
//d.set_values(100,200,300);not accessible when derived as private
d.display1();
//d.display2(); not accessible when derived as private
d.display();
cout<<"\nValue of X in main="<<d.x;
//cout<<"\nValue of Y in main="<<d.y; base1::y not accessible
//cout<<"\nValue of Z in main="<<d.z; base1::z not accessible
//cout<<"\nValue of C in main="<<d.a; base2::a not accessible
//cout<<"\nValue of B in main="<<d.b; base2::b not accessible
//cout<<"\nValue of C in main="<<d.c; base2::c not accessible
getch();
}





Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: