May 11, 2013 at 11:20pm UTC
I am working on a inheritance program which create classes that represents musical instruments such as; piano, violin, trumpet which are classified as stringed, woodwind, percussion instruments and all of them could be derived from a class instrument. Functions play() and tune-up() would be used by all instruments whereas polish() would only be used by brass instruments and strum() and pluck() would only be available to stringed instruments. The actual functions can simply print out the action of the instrument.
this is the code of the program.. there are no errors, but the output displayed is not correct.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
#include <iostream>
using namespace std;
enum note { middleC,Csharp,Cflat };
class Instrument {
public :
virtual void display(note) const = 0;
};
class Stringed : public Instrument {
public :
void display(note) const {
//cout <<"Stringed"<< endl;
}
};
class Woodwind : public Instrument {
public :
void display(note) const {
//cout <<"Woodwind"<< endl;
}
};
class Percussion : public Instrument {
public :
void display(note) const {
//cout <<"Precussion"<< endl;
}
};
class Brass : public Instrument {
public :
void display(note) const {
//cout <<"Brass"<< endl;
}
};
//Creating function play
void play (Instrument& i) {
i.display(middleC);
cout<< "Instrument Stringed - Voilin Play \n " ;
cout<< "Instrument Woodwind - Drum Play \n " ;
cout<< "Instrument Percussion - Piano Play \n " ;
cout<< "Instrument Brass - Trumpet Play \n " ;
}
void tune_up (Instrument& i) {
i.display(middleC);
cout<< "Instrument Stringed - Violin Tune_up \n" ;
cout<< "Instrument Woodwind - Drum Tune_up \n " ;
cout<< "Instrument Percussion - Piano Tune_up \n " ;
cout<< "Instrument Brass - Trumpet Tune_up \n " ;
}
void polish (Instrument& i) {
i.display(middleC);
cout<< "Instrument Brass - Trumpet Polish \n" ;
}
void strum (Instrument& i) {
i.display(middleC);
cout<< "Instrument Stringed - Violin Strum \n" ;
}
void pluck (Instrument& i) {
i.display(middleC);
cout<< "Instrument Stringed- Violin Pluck \n" ;
}
//Main program
int main() {
Stringed Violin;
Woodwind Drum;
Percussion Piano;
Brass Trumpet;
play(Violin);
play(Drum);
play(Piano);
play(Trumpet);
tune_up(Violin);
tune_up(Drum);
tune_up(Piano);
tune_up(Trumpet);
polish(Trumpet);
strum(Violin);
pluck(Violin);
}
Can someone please help me and let me know whats wrong in the program? is it the main program or in the function declaration???
Please help...
Last edited on May 11, 2013 at 11:23pm UTC
May 11, 2013 at 11:38pm UTC
I am getting all functions repeated times like.....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Instrument Stringed - Violin play
Instrument Woodwind - Drum Play
Instrument Percussion - Piano Play
Instrument Brass - Trumpet Play
Instrument Stringed - Violin play
Instrument Woodwind - Drum Play
Instrument Percussion - Piano Play
Instrument Brass - Trumpet Play
Instrument Stringed - Violin tune_up
Instrument Woodwind - Drum tune_up
Instrument Percussion - Piano tune_up
Instrument Brass - Trumpet tune_up
Instrument Stringed - Violin tune_up
Instrument Woodwind - Drum tune_up
Instrument Percussion - Piano tune_up
Instrument Brass - Trumpet tune_up
Instrument Stringed - Violin tune_up
Instrument Woodwind - Drum tune_up
Instrument Percussion - Piano tune_up
Instrument Brass - Trumpet tune_up
So how can i correct that????
Last edited on May 11, 2013 at 11:38pm UTC
May 12, 2013 at 4:39am UTC
I assume that "play(Violin);" line should display only "Instrument Stringed - Violin play"
What I would do is to define play, tune_up, and so on in each of your classes. IE
class Stringed : public Instrument {
public:
void display(note) const {
//cout <<"Stringed"<< endl;
}
void play(){
cout<< "Instrument Stringed - Violin Play \n ";
}
};
Then replace play with something like
void play (Instrument& i) {
i.display(middleC);
i.play();
}
May 12, 2013 at 8:34am UTC
I din get it void play() is already created then how can i create again??
Please help me in this i am stuck at it from a long time.