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

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.