Classes

I'm just learning about classes and inheritance. I made a program that uses 3 classes: class a, class b, and class c, where each class inherits properties from the former class.

Everything works great except for a problem I've traced to class b. Class b is a class that sets and defines several properties of a circle. When I try get any sort of output for my functions that return circumference and area values I get nothing - the functions return nothing at all (not even 0).

Note that for the following code I do reference other classes in some of it (since I'm just copying/pasting from what I have). However none of that is actually relevant for the problem - its totally self-contained within this circle class.

Here is my header file:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef H_circleType
#define H_circleType

#include "pointType.h"

using namespace std;

class circleType: public pointType
{
public:
	void setCenter(double xCoord, double yCoord);
	//Function to set the center according to parameters
	//Postcondition: x = xCoord, y = yCoord

	void setXCenter(double xCoord);
	//Function to set the x-coordinate of the center
	//Postcondition: x = xCoord

	void setYCenter(double yCoord);
	//Function to set the y-Coordiante of the center
	//Postcondition: y = yCoord

	void printCenter() const;
	//Function to print the center of the circle in the following format:
	//Postcondition: Output (x, y)

	double getXCenter() const;
	//Function to print the x-coordinate of the center of the circle
	//Postcondition: The value of x is returned

	double getYCenter() const;
	//Function to print the y-coordinate of the center of the circle
	//Postcondition: The value of y is returned

	void setRadius(double radius);
	//Function to set the radius according to the parameter
	//Postcondition: r = radius

	void printRadius() const;
	//Function to print the radius of the circle
	//Postcondition: The message 'The radius is r' is returned

	double getRadius() const;
	//Function to print the radius of the circle
	//Postcondition: The value of r is returned

	void setPi(double pi);
	//Function to set pi according to the parameter
	//Postcondition: pi1 = PI

	void printPi() const;
	//Function to print pi
	//Postcondition: The message 'pi = pi1' is returned

	double getPi() const;
	//Function to print pi
	//Postcondition: The value of pi1 is returned

	double calculateCircumference() const;
	//Function to calculate and print the circumference of the circle
	//Postcondition: The value of the circumference is returned

	double calculateArea() const;
	//Function to calculate and print the area of the circle
	//Postcondition: The value of the area is returned

	circleType(double xCoord = 0.0, double yCoord = 0.0, double radius = 0.0, double pi = 3.1416);
	//Constructor
	//Sets the x- and  y-coordinates and the radius according to parameters.  The default values of the
	//parameters are 0.
	//Postcondition: x = xCoord, y = Coord, r = radius, pi = PI

private:
	double r;
	double pi1;
};

#endif 


Here is my class file:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <cmath>
#include "pointType.h"
#include "circleType.h"

using namespace std;

void circleType::setCenter(double xCoord, double yCoord)
{
	setCoordinates(xCoord, yCoord);
}

void circleType::setXCenter(double xCoord)
{
	setXCoordinate(xCoord);
}

void circleType::setYCenter(double yCoord)
{
	setYCoordinate(yCoord);
}

void circleType::printCenter() const
{
	cout << "(" << getXCoordinate() << ", " << getYCoordinate() << ")" << endl;
}

double circleType::getXCenter() const
{
	return getXCoordinate();
}

double circleType::getYCenter() const
{
	return getYCoordinate();
}

void circleType::setRadius(double radius)
{
	if (radius >= 0)
		r = radius;
	else
		r = 0;
}

void circleType::printRadius() const
{
	cout << "The radius is " << r << "." << endl;
}

double circleType::getRadius() const
{
	return r;
}

void circleType::setPi(double pi)
{
	if (pi >= 3 && pi <= 4)
		pi = pi1;
	else
		pi1 = 3.1416;
}

void circleType::printPi() const
{
	cout << "pi = " << pi1 << endl;
}

double circleType::getPi() const
{
	return pi1;
}

double circleType::calculateCircumference() const
{
	return 2 * pi1 * r;
}

double circleType::calculateArea() const
{
	return pi1 * pow(r, 2);
}

	//Constructor
circleType::circleType(double xCoord, double yCoord, double radius, double pi) : pointType(xCoord, yCoord)
{
	if (radius >= 0)
		r = radius;
	else
		r = 0;

	pi1 = 3.1416;
}


And here is an excerpt of my main program:
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
#include <iostream>
#include <cmath>
#include <iomanip>
#include "pointType.h"
#include "circleType.h"
#include "cylinderType.h"

using namespace std;

int main()
{
	double xVariable;
	double yVariable;
	double radius;
	double height;

	circleType circle;

	cout << fixed << showpoint << setprecision(2);

         circleType.setRadius(radius);
         cout << "The area of the circle is ";
         circleType.calculateArea();

         cout << "The circumference of the circle is ";
         circleType.calculateCircumference();

         system("PAUSE");

         return 0;
}


With this bit of code I get no result for the calculateArea and calculateCircumference functions. If I check to see that my radius is set properly with the printRadius function I get the correct output (whatever I entered in). Similarly if I check pi with the printPi function I get the correct output (the 3.1416 I've set it to default to in the constructor).

I've tried making a dummy variable in the calculateCircumference function (ie. I make a variable temp, set all the calculations to equal temp, and then return temp so I'm not doing calculations in the return statement) and that didn't help. I really have no idea why this isn't giving me any results.

Any help would definitely be appreciated! Thank you in advance :)
you won't get anything cause you're not printing out the value.

1
2
3
4
5
6
circleType.setRadius(radius);
cout << "The area of the circle is ";
circleType.calculateArea();

cout << "The circumference of the circle is ";
circleType.calculateCircumference();


should be

1
2
3
4
5
6
circleType.setRadius(radius);
cout << "The area of the circle is ";
cout << circleType.calculateArea(); // compute the area and print it out

cout << "The circumference of the circle is ";
cout << circleType.calculateCircumference(); // compute the circumference and print it out 
Last edited on
Oh duh! I feel silly now. :)

Thank you!
Topic archived. No new replies allowed.