In: Computer Science
C++
Create a class called Musicians to contain three functions string ( ), wind ( ) and perc ( ).
Each of these functions should initialize a string array to
contain the following instruments:
- veena, guitar, sitar, sarod and mandolin under
string ( )
- flute, clarinet saxophone, nadhaswaram and
piccolo under wind ( )
- tabla, mridangam, bangos, drums and tambour
under perc ( )
It should also display the contents of the arrays that are initialized.
Create a derived class called TypeIns to contain a function called get ( ) and show ( ). The get ( ) function must display instruments as follows:
Type of instruments to be displayed
a. String instruments
b. wind instruments
c. Percussion instruments
The show ( ) function should display the relevant detail according to our choice. The base class variables must be accessible only to its derived classes.
#include <iostream>
#include <string>
using namespace std;
#define SIZE 5 /// as given all array contains 5 instruments
class Musicians{
protected: ///protected variables that are accessible only in
this class and its derived class
string stringArray[SIZE];
string windArray[SIZE];
string percArray[SIZE];
public:///public methods that are accessible everywhere within
file
void display();
void string();
void wind();
void perc();
};
/*
Method to initialize stringArray
*/
void Musicians::string(){
stringArray[0]="veena";
stringArray[1]="guitar";
stringArray[2]="sitar";
stringArray[3]="sarod";
stringArray[4]="mandolin";
}
/*
Method to initialize windArray
*/
void Musicians:: wind(){
windArray[0]="flute";
windArray[1]="clarinet";
windArray[2]="saxophone";
windArray[3]="hadhaswaram";
windArray[4]="piccolo";
}
/*
Method to initialize percArray
*/
void Musicians::perc()
{
percArray[0]="tabla";
percArray[1]="mridangam";
percArray[2]="bangos";
percArray[3]="drums";
percArray[4]="tambour";
}
/*
Method to display stringArray ,windArray and pecArray
contents
*/
void Musicians::display(){
cout<<"\nString Array contains : ";
for(int i=0;i<SIZE;i++){
cout<<stringArray[i]<<" ";
}
cout<<"\n";
cout<<"\nWind Array contains : ";
for(int i=0;i<SIZE;i++){
cout<<windArray[i]<<" ";
}
cout<<"\n";
cout<<"\nPerc Array contains : ";
for(int i=0;i<SIZE;i++){
cout<<percArray[i]<<" ";
}
cout<<"\n";
}
/*
Derived class = TypeIns publically derived from musicians
hence;
1) public members of musician becomes public in TypeIns
2) protected members of musician becomes protected in TypeIns
3) private members of Musician gets inherited by TypeIns but
becomes NOT accessible
*/
class TypeIns:public Musicians{
public: /// methods thaat are accessible in file
char get();
void show(char choice);
};
/*
get() method who takes input (a/b/c) from user and returns it
*/
char TypeIns::get(){
cout<<"\nType of instruments to be displayed\n";
cout<<"a. String instruments\n";
cout<<"b. wind instruments\n";
cout<<"c. Percussion instruments\n";
char choice;
cin>>choice;
return choice; //return the choice
}
/*
show() method that takes choice (a/A ,b/B ,c/C )
displays content of stringArray if choice is a/A
displays content of windArray if choice is b/B
displays content of percArray if choice is c/C
*/
void TypeIns::show(char choice){
switch(choice){
case 'a':
case 'A': ///display stringArray
cout<<"\nString Instruments : ";
for(int i=0;i<SIZE;i++){
cout<<stringArray[i]<<" ";
}
cout<<"\n";
break;
case 'b':
case 'B':///display windArray
cout<<"\nWind Instruments : ";
for(int i=0;i<SIZE;i++){
cout<<windArray[i]<<" ";
}
cout<<"\n";
break;
case 'c':
case 'C':///display percArray
cout<<"\nPercussion Instruments : ";
for(int i=0;i<SIZE;i++){
cout<<percArray[i]<<" ";
}
cout<<"\n";
break;
default:
cout<<"\nKindly select a or b or c ";
}
}
int main()
{
TypeIns typeIns; //create object
///initialize typeIns using string() wind() perc()
typeIns.string();
typeIns.perc();
typeIns.wind();
///calling base class method to display contents
typeIns.display();
///asking choice and calling show
typeIns.show(typeIns.get());
return 0;
}
-------------------------------------------------------------------------------------------------------