Problem to overload base methods in derived ?

There are three classes should inherit two overloaded functions here.
The problem is that derived classes call one function from second only
against the class declaration despite of the overloading params.


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
class a_class {
public:
		
	void	f(double )	 {printf("Class A (double)\n");}
	void	f(unsigned long) {printf("Class A (unsigned long)\n");}	
};

class b_class: public a_class {
public:
	
	void	f(unsigned long) {printf("Class B (unsigned long)\n");}
};

class c_class : public b_class  {
public:
	
	void	f(double) {printf("Class C (double)\n");}
};

	void main()
	{	a_class a;
		b_class b;
		c_class c;
		
		double        x=0.001;
		unsigned long y=10;
		
			//Print Out:
		a.f(x); //Class A (double)	  -ok
		a.f(y); //Class A (unsigned long) -ok

		b.f(x); //Class B (unsigned long) -?
		b.f(y); //Class B (unsigned long) -?

		b.f(x); //Class B (unsigned long) -?
		b.f(y);	//Class B (unsigned long) -?		
	}


They're not overridden, they're hidden. When you create a function with the same name but different parameters in a child class, it "hides" the parent class functions.

If you want them to be un-hidden, you need the usind directive.

Also main() should return an int.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class a_class {
public:

	void	f(double )	 {printf("Class A (double)\n");}
	void	f(unsigned long) {printf("Class A (unsigned long)\n");}
};

class b_class: public a_class {
public:
    using a_class::f;  //  <-  THIS
	void	f(unsigned long) {printf("Class B (unsigned long)\n");}
};

class c_class : public b_class  {
public:
    using b_class::f;  // <--  AND THIS
	void	f(double) {printf("Class C (double)\n");}
};


Also, your 3rd set of f calls was probably supposed to be c.f()?
Also, you can blot out both functions from base class, so any of them won't be hidden. All you've got to do is:

1
2
3
4
5
6
class b_class: public a_class {
public:
	
	void	f(unsigned long) {printf("Class B (unsigned long)\n");}
        void f(double) {printf("Class B (double)\n");}
};

using b_class::f;


Whad's it mean using b_class:: ? b_class inherited or what?

Also, your 3rd set of f calls was probably supposed to be c.f()?


Maybe more.
Both don't work. What C++ class for?
@warrant

My solution works, I'm sure.
What your solution has to do with class?
I don't understand your problem man. Tell me exactly what you're trying to say.
Did I ask you or something?
warrant wrote:
What C++ class for?

warrant wrote:
What your solution has to do with class?

warrant wrote:
Did I ask you or something?


Short term memory, eh?

Well, what do you not understand about classes? You used them yourself in your own code, so I assume you made the code yourself and didn't just copy-paste someone else's code.

The code that was given to you was meant as example code, because it was an example of code that was correct.
Topic archived. No new replies allowed.