Home c++ Method inheritance in C++

Method inheritance in C++

Author

Date

Category

I want to implement the Vector class as an entity of linear space, and then inherit from it the usual DoubleVector and Matrix. To do this, I would like to define a method for adding 2 objects to the parent , but so that the inheritor of would return an object of the inheritor class. The problem is that when inheriting, the type of the returned object remains the same as that of the object of the parent’s class, but I would like the inheritor’s method to immediately return an object of the inherited class. Example:

class A {
public:
  int n {0};
  A (int n) {this- & gt; n = n; }
  void operator + = (const A & amp; other) {n + = other.n; }
  A operator + (const A & amp; other) {
    auto res = A (this- & gt; n);
    res + = other;
    return res;
  }
};
class B: public A {
public:
  B (int n): A (n) {}
  void operator - = (const B & amp; other) {n - = other.n; }
  B operator- (const B & amp; other) {
    auto res = B (this- & gt; n);
    res - = other;
    return res;
  }
};
int main () {
  auto a = (B (5) + B (6)) - B (4);
  std :: cout & lt; & lt; a.n & lt; & lt; std :: endl;
}

The sum of two objects of class B is class A , for which the subtraction operation is not defined, so the code will fail. Question: is it possible to solve this problem without rewriting the methods?


Answer 1

In your version, you just need to take out the – operator, making it free, and not hard-wired to the class, and a constructor for casting types:

class A {
public:
  int n {0};
  A (int n) {this- & gt; n = n; }
  void operator + = (const A & amp; other) {n + = other.n; }
  A operator + (const A & amp; other) {
    auto res = A (this- & gt; n);
    res + = other;
    return res;
  }
};
class B: public A {
public:
  B (int n): A (n) {}
  B (const A & amp; a): A (a) {}
  void operator - = (const B & amp; other) {n - = other.n; }
};
B operator- (const B & amp; that, const B & amp; other) {
  auto res = B (that.n);
  res - = other;
  return res;
}
int main () {
  auto a = (B (5) + B (6)) - B (4);
  std :: cout & lt; & lt; a.n & lt; & lt; std :: endl;
}

Answer 2

When an operator is called on a parent class, only the data of that parent is changed. The heir’s personal data (which are defined in class B ) do not change. The parent class does not know anything about other descendants, so it returns itself.

For the inheritor to return itself, you still need to override the method. Like this:

class B: public A {
public:
  B (int n): A (n) {}
  void operator - = (const B & amp; other) {n - = other.n; }
  B operator- (const B & amp; other) {
    auto res = B (this- & gt; n);
    res - = other;
    return res;
  }
  B operator + (const A & amp; other) {
   // the parent method works here
   A :: operator + = (other);
   // return ourselves (inheritor)
   return * this; }
};

Answer 3

The problem is that although the class B has the + operator, it returns an object of type A , and this type has neither operator- (B) nor conversion operator to B , in order to execute (B (5) + B (6)) - B (4) . For this to be possible, the operator defined in the base class must be executed last:

A a = B (5) + (B (6) - B (4))

Alternatively, the base class operator must return a different type, which can be implicitly converted to B (there are many possible options). Here’s an example based on what you wrote:

class A {
public:
  int n {0};
  A (int n): n (n) {}
  // return the contained object (or link)
  int operator + = (const A & amp; other)
  {
    n + = other.n;
    return n;
  }
  int operator + (const A & amp; other) {
    A a = * this + = other;
    return a.n;
  }
};
class B: public A {
public: 
B (int n): A (n) {}
   B Operator - = (Const B & Amp; Other)
   {
     N - = Other.n;
     RETURN * THIS;
   }
   // better operator determine if not a member of the class
   // it does nothing with this object
   B Operator- (Const B & Amp; Other) {
    B B = * this - = Other;
    Return B;
   }
   Operator int () {Return N; }
};
INT MAIN ()
{
   B B = (b (5) + b (6)) - b (4);
   A & AMP; a = b;
   STD :: COUT & LT; & LT; a.n & lt; & lt; STD :: ENDL;
   RETURN EXEC ();
}

p.s. The basic class does not need a user of the heir, so it’s more appropriately closed (protected) inheritance

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions