Ref: Kamthane.
/*C++ program to demonstrate example of private simple inheritance.*/
#include
using namespace std;
class A
{ private:
int a;
protected:
int x; //can access by the derived class
public:
void setVal(int v)
{ x=v; }
};
class B:private A
{
public:
void printVal(void)
{
setVal(10); //accessing public member function here //protected data member direct access here
cout << "value of x: " << x << endl;
}
};
int main()
{
B objB; //derived class creation
objB.printVal();
return 0;
}
/* C++ program to demonstrate example of multilevel inheritance.*/
#include
using namespace std;
//Base Class : class A
class A
{
private:
int a;
public:
void get_a(int val_a)
{ a=val_a; }
void disp_a(void)
{ cout << "Value of a: " << a << endl; }
};
class B: public A
{
private:
int b;
public:
void get_b(int val_a, int val_b)
{
get_a(val_a); //assign value of a by calling function of class A
b=val_b;
}
void disp_b(void)
{
//display value of a
disp_a();
cout << "Value of b: " << b << endl;
}
};
//Here class C is derived class and B is Base class
class C: public B
{
private:
int c;
public:
//assign value of a from here
void get_c(int val_a, int val_b,int val_c)
{
/*** Multilevel Inheritance ***/
//assign value of a, bby calling function of class B and Class A
//here Class A is inherited on Class B, and Class B in inherited on Class B
get_b(val_a,val_b);
c=val_c;
}
void disp_c(void)
{
disp_b(); //display value of a and b using disp_b()
cout << "Value of c: " << c << endl;
}
};
int main()
{
//create object of final class, which is Class C
C objC;
objC.get_c(10,20,30);
objC.disp_c();
return 0;
}
---------------------
/* C++ program to demonstrate example of Multiple inheritance.*/
class A
{
protected:
int a;
}
class B
{
protected:
int b;
}
class C
{
protected:
int c;
}
class D
{
protected:
int d;
}
class E:public A,B,C,D
{
int e;
public:
void getdata()
{
cout<<"\n Enter the values of a,b,c,d,e";
cin>>a>>b>>c>>d>>e;
}
void showdata()
{
cout<<"\n Values of a"<
No comments:
Post a Comment