“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
there will be a compilation error bcoz A::func() is only declared but not defined anywhere. wen derv.func() is called , A::func() is called as it is publicly inherited from class A . B::func() can’t be called from main as it is private & also bcoz its prototype is different.
using A::func() can only be used if class B was privately inherited from A.
Submitted By: Abul Asim M. R. Qarshi — December 8, 2006
We get syntax error;
here we are using overloading of function method but without function definition;
so we ge error like this
`class B derv’ has incomplete type and cannot be initialized
there will be compilation error because function overloading will not work for inheritance.
even if you want it to work you must include the following code line in derived class.
A::func();
For your reference this is what MS VC compiler gives :’func’ : function does not take 0 parameters.
I believe the reason is, name hiding in C++. If you have same named identifier in two scopes. The later will hide the former. Eg. if you have variable X on global scope and a same named variable X there in one of the function scope, then the later will have previllage over the global one. And same applies to the functions too.
Overloading concept is applicable within the same scope. Hence it is irrevalent to overload the base class functions within derived classes. The concept of overriding of functions should be used for this sort of requirements.
Well, I have read all the answers above, and as already been said, func() is not visible because func(int a) has hidden it. (you can’t say that func(int a) is just an overloaded version of func(), overloading works in the same scope). To make it work, you have to provide a corresponding void func() in B and inside that you can call A::func();
Compile error: no matching function B::func()
Also note that func is private.
the output will be the compile error because B doent know the defination of func().
To make this program run .it should be like
Class A
{
public:
void func();
};
class B:pubic A
{
using A::func();
void func(int a);
};
int main()
{
B derv;
derv.func();
}
there will be a compilation error bcoz A::func() is only declared but not defined anywhere. wen derv.func() is called , A::func() is called as it is publicly inherited from class A . B::func() can’t be called from main as it is private & also bcoz its prototype is different.
using A::func() can only be used if class B was privately inherited from A.
well its overloading instead or overriding.. so method will be called of the base class that is A.
calling derv.func() will invoke A::func()
the output will be compiler error because the func() has no defintion
this program contains the concept of function overloading!
error will be there-too few parameters passed in derv::func(int x) !
compiler complains of syntax errors:
Class A // upper case C
class B:public A // what is a “pubic”
We get syntax error;
here we are using overloading of function method but without function definition;
so we ge error like this
`class B derv’ has incomplete type and cannot be initialized
It is an error as we have not defined function func() and a private function of Class B cannot be invoked from Main(). This is absurd!
The compiler will give following error
Error: B::func(int) is not accessible from main().
Too few arguments in call to “B::func(int)”.
If ignore the typo and suppose the functions are defined somewhere.
The derv.func(); will invoke A::func(). because:
A::func()
B::func(int a)
are two different functions. A::func() is public. Sure the main return integer, but there is no return clause?
there will be compilation error because function overloading will not work for inheritance.
even if you want it to work you must include the following code line in derived class.
A::func();
ofcourse the function needs to be public
Friends,
For your reference this is what MS VC compiler gives :’func’ : function does not take 0 parameters.
I believe the reason is, name hiding in C++. If you have same named identifier in two scopes. The later will hide the former. Eg. if you have variable X on global scope and a same named variable X there in one of the function scope, then the later will have previllage over the global one. And same applies to the functions too.
The derv.func(); will invoke A::func(). because:
A::func()
B::func(int a)
are two different functions. A::func() is public
How can you create an object of an abstract class?
int main()
return type is int, but no return;
line 6: class B:pubic A
pubic A or public A?
Overloading concept is applicable within the same scope. Hence it is irrevalent to overload the base class functions within derived classes. The concept of overriding of functions should be used for this sort of requirements.
Well, I have read all the answers above, and as already been said, func() is not visible because func(int a) has hidden it. (you can’t say that func(int a) is just an overloaded version of func(), overloading works in the same scope). To make it work, you have to provide a corresponding void func() in B and inside that you can call A::func();
The answer suggested by “Muhammad Ishaq” is correct and verified. Assuming typo and other minor errors
Leave an Answer/Comment