Slip22
Q.1) Write
the definition for a class called ‘point’ that has x & y as integer data
members. Overload the assignment operator (=) to copy one object to another.
(Use Default and parameterized constructor to initialize the appropriate
objects)
Write a C++
program to illustrate the use of above class.
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class point
{
int x,y;
public:
point()
{
}
point(int a,int b=200)
{
x=a;y=b;
}
void operator=(point ob2)
{
x=ob2.x;
y=ob2.y;
}
void display()
{
cout<<"\nx =
"<<x<<"\t y = "<<y;
}
};
void main()
{
point ob1(100,200),ob2;
int n,m;
clrscr();
ob1.display();
ob2=ob1;
cout<<"\nAfter overload = operator
.\n";
ob2.display();
cout<<"\nEnter number : ";
cin>>n;
cout<<"\nEnter number : ";
cin>>m;
point ob3(n,m);
ob3.display();
ob2=ob3;
cout<<"\nAfter overload = operator
.\n";
ob2.display();
getch();
}
Q.2) Create
a base class Conversion. Derive three different classes Weight (Gram,
Kilogram), Volume(Milliliter, Liter), Currency(Rupees, Paise) from Conversion
class.
Write a C++ program to perform read,
convert and display operations. (Use Pure virtual function)
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class conversion
{
public:
virtual void convert()=0;
};
class weight:public conversion
{
public:
int kilo,gram;
void readw()
{
cout<<"\nenter the gram:";
cin>>gram;
}
void convert();
void displayw()
{
cout<<kilo;
}
};
class volume:public conversion
{
public:
int liter,mili;
void readv()
{
cout<<"\nenter the mililiter:";
cin>>mili;
}
void convert();
void displayv()
{
cout<<liter;
}
};
class currency:public conversion
{
public:
int rupee,paise;
void readc()
{
cout<<"\nenter the rupee:";
cin>>rupee;
}
void convert();
void displayc()
{
cout<<paise;
}
};
void weight::convert()
{
kilo=gram/1000;
}
void volume::convert()
{
liter=mili/1000;
}
void currency::convert()
{
paise=rupee*100;
}
void main()
{
clrscr();
int ch;
weight w;
volume v;
currency c;
do
{
cout<<"\n1.Convert gram to kilogram";
cout<<"\n2.Convert mililiter to liter";
cout<<"\n3.Convert rupees to paise";
cout<<"\n4.Exit";
cout<<"\nEnter your choice:\t";
cin>>ch;
switch(ch)
{
case 1:
w.readw();
w.convert();
w.displayw();
break;
case 2:
v.readv();
v.convert();
v.displayv();
break;
case 3:
c.readc();
c.convert();
c.displayc();
break;
case 4:
exit(0);
}
}while(ch!=4);
getch();
}