You are on page 1of 23

PROGRAM 24: C++ program for multilevel inheritance

#include <iostream.h>
class data
{
int a, b;
public:
void put(int ,int );
void get();
};

class record:public data


{
int c,d;
public:
void value(int x,int y)
{
c=x;
d=y;
}

void show();
};
class classes:public record
{
int e;
public:
void enter(int x)
{
e=x;
}
void output()
{
cout<<"value of e is"<<" "<<e<<endl;
}
};
void data::put(int x,int y)

54
{
a=x;
b=y;
}
void data::get()
{
cout<<"Values of a & b are "<<a<<" and "<<b<<endl;
}
void record::show()
{
cout<<"values of c & d are"<<c<<" and "<<d<<endl;
}
int main()
{
classes f;
f.put(5,10);
f.value(20,40);
f.enter(80);
f.get();
f.show();
f.output();
}

OUTPUT:

55
PROGRAM25: C++ program for multiple inheritances.

#include <iostream.h>
class data
{
int a, b;
public:
void put(int ,int );
void get();
};
class record
{
int c,d;
public:
void value(int x,int y)
{
c=x;
d=y;
}

void show();
};
class classes: public data, public record
{
int e;
public:
void enter(int x)
{
e=x;
}
void output()
{
cout<<"value of e is"<<" "<<e<<endl;
}
};
void data::put(int x, int y)
{

56
a=x;
b=y;
}

void data::get()
{
cout<<"Values of a & b are "<<a<<" and "<<b<<endl;
}

void record::show()
{
cout<<"values of c & d are"<<c<<" and "<<d<<endl;
}
int main()
{
classes f;
f.put(5,10);
f.value(20,30);
f.enter(60);
f.get();
f.show();
f.output();
}

OUTPUT:

PROGRAM 26 : Program for hybrid inheritance.


57
#include <iostream.h>
class data
{
int a, b;
public:
void put(int ,int );
void get();
};
class record: public data
{
int c,d;
public:
void value(int x ,int y)
{
c=x;
d=y;
}
void show();
};
class info
{
int f, g;
public:
void input(int x, int y)
{
f=x;
g=y;
}
void out()
{
cout<<"values of f & g are "<<f<<" and "<<g<<endl;
}
};
class classes: public record, public info
{
int e;
public:
void enter(int x)
58
{
e=x;
}
void output()
{
cout<<"value of e is"<<" "<<e<<endl;
}
};
void data::put(int x,int y)
{
a=x;
b=y;
}
void data::get()
{
cout<<"Values of a & b are "<<a<<" and "<<b<<endl;
}
void record::show()
{
cout<<"values of c & d are"<<c<<" and "<<d<<endl;
}
int main()
{
classes f;
f.put(5,10);
f.value(20,40);
f.input(160,320);
f.enter(80);
f.get();
f.show();
f.out();
f.output();
}

OUTPUT:

59
60
PROGRAM 27: Program using virtual base class

#include <iostream.h>
class student
{
protected:
int roll;
public:
void get_number(int a)
{
roll=a;
}
void put_number(void)
{
cout<<"Roll No. "<<roll<<endl;
}
};
class test: virtual public student
{
protected:
float part1, part2;
public:
void get_marks(float x, float y)
{
part1=x;
part2=y;
}
void put_marks(void)
{
cout<<"Marks obtained :"<<endl;
cout<<"Part1 = "<<part1<<endl;
cout<<"Part2= "<<part2<<endl;
}
};
class sports : public virtual student
{
protected:

61
float score;
public:
void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout<<"Sports wt: "<<score<<endl;
}
};
class result: public test, public sports
{
float total;
public:
void display(void);
};
void result ::display(void)
{
total=part1+part2+score;
put_number();
put_marks();
put_score();
cout<<"Total Score : "<<total<<endl;
}
int main()
{
result student_1;
student_1.get_number (678);
student_1.get_marks (30.5,25.5);
student_1.get_score (7.0);
student_1.display ();
}

62
OUTPUT:

63
PROGRAM 28 : Program using constructors in derived class.

#include <iostream.h>
class data
{
int a;
public:
data(int x )
{
a=x;
}
void get()
{
cout<<"the value of a is "<<a<<endl;
}
};
class record
{
int c;
public:
record(int x)
{
c=x;
}
void show()
{
cout<<"the value of c is "<<c<<endl;
}
};
class info: public data, public record
{
int m,n;
public:
info(int a, int c, int b, int d):data(a),record(b)
{ m=c;
n=d;
}

64
void input()
cout<<"values of m & n are "<<m<<" "<<n;
};
int main()
{
info f(10,20,30,40);
f.get();
f.show();
f.input();
}

OUTPUT:

65
PROGRAM 29: Pointer to objects
#include<iostream>

#include<stdlib.h>

using namespace std;

class item

int code;

float price;

public:

void getdata(int a,float b)

code=a;

price=b;

void show(void)

cout<<"Code:"<<code<<"\n";

cout<<"Price:"<<price<<"\n";

};

66
const int size=2;

int main()

system("clear");

item *p=new item[size];

item *d=p;

int x,i;

float y;

for(i=0;i<size;i++)

cout<<"Input code and price for item"<<i+1<<"\n";

cin>>x>>y;

p->getdata(x,y);

p++;

for(i=0;i<size;i++)

cout<<"Item:"<<i+1<<"\n";

d->show();

d++;

67
return 0;

PROGRAM 30: Pointer to derived constructor


68
#include<iostream.h>

class BC

public:

int b;

void show();

{ cout<<"b="<<b<<"\n";}

};

class DC :public BC

public:

int d;

void show()

{ cout<<"b="<<b<<"\n"<<"d="<<d<<"\n";}

};

int main()

BC *bptr;

BC base;

bptr=&base;

bptr->b=100;

69
cout<<"bptr points to the base object\n";

bptr->show();

DC derived;

bptr=&derived;

bptr->b=200;

cout<<"bptr now points to derived object\n";

bptr->show();

DC *dptr;

dptr=&derived;

dptr->d=300;

cout<<"dptr is derived type pointer\n";

dptr->show();

cout<<"using((DC *)bptr)\n";

((DC *)bptr) ->d=400;

((DC *)bptr) -> show();

return 0;

70
}

PROGRAM31: Initialization list in constructors


#include<iostream>
71
#include<stdlib.h>

using namespace std;

class alpha

int x;

public:

alpha(int i)

{x=i;

cout<<"\nalpha consructed";

void show_alpha(void)

cout<<"x="<<x<<"\n";

};

class beta

float p,q;

public:

beta(float a,float b): p(a),q(b+p)

cout<<"\nbeta constructed";

void show_beta(void)

72
{

cout<<"p="<<p<<"\n";

cout<<"q="<<q<<"\n";

};

class gamma:public beta,public alpha

int u,v;

public:

gamma(int a,int b,float c):alpha(a*2),beta(c,c),u(a)

v=b;

cout<<"\ngamma constructed";

void show_gamma(void)

cout<<"u="<<u<<"\n";

cout<<"v="<<v<<"\n";

};

int main()

system("clear");

gamma g(2,4,2.5);

73
cout<<"\nDisplay member values\n";

g.show_alpha();

g.show_beta();

g.show_gamma();

return 0;

PROGRAM 32: Program to implement virtual function


#include<iostream.h>

#include<conio.h>

74
class Base

Public:

void display(){ cout<<”\n display base”;}

virtual void show(){cout<<”\n show base”;}

};

class Derived : public Base

public:

void display(){ cout<<”\n display derived”;}

virtual void show(){cout<<”\n show derived”;}

};

int main()

Base B;

Derived D;

Base *bptr;

cout<<”\n bptr points to base”;

bptr =&B;

bptr -> display();

bptr -> show();

cout<<”\n\n bptr points to Derived \n”;

bptr =&D;

75
bptr -> display();

bptr ->show();

getch();

return 0;

76

You might also like