Answer to Question #244813 in C++ for mannya

Question #244813

Create a class MusicalInstruments which contains methods void play(), String getDescription(),float getPrice().play() need not be given a definition in MusicalInstrument. Derive classes StringInstr and Percurssion from MusicalInstruments. StringInstr contains method, int getNoOfStrings(). Percurssion contains method, String getType(). Create class Violin that inherits from StringInstr. Create class Tabla that inherits from Percurssion.

Write main() to test the classes and to illustrate run-time polymorphism.

In main() create an object of Tabla with appropriate data values and print it.


1
Expert's answer
2021-09-30T13:01:05-0400
#include <iostream>
#include <string>
using namespace std;




class MusicalInstruments{
private:
	string description;
	float price;
public:


	MusicalInstruments(string description,float price){
		this->description=description;
		this->price=price;
	}


	virtual void play(){
		cout<<"Is plaiying\n";
	} 
	string getDescription(){
		return description;
	}
	float getPrice(){
		return price;
	}
};


class StringInstr: public MusicalInstruments {
private:
	int noOfStrings;
public:
	StringInstr(string description,float price,int noOfStrings):MusicalInstruments(description,price){
		this->noOfStrings=noOfStrings;
	}


	int getNoOfStrings(){
		return noOfStrings;
	}
};






class Percurssion: public MusicalInstruments {
private:
	string type;
public:
	Percurssion(string description,float price,string type):MusicalInstruments(description,price){
		this->type=type;
	}
	string getType(){
		return type;
	}
};
class Violin:public StringInstr{
public:
	Violin(string description,float price,int noOfStrings):StringInstr(description,price,noOfStrings){


	}
	void play(){
		cout<<"Violin is plaiying\n";
	}
};


class Tabla:public Percurssion{
public:
	Tabla(string description,float price,string type):Percurssion(description,price,type){


	}
	void play(){
		cout<<"Tabla is plaiying\n";
	}
};




int main(){
	MusicalInstruments** musicalInstruments=new MusicalInstruments*[2];


	musicalInstruments[0]=new Violin("Violin description",50,10);
	musicalInstruments[1]=new Tabla("Tabla description",25,"Tabla type");


	for(int i=0;i<2;i++){
		cout<<"Description: "<<musicalInstruments[i]->getDescription()<<"\n";
		cout<<"Price: "<<musicalInstruments[i]->getPrice()<<"\n";
		musicalInstruments[i]->play();


	}
	for(int i=0;i<2;i++){
		
		delete musicalInstruments[i];


	}
	delete[] musicalInstruments;
	int k;
	cin>>k;
	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