-1

C++

I have been working hard on learning Inheritance and Polymorphism for weeks. I can have Mammals with Dogs and Cats, O can derive Puppy's from Dogs and what not. I have a NaughtleNum base class with a + operator And the derived class is Naughty1. (Sorry, but I hate Foo Bar)

Here is the Code:

#include <iostream> 
#include <math.h>

using namespace std;

typedef unsigned char UCHAR;
typedef unsigned short int SHORT;

class NaughtleNum;
class Naughty1;

class NaughtleNum {
public:
    NaughtleNum() {
        cout << "Hello NaughtleNum 1\n";
    }; //constructors
    virtual~NaughtleNum() {
        cout << "Bye Bye NaughtleNum\n";
    }; //destructor
    virtual NaughtleNum * operator + (NaughtleNum * ) = 0;
};

class Naughty1: public NaughtleNum {
public: 
    Naughty1() {
        cout << "Hello Naughty1\n";
        value = 3;
    }; //constructors
    ~Naughty1() {
        cout << "Bye Bye Naughty1\n";
    }; //destructor
    Naughty1 * operator + (Naughty1 * ) {}
private: 
    UCHAR value;
};

int main() {

    cout << "I am Here\n";

    NaughtleNum * N1;
    N1 = new Naughty1;

    return 0;
}
      

I want to have a + operator for two Naughty Numbers!

Messages are:

daddio@LittleBeast:~/Programs/MyLib/Naughty> g++ -g -o NaughtleNum NaughtleNum.cpp
NaughtleNum.cpp: In function ‘int main()’:
NaughtleNum.cpp:35:12: error: cannot allocate an object of abstract type ‘Naughty1’
     N1=new Naughty1;
            ^
NaughtleNum.cpp:19:7: note:   because the following virtual functions are pure within ‘Naughty1’:
 class Naughty1: public NaughtleNum
       ^
NaughtleNum.cpp:16:30: note:    virtual NaughtleNum* NaughtleNum::operator+(NaughtleNum*)
         virtual NaughtleNum* operator + (NaughtleNum*) =0;
                              ^
daddio@LittleBeast:~/Programs/MyLib/Naughty> 

What am I missing here?

Community
  • 1
  • 1
seagods
  • 11

1 Answers1

0

The reason you are getting the error for an abstract class is because you haven't overridden the base class's binary addition operator! If you have a virtual operator in a base class, then the derived class must have the same signature for that operator.

Naughty1 * operator + (Naughty1 * ) {}

Is completely and 100% different from:

virtual NaughtleNum * operator + (NaughtleNum * ) = 0;

However, before you go out and change the signature so that they match, please consider if you really need operator polymorphism here. Each class is going to have its own unique binary addition operator, so there is no need for a virtual operator in the base class. It doesn't make sense for a Naughty1 to have an addition operator that will return and accept a Naughtlenum but rather one that will accept and return a Naughty1.

On a side note, the addition operator should just return an object, not a pointer to it.

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
  • Thanks for all the replies. I had got somewhat frazzled when I posted the question Quite apart from not overloading assignment, it looks like I need to rethink the base class altogether. – seagods Feb 12 '18 at 07:31