Aim: Write a
program to demonstrate the virtual derivation of a class.
Ans:
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int
x;
public:
void a1()
{
clrscr();
cout<<"Enter
any value\n";
cin>>x;
cout<<"Double
value is
"<<x+x<<"\n\n";
}
};
class
B:virtual public A
{
public:
void b1()
{
cout<<"Enter
any value\n";
cin>>x;
cout<<"Square
of number is\n"<<x*x<<"\n\n";
}
};
class
C:virtual public A
{
public:int y;
void c1()
{
y=10;
cout<<"Enter
any number\n";
cin>>x;
cout<<"new
value after increment is\n"<<x+y<<"\n\n";
}
};
class
D:public B,public C
{
public:int z;
void d1()
{
z=2;
cout<<"Enter
value\n";
cin>>x;
cout<<"new
value is "<<x-z;
}
}o1;
void main()
{
o1.a1();
o1.b1();
o1.c1();
o1.d1();
getch();
}
No comments:
Post a Comment