C ++ Inheritance: The instance of the child class uses the child and parent constructors

advertisements

I am practicing inheritance in c++ and I am having some constructor issues.

When I create an instance of the child class (Circle), it creates an instance of both child and parent. Or does it?

I would like to ONLY create an instance of Circle instead of both shape and circle.

If I remove the : Shape(0,0) from Circle's constructor, then I get the error: "no default constructor exists for class Shape"

Do you always have to reference parent's constructor? And that means that the parent's constructor is always called? Does that mean that there is an instance of both child and parent?

Child Class:

#include "Circle.h"
#include <iostream>

using namespace std;

Circle::Circle(float radius) : Shape(0.0, 0.0) {
   this->radius = radius;
}

float Circle::computeArea() {

    cout << "Circle area is " << (3.14 * radius * radius) << "\n";
    return float(3.14 * radius * radius);
}

Parent package:

#include "Shape.h"
#include <iostream>

using namespace std;

Shape::Shape(float base, float height) {
   this->base = base;
   this->height = height;

   cout << "Shape: base - " << base << " height - " << height << "\n";
}

float Shape::computeArea() {

   cout << "Area is " << base*height << "\n";
   return base * height;
}

Main package:

#include "Circle.h"
#include "Square.h"
#include <iostream>

using namespace std;

int main() {

    Square mySquare(10.0, 10.0);
    Circle myCircle(5.0);

    float squareArea = mySquare.computeArea();
    float circleArea = myCircle.computeArea();

    cout << "done";

    return 0;
}

Output:

Shape: base - 10 height - 10
Shape: base - 0 height - 0
area is 100
circle area is 78.5
done


It doesn't seem to me you have a problem with your code, you're just confused about how inheritance works in C++.

Every time you derive a class from a base class, before entering in the subclass constructor, the compiler automatically calls the base class constructor for you if it has no arguments. For example, if your Shape class had a constructor like this

Shape::Shape(){ }

then the subclass wouldn't have to call the base class constructor in the initialization list (the compiler does that for you). Now your constructor can look like this:

Circle::Circle(float radius) {
   this->radius = radius;
}

If you don't have a constructor without parameters you must explicitly call it in the initialization list.

Every object of type Circle is a kind of Shape with more information inside. This doesn't mean the compiler creates two objects just because it initializes also the parent's components, you still have one object.

Now, if you want your Circle to be different from your Shape object then you don't need the inheritance anymore. (Although I assume this is not what you want)

You can not/ don't want to avoid calling the base class constructor, that's the whole point of inheritance: it allows you to extend the characteristics of one object, which is based on another object's implementation.