Hi Everyone,
I'm doing this assignment and i'm stuck.
I'm getting junk for my area calculations.
any help would be appreciated.

heres my code.

CPP file

// Ass2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cmath> 
#include "BasicShape.h"
#include "Circle.h"
#include "Rectangle.h"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{

// CIRCLE


long CirX; 
long CirY; 
double CirRad; 


cout << "CIRCLE:\n";
cout << "Enter the X coordinate of the Circle: ";
cin >> CirX;
cin.ignore ();
cout << "Enter the Y corrdinate of the Circle: ";
cin >> CirY;
cin.ignore ();
cout << "Enter the Radius of the Circle: ";
cin >> CirRad;
cin.ignore ();


Circle objCircle(CirX, CirY, CirRad);



cout << "X coordinate of the Circle: " << CirX << endl;
cout << "Y coordinate of the Circle: " << CirY << endl;
cout << "Radius of the Circle: " << objCircle.getRadius () << endl;
cout << "Area of the Circle: " << objCircle.getArea () << endl;


// RECTANGLE




long RectWidth;
long RectLength;



cout << "RECTANGLE:\n";
cout << "Enter the Width of the Rectangle: ";
cin >> RectWidth;
cin.ignore ();
cout << "Enter the Length of the Rectangle: ";
cin >> RectLength;
cin.ignore ();

Rectangle objRectangle(RectWidth,RectLength);

cout << "Width of the Rectangle: " << objRectangle.getWidth () << endl;
cout << "Length of the Rectangle: " << objRectangle.getLength () << endl;
cout << "Area of the Rectangle: " << objRectangle.getArea () << endl; 


cin.get();
return 0;
}

CLASS BASIC SHAPE


#ifndef BASICSHAPE_H
#define BASICSHAPE_H

class BasicShape
{
private:
double area;

public: 
double getArea() const
{ return area; }

virtual double calcArea() =0;
}; 
# endif


CLASS CIRCLE

#ifndef CIRCLE_H
#define CIRCLE_H
#include "BasicShape.h"


class Circle : public BasicShape
{
private:
long centerX,centerY;
double radius;
public:
Circle()
{ centerX = 0;
centerY = 0;
radius = 0; } 


Circle(long x, long y, double d):BasicShape()
{ centerX = x; centerY = y; radius = d; }

long getCenterX() const
{ return centerX; }

long getCenterY() const
{ return centerY; }

double getRadius() const
{ return radius; } 

virtual double calcArea() 
{ return (3.14159 * pow(radius, 2.0)); }
};
#endif


CLASS RECTANGLE

#ifndef RECTANGLE_H
#define RECTANGLE_H
#include "BasicShape.h"

class Rectangle : public BasicShape
{
private:
long width,length;

public:
Rectangle()
{ width = 0;
length = 0; }







Rectangle(long w, long l): BasicShape ()
{ width = w;
length = l; }

long getWidth() const
{ return width; }

long getLength() const
{ return length; }

virtual double calcArea() 
{ return width * length; }

};
#endif

Dani AI

Generated

Good catch by and good follow-up by . Beyond the immediate fix, here are compact, practical tips and a safer design pattern to avoid the same trap in future.

Prefer an interface that computes the area on demand instead of keeping a writable area field in the base class. That avoids stale or uninitialized values unless every mutator keeps the cache perfectly in sync. Make the base class abstract, give it a virtual destructor, and make the area accessor const. Use override in derived classes to catch signature mismatches at compile time. See notes on abstract classes and destructors here: abstract class and virtual destructor. Also read about virtual functions and overrides: virtual functions.

A few practical points:

  • Mark calculation methods const so callers can use const references.
  • For squaring use r * r rather than std::pow (faster and clearer) — see std::pow.
  • For pi, use a reliable source (C++20 std::numbers or compute std::acos(-1.0)), e.g. via std::acos.
  • If you must cache the area for performance, implement lazy evaluation with an invalidation flag so the value is recomputed when shape parameters change.

Example of a minimal, safer interface (illustrative):

class BasicShape {
public:
  virtual ~BasicShape() = default;
  virtual double area() const = 0;
};

class Circle : public BasicShape {
  double radius;
public:
  explicit Circle(double r) : radius(r) {}
  double area() const override { return std::acos(-1.0) * radius * radius; }
};

Finally, use consistent floating types for geometry (typically double), initialize members via initializer lists, and add small unit tests to verify numeric output and edge cases (zero, negative inputs, very large values).

Recommended Answers

All 2 Replies

As far as I can see, you are never calling calcArea().

Thats where the problem was... called calcArea() instead of getArea() and its working perfectly.
Thanks buddy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.