Compilation error when base and derived classes defined in separate namespaces - c++

I have defined base class and derived class in separate namepsaces(This is a requirement as several classes can be derived from a single base class and based on behaviour of derived classes they are to be place din separate namespaces.)
Base.h
namespace global
{
{
class Base
{
public:
Base();
virtual ~Base();
virtual int someFunc(int arg1);
}
}
Derived.h
namespace global
{
namespace derived
{
class Derived: public Base()
{
public:
Derived();
~Derived();
}
}
}
Derived.cpp
namespace global
{
namespace derived
{
Derived::Derived() {}
Derived::~Derived() {}
int Derived::someFunc(int arg1)
{
//some code here
}
}
}
When I try to compile this code, I get the error:
no 'int global::derived::Derived::someFunc(int arg1)' member function declared in class global::derived::Derived.
So, do I need to declare someFunc in Derived again?
like:
namespace global
{
namespace derived
{
class Derived: public Base()
{
public:
Derived();
~Derived();
int someFunc(arg1 int);
}
}
}
Now, if there is some Function in a totally separate namespace, that accepts base class reference, how can I pass it derived class reference?
tryFunc(Base &b);
Derived d;
tryFunc(d);
Is this correct?
Thanks.

You basically figured out everything already, you got to declare someFunc in the class body of the derived class.
Also the way of passing to tryFunc(Base &b)is correct

Related

Why object of derived class d cannot call the protected member function of the class base?

Here the object of derived class d cannot call the protected member function of the class base.
#include <iostream>
using namespace std;
class base
{
protected:
int i,j;
void setij(int a,int b)
{
i=a;
j=b;
}
void showij()
{
cout<<i<<" "<<j<<endl;
}
};
class derived : protected base
{
int k;
public:
void show()
{
base b;
b.setij(10,20);
b.showij();
}
};
int main()
{
base b;
derived d;
d.setij(3,4);
d.showij();
d.show();
return 0;
}
I expect the output is 10 20, but the compiler is showing error.
You used protected inheritance. The problem is not that the derived cannot access protected methods of the base, but the problem is that you cannot access base methods from outside of derived.
If you don't know what protected inheritance means you can read eg here Difference between private, public, and protected inheritance
I doubt you wanted to use protected inheritance here (why would you?). Change it to public inheritance and your code should be fine:
class derived : public base ...
PS: The error message should have told you what is the actual problem (albeit in a cryptic way). Please next time include it in the question. If you cannot understand it, probably others will.
There is a lot wrong with this code. Even if you change the inheritance of class derived from protected to public, the following problems still exist:
In class derived, statements b.setij(10,20); and b.showij(); will still generate compiler errors. See Why can't a derived class call protected member function in this code? for a good explanation. The short explanation: a method can only call a protected method in a base class on the object for which it was originally invoked.
Function main will not be able to call d.setij(3,4); or d.showij(); because these are protected methods in class base.
This should run:
#include <iostream>
using namespace std;
class base
{
protected:
int i,j;
void setij(int a,int b)
{
i=a;
j=b;
}
void showij()
{
cout<<i<<" "<<j<<endl;
}
};
class derived : public base
{
int k;
public:
void show()
{
this->setij(10,20);
this->showij();
}
};
int main()
{
derived d;
d.show();
return 0;
}

If same method in both base derived class. which is not virtual,If we call from using base pointer always call base method?

Suppose i have two c++ classes.
#include <iostream>
class base
{
public:
void show()
{
std::cout<<"In base show";
}
};
class derived
{
public:
void show()
{
std::cout<<"In derived show";
}
};
int main()
{
base *bptr = new derived();
bptr->show();
}
why it is called always base show method.base pointer pointing to derived object?
main.cpp: In function ‘int main()’:
main.cpp:23:29: error: cannot convert ‘derived*’ to ‘base*’ in initialization
base *bptr = new derived();
This is the compiler error you should be getting. Your class derived is actually not derived. You need to inherit from base.
class derived : public base {
// your implementation
}
Then, your code will compile, but when you run it, it will still display the message from your base class. As said in the comments, you must declare methods that can be overwritten by inheriting classes as virtual, this instructs the compiler to generate the vtable so it can call the right method at runtime.
class base
{
public:
virtual void show()
{
std::cout<<"In base show";
}
};
It is also good style to mark the method in your derived class as override. Using override on a non-virtual method will result in a compiler error, the keyword you missed. Let the compiler tell you your mistakes, that's its job.
class derived : public base
{
public:
void show() override
{
std::cout<<"In derived show";
}
};

Please tell me why virtual function is not working in the following code

When i run this code , Output is as expected "This is derived 2" due to late binding because we used virtual function at the base class.
#include <iostream>
using namespace std;
class Base {
public :
virtual void show() {
cout<<"This is base class"<<endl;
}
};
class Derived : public Base {
public:
void show() {
cout<<"This is derived class"<<endl;
}
};
class D2 : public Derived {
public :
void show () {
cout<<"This is derived 2"<<endl;
}
};
int main() {
Base *obj = new D2();
obj->show();
return 0;
}
Similarly, if i change the code to
#include <iostream>
using namespace std;
class Base {
public :
void show() {
cout<<"This is base class"<<endl;
}
};
class Derived : public Base {
public:
virtual void show() {
cout<<"This is derived class"<<endl;
}
};
class D2 : public Derived {
public :
void show () {
cout<<"This is derived 2"<<endl;
}
};
int main() {
// your code goes here
Derived *obj = new D2();
obj->show();
return 0;
}
Same thing happens, virtual function is defined in the base class which in this case is "Derived" class and Pointer is also of the type "Derived" so again calling show() gives me the most derived version.
But i can't understand what is happening when virtual function is defined in the "Derived" class and i use the base pointer of the class "Base". The output of the following code
#include <iostream>
using namespace std;
class Base {
public :
void show() {
cout<<"This is base class"<<endl;
}
};
class Derived : public Base {
public:
virtual void show() {
cout<<"This is derived class"<<endl;
}
};
class D2 : public Derived {
public :
void show () {
cout<<"This is derived 2"<<endl;
}
};
int main() {
// your code goes here
Base *obj = new D2();
obj->show();
return 0;
}
Output : "This is base class". Please help me understand this output.
The reason your last snippet does not work is that the void show() function is virtual in your derived class only, but not in the base class. If any of the classes derived from Derived would override it, and then you call it through a pointer to Derived, you would see polymorphic behavior. The way you did it, however, the call through the base pointer is non-virtual, hence it is dispatched to the Base's implementation.
You can fix this by declaring show virtual in the base class:
class Base {
public :
virtual void show() {
// ^^^^^^^
cout<<"This is base class"<<endl;
}
};
Demo 1
or by accessing D2 through a pointer to Derived:
Derived *obj = new D2();
obj->show();
Demo 2
In your final example, D2 is a Base*, so whether or not that class gets derived doesn't matter.
If you had made obj a pointer to Derived or D2, the results would be different.
The static type of object obj is Base *
Base *obj = new D2();
So the compiler can call only member functions declared in this class. In class Base function show is not virtual. So it was selected by the compiler at compilation time. Base class knows nothing about virtual functions declared in derived classses. In your example the base class even does not have a table of addresses of virtual functions because neither function in the class declared as virtual.
That a virtual function of a derived class would be called the derived class shall to substitute the address of the base class virtual function for the address of its own function in the table of addresses of virtual functions in the base class.

Why derived class overloaded function hides base class function? [duplicate]

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()
{
}

Access protected data members of the base class from the derived class

I have a base class and the derived class. I need to access the protected member of the base class in the derived class. However, Eclipse does not allow me to access the data member as if it were a member of a derived class without caring that it was inherited. How do I do that?
class BaseClass {
protected:
static int a;
int b;
}
class DerivedClass: public BaseClass {
void SomeMethod {
a=10; // cannot resolve symbol
b=10; // cannot resolve symbol
BaseClass::a=10; //does not complain
BaseClass::b=10; //does not complain
}
}
I couldn't completely understand your question, but fixing the syntax errors, the following should work:
class BaseClass {
protected:
static int a;
int b;
}; // <-- Missing semicolon
int BaseClass::a = 0; // Define static member
class DerivedClass: public BaseClass {
void SomeMethod() { // <-- Missing ()
a=10;
b=10;
}
};// <-- Missing semicolon