Below program contains two show() functions in parent and child classes, but first show() function takes FLOAT argument and second show() function takes INT argument.
.If I call show(10.1234) function by passing float argument, it should call class A's show(float a) function , but it calls class B's show(int b).
#include<iostream>
using namespace std;
class A{
float a;
public:
void show(float a)
{
this->a = a;
cout<<"\n A's show() function called : "<<this->a<<endl;
}
};
class B : public A{
int b;
public:
void show(int b)
{
this->b = b;
cout<<"\n B's show() function called : "<<this->b<<endl;
}
};
int main()
{
float i=10.1234;
B Bobject;
Bobject.show((float) i);
return 0;
}
Output:
B's show() function called : 10
Expected output:
A's show() function called : 10.1234
Why g++ compiler chosen wrong show() function i.e class B's show(int b) function ?
If you have a function in a derived class that has the same name as a function in the base class, it hides all of the functions in the base class. You either need to rename your function, or use a using declaration in your derived class:
using A::show;
or, you can explicitly call the base class function:
Bobject.A::show(i);
There is no polymorphism involved here. You should declare your functions virtual to make them polymorphic, and make them have the same signature.
Use a using declaration.
class B : public A{
int b;
public:
using A::show;
…
};
You're mixing things:
If you had 2 functions in class A with the following signatures:
void Show(int a);
void Show(float a);
Then the compiles would have chosen the "correct" function.
When you define a name in a derived class, it hides the name from the base class.
You can achieve what you want by adding this to B's definition:
using A::show;
That will let you use both A's show() and B's show().
Related
I have this situation:
class A
{
public:
A();
virtual ~A();
void doSomething();
};
class B : public A
{
public:
B();
void doSomething(int parameter);
};
If I do the following:
int main()
{
B b;
b.doSomething();
}
It gives me a compile error (no matching function).
How can I solve that without changing the B funcion name? B derives from A so it has also the doSomething() function without parameters. Why is it not working?
Thanks for the help.
Currently, B::doSomething hides A::doSomething.
You might use using to resolve your issue:
class B : public A
{
public:
using A::doSomething;
B();
void doSomething(int parameter);
};
This answer is just an alternative to this solution in case you aren't allowed to modify class B.
If you aren't allowed to modify class B for adding using A::doSomething; inside the class (as already suggested here), you can take a pointer to the member function A::doSomething(), then call it through that member function pointer on the instance b:
auto fptr = &A::doSomething;
B b;
(b.*fptr)();
or simpler, by properly qualifying the member function no member function pointer is needed:
B b;
b.A::doSomething(); // calls A::doSomething()
So i have been trying to use derived class data members from the base class and i am not able to figure out how to do that. I see a way to do this as just passing the member i need in parameter when i call the base class method but i was just thinking that there should be another way to do that. So i have replicated this as below.
#include<iostream>
using namespace std;
class B;
class A{
public:
display(){
cout<<cord<<endl;
}
int cord = 25;
};
class B : public A{
public:
B(){
A a;
a.display();
}
int cord = 30;
};
class C : public A{
public:
C(){
A a;
a.display();
}
int cord = 35;
};
int main(){
B b;
C c;
B.display();
}
The above code as giving output as
25
25
25
What i want it to give out is
30
35
30
Every way to do this will be appreciated, whatever is better and if you want to me add something or anything ask in comments, i'll do right away.
define a virtual getter like
virtual int getCord() const { return cord; }
in each class and call it in display
void display(){ cout << getCord() <<endl; }
and in the constructor of B and C you also need to replace
A a;
a.display();
just by
display();
else there is no chance you access to the value of the sub classes explicitly calling display on an instance of A
using derived class data members from base class method
This is because of that I let the redefinition of cord in B and C, but I do not recommend you to do that kind of redefinition in 'real' codes ;-)
Lets take the B constructor:
B(){
A a;
a.display();
}
In it you create a completely separate object a of type A, and call display on it. That display call will be using the a object, not knowing anything about the B class or its totally separate cord member variable at all.
One possible way to solve this is to create a constructor of A that takes the value of cord as an argument, pass the "correct" value in the B constructor initializer list, and then call the display function on this object:
struct A{
A() = default;
explicit A(int c)
: cord(c)
{
}
display(){
cout<<cord<<endl;
}
int cord = 25;
};
struct B : A{
B()
: A(30)
{
display(); // Equivalent to this->display();
}
};
Of course, you need to do something similar for the C class (or structure).
Note that I removed the member variable cord from the B class. That's because if you declare a new member variable with the same name as a member variable in a base class, then you effectively create a completely new member variable that is unrelated to the one in the parent class. And for the simple example you show there's no need to "override" the member variable, as it already exists in all child-classes as well.
I want define a function in an abstract class which calls a abstract (virtual) function. But when I inherit from the abstract class this function is not inherited:
class A {
public:
virtual void f(int t) = 0;
void f() {
f(0);
}
};
class B : public A {
public:
void f(int t) override { }
// using A::f; // uncomment to fix the problem
};
int main() {
B b;
b.f(0); // works
b.f(); // error: no matching function for call to ‘B::f()’
}
Why doesn't this work? The workaround is to use using A::f, but why is A::f() not inherited?
It doesn't work because B does not have an f(), only an f(int).
If B did not have any function named f, then the superclass would be searched for a matching function, with overload resolution taking place. But because the subclass, B, does already have a function named f, the superclass is not searched, and the overload resolution happens in B.
This is what the using keyword is for, to make the superclass's f() a part of B's namespace. The answer here is, really, "that's because this is how C++ works".
I have a class (B) that inherits another class (A). I want to call a function from class A that has been overridden. I also want to be able to call the overridden function independent of what class inherited the base (say class C : public A , where I want to call C's version of the function.)
Here's an example
class A {
public:
void callF();
virtual void f() {};
};
class B : public A {
public:
void f();
};
void A::callF()
{
//FYI, I want to be able to call this without knowing what the super class is.
f();
}
void B::f()
{
std::cout << "I want this function to be called, but instead the default f() is called.";
}
Edit:
In my real code, I have an std::vector<A> aVector;. Then I would call aVector.push_back(B());. If I called aVector[0].callF();, The default a::f() would be called.
As answered below, I have a problem with slicing.
Your construction:
vector_of_A.push_back( B() );
doesn't store a B in the vector. It constructs a B, then constructs an A from that, then stores that A in the vector. As a consequence, you experience slicing.
See this for more info:
https://stackoverflow.com/a/4403759/8747
Your code is correct.
You may be getting the behavior you observed because your were calling f() or callF() from the constructor of A. That's the only case I can think of where A::f() would get invoked instead of B::f().
Your code works for me with this little test program:
int main()
{
// Instantiate B, and reference it via a base class pointer.
A* b = new B;
b->callF();
delete b;
}
Output:
I want this function to be called, but instead the default f() is called.
When calling a virtual member function within a base class member function, it's the derived member function that will be invoked.
I want to know the below code is correct or not
class A
{
public :
int show (int x, int y);
};
class B : public A
{
public :
float show (int a, int b); // can i overload this function ?
};
the show function is present in both base and derived class with different written types.
I know function overloading concept (can not overload with different return types).
Is this possible to do so?
The code will be compiled successfully. The method A::show will not be overloaded but hidden.
You can call this method with the scope operator.
Check this link or this link
Class A
{
Public :
virtual int show (int x, inty) = 0;
};
class B:Public A
{
Public :
float show (int x, int y);
};
When i declare base obj and point it to derived class :
A aObj;
B bObj;
aObju = &bObj;
bObj.Show(); // Which function will be called base class or derived class?
If you create a derived class object like
1)
B b;
b.show();
OR
2)
A* b = new B();
b->show();
Will always look into derived class, and call B::show().
This is for your particular example, where no virtual functions are present, if virtual functions are present in the base class, the second case can give different results in other cases, but in this particular example even base class virtual functions will make no difference.