Answer to Question #214432 in C++ for shayan abbasi

Question #214432

Write a program class Publisher with data member to store name of publisher, Class Author with data member to store name of author. Now class Book will inherit both classes Publisher, Author and have data member cost of book, and static data member to count number of Book objects created. All data members of class must be initialized by parameterized constructors. Create few objects of class Book in main function and by using static member display how many objects have created. Write suitable functions needed.


1
Expert's answer
2021-07-07T01:50:34-0400
using namespace std;


#include <stdio.h>


/*
	Write a program class Publisher with data member to store name of publisher, 
	Class Author with data member to store name of author. Now class Book will 
	inherit both classes Publisher, Author and have data member cost of book, and 
	static data member to count number of Book objects created. All data members of 
	class must be initialized by parameterized constructors. Create few objects of class Book 
	in main function and by using static member display how many objects have created. 
	Write suitable functions needed.
*/


#define NO_OF_BOOKS	5
int Count=0;
class Publisher
{
	public: 
		string PublisherName;
		set_publisher_name(string s)	
		{
			PublisherName = s;
		}
};


class Author: public Publisher
{
	public: 
		string AuthorName;
		set_author_name(string s)	
		{
			AuthorName = s;
		}
};


class Book: public Author
{
	public:
		float Cost;
		set_cost(float f)
		{
			Cost = f;
			Count++;			
		}
};




main(void)
{
	class Book B[NO_OF_BOOKS];
	int n;
	
	B[0].set_publisher_name("Publisher-1");
	B[0].set_author_name("Author-1");
	B[0].set_cost(111);
	
	B[1].set_publisher_name("Publisher-2");
	B[1].set_author_name("Author-2");
	B[1].set_cost(222);


	B[2].set_publisher_name("Publisher-3");
	B[2].set_author_name("Author-3");
	B[2].set_cost(333);
	


	cout<<"\n         Publisher               Author                  Cost                Count\n";
	for(n=0;n<Count;n++)
	{
		cout<<setw(20)<<B[n].PublisherName<<setw(20)<<B[n].AuthorName<<setw(20)<<B[n].Cost<<setw(20)<<Count<<endl;
	}
	cout<<"\n\tNo. of Objects = "<<Count;
	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

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS