problem with inheritance program

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
What is specfically wrong with the output ?
I'm guessing it has to do with
1
2
3
4
5
6
7
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 ";
}

as this will output all the instruments.
The function play is the same.
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
Please help...
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();
}


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.
You need to look up polymorphism. I changed you code a little, so you can start from here. Read the comments in the instrument class.

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
using namespace std;
enum note { middleC,Csharp,Cflat };



class Instrument {                                 
public:
	virtual void display(note) const = 0; //this function must be implemented
	virtual void play()=0;  //this function must be implemented
	virtual void tune_up()=0; //this function must be implemented
	virtual void polish(){ //if this function is not re-implemented it will execute the line below
	  cout<< "This instrument cannot be polished \n ";
	}
	virtual void strum(){ //if this function is not re-implemented it will execute the line below
	  cout<< "This instrument cannot be strummed \n ";
	}
	virtual void pluck(){ //if this function is not re-implemented it will execute the line below
	  cout<< "This instrument cannot be plucked \n ";
	}
};


class Stringed : public Instrument {
public:
	void display(note) const {
		//cout <<"Stringed"<< endl;
	}
	void play(){
	  cout<< "Instrument Stringed - Violin Play \n ";
	}
	void tune_up(){
	  cout<< "Instrument Stringed - Violin Tune_up \n ";
	}	
	void pluck(){ //re-implement pluck
	  cout<< "Instrument Stringed- Violin Pluck \n";
	}
};


class Woodwind : public Instrument {
public:
	void display(note) const {
		//cout <<"Woodwind"<< endl;
	}
	void play(){
	  cout<< "Instrument Woodwind - Drum Play \n ";
	}
	void tune_up(){
	  cout<<"IInstrument Woodwind - Drum Tune_up \n ";
	}	
};



class Percussion : public Instrument {
public:
	void display(note) const {
		//cout <<"Precussion"<< endl;
	}
	void play(){
	  cout<<"Instrument Percussion - Piano Play \n ";
	}
	void tune_up(){
	  cout<<"Instrument Percussion - Piano Tune_up \n ";
	}	
};



class Brass : public Instrument {
public:
	void display(note) const {
		//cout <<"Brass"<< endl;
	}
	void play(){
	  cout<<"Instrument Brass - Trumpet Play \n ";
	}
	void tune_up(){
	  cout<<"Instrument Brass - Trumpet Tune_up \n ";
	}	
	void polish () {
		cout<< "Instrument Brass - Trumpet Polish \n";
  }
}; 

//Creating function play

void play (Instrument& i) {
i.display(middleC);
i.play();
}


void tune_up (Instrument& i) {
	i.display(middleC);
	i.tune_up();
}



void polish (Instrument& i) {
	i.display(middleC);
	//cout<< "Instrument Brass - Trumpet Polish \n";
  i.polish();
}



void strum (Instrument& i) {
	i.display(middleC);
	//cout<< "Instrument Stringed - Violin Strum \n";
	i.strum();
}



void pluck (Instrument& i) { 
	i.display(middleC); 
	//cout<< "Instrument Stringed- Violin Pluck \n";
	i.pluck();
} 

//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); 
	polish(Piano);      
	strum(Violin);          
	pluck(Violin);         
	
}
Topic archived. No new replies allowed.