Question

In: Computer Science

C++ Create a class called Musicians to contain three functions string ( ), wind ( )...

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.

Solutions

Expert Solution

#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;
}

-------------------------------------------------------------------------------------------------------


Related Solutions

In C++, create a class to hold a set of strings called setTree. setTrees contain copies...
In C++, create a class to hold a set of strings called setTree. setTrees contain copies of the strings inserted so that the strings cannot be changed due to the fact that changing the strings inside the set could break the binary search tree. The strings are case sensitive. TreeSet implements the following: bool add(const string& s) -- add s to the set, if it's not already there. Return true if the set changed, false otherwise. void clear() -- remove...
In C++ Create two functions called TheNumber. One version of TheNumber should accept a string and...
In C++ Create two functions called TheNumber. One version of TheNumber should accept a string and output the accepted string 10 times. The other version of TheNumber should accept a double and output the accepted double 10 times. This uses function overloading.
Create a class called Dishwash with a double called CubicFeet, a string called color, and a...
Create a class called Dishwash with a double called CubicFeet, a string called color, and a method called Washing. The Washing method should return a string to the main class with the text "Now washing dishes!" In the main class create an array of DishWasher size 3. Give each DishWasher a different color and CubicFeet. Use a foreach loop to display that info, call the Washing method, and display the text returned from the Washing method call. c#
Create a class called Sphere. The class will contain the following    Instance data double radius...
Create a class called Sphere. The class will contain the following    Instance data double radius Methods Constructor with one parameter which will be used to set the radius instance data Getter and Setter for radius             Area - calculate the area of the sphere (4 * PI * radius * radius)             Volume - calculate and return the volume of the sphere (4/3 * PIE * radius * radius * radius) toString - returns a string with the...
with PHP Create a class called Employee that includes three instance variables—a first name (type String),...
with PHP Create a class called Employee that includes three instance variables—a first name (type String), a last name (type String) and a monthly salary int). Provide a constructor that initializes the three instance data member. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its 0. Write a test app named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary....
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions:...
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions: getArea() setArea() printArea() Create classes to inherit from the base class Circle Square Rectangle Both implement the functions derived from the abstract base class AND must have private variables and functions unique to them like double Radius double length calculateArea() Use the spreadsheet info.txt read in information about the circle, rectangle, or square text file: circle   3.5   square   3   rectangle   38   36 circle   23  ...
In Java, design and implement a class called Cat. Each Cat class will contain three private...
In Java, design and implement a class called Cat. Each Cat class will contain three private variables - an integer representing its speed, a double representing its meowing loudness, and a String representing its name. Using the Random class, the constructor should set the speed to a random integer from 0 to 9, the meowing loudness to a random double, and the name to anything you want; the constructor should take no parameters. Write “get” and “set” methods for each...
Create a class, called Song. Song will have the following fields:  artist (a string) ...
Create a class, called Song. Song will have the following fields:  artist (a string)  title (a string)  duration (an integer, recorded in seconds)  collectionName (a string) All fields, except for collectionName, will be unique for each song. The collectionName will have the same value for all songs. In addition to these four fields, you will also create a set of get/set methods and a constructor. The get/set methods must be present for artist, title, and duration....
create overloaded functions called lastValue. The first function should take as a parameter a string and...
create overloaded functions called lastValue. The first function should take as a parameter a string and the second function should take as a parameter an int. each of you functions should return an int value. in the case of the function that takes the string as an argument you will return the ascii value of the last character in the string. in the case of the function that takes an int parameter your function will return the last digit in...
C++ Classes & Objects Create a class named Student that has three private member: string firstName...
C++ Classes & Objects Create a class named Student that has three private member: string firstName string lastName int studentID Write the required mutator and accessor methods/functions (get/set methods) to display or modify the objects. In the 'main' function do the following (1) Create a student object "student1". (2) Use set methods to assign StudentID: 6337130 firstName: Sandy lastName: Santos (3) Display the students detail using get functions in standard output using cout: Sandy Santos 6337130
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT