Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do we need to have a default constructor?

My question is simple. When do we need to have a default constructor? Please refer to the code below:

class Shape {     int k;  public:     Shape(int n) : k(n) {}     ~Shape() {} };  class Rect : public Shape {     int l;  public:     Rect(int n): l(n)     {}      //error C2512: 'Shape' : no appropriate default constructor available      ~Rect() {} }; 
  1. Why is the compiler not generating the zero argument default constructor implicitly in the class Rect?

  2. As per my knowledge, if a class (Rect) is derived from another class (Shape) that has default constructor (either implicitly generated or explicitly provided), the default constructor should be generated by the compiler.

like image 696
XMarshall Avatar asked Mar 31 '11 11:03

XMarshall


People also ask

Do you always need a default constructor?

What is the default constructor? Java doesn't require a constructor when we create a class. However, it's important to know what happens under the hood when no constructors are explicitly defined. The compiler automatically provides a public no-argument constructor for any class without constructors.

Why do you need a default constructor?

What is the significance of the default constructor? They are used to create objects, which do not have any specific initial value. Is a default constructor automatically provided? If no constructors are explicitly declared in the class, a default constructor is provided automatically by the compiler.

When you must obtain a default constructor?

A default (no-argument) constructor is automatically created only when you do not define any constructor yourself. If you need two constructors, one with arguments and one without, you need to manually define both.

Do you have to have a default constructor C++?

For example, all members of class type, and their class-type members, must have a default constructor and destructors that are accessible. All data members of reference type and all const members must have a default member initializer.


1 Answers

A default constructor is not synthesised if you created your own constructor with arguments. Since you gave Shape a constructor of your own, you'd have to explicitly write out a default Shape constructor now:

class Shape {       int k;    public:       Shape() : k(0) {}       Shape(int n) : k(n) {}       ~Shape() {} }; 

(You can leave out the empty ~Rect() {} definitions, as these will be synthesised.)

However, it looks to me like you don't want a default constructor for Shape here. Have Rect construct the Shape base properly:

class Shape {       int area; // I've had to guess at what this member means. What is "k"?!    public:       Shape(const int area)          : area(area)       {} };  class Rect : public Shape {      int l;      int w;    public:      Rect(const int l, const int w)         : Shape(l*w)         , l(l)         , w(w)      {} }; 

Also note that this example is oft cited as an abuse of OO. Consider whether you really need inheritance here.

like image 130
Lightness Races in Orbit Avatar answered Sep 20 '22 22:09

Lightness Races in Orbit