inheritance & overload without overriding

Hello,

I have two classes base and derived, base has a void foo(), derived classes doesn't override void foo() instead, I wrote an over loaded function which is void foo(int), If I create an instance of derived class, compiler doesn't recognize void foo(), it only sees void foo(int)

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
class base {
public:
	int a;
	base(){
		a = 1;
	}
	virtual void foo(){
		cout<<"this is base\n";
	}
};

class derived : public base {
public:
	int b;
	derived(){
		b = 2;
	}
        // overload without overriding 
	void foo(int x ){
		cout<<"this is derived\n"<<x;
	}

};

derived a;
// this is inside base class but it doesn't recognized by compiler
a.foo();


my Other question is;
in order to override a function, it must have same name return type variable... what if I change return type ? it doesn't count as either override or overload, than what is it ?

thank you for your replies
Please try to change your derived class to this
1
2
3
4
5
6
7
8
9
10
11
12
13
class derived : public base {
public:
	int b;
        using base::foo;
	derived(){
		b = 2;
	}
        // overload without overriding 
	void foo(int x ){
		cout<<"this is derived\n"<<x;
	}

};
thanks, it works. Do I have to introduce all base functions to derived classes ? isn't there a cleaner way of doing this ?
Are you sure you want to design your objects with this sort of interface?
I made these two classes to show the situation, however I would like to hear how I can make a better implementation ? by the way what is wrong with this ?
I was just curious how you got there. But if it's contrived, that's ok.

A better implementation would be to avoid the situation altogether.
Topic archived. No new replies allowed.