Slip28
Q.1) Create a
class Point that has x & y as integer data members. Write a C++ program to
perform following functions:
void setpoint(int, int) To set the specified values
of x and y in object.
void
showpoint() To display contents of point object.
point
addpoint(point) To add the corresponding
values of x, and y in point
object argument to current point object return
point.
#include<iostream.h>
#include<conio.h>
class point
{
int x;
int y;
public:
void setpoint(int a,int b)
{
x=a;
y=b;
}
void showpoint()
{
cout<<"a"<<x;
cout<<"\nb"<<y;
}
void ss()
{
cout<<x;
}
friend point addpoint(point p1);
};
point addpoint(point p)
{ point p1;
p1.x=p.x;
p1.x+=p.y;
return p1;
}
void main()
{
point pp;
clrscr();
point s;
pp.setpoint(5,8);
pp.showpoint();
s=addpoint(pp);
cout<<"\nSum is";
s.ss();
getch();
}
Q.2) Create a base class Media. Derive
two different classes Book (Book_id, Book_name, Publication, Author,
Book_price) and CD (CD_title, CD_price) from Media. Write a C++ program to
accept and display information of both Book and CD. (Use pure virtual function)
#include<iostream.h>
#include<conio.h>
class media
{
public:
virtual void display()=0;
};
class book:public media
{
public:
int b_id,b_price;
char
b_name[20],b_publication[20],b_author[20];
void getbook()
{
cout<<"\nenter the
b_id:";
cin>>b_id;
cout<<"\nenter the
b_price:";
cin>>b_price;
cout<<"\nenter the
b_name:";
cin>>b_name;
cout<<"\nenter the
b_publication:";
cin>>b_publication;
cout<<"\nenter the
b_author:";
cin>>b_author;
}
void display();
};
class cd:public media
{
public:
char cd_title[10];
int cd_price;
void getcd()
{
cout<<"\nenter the
cd_title is:";
cin>>cd_title;
cout<<"\nenter the
cd_price is:";
cin>>cd_price;
}
void display();
};
void book::display()
{
cout<<"\nb_id:"<<b_id;
cout<<"\nb_price:"<<b_price;
cout<<"\nb_name:"<<b_name;
cout<<"\nb_publication:"<<b_publication;
cout<<"\nb_author:"<<b_author;
}
void cd::display()
{
cout<<"\ncd_title
is:"<<cd_title;
cout<<"\ncd_price
is:"<<cd_price;
}
void main()
{
clrscr();
book b;
b.getbook();
b.display();
cd c;
c.getcd();
c.display();
getch();
};