Chapter:9 Inheritance, solved Exercise question book by Robert Lafore
OOP BY ROBERT LOFORE
4th Edition
Chapter:9
Inheritance
Question no 1
Imagine a publishing company that markets both book and audiocassette versions of its works. Create a class publication that stores the title (a string) and price (type float) of a publication. From this class derive two classes: book, which adds a page count (type int), and tape, which adds a playing time in minutes (type float). Each of these three classes should have a getdata() function to get its data from the user at the keyboard, and a putdata() function to display its data. Write a main() program to test the book and tape classes by creating instances of them, asking the user to fill in data with getdata(), and then displaying the data with putdata().
Program Source code:#include <iostream>
#include<string>
using namespace std;
class publication
{
protected:
string title;
float price;
public:
publication()
{
title=" ";
price=0.0;
}
publication(string t,float p)
{
title=t;
price=p;
}
};
class book : public publication
{
int pagecount;
public:
book()
{
pagecount=0;
}
//After : base class constructor is called
book(string t,float p,int pc):publication(t,p)
{
pagecount=pc;
}
void display()
{
cout<<"title :"<<title<<endl;
cout<<"Price: "<<price<<endl;
cout<<"Pagecount :"<<pagecount<<endl;
}
};
class CD : public publication
{
float time;
public:
CD()
{
time=0.0;
}
//After : base class constructor is called
CD(string t,float p,float tim):publication(t,p)
{
time=tim;
}
void display()
{
cout<<"title :"<<title<<endl;
cout<<"Price: "<<price<<endl;
cout<<"time in minutes :"<<time<<endl;
}
};
int main()
{
cout<<endl<<"Book data"<<endl;
book b("C++",230,300);
b.display();
cout<<endl<<"CD Data"<<endl;
CD c("programming",100,120.5);
c.display();
return 0;
}
Program output in Dev c++ :
Comments
Post a Comment