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.
#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;
}
Comments
Leave a comment