Inheritance program...

I made some changes in my previous program and tried to debug. there are no errors but i am not getting correct output. can some one please correct me where am i wrong..
The program required is to 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.

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
#include <iostream>
using namespace std;
enum note { middleC,Csarp,Cflat };

class Instrument {
public:
	virtual void play (note) const = 0;
	virtual char* tune_up (note) const = 0;
};

class Piano : public Instrument {
public:
	void play (note) const {
		cout <<"Piano::play"<< endl;
	}
	char* tune_up(note) const { return "Piano"; }
};

class Violin : public Instrument {
public:
	void play (note) const {
		cout <<"Violin::play"<< endl;
	}
	char* tune_up(note) const { return "Violin"; }
};

class Trumpet : public Instrument {
public:
	void play (note) const {
		cout <<"Trumpet::play"<< endl;
	}
	char* tune_up(note) const { return "Trumpet"; }
};

class Stringed : public Instrument {
public:
	void play(note) const {
		cout <<"Stringed::play"<< endl;
	}
	char* tune_up(note) const { return "Stringed"; }
};

class Woodwind : public Instrument {
public:
	void play(note) const {
		cout <<"Woodwind::play"<< endl;
	}
	char* tune_up(note) const { return "Woodwind"; }
};

class Percussion : public Instrument {
public:
	void play (note) const {
		cout <<"Precussion::play"<< endl;
	}
	char* tune_up(note) const { return "Percussion"; }
};

class Brass : public Instrument {
public:
	void play (note) const {
		cout <<"Brass::play"<< endl;
	}

};

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

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

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

int main() {
	Piano Brass ;
	polish(Brass);
	Violin Stringed ;
	strum (Stringed);
	pluck (Stringed);
}

Piano::play
Violin::play


Is this the output that is unexpected?

First call is polish(Brass) which calls Brass.play(middleC) which couts << "Piano::play\n"
Then you call strum(Stringed) which calls Stringed.play(middleC) which couts << "Violin::play\n"
Then you call pluck(Stringed) which calls Stringed.tune_up(middleC) which only returns a C-string, which you do nothing with.
Yes i am getting the same output...
The actual output should be the action of the instruments..!!
I think there is something wrong in the main program but not able to understand what to do...
Piano Brass;

You know this has absolutely nothing to do with the class Brass right? Brass here is only the name of an object of Piano. Same goes for Stringed.
Made some changes but still its not the desired output..

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
#include <iostream>
using namespace std;
enum note { middleC,Csarp,Cflat };

class Instrument {
public:
	virtual void display(note) const = 0;
	
};

class Piano : public Instrument {
public:
	void display(note) const {
		cout <<"Piano"<< endl;
	}

};

class Violin : public Instrument {
public:
	void display(note) const {
		cout <<"Violin"<< endl;
	}
};

class Trumpet : public Instrument {
public:
	void display(note) const {
		cout <<"Trumpet"<< endl;
	}
};

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::play"<< endl;
	}
};

class Brass : public Instrument {
public:
	void display(note) const {
		cout <<"Brass::play"<< endl;
	}

};

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

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

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

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

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

int main() {
	Piano Brass ;
	play(Brass);
	tune_up(Brass);
	polish(Brass);	
	Trumpet Woodwind;
	play(Woodwind);
	tune_up(Woodwind);
	Trumpet Percussion;
	play(Percussion);
	tune_up(Percussion);
	Violin Stringed ;
	play(Stringed);
	tune_up(Stringed);
	strum (Stringed);
	pluck (Stringed);
}
Okk does not mean i used name it different since the derived class of instrument is also named Brass?
I am really not getting.. Can some one please help me and tell me what changes should i make in the main program??
Why don't you tell us what you expect there to be printed? Then we can compare what is actually printed.
As its mention the function must print out the action of the instrument so i am expecting the output should be like Brass- play, tune_up and polish, Stringed- play, tune_ up, strum and pluck..
I think you misunderstand C++ in this regard.

You are not sending Brass [line 82-85], you are actually passing a Piano object.
Line 87-88 you're passing a Trumpet object.
etc...

Perhaps I can best explain it by rewriting your code...

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
#include <iostream>
using namespace std;
enum note { middleC,Csarp,Cflat };

class Instrument {
public:
	virtual void display(note) const = 0;

};

class Piano : public Instrument {
public:
	void display(note) const {
		cout <<"Piano"<< endl;
	}

};

class Violin : public Instrument {
public:
	void display(note) const {
		cout <<"Violin"<< endl;
	}
};

class Trumpet : public Instrument {
public:
	void display(note) const {
		cout <<"Trumpet"<< endl;
	}
};

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::play"<< endl;
	}
};

class Brass : public Instrument {
public:
	void display(note) const {
		cout <<"Brass::play"<< endl;
	}

};

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

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

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

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

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

int main() {
	Piano as_you_can_see ;
	play(as_you_can_see);
	tune_up(as_you_can_see);
	polish(as_you_can_see);

	Trumpet these_are_variable_names;
	play(these_are_variable_names);
	tune_up(these_are_variable_names);

	Trumpet this_is_the_name_of_a_single_object_of_Trumpet;
	play(this_is_the_name_of_a_single_object_of_Trumpet);
	tune_up(this_is_the_name_of_a_single_object_of_Trumpet);

	Violin and_this_is_the_name_of_a_Violin ;
	play(and_this_is_the_name_of_a_Violin);
	tune_up(and_this_is_the_name_of_a_Violin);
	strum (and_this_is_the_name_of_a_Violin);
	pluck (and_this_is_the_name_of_a_Violin);

	// Let's create a second Violin object:

	Violin i_think_youre_confused_about_classes_and_naming_since_you_used_the_exact_class_names_as_object_names ;
	play(i_think_youre_confused_about_classes_and_naming_since_you_used_the_exact_class_names_as_object_names);
	tune_up(i_think_youre_confused_about_classes_and_naming_since_you_used_the_exact_class_names_as_object_names);
	strum (i_think_youre_confused_about_classes_and_naming_since_you_used_the_exact_class_names_as_object_names);
	pluck (i_think_youre_confused_about_classes_and_naming_since_you_used_the_exact_class_names_as_object_names);
}
Thank u that made me understand where i was wrong.. But still i am not able to find how can i display the action of instrument in the call functions..!!!
Topic archived. No new replies allowed.