Our professor was very unclear about this so I hope I'm not too vague about this. Basically we need to write an abstract base class from which other (non abstract) classes inherit, the header file of the abstract base class looks like this:
//AbstractBase.h
class AbstractBase
{
public:
AbstractBase();
virtual int operator+ (int)=0;
}
I have no idea what to put in the implementation .cpp file since it's an abstract class (so you can't create any objects from that class, I'm probably very very wrong here) so I just didn't make one.
Then there's the derived class with a header file and implementation as follows:
//Headerfile for DerivedClass
#include "AbstractBase.h"
class DerivedClass: public AbstractBase
{
public:
DerivedClass(double &);
DerivedClass * operator+ (DerivedClass*);
double getVar();
void setVar(double)
private:
double Var;
}
//Implementation of DerivedClass
#include "DerivedClass.h"
class DerivedClass::DerivedClass(double &Input) :Var(Input)
{}
double DerivedClass::getVar()
{
return Var;
}
void DerivedClass::setVar(double Input)
{
Var=Input;
}
DerivedClass * DerivedClass::operator+ (DerivedClass * object)
{
double Sum;
Sum=Var+object->getVar()
DerivedClass result(Sum);
return result;
}
So my question comes down to this:
- the overloading part (last lines) should do the following: when I add two object of the class DerivedClass, it should just add the private variables (double Var) together. I've done some searching but couldn't immediately find something that I could use (perhaps because of my limited knowledge on this matter).
A small side question:
- what do I do with the AbstractBase class? My task description states that it should only contain 4 virtual functions that are all overloading an operator. But it's an abstract base class and I don't know yet HOW I would overload them (i.e. it's like saying "add two things, I have no idea how but I know you can add em up...", that's all the abstract class says)
Edit: some clarification on how I think it should happen.
The abstract base class contains a virtual function operator+. In the derived class, I define a new operator+ which knows how to handle the addition of objects of the derived class. If I use a pointer of the type DerivedClass* it will ignore the virtual function and go for the operator+ function defined in the DerivedClass (the basic principle of virtual functions?). The problem seems that the derived class is still seen as an abstract class, so I can't use the code provided by Component 10, because that instantiates an object of DerivedClass (which isn't possible because apparently it's abstract)
Edit 2:
Our professor decided to leave out the operator overloading because it involved too complex code :D Thanks for the help anyway, I did learn how to correctly implement it (sort of).