0

class BASIC_SHAPE (abstract)

class BASIC_SHAPE
{
public:

    double GET_AREA(double _AREA) { AREA = _AREA; return AREA; }
    virtual double CALC_AREA() = 0;

private:
    
    double AREA =0;
};

Class CIRCLE

class CIRCLE:public BASIC_SHAPE
{
public:

    CIRCLE() { RADIUS = 0; }
    CIRCLE(double _RADIUS) { RADIUS = _RADIUS; }
    virtual double CALC_AREA() {
        double TEMP2 = 3.14 * pow(RADIUS, 2);
        return GET_AREA(TEMP2);
    }
    
private:
    
    double RADIUS;
};

Class TRIANGLE

class TRIANGLE: BASIC_SHAPE
{
public:

    TRIANGLE() { BASE = 0; HEIGHT = 0; }
    TRIANGLE(double _BASE , double _HEIGHT) : BASE{_BASE}, HEIGHT{_HEIGHT} {}
    virtual double CALC_AREA() {
        double TEMP = 1 / 2 * (BASE * HEIGHT);
        return GET_AREA(TEMP);
    }

private:

    double BASE, HEIGHT;

MAIN

CIRCLE SHAPE2;
TRIANGLE SHAPE3;

void main()
{
    double RAD;

    std::cout << "Enter a Circle Radius : ";
    std::cin >> RAD;
    CIRCLE SHAPE2(RAD);
    CIRCLE* LEAD1 = new CIRCLE(RAD);
    std::cout << "The Area is : " << LEAD1->CALC_AREA();

    double BASE , HEIGHT;

    std::cout << "\n\nEnter a Triangle Base : ";
    std::cin >> BASE;
    std::cout << "\nEnter a Triangle Height : ";
    std::cin >> HEIGHT;
    TRIANGLE SHAPE3(BASE, HEIGHT);
    std::cout << SHAPE3.CALC_AREA();
}

it keeps returning zero when I input the BASE & HEIGHT

I have tried using arrow operator and get it with pointers put nothing worked , I,ve tried use pointers and other methods to give me the answer or the SUM of area but nothing happens . constructors or abstract Class are suspected but IDK how ??

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Kobrakixx
  • 13
  • 5
  • 5
    `1/2` is `0` and then `0 * (BASE * HEIGHT)` is `0`. Change it into `1.0 / 2`. – rturrado Nov 21 '22 at 21:29
  • 2
    BTW, prefer not using leading underscores for identifiers (see [here](https://stackoverflow.com/a/20496955/260313)). – rturrado Nov 21 '22 at 21:34
  • Please don't use ALL CAPS for your identifiers, it's harder to read code that looks like it's shouting at people. Your `get_area` function is also very bizarre. If it's a getter, why does it take an argument? Why `base_shape` even need an `area` member variable instead of just having a `double get_area()` virtual member function? – Nathan Pierson Nov 21 '22 at 21:59
  • Why pass a parameter to `getArea()` ? It's making `getArea()` a setter and a getter which is wrong. Note that I've used my preferred naming convention - yours are horrid :) – John3136 Nov 21 '22 at 22:11
  • An identifier beginning with an underscore followed by a capital letter is a **reserved** identifier, and using it is **undefined behavior**. – Eljay Nov 21 '22 at 22:33

1 Answers1

-1

As pointed out in the comments by @rturrado 1/2 is 0. 0 times anything is 0. either use 0.5 or 1/2.0*....

Also, why does your GET_AREA method set the area for the base class and return the set area? You need to have different getters and setters. Getter methods should not set and similarly setter methods should not get.

Also, it would be best if you get in the habit of using managed pointers.

asdf
  • 460
  • 1
  • 10
  • 31