Anda di halaman 1dari 33

Experiment-1

Write a program to perform a Data abstraction


#include<iostream.h>
#include<conio.h>
#include<string.h>
class car
{
int top_speed;
char color[10];
public:
void get_data()
{
cout<<"\nenter top speed = ";
cin>>top_speed;
cout<<"enter color = ";
cin>>color;
}
void display()
{
cout<<"\nrecord is";
cout<<" top speed = "<<top_speed<<" color = "<<color;
}
};
void main()
{
clrscr();
car bmw,audi;
cout<<"enter data of bmw";
bmw.get_data();
cout<<"\nenter data of audi";
audi.get_data();
cout<<"\ninformation of bmw";
bmw.display();
cout<<"\n\ninformation of audi";
audi.display();
getch();
}

OUTPUT:
enter data of bmw
enter top speed = 350
enter color = red
enter data of audi
enter top speed = 400
enter color = blue
information of bmw
record is top speed = 350 color = red
information of audi
record is top speed = 400 color = blue

Experiment-2
Write a program to perform Function Overloading
2

#include<iostream.h>
#include<conio.h>
void add(int,int);
void add(int,int,int);
void add(float,int);
void main()
{
int a=5,b=6,c=8;
float d=1.2;
clrscr();
cout<<" now perform addition \n";
add(a,b);
add(d,a);
add(a,b,c);
getch();
}
void add(int x,int y)
{
int z=x+y;
cout<<"\n sum of a & b is = "<<z;
}
void add(int x,int y,int v)
{
int z=x+y+v;
cout<<"\n sum of a & b & c is = "<<z;
}
void add(float x, int y)
{
float z=x+y;
cout<<"\n sum of floating 'a' & integer 'b' is = "<<z;
}

OUTPUT:
2

now perform addition


sum of a & b is = 11
sum of floating 'a' & integer 'b' is = 6.2
sum of a & b & c is = 19

Experiment-3
Write a program to perform the Function Overriding
#include<iostream.h>
#include<conio.h>

class BC
{
public:
int b;
void show()
{
cout<<" I m in class Base";
}
};
class DC:public BC
{
public:
int d;
void show()
{
cout<<" I m in class Derived ";
}
};
void main()
{
clrscr();
DC d1;
d1.show();
getch();
}

OUTPUT:
I m in class Derived

Experiment-4
Explain class & Data encapsulation concept with the
help of program.
#include<iostream.h>
#include<conio.h>
class calc
{

int a,b;
public:
add(int,int);
sub(int,int);
mul(int,int);
div(int,int);
};
int calc::add(int a,int b)
{
return(a+b);
}

int calc::sub(int a,int b)


{
return(a-b);
}

int calc::mul(int a,int b)


{
return(a*b);
}

int calc::div(int a,int b)


{
return(a/b);
}
void main()
{
clrscr();
calc obj;
int a,b,result;
char op,ch;
do
{
cout<<"\n"<<"Enter 1st number,operator,2nd number";
cin>>a>>op>>b;
switch(op)
{
case '+':cout<<"The sum is:";
result=obj.add(a,b);
break;

case '-':cout<<"The difference is:";


result=obj.sub(a,b);
break;
case '*':cout<<"The product is:";
result=obj.mul(a,b);
break;
case '/':cout<<"The quotient is:";
result=obj.div(a,b);
}
cout<<result;
cout<<"\n"<<"Do you want to continue(y/n)?";
cin>>ch;
}while(ch=='y');
}

OUTPUT:
Enter 1st number,operator,2nd number 2 + 3
The sum is: 5
Do you want to continue(y/n)? y
2

Enter 1st number, operator,2nd number 2 * 3


The product is: 6

Experiment-5

Write a program to use Friend function.


#include<iostream.h>
#include<conio.h>
class DB;
class DM
{
public:
float inch,feet;
DM(float x,float y)
{
inch = x;
feet = y;

}
friend void add(DB,DM);
};
class DB
{
public:
float mtr,cm;
DB(float x,float y)
{
mtr=x;
cm=y;
}
friend void add(DB,DM);
};
DM add(DM dmo,DB dbo)
{
int a,b;
dmo.inch+=(dbo.cm/2.5);
dmo.feet+=(dbo.mtr*2.5);
cout<<dmo.feet<<endl;
cout<<dmo.inch<<endl;
a=dmo.inch;
if(a>=12)
{
dmo.inch=a%12;
dmo.feet+=a/12;
}
return dmo;
}

void main()
{
clrscr();
float x1,y1,x2,y2;
cout<<"Enter feets & inches"<<endl;
cin>>x1>>y1;
cout<<"Enter meters & cm\n";
cin>>x2>>y2;
DM A(x1,y1);
DB B(x2,y2);
A=add(A,B);
cout<<A.feet<<"feet & "<<A.inch<<"inches";
getch();
}

OUTPUT:
Enter feets & inches 2
6
Enter meters & cm 1
50
6.5 feet & 2 inches

Experiment-6
Write aprogram to perform Unary Operator
Overloading.
#include<iostream.h>
class A
{
int x,y;
public:
getdata(int a,int b)
{
x=a;
y=b;
}
void display()
{
cout<<endl<<x<<endl<<y;
}
void operator-();
};
void A::operator-()
{

x=-x;
y=-y;
}
void main()
{
A a1;
a1.getdata(3,5);
a1.display();
-a1;
a1.display();
}

OUTPUT:
Before operator overloading:
3
5
After operator overloading:
-3
-5

Experiment-7

Write a program to show Binary Operator Overloading.


#include<iostream.h>
#include<conio.h>
class A
{
int x,y;
public:
void getdata(int a,int b)
{
x=a;
y=b;
}
void operator+(A);
void display()
{
cout<<x<<"\n";
cout<<y<<"\n";
}
};
void A:: operator+(A a4)
{
x=x+a4.x;
y=y+a4.y;
}

void main()
{
clrscr();
A a1,a2;
a1.getdata(4,6);
a2.getdata(5,9);
a1+a2;
a1.display();
a2.display();
getch();
}

OUTPUT:
46
59
9 15

Experiment-8
Write a program to Overloading Input and Output
operator
#include<iostream.h>
#include<conio.h>
class A
{
private:
int x,y;
public:
friend void operator>>(istream &manoj, A &a2)
{
manoj>>a2.x>>a2.y;
}
friend void operator<<(ostream &yadav, A &a3)
{
yadav<<a3.x<<a3.y;
}
};
main()
{
clrscr();
A a1;
cout<<"Enter two numbers: ";
cin>>a1;
cout<<a1;
getch();
}

OUTPUT:
Enter two numbers: 2 3
23

Experiment-9
Write a program to perform Single level Inheritance
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int a;
public:
void get_a()
{
cout<<"enter value of a";
cin>>a;
}
};
class B:public A
{
public:
void display()
{
cout<<" value of a is";
cout<<a;
}
};
void main()
{
B b1;
b1.get_a();
b1.display();
getch();
}

OUTPUT:
Enter value of a 7
Value of a is 7

Experiment-10
Write a program to perform Multilevel Inheritance
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll;
public:
void get_roll()
{
cout<<" enter roll number of student";
cin>>roll;
}
};
class test:public student
{
protected:
float sub1,sub2;
public:
void get_marks()
{
cout<<" enter marks of two subjects ";
cin>>sub1>>sub2;
}
};
class result:public test
{
float total;
public:
void display_results()
{
total=sub1+sub2;
cout<<" total marks of roll no "<<roll<<" is = "<<total;
}
};

void main()
{
clrscr();
result r1;
r1.get_roll();
r1.get_marks();
r1.display_results();
getch();
}

OUTPUT:
enter roll number of student 1
enter marks of two subjects
10
20
total marks of roll no 1 is = 30

Experiment-11
Write a program to perform Multiple Inheritance
#include<iostream.h>
#include<conio.h>
class english
{
protected:
float sub1;
public:
void get_sub1()
{
cout<<" enter marks in english = ";
cin>>sub1;
}
};
class hindi
{
protected:
float sub2;
public:
void get_sub2()
{
cout<<" enter marks in hindi = ";
cin>>sub2;
}
};
class result:public english,public hindi
{
float total;
public:
void display_results()
{
total=sub1+sub2;
cout<<" total marks of student is = "<<total;
}
};
void main()
{
clrscr();
result r1;
r1.get_sub1();

r1.get_sub2();
r1.display_results();
getch();
}

OUTPUT:
enter marks in english = 45
enter marks in hindi

= 55

total marks of student is = 100

Experiment 12.
Write a program to Use of Virtual Class in Hybrid
Inheritance.
#include<iostream.h>
#include<conio.h>
class student
{
protected: int roll_number;
public:void get_roll(int);
};
class test:virtual public student
{
protected: int marks;
public:void get_marks(int);
};
class sports:virtual public student
{
protected: int score;
public:void get_score(int);
};
class result:public test,public sports
{
int total;
public:void display();
};
void student::get_roll(int x)
{
roll_number=x;
}
void test::get_marks(int x)
{
marks=x;
}
void sports::get_score(int x)
{
score=x;
}
void result::display()
{
total=marks+score;
cout<<"\nThe roll number is: "<<roll_number;
cout<<"\nThe result is: "<<total;
}

void main()
{
clrscr();
result a;
a.get_roll(34);
a.get_marks(900);
a.get_score(100);
a.display();
getch();
}

OUTPUT:
The roll number is: 34
The result is: 1000

Experiment-13
2

Write a program to Swapping using Template.


#include<iostream.h>
#include<conio.h>
template <class T>
void swap(T &x,T&y)
{
T temp=x;
x=y;
y=temp;
}
void function(int m,int n)
{
cout<<"Values before swap: "<<m<<n;
swap(m,n);
cout<<"\nValues after swap: "<<m<<n;
}
void main()
{
clrscr();
function(2,3);
getch();
}

OUTPUT:
Values before swap: 23
Values after swap: 32

Experiment-14
Write A program to perform Virtual Function

#include<iostream.h>
#include<conio.h>
class Base
{
public:
void display()
{
cout<<"\nDispaly of Base";
}
virtual void show()
{
cout<<"\nShow of Base";
}
};
class Derived:public Base
{
public:
void display()
{
cout<<"\nDisplay of Derived";
}
void show()
{
cout<<"\nShow of Derived";
}
};
void main()
{
Base B;
Derived D;
Base *bptr;
bptr=&B;
bptr->display();
bptr->show();
bptr=&D;
bptr->display();
bptr->show();
}

OUTPUT:
2

Display of Base
Show of Base
Display of Base
Show of Derived

Experiment-15
Write a program to perform Exception Handling.
#include<iostream>
#include<conio.h>
int main()
{

int a,b;
cout<<"Enter values of a and b \n";
cin>>a;
cin>>b;
int x=a-b;
try
{
if(x!=0)
{
cout<<"Result(a/x) = "<<a/x<<"\n";
}
else
{
throw(x);
}
}
catch(int i)
{
cout<<"Exception caught: x= "<<x<<endl;
}
getch();
return 0;
}

OUTPUT:
Enter Values of a and b
20 15
Result(a/x)=4
2

Enter Values of a and b


10 10
Exception caught:
x= 0

Anda mungkin juga menyukai