May be my question is wrong. I am new to C++.
Is there any way call a base class member function using derived class object if that function is being overridden in derived class?
For example:
class A {
public:
void add() { cout<<"A"; }
};
class B: public A {
public:
void add() { cout<<"B"; }
};
int main() {
B bObj;
bObj.add(); // calls member function of class B
return 0;
}
First of all, you aren't really overriding the add function, merely hiding the name, since A::add is not declared virtual.
To call A::add, just be explicit about it:
bObj.A::add();
Typically, B::add() exists for a reason and you probably shouldn't be calling A::add() on a B. Which isn't to say you can't - you just have to turn it into an A first:
B bObj;
static_cast<A&>(b).add(); // calls A::add()
Or explicitly specify that it's A::add():
bObj.A::add();
From within B, you have to qualify the call:
class B: public A {
public:
void add() { cout<<"B"; }
void test() {
add(); // calls B::add
A::add(); // calls A::add
}
};
Related
May be my question is wrong. I am new to C++.
Is there any way call a base class member function using derived class object if that function is being overridden in derived class?
For example:
class A {
public:
void add() { cout<<"A"; }
};
class B: public A {
public:
void add() { cout<<"B"; }
};
int main() {
B bObj;
bObj.add(); // calls member function of class B
return 0;
}
First of all, you aren't really overriding the add function, merely hiding the name, since A::add is not declared virtual.
To call A::add, just be explicit about it:
bObj.A::add();
Typically, B::add() exists for a reason and you probably shouldn't be calling A::add() on a B. Which isn't to say you can't - you just have to turn it into an A first:
B bObj;
static_cast<A&>(b).add(); // calls A::add()
Or explicitly specify that it's A::add():
bObj.A::add();
From within B, you have to qualify the call:
class B: public A {
public:
void add() { cout<<"B"; }
void test() {
add(); // calls B::add
A::add(); // calls A::add
}
};
I have the following class hierarchy:
class A
{
public:
virtual void func()
{
cout<<"A"<<endl;
}
};
class B: public A
{};
class C: public B
{
public:
virtual void func()
{
cout<<"C"<<endl;
}
};
class D: public C
{
public:
virtual void func()
{
//I don't want to use the class C's implementation
//So I should call the next base function: B::func()
//But since B does not re-define the virtual function, we might as well
//directly call A::func()
//But what is the correct practice? Call A::func() directly or call B::func()
//A::func();
//B::func();
}
};
int main()
{
A *ob;
D dob;
ob = &dob; ob->func();
return 0;
}
I don't want to use the class C's implementation
So I should call the next base function: B::func()
But since B does not re-define the virtual function, we might as well directly call A::func()
But what is the correct practice? Call A::func() directly or call B::func()
There's no real diffrence, since the B structure contains a copy of the A::func() itself - When a deriviate doesn't override the parent classes implementation, the function is copied. However, I believe casting the C object another time back to A from B would be less efficient, and thus I believe B::func() is the better way of the two.
This question already has answers here:
Why does an overridden function in the derived class hide other overloads of the base class?
(4 answers)
Closed 8 years ago.
Consider the following code:
class Base
{
public:
void Fun()
{
}
};
class Derived : public Base
{
public:
void Fun(int x)
{
}
};
int main()
{
Derived d;
d.Fun(5);
d.Fun(); // Compilation error
}
On compilation, the compiler complains that there is no version of Fun() that takes zero arguments. Why is this so? I can understand compiler hiding the base class version if the function signatures match in Base and derived classes, but why is the compiler not able to resolve both functions if they take different parameters/different no. of parameters?
you have to create function body in derived class
void Fun()
{
}
Compile time first it will check function body in derived class.
You can use base class function if you are not overloading the function in derived class..
In the below example I changed the function name of derived class...
class Base
{
public:
void Fun()
{
}
};
class Derived : public Base
{
public:
void Fun1(int x)
{
}
};
int main()
{
Derived d;
d.Fun1(5);
d.Fun(); // No Compilation error
}
If you want to overload the function in derived class then you will have to override the base class function. like this
class Base
{
public:
void Fun()
{
printf("Base Class\n");
}
};
class Derived : public Base
{
public:
void Fun()
{
Base::Fun();
}
void Fun(int x)
{
printf("Derived Class\n");
}
};
int main()
{
Derived d;
d.Fun(5);
d.Fun(); // No Compilation error
}
compilation error, as there is no any 'Fun()' in derived class. You are calling Fun() by using derived class object. In CPP this is called as 'method hiding'. To solve this create function Fun() in derived class
void Fun()
{
}
I am writing a simple piece of code.
class A
{
public:
virtual func ()
{ // allocate memory
}
};
class B : public A
{
public:
func ()
{ // some piece of code
// but call base class same function Ist
}
}
main()
{
A *ptr = new B;
ptr->func () //here I want to call base class function first
//and then derived class function
// How to implement ??
}
How to call base class function first and then call same function from derived class ??.
I dont want to call each function explicetly, I will just call derived class function and the base class function should be automatically called.
I dont want any constructor to call these functions.
Is there any way to implement this or this is all rubbish.
Call the method func of the parent class (you need to do this explicitly) in the implementation of B:
class B: public A
{
public:
func()
{
A::func();
...
}
}
You can call A::func() explicitly.
class B : public A
{
public:
void func ()
{
A::func(); // call base class func()
// some more code
}
}
You can't arrange for it to happen automatically; you'll have to call the base-class function from the derived-class override:
void B::func() {
A::func();
// then do something else
}
Why can B::Func call A::Func using syntax that makes it look like a static method call? Shouldn't this fail because it is an instance method?
class A {
public:
void Func() {
printf( "test" );
}
};
class B : private A {
public:
void Func() {
A::Func(); // why does it work? (look below in main())
}
};
int main() {
B obj;
obj.Func();
// but we cannot write here, because it's not static
// A::Func();
return 0;
}
That isn't "called like static". That's merely the syntax used to explicitly specify which member function to call. Even if Func were virtual, that syntax could be used to call a base class version.
class B : public A {
public:
void Func() {
this->A::Func(); /* this-> can be omitted */
}
};
int main() {
B obj;
obj.A::Func();
return 0;
}
Edit: obj.A::Func() would be invalid actually, in your case, because the inheritance is private, so main cannot see that A is a base of B. I've changed B's inheritance to public to make the answer correct, but it would otherwise still work outside of the class, when in a friend function.