-2

Let's say I have the following code. Would it be possible to derive class add from class math and declare the function sum, then derive a class from add called addNumbers which implements the function sum.

class math
{
public:
virtual int sum() = 0;
}

class add : public math
{
public:
int sum();
}

class addNumbers : public add
{
private:
int a, b;

public:
add::sum()
{ a + b}

}
user3076906
  • 33
  • 10

2 Answers2

1

Would it be possible to derive class add from class math and declare the function sum, then derive a class from add called addNumbers which implements the function sum.

No.

sum has to be implemented in add if it is declared the way you have it. Otherwise, you should see a linker error.

If add does not have sufficient information to implement sum, don't declare sum in add. Declare and define it in addNumbers.

Update, in response to OP's comment

Use

class add : public math
{
   // Add whatever is needed for this class
}

class addNumbers : public add
{
   private:
      int a, b;

   public:
      int sum()
      { 
         return (a + b)
      }

};
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

You can implement sum function in the addNumbers . the sum function declare in math class virtually . however you wasn't define sum function in add virtually but it is virtual . Thus you can define/implement sum in the addNumbers and it will be overwrite on previous declaration in add Class.

erfan zangeneh
  • 334
  • 1
  • 2
  • 10