So I have these header files:
Shape.h:
class Shape
{
public:
Shape();
virtual ~Shape();
virtual double getArea();
virtual void printDraw();
bool isLegal(Shape shape);
};
Trig.h:
class Trig : public Shape
{
public:
Trig(Point pointA, Point pointB, Point pointC);
virtual ~Trig();
virtual double getArea();//override
virtual void printDraw();//override
bool isLegal(Trig trig);//override
};
but when I try to implement Trig.cpp, I get errors.
I tried :
Trig::Trig(Point pointA, Point pointB, Point pointC) {..}
I’ve looked through the internet, but I don’t seem to do the inheritance properly.
[My code MUST have header file that includes the declarations only!… Requirements of the assignment!]
I’m new in using both inheritance and virtual functions in C++ [I’ve used inheritance in Java]
And concerning the virtual functions.. is there any difference in their implementation than in normal functions ? [I understand the difference in the behavior].
In a comment you explain that
That means that you have forgotten to provide an implementation of that constructor.
Now, with the technical problem fixed, look at the design.
The first two methods suffer from two maladies: (1) although they should never mutate an object they are not declared
const, so they cannot be called on aconstobject, and (2) thegetprefix reduces readability, is more to type and has no general advantage. I’ve found agetprefix useful in some rare circumstances as a disambiguation device, but all usages I’ve seen by beginners have been inappropriate copying of a Java convention, which makes sense in Java but not in C++. So, instead of …do
Then, the method …
is wrong in so many ways… Ouch! But let’s start with the purely technical.
From a purely technical point of view it is needlessly inefficient to pass the argument by value, which incurs a copy operation for each call. Instead pass the object by reference. And make that a reference to
const, in order to supportconstobjects and rvalue objects as arguments:Next, naming:
isLegalis an ungood name because most any C++ object will be legal. It would have to be, say, pornographic and residing in a non-Western country, in order to become illegal. And I can’t for the life of me think of any way to make an object pornographic, or make it reside in some specific geographic region.So,
Next, low level design, there is no good reason to have that non-
virtualmethod as an ordinary member function, because that requires that you call it on an object. But all the information it needs is in the ordinary argument. We can see this confusion in the derived class, where …is not at all an override: it’s technically an overload of the function name, which means, just a different function with the same name. There’s no virtuality here, no override. And it is not needed.
So, make that a
staticmember function, which does not have to be called on an object:Finally, higher level design, the whole machinery of C++ constructors and destructors is there in order to avoid such methods and checking.
The idea is that you …
Establish a valid object in every constructor.
Keep the object valid in every method.
Then the object simply can’t become invalid. This approach is called single phase construction, and the properties of the object that makes it “valid’ are known as the class’ class invariant. Which should be established by every constructor, and maintained by every method.
This means, final version, the
isValidfunction IS REMOVED: it has no job to do, because that job is (properly) done by the constructor(s) and the methods.Okay, there are some technical challenges with single phase construction, in particular how to do derived class specific initialization in a base class constructor. This is covered by the C++ FAQ. It’s often a good idea to read the FAQ.