Answer to Question #227623 in C++ for Zia

Question #227623

Create a class Computer derived from Book having following members

private String type - It could be Networking, DataStructure, DBMS

Setter and getter method for type instance variable

Create a class Mathematics derived from Book having following members

private String type – It could be Algebra, Geometry

Setter and getter method for type instance variable

Create a class TestBook having main method. Create an object of Computer. Scan data from user, set and print details of Computer book. Create an object of Mathematics class. Scan data from user, set and print

details of Mathematics book.

Modify the TestBook class in above program. Create an array of Book type of

size 4. Store 2 Computer and 2 Mathematics books data in array and print all

book details.




1
Expert's answer
2021-08-19T14:32:17-0400
#include<iostream>
#include<string> 
using namespace std;
class Book{
	private:
		string name, author;
	public:
	void setBook(string n, string a){
			name = n;
			author = a;
		}
	string getBook(){
		return "Book name:  "+name +"\nBook author:  "+ author;
	}
};
class Computer: public Book{
	private:
		string type;
	public:
		Computer(){
		}
		Computer(string n, string a, string t){
			setBook(n,a);
			setType(t);
		}
		void setType(string t){
			type =t;
		}
		string getType(){
			return type;
		}
		string getComputer(){
			return getBook() +" \nBook type:  " + type;
		}
};
class Mathematics: public Book{
	private:
		string type;
	public:
		Mathematics(){
		}
		Mathematics(string n, string a, string t){
			setBook(n,a);
			setType(t);
		}
		void setType(string t){
			type =t;
		}
		string getType(){
			return type;
		}
		string getMaths(){
			return getBook() +" \nBook type:  " + type;
		}
};
class TestBook{
	
	public:
		friend int main();
		void displayComp(){
			string name,  author, type;
			cout<<"Enter the book name\n";
			cin>>name;
			cout<<"Enter the book author\n";
			cin>>author;
			cout<<"Enter the book type\n";
			cin>>type;
			Computer comp(name,author,type);
			cout<<"The book details are:\n";
			cout<<comp.getComputer()<<endl;
		}
		void displayMathematics(){
			string name,  author, type;
			cout<<"Enter the book name\n";
			cin>>name;
			cout<<"Enter the book author\n";
			cin>>author;
			cout<<"Enter the book type\n";
			cin>>type;
			Mathematics maths(name,author,type);
			cout<<"The book details are:\n";
			cout<<maths.getMaths();
		}
		void CreateBook(){
			Computer comp;
			Mathematics maths;
			Book * book = &comp;
			Book * book2 = &maths;
	}
		
};
int main(){
TestBook book;	
book.displayComp();
	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