Not returning correct area.

The problem is the get are functions are not returning the correct are. Not sure what the problem is. Also if anyone knows what the x,y coordinate might have to do with the area let me know.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream> 
#include "Circle.h" 
#include "Rectangle.h" 
using namespace std;

int main() {
	double x, y, length, width;
	double rad;

	// Demonstrate a Circle. 
	cout << "Please enter the x coordinate of the circle's center: ";
	cin >> x; 
	cout << "Please enter the y coordinate of the circle's center: "; 
	cin >> y;
	cout << "Please enter the radius of the circle: ";
	cin >> rad; 

	Circle c(x,y,rad); 

	cout << "The area of the circle is " << c.getArea() << ".";
	// Demonstrate a Rectangle. 
	cout << "\n\nPlease enter the length of the rectangle: "; 
	cin >> length; 
	cout << "Please enter the width of the rectangle: "; 
	cin >> width; 
	
	Rectangle r(width, length); 

	cout << "The area of the rectangle is " << r.getArea() << ".\n";
	
	return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef RECTANGLE_H 
#define RECTANGLE_H
#include "BasicShape.h"
class Rectangle : public BasicShape{

private:     
	// length, width   
	double length, width;
public:     
	// functions: constructor, width, length, area 
	Rectangle(double width, double length) {};
	double getLength() { return length; }
	double getWidth() { return width; }
	double getArea() { return length * width; }
};

1
2
3
4
5
6
7
8
9
10
11
12
#ifndef BASICSHAPE_H 
#define BASICSHAPE_H
class BasicShape {
protected:     
	// area     
	double area;
public:     
	// functions: area
	BasicShape() {};
	virtual double getArea() = 0;
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef CIRCLE_H 
#define CIRCLE_H 
#include "BasicShape.h"

class Circle : public BasicShape{
private:     
	// x, y, radius    
	double x;
	double y;
	double radius;
public:     
	// functions: constructor, x, y, area
	Circle(double x, double y, double radius) {};
	double getX() { return x; }
	double getY() { return y; }
	double getRadius() { return radius; }
	double getArea() { return 3.14 * radius * radius; }
};
Where do you actually initialize the variables in your classes?

For example: Circle(double x, double y, double radius) {}; doesn't assign any values to the class variables.

Topic archived. No new replies allowed.