Consider a publishing company that markets book . Create a base class Publication that stores the title (a string), number of copies sold and price(type float) of a publication. Class Book which adds a page_count(int) is derived from the class Publication. Each class should have get_data() function to get its data from the user at the keyboard. Write the main() function to test the Book and publication classes by creating instances of them asking the user to fill in data with get_data() and then displaying it using put_data().
#include <iostream>
using namespace std;
//define the publication class
class Publication{
protected:
string title;
int num_of_copies;
float price;
public:
//get data from the user
void get_data()
{
cout<<"\nEnter the Title of Publication: ";
cin>>title;
cout<<"\nEnter the Number of copies sold: ";
cin>>num_of_copies;
cout<<"\nEnter the price: ";
cin>>price;
}
//display the data
void put_data(){
cout<<"\nThe title of the publication is "<<title<<endl;
cout<<"The number of copies sold is "<<num_of_copies<<endl;
cout<<"The price is "<<price<<endl;
}
};
//define the book class
class Book:public Publication{
private:
int page_count;
public:
//get data from the user
void get_data(){
cout<<"\nEnter number of pages: ";
cin>>page_count;
}
//display the data
void put_data(){
cout<<"The number of pages is "<<page_count<<endl;
}
};
int main(){
Publication pub;
Book b;
pub.get_data();
b.get_data();
pub.put_data();
b.put_data();
return 0;
}
Comments
Leave a comment