Professional Documents
Culture Documents
#include <iostream.h>
class data
{
int a, b;
public:
void put(int ,int );
void get();
};
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:
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>
class item
int code;
float price;
public:
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 *d=p;
int x,i;
float y;
for(i=0;i<size;i++)
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;
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;
bptr->show();
DC *dptr;
dptr=&derived;
dptr->d=300;
dptr->show();
cout<<"using((DC *)bptr)\n";
return 0;
70
}
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:
cout<<"\nbeta constructed";
void show_beta(void)
72
{
cout<<"p="<<p<<"\n";
cout<<"q="<<q<<"\n";
};
int u,v;
public:
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;
#include<conio.h>
74
class Base
Public:
};
public:
};
int main()
Base B;
Derived D;
Base *bptr;
bptr =&B;
bptr =&D;
75
bptr -> display();
bptr ->show();
getch();
return 0;
76