"using" kyeword in C++

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
#include <iostream>

using namespace std;

class Parent{

protected:
	void Test1(){
		cout<<"I am protected in base class";
	}
	void Test2(){
		cout<<"I am Protected too in base class";
	}
};



class Child:public Parent{

public:
	using Parent::Test1;    // What will happen here
};



void main()
{
	Child obj;
	
	obj.Test1();           // Why it has accesssible, it is protected in base class
	//obj.Test2();         //Compilation Error
}



Why the above code is working for Test1() function.
Last edited on
Any thing on this????
I guess putting the using statement in the public section of the Child class makes the function public in Child.

=P

But you seemed to figure that out on your own.... so what else do you want to know?
Disch, dose comiler treat it as overridden function with same definition and change its scope to public
Sort of.

Overriding the function would result in duplicate code. This makes it public without recreating the function.

Note it would only work if you're accessing it via a Child:

1
2
3
4
5
Child obj
Parent* ptr = &obj;

obj.Test1();  // OK
ptr->Test1(); // ERROR -- protected 
thanks Disch....:)
Topic archived. No new replies allowed.