Define the undefined method of base class in derived classes - c++

I have a base class A and two classes B, C derived from A. Declaration of method func is given in class A. How can I define method func separately for B and C ?
class A {
public:
void func();
};
class B : public A {
//some members
};
class C : public A {
//some members
};
//define B's func here without changing the definition of the three classes
//define C's func here without changing the definition of the three classes

You have to make the method you want to overwrite "virtual" or "pure virtual" and if a class has virtual methods, also the destructor must be virtual:
class A {
public:
virtual ~A{};
virtual void func() = 0;
};
class B : public A {
void func() {};
};
class C : public A {
void func() {};
};

No, you cannot implement a member function for a class without it being declared in the class.
class A {
public:
void func();
};
class B : public A {
//some members
};
class C : public A {
//some members
};
void B::func() {}
void C::func() {}
/tmp/164435074/main.cpp:17:9: error: out-of-line definition of 'func' does not match any declaration in 'B'
void B::func() {}
^~~~
/tmp/164435074/main.cpp:18:9: error: out-of-line definition of 'func' does not match any declaration in 'C'
void C::func() {}
^~~~

Related

Override virtual protected method that is a friend of another class

Basically, I want to somehow simulate friendship inheritance with the restriction that it can only happen from inside a certain method.
So essentially, this is what I want
class A; // Forward declaration
class Base{
friend class A; // friend declaration so that A is able to see protected methods
protected:
virtual void method() {// some definition, might also be pure virtual}
}
class Derived : public Base{
A aObj;
void method(){//override the one in base and also gain access to aObj private members.}
public:
//public interface
}
class A {
int var;
friend void Base::method();
public:
// public interface
}
is there anyway to achieve this?
How about this
class Base {
friend class A;
protected:
virtual void method() = 0;
std::tuple<int> GetAProperties(const A& a) {
// You can change the tuple params
// as per your requirement.
return std::make_tuple(a.var);
}
}
class Derived : public Base {
A aObj;
void method() override {
auto objProperties = GetAProperties(aObj);
}
}
You could get pointer to private's A member in Base, and then pass these member pointers to the Derived:
class A; // Forward declaration
class Base{
friend class A; // friend declaration so that A is able to see protected methods
private:
void method(A&);
virtual void do_method(A& a,int A::* var) {// some definition, might also be pure virtual
(a.*var)++;
}
};
class A{
int var;
friend void Base::method(A&);
};
class Derived : public Base{
A aObj;
virtual void do_method(A& a,int A::* var) {// some definition, might also be pure virtual
a.*var+=2;
}
public:
//public interface
};
void Base::method(A& a){
do_method(a,&A::var);
}
NB: do not use it on the critical pass!!

Errors on postfix operator ++ in a class derived from a template class [duplicate]

In a derived class If I redefine/overload a function name from a Base class,
then those overloaded functions are not accessable/visible to derived class.
Why is this??
If we don't overload the oveloaded function from the base class in derived class
then all the overloaded versions of that function are available to derived class
objects, why is this??
what is the reason behind this. If you explain this in compiler and linker level
that will be more helpful to me. is it not possible to support this kind of scinario??
Edited
For examble:
class B
{
public:
int f() {}
int f(string s) {}
};
class D : public B
{
public:
int f(int) {}
};
int main()
{
D d;
d.f(1);
//d.f(string); //hidden for D
}
Now object 'd' can't access f() and f(string).
TTBOMK this doesn't have a real technical reason, it's just that Stroustrup, when creating the language, considered this to be the better default. (In this it's similar to the rule that rvalues do not implicitly bind to non-const references.)
You can easily work around it be explicitly bringing base class versions into the derived class' scope:
class base {
public:
void f(int);
void g(int);
};
class derived : public base {
public:
using base::f;
void f(float);
void g(float); // hides base::g
};
or by calling the explicitly:
derived d;
d.base::g(42); // explicitly call base class version
The functions are available, you just need to call them explicitly:
struct A {
void f(){}
};
struct B : public A {
void f() {}
};
int main() {
B b;
b.f(); // call derived function
b.A::f(); // call base function
}

Member functions of a derived class

When a class is derived from a base class, will the member functions of the base class become member function of the derived class. That is, for example if I write:
class A
{
public : void f();
};
class B : public A
{
public : void f1();
};
Now if the question is to name the member functions in class B, then will f() also become a member function of class B ?
then will f() also become a member function of class B ?
No. f() is still a member function of class A, class B just inherits it. In your case it's a public member function and public inherit, means you can call f() on a B object.
B b;
b.f();
On the other hand, class B can define own member function f():
class A
{
public : void f();
};
class B : public A
{
public : void f(); // will hide A::f()
};
Yes, class B has two functions: f1() and f().
If f() was protected in A, too, but f() could only be used from within the member functions of A and B (and from friends).
And if f() was private, B would have only one function f1().
class A {
public : void f();
};
class B : public A
{
public : void f1();
};
Now code is valid.
It is visible. Even when you change f1 name to f it is still visible in B class. If you wanna use it use scope operator ::.
Like this:
A::f()
More about basic inheritence
http://www.learncpp.com/cpp-tutorial/112-basic-inheritance-in-c/

Calling a subclass from mainclass

I have a base class Board_S and 10 other classes that inherit the base Board_S class.
I have an object for Board_S which calls the Board_S class and its function which should call one more function from a specific sub class.
I am getting the error : Class does not name a type;
How to do I call the subclass function from this main Class.
Please Help
The only way to do this correctly is to declare a virtual function in your base class and implement it in your derived class.
class A
{
public:
A() {}
virtual ~A() {}
virtual void func() {}
void CallFunc() { func(); }
};
class B : public A
{
public:
B() {}
virtual ~B() {}
virtual void func() {} // overrides it
};
int main()
{
A a;
A* pB = new B;
a.CallFunc(); // will call the base class version of func()
pB->CallFunc(); // will call the derived class version of func()
delete pB;
}
If you don't need the function overridden in a derived class (and the function isn't declared as a pure virtual function), you do not override it.

overloaded functions are hidden in derived class

In a derived class If I redefine/overload a function name from a Base class,
then those overloaded functions are not accessable/visible to derived class.
Why is this??
If we don't overload the oveloaded function from the base class in derived class
then all the overloaded versions of that function are available to derived class
objects, why is this??
what is the reason behind this. If you explain this in compiler and linker level
that will be more helpful to me. is it not possible to support this kind of scinario??
Edited
For examble:
class B
{
public:
int f() {}
int f(string s) {}
};
class D : public B
{
public:
int f(int) {}
};
int main()
{
D d;
d.f(1);
//d.f(string); //hidden for D
}
Now object 'd' can't access f() and f(string).
TTBOMK this doesn't have a real technical reason, it's just that Stroustrup, when creating the language, considered this to be the better default. (In this it's similar to the rule that rvalues do not implicitly bind to non-const references.)
You can easily work around it be explicitly bringing base class versions into the derived class' scope:
class base {
public:
void f(int);
void g(int);
};
class derived : public base {
public:
using base::f;
void f(float);
void g(float); // hides base::g
};
or by calling the explicitly:
derived d;
d.base::g(42); // explicitly call base class version
The functions are available, you just need to call them explicitly:
struct A {
void f(){}
};
struct B : public A {
void f() {}
};
int main() {
B b;
b.f(); // call derived function
b.A::f(); // call base function
}