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.
Related
I have a super Class (A) and two sub Class (B ,C)
an abstract Function in A have two difference return type in B and C!
How i have to Declare these??
return type is important
class A { //Super Class
public:
A();
virtual (some Type) QWERTY() = 0;
};
class B : public A { //Sub Class
public:
B();
double QWERTY();
};
class C : public A { //Sub Class
public:
C();
unsigned int QWERTY();
};
i'v to call sub Class function with super Class pointer
Since the functions are different in each sub-classes, you'll have to access them from pointers to those sub-classes.
This is exactly the kind of situation where dynamic_cast<> can help: it can conditionally convert a pointer from a base class to a sub-class if and only if it happens to be of the correct type:
void foo(A* a_ptr) {
B* b_ptr = dynamic_cast<B*>(a_ptr);
C* c_ptr = dynamic_cast<C*>(a_ptr);
if(b_ptr) {
b_ptr->QWERTY();
}
if(c_ptr) {
c_ptr->QWERTY();
}
}
It's, however, worth mentioning that this is some pretty ugly code, and might be suitable to solve the quiz you are presenting us, but in a normal environment, there are some design reevaluation that would happen before going to implement things this way.
Here is the code
,it is my homework using overriden methods teacher told us to analyze the code. I know the code is outputting 2, I have no clue how this code work.
public:
int a;
virtual void who(void) { a = 1; }
};
class B:public A{
public:
int a;
void who(void) { a = 2; }
};
class C :public B {
};
int main(void) {
A x; B y; C z; A *p;
p = &z;
p->who();
cout << z.a << endl;
system("pause");
return 0;
}
B overrides the who() function of its parent, A. This is called polymorphism. C inherits from B, but doesn't override anything; thus, it uses all of the implementation of B.
p is a pointer to an object of class A. One of the key features of class inheritance is that a pointer to a derived class is type-compatible with a pointer to its base class [1].
This means that when you call a member function of a pointer (p->who()), and the class of the object the pointer is pointing to overrides a member of its parent, is going to use the overridden member.
Sources:
[1] http://www.cplusplus.com/doc/tutorial/polymorphism/
as long as you create a function with same input and output with name; in short: same function declaration.. the new one will be used as you refer to one which has super class that has same function. in your case; super class for C is B and it doesn't see A, but B sees A and use all functions it has except what's B declare a new implementation for.
This is a very basic question, but my C++ skills are a bit rusty...
I have a base class that has a member function that takes 3 arguments, e.g.:
class MyBaseClass
{
public:
int func(int a, char b, int c);
};
and a derived class that overloads that function with a 1-argument version., e.g:
class MyDerivedClass : public MyBaseClass
{
public:
int func(float a);
};
When I try to call the function from the base class on an object of the derived class, like this:
MyDerivedClass d;
d.func(1, 'a', 0);
the compiler complains that MyDerivedClass::func() does not take 3 arguments. That is true, but shouldn't I be able to access the base class function through an object of the derived class?
What am I doing wrong?
MyDerivedClass::func is hiding the name MyBaseClass::func. You can fix this with a using declaration:
class MyDerivedClass : public MyBaseClass
{
public:
using MyBaseClass::func;
int func(float a);
};
You probably intended to declare the method MyBaseClass::func() as virtual
But, if you really want to achieve
when I try to call the function from the base class on an object of
the derived class, like this
then, you can try
MyDerivedClass d;
d.MyDerivedClass::func( 3.14f );
This compiles and works, but does not seem to be good design.
the compiler complains that MyDerivedClass::func() does not take 3
arguments
That is indeed true, per your class definition, MyDerivedClass::func() takes only one argument.
Add "using Class::func;" in MyDerivedClass.
class MyDerivedClass : public MyBaseClass
{
public:
using MyBaseClass::func;
int func(float a);
};
You'll have to forgive me if this is a really basic question; I haven't used C++ this much in a long time so I've forgotten how a lot of it works.
Anyway, I have a base class and a couple derived classes like this (super oversimplified, but the gist is the same):
class Base
{
public:
Base () { }
int SomeFunction (int x, int y); // abstract definition
};
class Derived1 : public Base
{
public:
Derived1() : Base() { }
int SomeFunction (int x, int y)
{
// actual implementation
return 4;
}
};
class Derived2 : public Base
{
public:
Derived2() : Base() { }
int SomeFunction (int x, int y)
{
// actual implementation
return 7;
}
};
Later on in main I have a list of Base objects:
Base *baseobjects[10];
Later I fill that array with instances of Derived1 and Derived2. That works with baseobjects[i] = &newDerived1 (where newDerived1 is an instance of the Derived1 class). That's all fine.
What I can't seem to figure out is how to later iterate through the baseobjects array and call SomeFunction on every instance in the list without explicitly knowing which derived class I'm using. I've done this in C# and it works fine, but apparently my C++ syntax is off:
int result = baseobjects[i]->SomeFunction(a, b);
That gives me a LNK2019 error when I try to compile, apparently because it's looking at the Base class for the implementation and it isn't there. I'm assuming I have to use some pointer tricks to get it to look at the proper derived method, but nothing I've tried yet has worked. Any suggestions?
Your method should be declared virtual. And in your case, probably pure virtual.
class Base
{
public:
Base () { }
virtual int SomeFunction (int x, int y) = 0; // abstract definition
};
Note that, while this is not absolutely required, you might as well declare a virtual destructor. Do it if you ever delete a derived instance trough a pointer of the base class.
class Base
{
public:
//Base () {} // This is not required, the default provided constructor is similar.
virtual ~Base() {} // virtual destructor.
virtual int SomeFunction (int x, int y) = 0; // abstract definition
};
Edit:
Also, regarding the link error you posted:
Either you forgot the = 0, either you are calling Base::SomeFunction() from somewhere.
As Thomas Edleson points out, = 0 does not mean that your function has no implementation: it can have one, but it only requires the derived classes to (re)implement it to not being abstract.
If you are interested in this topic, I suggest you read this post.
If you want to override a method, it must be virtual.
class Base
{
public:
Base () { }
virtual int SomeFunction (int x, int y); // abstract definition
}
Seccond thing is that your derivated classes did not extends of your base-class.
class Derived1 : public Base
{
public:
Derived1() : Base() { }
int SomeFunction (int x, int y)
{
// actual implementation
return 4;
}
}
You have to declare the member function SomeFunction()
virtual
abstract
So the declaration for Base should look like this:
class Base
{
public:
Base() { }
virtual int SomeFunction(int x, int y) = 0;
};
You can omit the virtual keyword in the derived classes.
To just slightly elaborate on ereOn's answer, you do need to declare you base class function (ie: your abstract definition) in order for your derived classes to be able to override it. What he didn't clarify though is that other than that, what you've posted is fine (that is, the calls to the function you posted will then work without any modification).
Note also, that if you're after a pure abstract definition, you should append = 0 to your function declaration as follows:
class Base
{
public:
Base () { }
virtual int SomeFunction (int x, int y) = 0; // pure abstract definition
};
This lets the compiler know that classes derived from Base must supply their own implementation of SomeFunction.
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().