“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();
The func() in Class A will never be looked at, never the less executed, due to automatic data hiding when two functions are named the same in inherited classes. As mentioned earlier here, the correct way to call a base class function, is to use the class::function_name notation inside the derived class’ function.
Class B: public A
void func( ) {
A::func();
}
Never the less, as the func fuction in B is not public, you will first get the error: “function does not take 0 parameters.”, and if you do pass a number, you will get an error stating something like “can’t access private member of class B”.
Adding public: to class B. Solves that problem. But due to previously explained data hiding, you won’t invoke A::func(); either way, unless directly calling it from inside a function of B.
Well, func() in class A will be called as class B publically inherit class A. So all the public member function of A can be called by B if not available in class B and this is case of function overloading and not overriding. No syntax error or anything like that.
The original code has a typo, so I’ll assume that was a mistake and that the intent is to discuss inheritance and overloading functions but still making the base class methods accessible from the child classes.
1. You’ll get a compile error because the original code declares the classes and methods, but never defines them. To fix that, define the methods:
class A
{
public:
void func();
};
void A::func() {
}
class B : public A
{
void func(int a);
};
void B::func(int a) {
}
int main(void)
{
B derv;
derv.func();
}
2. However, this will still fail compilation. This time, it is because you have overloaded the method func() from class A. There are two ways to fix this:
a.) You can change the call in main() from derv.func(); to derv.A::func(); so that it will explicitly call the method in the base class.
b.) You can use the “using” keyword in the base class definition to expose the overloaded method from the base class. The other posts above use the wrong syntax ‘using A::func();’ You should not include the parenthesis. So just change this to ‘using A::func;’ However, you can’t just do what the others have done here because they did not put this in a public: section so it would not be accessible outside the class.
The correct way:
class B : public A
{
public:
using A::func;
void func(int a);
};
Do you guys even try to compile your answers before posting them here as correct?
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
All other typos (and mishaps?) aside:
The func() in Class A will never be looked at, never the less executed, due to automatic data hiding when two functions are named the same in inherited classes. As mentioned earlier here, the correct way to call a base class function, is to use the class::function_name notation inside the derived class’ function.
Class B: public A
void func( ) {
A::func();
}
Never the less, as the func fuction in B is not public, you will first get the error: “function does not take 0 parameters.”, and if you do pass a number, you will get an error stating something like “can’t access private member of class B”.
Adding public: to class B. Solves that problem. But due to previously explained data hiding, you won’t invoke A::func(); either way, unless directly calling it from inside a function of B.
Well, func() in class A will be called as class B publically inherit class A. So all the public member function of A can be called by B if not available in class B and this is case of function overloading and not overriding. No syntax error or anything like that.
It will call the func in base class.an example of function overloading.
The original code has a typo, so I’ll assume that was a mistake and that the intent is to discuss inheritance and overloading functions but still making the base class methods accessible from the child classes.
1. You’ll get a compile error because the original code declares the classes and methods, but never defines them. To fix that, define the methods:
class A
{
public:
void func();
};
void A::func() {
}
class B : public A
{
void func(int a);
};
void B::func(int a) {
}
int main(void)
{
B derv;
derv.func();
}
2. However, this will still fail compilation. This time, it is because you have overloaded the method func() from class A. There are two ways to fix this:
a.) You can change the call in main() from derv.func(); to derv.A::func(); so that it will explicitly call the method in the base class.
b.) You can use the “using” keyword in the base class definition to expose the overloaded method from the base class. The other posts above use the wrong syntax ‘using A::func();’ You should not include the parenthesis. So just change this to ‘using A::func;’ However, you can’t just do what the others have done here because they did not put this in a public: section so it would not be accessible outside the class.
The correct way:
class B : public A
{
public:
using A::func;
void func(int a);
};
Do you guys even try to compile your answers before posting them here as correct?
Leave an Answer/Comment