In inheritance, you can hide or change the functionality of members in a base class. Usually, you either overwrite a member function/variable with the same name but different contents in the derived class, but also, you are allowed to change the access specifier in the derived class.
The latter is something that confuses me a little bit. Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
class X
{
protected:
void func1() { cout << "Hi\n"; }
};
class Y : public X
{
public:
X::func1;
};
int main()
{
return 0;
}
It seems that I cannot do the same for namespaces:
#include <iostream>
usingnamespace std;
namespace X
{
void func1() { cout << "Hi\n"; }
}
namespace Y
{
//X::func1; func1 already exists in the namespace X! Doesn't work.
void func1(); // But here we're not using the X namespace, so we're creating a new func1 in the Y namespace.
}
int main()
{
X::func1();
Y::func1();
return 0;
}
What I am trying to do is create the same function (func1) in X and have it in Y as well. You can do this in inheritance because a derived class can change the access specifier of base class members.
I guess that was a bad comparison to start with. I was just wondering if copying other class's members are only in a child to parent class relationship.
We could use any feature in any way we want, but the features apply to appropriate situations only to avoid us abusing them. And honestly I'd feel like I was abusing namespaces if I attempted what you asked about.