Originally I created an inheritance for these two classes. I had everything working except one function in circleType, and last night laserlight helped debug that. Well, today I realize that this is a composition relationship not inheritance. So, I reworked all the code to reflect the new relation. And, again tonight I have one function that doesn't seem to be working right.
The problem is in the circle function calls at the end of main. Everything works through the calc and print of the area and circum. But, the first cindyCircle.printPoint() function prints (0, 0) when it should print (3, 3). I thought this problem was in the cindyCircle.setPointRadius() function. But, the functions following it worked, so then I narrowed it down the the cindyCircle.printPoint() but I still can't get it to work. THe second cindyCircle.printPoint() works fine after I explicitly setX and setY.
Can anyone see what I am not?
Thanks!
pointClass.h
Code:
#ifndef H_POINT
#define H_POINT
#include <iostream>
using namespace std;
class pointType
{
public:
pointType();
//default constructor
pointType(int xCoord, int yCoord);
//constructor
void setPoint(int xCoord, int yCoord);
//stores the values of xCoord into x, and yCoord into y
void setX(int xCoord);
//stores the value of xCoord into x and y remains unchanged
void setY(int yCoord);
//x remains unchanged and stores the value of yCoord into y
void getPoint(int & xCoord, int & yCoord);
//returns the value of x into xCoord and y into yCoord
void getX(int & xCoord);
//returns just the value of x into xCoord
void getY(int & xCoord);
//returns just the value of y into yCoord
void printPoint() const;
//prints (x, y)
void printX()const;
//prints just x
void printY()const;
//prints just y
private:
int x;
int y;
};
#endif
pointClass.cpp
Code:
#include "pointClass.h"
pointType::pointType()
{
setPoint(0, 0);
}
pointType::pointType(int xCoord, int yCoord)
{
setPoint(xCoord, yCoord);
}
void pointType::setPoint(int xCoord, int yCoord)
{
x = xCoord;
y = yCoord;
}
void pointType::setX(int xCoord)
{
x = xCoord;
}
void pointType::setY(int yCoord)
{
y = yCoord;
}
void pointType::getPoint(int & xCoord, int & yCoord)
{
xCoord = x;
yCoord = y;
}
void pointType::getX(int & xCoord)
{
xCoord = x;
}
void pointType::getY(int & yCoord)
{
yCoord = y;
}
void pointType::printPoint() const
{
cout << "(" << x << ", " << y << ")" << endl;
}
void pointType::printX() const
{
cout << "The value of x is: " << x << endl;
}
void pointType::printY() const
{
cout << "The value of y is: " << y << endl;
}
circleClass.h
Code:
#ifndef H_CIRCLE
#define H_CIRCLE
#include <iostream>
#include <string>
#include <cmath>
#include "pointClass.h"
const double PI = 3.14159;
class circleType: public pointType
{
public:
circleType();
circleType(int xCoord, int yCoord, int radius);
void setRadius(int radius);
void setPointRadius(int xCoord, int yCoord, int radius);
void getRadius(int & radius);
void getPointRadius(int & xCoord, int & yCoord, int & radius);
double calcArea();
double calcCircum();
void WhereAmI();
void printRadius() const;
void printArea();
void printCircum();
void printPointRadius() const;
private:
pointType x, y, point;
int r;
};
#endif
circleClass.cpp
Code:
#include "pointClass.h"
#include "circleClass.h"
circleType::circleType()
:x(), y()
{
r = 0;
}
circleType::circleType(int xCoord, int yCoord, int radius)
: x(x), y(y)
{
r = radius;
}
void circleType::setRadius(int radius)
{
r = radius;
}
void circleType::setPointRadius(int xCoord, int yCoord, int radius)
{
x.setX(xCoord);
y.setY(yCoord);
point.setPoint(xCoord, yCoord);
r = radius;
}
void circleType::getRadius(int & radius)
{
radius = r;
}
void circleType::getPointRadius(int & xCoord, int & yCoord, int & radius)
{
point.getX(xCoord);
point.getY(yCoord);
radius = r;
}
double circleType::calcArea()
{
return (PI * pow(r, 2));
}
double circleType::calcCircum()
{
return (2 * PI * r);
}
void circleType::printRadius() const
{
cout << "The current value of radius is: " << r << endl;
}
void circleType::printArea()
{
cout << "The area of a circle with a radius of "
<< r << " is " << calcArea() << endl;
}
void circleType::printCircum()
{
cout << "The circumference of a circle with a radius of "
<< r << " is " << calcCircum() << endl;
}
void circleType::printPointRadius() const
{
point.printPoint();
x.printX();
y.printY();
cout << " and the radius of the circle is " << r << "." << endl;
}
void circleType::WhereAmI()
{
int xCoord,
yCoord;
point.getPoint(xCoord, yCoord);
x.getX(xCoord);
y.getY(yCoord);
if (xCoord > 0 && yCoord > 0)
cout<< "My Center is in the first quadrant of the cartesian plane." << endl;
else if (xCoord > 0 && yCoord < 0)
cout << "My Center is in the second quadrant of the cartesian plane." << endl;
else if (xCoord < 0 && yCoord < 0)
cout << "My Center is in the third quadrant of the cartesian plane." << endl;
else
cout << "My Center is in the fourth quadrant of the cartesian plane." << endl;
}
main.cpp
Code:
#include "pointClass.h"
#include "circleClass.h"
//to test the pointClass prior to building the circleClass
int main()
{
int mainX = 0,
mainY = 0,
returnValue = 0;
//operation calls to check the point class
pointType cindyPoint;
cout << "The x and y values of cindyPoint are: ";
cindyPoint.printPoint();
cindyPoint.setX(19);
cindyPoint.setY(57);
cout << "The new x and y values of cindyPoint are: ";
cindyPoint.printPoint();
cout << "Before the get functions, mainX = " << mainX << ", mainY = " << mainY
<< ", \n\tand returnValue (mainX + mainY) = " << returnValue << "." << endl;
cindyPoint.getX(mainX);
cindyPoint.getY(mainY);
returnValue = mainX + mainY;
cout << "After the get functions, mainX = " << mainX << ", mainY = " << mainY
<< ", \n\tand returnValue (mainX + mainY) = " << returnValue << "." << endl;
pointType badPoint;
cout << "The x and y values of badPoint are: ";
badPoint.printPoint();
badPoint.setX(32);
badPoint.setY(23);
badPoint.printX();
badPoint.printY();
cout << "The x and y values of badPoint are now: ";
badPoint.printPoint();
badPoint.setPoint(-62, -78);
cout << "The x and y values of badPoint have been reset to: ";
badPoint.printPoint();
//operation calls to check the circle class
circleType cindyCircle;
cout << "\ncindyCircle has default x and y values of: ";
cindyCircle.printPointRadius();
cindyCircle.setPointRadius(3, 3, 3);
cout << "cindyCircle has reset values to: ";
cindyCircle.printPointRadius();
cindyCircle.calcArea();
cindyCircle.printArea();
cindyCircle.calcCircum();
cindyCircle.printCircum();
cout << "The x and y values of cindyPoint now are: ";
cindyCircle.printPoint();
cindyCircle.WhereAmI();
cindyCircle.setX(-3);
cindyCircle.setY(-3);
cout << "The x and y values of cindyPoint now are: ";
cindyCircle.printPoint();
cindyCircle.WhereAmI();
}