Write a program for a publishing company that markets both printed books and audio visual
lectures stored on CDs. Write a class Publication thatstores title and price. Derive a class book which
has an additional member as no_pages and a class Lecture with member play_time.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Publication {
public:
Publication(string title, double price) :
title(title), price(price) {}
virtual void Display();
private:
string title;
double price;
};
void Publication::Display() {
cout << "Title: " << title <<endl;
cout << "Price: $" << fixed << setprecision(2) << price << endl;
}
class Book : public Publication {
public:
Book(string title, double price, int no_page) :
Publication(title, price), no_page(no_page) {}
virtual void Display();
private:
int no_page;
};
void Book::Display() {
Publication::Display();
cout << "Pages: " << no_page << endl;
}
class Lecture : public Publication {
public:
Lecture(string title, double price, int play_time) :
Publication(title, price), play_time(play_time) {}
virtual void Display();
private:
int play_time;
};
void Lecture::Display() {
Publication::Display();
cout << "Time: " << play_time/60 << ":" << setw(2) << setfill('0')
<< play_time%60 << endl;
}
int main() {
Publication* store[2];
store[0] = new Book("Head First C", 40.59, 632);
store[1] = new Lecture("Modern C++ Course", 20.99, 245);
store[0]->Display();
cout << endl;
store[1]->Display();
}
Comments
Leave a comment