Answer to Question #192671 in C++ for Sankalp

Question #192671

C ++ program to demonstrate runtime polymorphism and display information of book(Title, price, number of pages) and Tape (Title, price, time). use following information· Class media with parameter to initialize media information title and price & virtualfunction display· Class Book will inherit media & it will contain data member number of page and displayfunctionClass tape will inherit class media & it will contain data member time and displayfunction

1
Expert's answer
2021-05-13T01:48:31-0400
#include <iostream>
#include <string>
using namespace std;


class media
{
	public:
	media(string title_, double price_);
	virtual void Display() = 0;
	protected:
	string title;
	double price;
};
media::media(string title_, double price_) : title(title_), price(price_)
{}


class book : public media
{
	public:
	book(string title, double price, int numPages);
	void Display();
	private:
	int numPage;
};


book::book(string title, double price, int numPages) : media(title, price), numPage(numPages)
{}


void book::Display()
{
	cout << "Title " << title << " price " << price << " number of pages " << numPage << endl;
}


class tape : public media
{
	public:
	tape(string title, double price, double time);
	void Display();
	private:
	double times;
};


tape::tape(string title, double price, double time) : media(title, price), times(time)
{
}


void tape::Display()
{
	cout << "Title " << title << " price " << price << " time " << times << endl;
}


int main()
{
	media *Book = new book("book", 350, 230);
	Book->Display();
	Book = new tape("tape", 420, 34);
	Book->Display();
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment