using keyword

why using keyword required?

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 Top
{
	int a;

public:
	
	void read(){};

};

class Bottom:public Top
{
	int b;

public:
	using Top::read;       //using declare name of parent function
	
	void read(int a){};
};

int main()
{

	Bottom b;
	b.read();              //error occurs
	getch();
	return 0;
}
You dont need using keyword here if you are trying to override function from "Top"

When you say "error occurs" I assume it is when you dont have using Top::read;

This is intended and correct behavior, you have an object of class Bottom so when you call read the read from Bottom is getting invoked (which overrides the one in base called Top). This function takes 1 argument, you are passing none, hence the error.

If you want the function from Top then either do not override it in bottom or typecast "b" to Top before calling read.
thank you codewalker for reply.

Bu in above example i did not override void read(){}; from Top.
I just define new void read(int a){}; in Bottom which is different in parameter list.
Only name is same.

And Derived class can call base class member function that all we knew very well.
so I am also trying to call base(Top) read() by Derived class(Bottom) object but it gives error.

So, I used following -
using Top::read;
to tell compiler that Base class(Top) also have function with same name i.e read(), So go there and please check it once and allow me to call that version of read().
Topic archived. No new replies allowed.