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 7 years ago.
Why won't the compiler call the base class function (parameterized one) through the derived class object? The derived class inherits the function from the base class, right?
#include<iostream>
using namespace std;
class Base
{
public:
int fun() { cout << "Base::fun() called"; }
int fun(int i) { cout << "Base::fun(int i) called"; }
};
class Derived: public Base
{
public:
int fun() { cout << "Derived::fun() called"; }
};
int main()
{
Derived d;
d.fun(5);
return 0;
}
However, when I used the scope resolution operator, I got the desired output. Can anyone provide me a logical explanation behind this? Can I still call the base class function (parameterized one) through derived class object? Thanks!
int main()
{
Derived d;
d.Base::fun(5);
return 0;
}
The derived function Derived::fun() hides all members with the same name from the base class. So you can only use the base class member via scope resolution. To avoid hiding, you can do:
class Derived: public Base
{
public:
int fun() { cout << "Derived::fun() called"; }
using Base::fun;
};
Related
This question already has answers here:
Derived class Private method is getting called
(2 answers)
Public virtual function derived private in C++
(3 answers)
Closed 1 year ago.
My Code looks like this -
#include <iostream>
using namespace std;
class Base {
public:
virtual void print() {};
};
class Derived: public Base {
private:
int secret;
void print() {
cout<<"the secret is "<<this->secret<<endl;
}
public:
explicit Derived(int a): secret(a) { }
};
int main() {
Base* b1;
Derived d1(1022);
d1.print(); // Wont Work - and should not work
b1 = &d1;
b1->print(); // Works and prints the secret
return 0;
}
I don't understand how can a derived class private function be called using a base class pointer? I understand that after declaring the base class function as virtual we essentially use late binding and check the contents of the pointer before associating it with a function call but does it ignore the access specifiers in the derived classes if the base class implementation of the same function is virtual and public?
This question already has answers here:
C++ virtual function from constructor [duplicate]
(7 answers)
Closed 7 years ago.
There is a c++ program:
# include <iostream>
using namespace std;
class base
{
public:
base()
{
cout<<"base"<<endl;
f();
}
virtual void f() {
cout<<"base f"<<endl;
}
};
class derive: public base
{
public:
derive()
{
cout<<"derive"<<endl;
f();
}
void f() {
cout<<"derive f"<<endl;
}
};
int main()
{
derive d;
return 1;
}
and it outputs:
base
base f
derive
derive f
I am wondering why base f appears?
I quess in base the constrctor expands to:
cout<<"base"<<endl;
this.f();
But this should point to derive so why base f is print out?
During construction, in the baseclass constructor, the actual type of the object is base, even though it lateron continues to become a derived. The same happens during destruction, btw, and you can also verify the type using typeid.
This question already has answers here:
Can't access derived class method from pointer of type base class
(3 answers)
Closed 8 years ago.
In my program I have a class with several derived classes. I'm am trying to store all instances of the derived classes in a vector. To do this the vector has the base class type, and they are all stored there fine. However when I try to access a method that belongs to a derived class from the vector I cannot do it as the base class does not have this method. Is there any way around this? Example code below.
#include <vector>
#include <iostream>
using namespace std;
class base
{
};
class derived
:public base
{
public:
void foo()
{
cout << "test";
}
};
int main()
{
vector<base*> *bar = new vector<base*>();
bar->push_back(new derived);
bar->push_back(new derived);
bar[0].foo();
}
Make foo a virtual method in base class. Then override it in derived class.
class base{
public:
virtual void foo()=0;
};
class derived
:public base
{
public:
void foo() overide
{
cout << "test";
}
};
You can now call foo with pointer/reference to base
int main(){ // return type of main should be int, it is portable and standard
vector<base*> bar; // using raw pointer is error prone
bar.push_back(new derived);
bar.push_back(new derived);
bar[0]->foo();
return 0;
}
Learn polymorphism and virtual function
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()
{
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why does an overridden function in the derived class hide other overloads of the base class?
Does f(int) in base class inherited in derived derived class D?
if yes then why overloading is not working here.
and if no then why f(int) is not inherited as class d is publicly derived.
I am confused.
class B {
public:
int f(int i) { cout << "f(int): "; return i+1; }
// ...
};
class D : public B {
public:
double f(double d) { cout << "f(double): "; return d+1.3; }
// ...
};
int main()
{
D* pd = new D;
cout << pd->f(2) << '\n';
cout << pd->f(2.3) << '\n';
}
This is the Classical example for Function Hiding.
The overload resolution in C++ does not work across classes, A function in derived class Hides all the functions with identical name in the Base class. Compiler only sees the function in derived class for an derived class object.
You can Unhide all the base class functions for your derived class by using the using Declaration. in your derive class.
Adding,
using B::f;
in your derived class will enable your derived class object to access all the functions with the name f() int the Base class as if they were overloaded functions in Derived class.
This should be a good read:
What's the meaning of, Warning: Derived::f(char) hides Base::f(double)?
I think if you give a derived class a function with the same name as functions existing in a base class, the derived class' function will hide the base class' functions. If you merely wanted to overload, use the using keyword.
class B {
public:
int f(int i) { cout << "f(int): "; return i+1; }
// ...
};
class D : public B {
public:
using B::f; //brings in all of B's "f" functions
//making them equals of D's "f" functions.
double f(double d) { cout << "f(double): "; return d+1.3; }
// ...
};
int main()
{
D* pd = new D;
cout << pd->f(2) << '\n';
cout << pd->f(2.3) << '\n';
}