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
}
Related
Consider this code:
#include <iostream>
class Base
{
public:
void Message()
{
std::cout<<"You are calling the function of class Base ";
}
};
class Derived : public Base
{
public:
void Message()
{
std::cout<<"You are calling the function of class Derived ";
}
};
int main()
{
Derived obj1;
obj1.Base::Message();
return 0;
}
These two lines of code:
Derived obj1;
obj1.Base::Message();
Call the class Derived which is derived from Base. The second line of code calls the Base class overridden function Message.
Why does the standard introduce obj1.Base::Message() when we can directly call the function Message with this code?
Base obj1;
obj1.Message();
Base::Message is necessary if you want to call that function on an object of type Derived. In the particular example you give, it's true that an alternate way of doing this is to use static_cast<Base&>(derived).Message(). However, in the case that Message is a virtual function, this won't work--you would always get Message from the most derived class.
One place where this is often used is if you are overriding a virtual function, but want to call the base function from within the derived one. For example:
class Base
{
public:
virtual void Message()
{
std::cout<<"You are calling the function of class Base ";
}
};
class Derived : public Base
{
public:
void Message() override
{
Base::Message();
std::cout<<"(and now for the Derived-only part of the message)";
}
};
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
}
};
How do I make a base class virtual function so that it always is called when a derived class calls its version of the function? I know I can call the base classes function by writing something like BaseClass::foo() at the beginning of the the function DerivedClass::foo(), but what if I want it to be called by default, without the creator of the derived class even knowing that the base classes function does anything?
class BaseClass
{
BaseClass();
virtual void foo() {
printf("base");
}
}
class DerivedClass : public BaseClass
{
DerivedClass();
void foo() {
printf("derived");
}
}
int main()
{
DerivedClass dc;
dc.foo();
}
Should print:
base
derived
That's not directly possible. You could split the function in non-virtual foo on the base class and (pure) virtual fooCore on the derived classes:
class Base {
protected:
virtual void fooCore() = 0;
public:
void foo(){
// do stuff, then call method of derived class
this->fooCore();
}
};
class Derived {
protected:
void fooCore() override {
//actual
};
};
From the "outside" the call Base::foo() stays the same.
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
}
};
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.