Here's my class structure:
class Parent {
public:
void foo(); // foo() defined in superclass
};
class Child : public Parent {
private:
int bar;
public:
Child(int);
int getBar();
};
Child::Child(int b) {
bar = b;
}
void Child::foo() { // foo() implemented in subclass
std::cout << getBar() << std::endl;
}
g++ gives me an error that foo()
is not within Child
scope, and changing it to void Parent::foo()
, I'm left with an error that getBar()
is not within Parent
scope.
I'm aware of virtual functions but I do not want to define foo()
in Child
, only implement it.
How do I gain method visibility within Child
of the Parent
method foo()
?
My thought process is the line class Child : public Parent
means Child
inherits member methods of Parent
, thus Child
should be able to see foo()
.
Child
(not define them), as in, add the linevoid foo()
underpublic:
in the classChild
? Then, you can go on to define the function undervoid Child::foo()
.virtual
keyword. Some uses of this are listed: stackoverflow.com/questions/2391679/…foo()
inChild
, only implement it" A function's implementation is its definition. So you do not want to definefoo()
inChild
; instead you want to definefoo()
inChild
. ??? Did you mean "declare" instead of "define"?