Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with C++ constructor

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(); } 
like image 518
eoinzy Avatar asked May 11 '11 16:05

eoinzy


People also ask

Can we use constructor in C?

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.

What happens if constructor fails?

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.

What a constructor should not do?

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.


1 Answers

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.

like image 108
J T Avatar answered Sep 17 '22 14:09

J T