0

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().

4
  • Why can't you declare the methods in the class Child (not define them), as in, add the line void foo() under public: in the class Child? Then, you can go on to define the function under void Child::foo().
    – RealPawPaw
    Commented Nov 18, 2019 at 0:11
  • @RealPawPaw then what is the point of a superclass for this example? Not trying to be short, just trying to understand super/subclass relationships. If it's defined in both the parent and child classes, there's no reason for it to be defined in the parent class.
    – gator
    Commented Nov 18, 2019 at 0:21
  • You don't have to use them - it's just a feature. But you'll have to use the virtual keyword. Some uses of this are listed: stackoverflow.com/questions/2391679/…
    – RealPawPaw
    Commented Nov 18, 2019 at 0:27
  • Your question contains a contradiction: "I do not want to define foo() in Child, only implement it" A function's implementation is its definition. So you do not want to define foo() in Child; instead you want to define foo() in Child. ??? Did you mean "declare" instead of "define"?
    – JaMiT
    Commented Nov 18, 2019 at 1:05

2 Answers 2

1

You are using wrong C++ terms: what you call "definition" is properly called "declaration", and what you call "implementation" is properly called "definition". Use correct terms to avoid confusion and misunderstanding.

So, if you define Child::foo you have to add corresponding declaration as well. I fixed it below.

Also look at the link RealPawPaw gave in his comment about when/why you should use virtual.

class Parent {
    public:
        /*virtual*/ void foo(); // this is declaration of Parent::foo
};

class Child : public Parent {
    private:
        int bar;
    public:
        Child(int); // this is declaration of constructor
        int getBar(); // this is declaration of Child::getBar
        void foo(); // this is declaration of Child::foo
};

// this is definition of Parent::foo
void Parent::foo() {
    std::cout << "Parent" << std::endl;
}

// this is definition of constructor
Child::Child(int b) {
    bar = b;
}

// this is definition of Child::getBar
int Child::getBar() {
    return bar;
}

// this is definition of Child::foo
void Child::foo() {
    std::cout << "Child: bar=" << getBar() << std::endl;
}
0

You need to write a prototype void foo(); in the public section of Child class as well to tell you are using an inherited method. Then you can implement it like you did.

You are not able to access getBar() because you have not declared it. Declare it and it will work fine.

Not the answer you're looking for? Browse other questions tagged or ask your own question.