0

Maybe it's a weird question -sorry if the title is confusing- ...this is the code:

#include <iostream>

class Inherited {
    public:
        int method1() {
            int value = 2;
            return value;
        }
        int method2() {
            int value = method1() + 3;
            return value;
        }
    };

class Inheriting : public Inherited {
    public:
        int method1() {
            int value = 3;
            return value;
        }
    };

int main(){
    Inheriting object;
    std::cout << "method1 value = " << object.method1() << "\n";
    std::cout << "method2 value = " << object.method2() << "\n";
    return 0;
}

and it's output:

method1 value = 3
method2 value = 5

So, Is there any way that method2 can use method1 from the inheriting class -and it's value could be 6 instead 5- or must I overload the two methods in the inheriting class? It seems that virtual methods may be the solution, but I can't see exactly how. Very grateful for any help.

Ángel
  • 133
  • 1
  • 11
  • 1
    If by "not overloading" you mean "not using virtual methods", the answer is definitely no. Well, there are some extremely ugly tricks based on template specializing the child classes but I guess you don't want them. – vdavid Aug 10 '18 at 21:48
  • Very grateful for your answer, @vdavid. I don't know what virtual methods are -I'm learning by myself- and I certainly don't want to use _ugly tricks_ or some kind of over-engineering. – Ángel Aug 10 '18 at 21:54

0 Answers0