Having Trouble with Inheritance in C++

Advertisements

I’m having a little trouble with inheritance.

Main.cpp

#include <iostream>
#include "Shapeclass.h"
using namespace std;

int main() {

    int shapecount, shapetype[200],i,height[200], width[200];
    string name;

    cout << "AREA CALULATOR";
    cout << "Enter Your Name ";
    cin >> name;
    cout << "Enter the amount of Shapes you want to calculate Area of: ";
    cin >> shapecount;

    Shape *p1[200];

    cout << "Enter 1 for Circle Enter 2 for Rectangle and 3 for Triangle";

    for ( i = 0; i < shapecount; i++)
    {
        cin >> shapetype[i];
    }
    
    for ( i = 0; i < shapecount; i++)
    {
        if (shapetype[i]==1)
        {
            cout << "Enter Radius of circle";
            cin >> width[i];
            p1[i] = new sphere(width[i]);
        }
        else if (shapetype[i] == 2) {
            cout << "Enter width of rectangle";
            cin >> width[i];
            cout << "Enter height of rectangle";
            cin >> height[i];
        }
        else if (shapetype[i] == 3) {
            cout << "Enter base of triangle";
            cin >> width[i];
            cout << "Enter height of triangle";
            cin >> height[i];
        }
    }
}

Shapeclass.h

#include <iostream>

using namespace std;


class Shape
{
protected:

    double area ;
    int height, width;
    string nama;
    void virtual calculate() = 0;
public:
    void display() {
        calculate();
    }
private:
};

class sphere :public Shape {
    void calculate(double height, double width) {
    }
};

class rectangle :public Shape {
public:
    rectangle(int width1, int height1)
    {
        width = width1;
        height = height1;
    }
    void calculate(double height, double width) {
        area = height * width;
    }
};

class triangle :public Shape {
public:
    triangle(int width1, int height1)
    {
        width = width1;
        height = height1;
    }
    void calculate(double height, double width) {
        area = height * width*0.5;
    }
};

class sphere :public Shape {
public:
    sphere(int width1)
    {
        width = width1;
    }
    void calculate(double width) {
        area = width*width*3.14 ;
    }
};

I have no idea why it says object of abstract class type "sphere" is not allowed. Specifically line 42.

I’m trying to initialize pointer array objects but it doesn’t want to work.

I’m trying to send the width of the sphere to the class but I can’t initialize the value of width with the pointer array. To be more specific.

>Solution :

The Shape::calculate() method is declared as pure virtual:

void virtual calculate() = 0;

So, it must be overloaded in derived classes to have them considered by the compiler as concrete ones you’d able to call.

In your case, the overloaded method would be responsible to determine the area of each shape according to its kind and update the instance property.

for example:

void rectangle::calculate() {
  area = height * width;
}

and

void triangle::calculate() {
  area = height * width * 0.5;
}

would compute the right area for given shapes.

Shape* s1 = new triangle(w, h);
s1->calculate(); // effective call to triangle::calculate()

Shape* s2 = new rectangle(w, h);
s2->calculate(); // effective call to rectangle::calculate()

Leave a ReplyCancel reply