Errors in inheritance program..!!!

Can someone please help me in the code for inheritance.
I have created a base class Instrument deriving Wind,Percussion as derived classes with function play() and tune(). Some errors are deducted dont understand where i did wrong.Please help me with this..!!!

here is the code..

#include <iostream>
using namespace std;
enum note { middleC,Csarp,Cflat };

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
class Instrument {
public:
	virtual void play (note) const = 0;
	virtual char* what (note) const = 0;
	virtual void adjust (int) = 0;
};
class Wind : public Instrument {
public:
	void play (note) const {
		cout <<"Wind::play"<< endl;
	}
	char* what() const { return "Wind"; }
	void adjust (int) {}
};

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

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

class Brass : public Wind {
public:
	void play (note) const {
		cout <<"Brass::play"<< endl;
	}
	char* what() const { return "Brass"; }
};

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

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

//new function 

void f (Instrument& i) { i.adjust (1);  }


int main() {
	Wind flute;
	tune(flute);

	Percussion drum;
	Stringed violin;
	Brass flugelhorn;
	Woodwind recorder;
	tune (flute);
	tune (drum);
	tune (violin);
	tune (drum);
	tune (flugelhorn);
	tune (recorder);
	f (flugelhorn); 
}


Last edited on
what the hell is
new function

?

and what are your errors?
your what() method in Wind does NOT correctly override your what() in Instrument, as that one takes a 'note' as a parameter. In essence the compiler thinks you're trying to instantiate an abstract Instrument class.

stuff like that.
sorry it was suppose to be a comment i made changes..!!!
the errors are:

instrument1.cpp
1 error C2259: 'Wind' : cannot instantiate abstract class
1> due to following members:
1> 'char *Instrument::what(note) const' : is abstract
1> see declaration of 'Instrument::what'
1> error C2259: 'Percussion' : cannot instantiate abstract class
1> due to following members:
1> 'char *Instrument::what(note) const' : is abstract
1> see declaration of 'Instrument::what'
1>error C2259: 'Stringed' : cannot instantiate abstract class
1> due to following members:
1> 'char *Instrument::what(note) const' : is abstract
1> see declaration of 'Instrument::what'
1> error C2259: 'Brass' : cannot instantiate abstract class
1> due to following members:
1> : see declaration of 'Instrument::what'
1> error C2259: 'Woodwind' : cannot instantiate abstract class
1> due to following members:
1> 'char *Instrument::what(note) const' : is abstract
1> see declaration of 'Instrument::what'
1> Generating Code...
1> Skipping... (no relevant changes detected)
1
1>Build FAILED.
1>
1>Time Elapsed 00:00:02.41
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
if you look at my reply i've given you the answer. it now compiles for me.
okay.

instead of char* what().... in your child classes, write:
char* what(note)... etc

it will then correctly override.
okk will try again..!!!
Topic archived. No new replies allowed.