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)
//Note: If you are not using Borland or Turbo C then add the following line after header files :
//using namespace std;
#include<iostream.h>
#include<conio.h>
class media // base class
{
public:
void virtual accept()=0; // pure virtual function
void virtual display()=0; //pure virtual function
};
class book: public media //inherit class book
{
int bid,bprice;
char bname[20],publication[10],auther[20];
public:
void accept()
{
cout<<"\nEnter book id: ";
cin>>bid;
cout<<"\nEnter book name: ";
cin>>bname;
cout<<"\nEnter book publication: ";
cin>>publication;
cout<<"\nEnter book Auther: ";
cin>>auther;
cout<<"\nEnter book price: ";
cin>>bprice;
}
void display()
{
cout<<"\n book id : "<<bid;
cout<<"\n book name : "<<bname;
cout<<"\n book publication : "<<publication;
cout<<"\n book Auther : "<<auther;
cout<<"\n book price : "<<bprice;
}
};
class cd: public media //inherit class cd
{
int cprice;
char title[20];
public:
void accept()
{
cout<<"\nEnter CD Title: ";
cin>>title;
cout<<"\nEnter CD price: ";
cin>>cprice;
}
void display()
{
cout<<"\n CD Title : "<<title;
cout<<"\n CD price : "<<cprice;
}
};
int main()
{
book b;
cd c;
b.accept();
c.accept();
cout<<"\n=============Information of book and CD=============\n";
b.display();
c.display();
getch();
return(0);
}
0 Comments