EDIT: This question came up and I think I aced it! Go StackOverflow!! :D
I have exams coming up, and one of the questions on last year's exams was to spot the problem with implementation of the following constructor and to write a corrected one.
Rectangle::Rectangle(string col, int len, int br) { setColour(col); length =len; breadth=br; }
The class definitions are as follows:
class Polygon { public: Polygon(string col="red"); void printDetails(); // prints colour only virtual double getArea()=0; void setColour(string col); private: string colour; }; class Rectangle : public Polygon { public: Rectangle(string, int, int); void printDetails(); // prints colour and area // for part 3, delete the line below double getArea(); private: int length; int breadth; };
I've written the code into the compiler and it runs fine. I'm guessing the question is relating to inheritance, since string colour;
is private, but setColour
is public so I cant figure it out. Unless its Rectangle::Rectangle(string col, int len, int br):length(len), breadth(br)
and then set the colour inside the construcor or something.
Its only worth 3 marks so its not that big a deal if nobody wants to answer, but I figure if I'm going to have a career as a programmer, its in my interest to know as much as possible. ;)
Thanks for any help.
PS, see getArea()
in Rectangle
. When I remove that it tells me it "cannot instantiate the abstract class". What does that mean?
EDIT: Here's the main:
void main (void) { Rectangle rect1 ("blue",5,6); Rectangle *prect2 = new Rectangle("red",5,6); rect1.setColour("red"); rect1.printDetails(); prect2->printDetails(); }
A Constructor in C is used in the memory management of C++programming. It allows built-in data types like int, float and user-defined data types such as class. Constructor in Object-oriented programming initializes the variable of a user-defined data type. Constructor helps in the creation of an object.
If an exception is thrown in a constructor, the object was never fully constructed. This means that its destructor will never be called. Furthermore, there is no way to access an object in an error state. The exception will immediately unwind the local variable.
Don't call delete this or the destructor in the constructor. Don't use init()/cleanup() members. If you have to call init() every time you create an instance, everything in init() should be in the constructor.
I don't see anything wrong, though you could make it more efficient by using an initialization list (otherwise your private members of both classes get initialized twice):
Rectangle::Rectangle(string col, int len, int br) : Polygon(col), length(len), breadth(br) { }
Notice that the initialization list can call the constructor of Polygon as well.
See Why should I prefer to use member initialization list? for a complete description of the advantages of using initialization lists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With