CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member
Join Date
Mar 2011
Posts
15

Hey,
I am working on a problem in which I am having a hard time starting. The problem being:

Every circle has a center and a radius. Given the radius, we can determine its position in the x-y plane. The center of the circle is a point in the x-y plane. Design a class, circleType, that can store the radius and center of the circle. Because the center is a point in the x-y plane and you designed the class to caputure the propteries of a point in programming exercise 3, you must derive the class circleType from the class pointType. You should be able to perform the usual operations on the circle, such as setting the radius, printing the radius, calculating and print the area and circumference, and carrying out the usual operations on the center. Also write a program to test various operations on a circle.

Although is references a previous exercise, we do not do this one, but class pointType should be designed to store and process a point in the x-y plane, perform operations on the point such as setting the coordinates of the point, printing the coordinates of the point, returning x coordinate and returning the y coordinate.

Sorry, I know this is a mouthful but I have difficulty in this language.

I have started the code, but OOP in C++ is still very new to me and classes are still very confusing. But here's what I have to far:

circleType.h
Code:
```#include "pointType.h"

class circleType: public pointType
{

};```
circleTypeImp.cpp
Code:
```#include <iostream>
#include "pointType.h"```
pointType.h
Code:
```using namespace std;

class pointType
{
public:
void printPoint() const;
void setPoint();
void getPoint();

pointType();
pointType(double x, double y);
~pointType();
private:
double xPlane, yPlane;
};```
pointTypeImp.cpp
Code:
```#include <iostream>
#include "pointType.h"

pointType::pointType()
{

}

pointType::pointType(double x, double y)
{
xPlane = x;
yPlane = y;
}

pointType::~pointType()
{

}

void pointType::printPoint() const
{

}

void pointType::setPoint()
{

}

void pointType::getPoint()
{

}```
main.cpp
Code:
```#include <iostream>
#include "pointType.h"
//#include "circleType.h"

using namespace std;

int main()
{

system("pause");
return 0;
}```

I don't want this done for me, I just need a little push to understand what I might need. I am confusing on the member functions and variables of the classes and what exactly I will need and what they should do. I never got the concept of getter and setter functions either and why I need to use them. If anyone can offer a little guidance to make things a little more clear, I would appreciate it. I know this is very incomplete, I am having a coding block and it's frustrating!

2. Member
Join Date
Jun 2009
Location
oklahoma
Posts
199

## Re: Circle and Radius Problem

What do you expect setPoint and getPoint to do?
You currently have them both defined as accepting nothing and returning nothing.

Start on those two functions and the printPoint function.

Then, make sure those same functions work as expected when derived in the circleType class.

3. ## Re: Circle and Radius Problem

Every circle has a center and a radius. Given the radius, we can determine its position in the x-y plane. The center of the circle is a point in the x-y plane. Design a class, circleType, that can store the radius and center of the circle. Because the center is a point in the x-y plane and you designed the class to caputure the propteries of a point in programming exercise 3, you must derive the class circleType from the class pointType. You should be able to perform the usual operations on the circle, such as setting the radius, printing the radius, calculating and print the area and circumference, and carrying out the usual operations on the center. Also write a program to test various operations on a circle.
IMO, this is a horrible exercise. In OOP, public inheritance means an IS-A relationship. That is, if Circle is publicly derived from Point then Circle is a Point. That's not true, therefore the class design is wrong. This is the kind of this you use to filter out unqualified applicants in a job interview.

Here's some further reading on the basic principles of OOP:
http://www.objectmentor.com/resources/articles/ocp.pdf
http://www.objectmentor.com/resources/articles/lsp.pdf
http://www.objectmentor.com/resources/articles/dip.pdf

4. Elite Member Power Poster
Join Date
Jan 2006
Location
Singapore
Posts
6,748

## Re: Circle and Radius Problem

Although is references a previous exercise, we do not do this one, but class pointType should be designed to store and process a point in the x-y plane, perform operations on the point such as setting the coordinates of the point, printing the coordinates of the point, returning x coordinate and returning the y coordinate.
I am confusing on the member functions and variables of the classes and what exactly I will need and what they should do. I never got the concept of getter and setter functions either and why I need to use them.
It looks like you have two constructors which look about right. You don't need the destructor since the compiler generated one will do. Instead of getPoint and setPoint, have getX, getY, setX, setY member functions. The former two should return a value and the latter two should have a parameter. With these, you can overload operator<< to output a pointType object, or if you have not been taught this, then just write a non-member print function that uses getX and getY.

D_Drmmr is right: if you dare to do what is right, have a pointType member variable in your circleType class. You then provide getPoint and setPoint member functions in circleType. Of course, be prepared to defend your deviation from the instructions with clear reasoning and proper citation of sources that discuss OOP.

5. Junior Member
Join Date
Mar 2011
Posts
15

## Re: Circle and Radius Problem

Thanks for the input guys. I am going to be working on it most the day after I get out of work, I will post my results tonight if there are no problems.

6. Elite Member Power Poster
Join Date
Oct 2007
Location
Seattle, WA
Posts
10,895

## Re: Circle and Radius Problem

Originally Posted by D_Drmmr
IMO, this is a horrible exercise. In OOP, public inheritance means an IS-A relationship. That is, if Circle is publicly derived from Point then Circle is a Point. That's not true, therefore the class design is wrong.
Note the word "publicly". It's perfectly fine to derive Circle privately from Point, although personally I would use composition instead.

7. Junior Member
Join Date
Mar 2011
Posts
15

## Re: Circle and Radius Problem

Here is what I have so far.. I am having trouble starting the circleType class, I am not sure what functions to put in it. I am assuming setRadius, printRadius, calculateArea, printArea, calculateCircumference, printCircumference, or combine the print functions into one. Also, I cannot get the main file to read an object from the circleType class, only the pointType. Can anyone point me in the right direction? Here is my program so far..

circleType.h
Code:
```class circleType: public pointType
{
public:
circleType();
circleType(double);
~circleType();

private:
};//End derived class circleType```
main.cpp
Code:
```#include <iostream>
//#include "pointType.h"
#include "circleType.h"

using namespace std;

int main()
{
pointType c;
//classType c;

double x;
double y;

cout << "Enter the coordinate of x: ";
cin >> x;
cout << endl;

cout << "Enter the coordinate of y: ";
cin >> y;
cout << endl;

c.setXPoint(x);
c.setYPoint(y);

c.getXPoint(x);
c.getYPoint(y);

c.printPoint();

system("pause");
return 0;
}//End main```
circleTypeImp.cpp
Code:
```#include <iostream>
#include "circleType.h"

circleType::circleType()
{

}//End circleType

circleType::circleType(double)
{
}//End circleType(double)

circleType::~circleType()
{

}//End ~circleType()```
pointType.h
Code:
```class pointType
{
public:
void printPoint() const;
void setXPoint(double);
void setYPoint(double);
double getXPoint(double);
double getYPoint(double);

pointType();
pointType(double x, double y);
~pointType();
private:
double xPoint;
double yPoint;
};//End base class pointType```
pointTypeImp.cpp
Code:
```#include <iostream>
#include "pointType.h"

pointType::pointType()
{

}//End pointType

pointType::pointType(double x, double y)
{
double xPoint;
double yPoint;
}//End pointType(double, double)

pointType::~pointType()
{

}//End ~pointType

void pointType::printPoint() const
{
cout << "The point is at (" << xPoint << "," << yPoint << ")" << endl;
cout << endl;
}//End printPoint

void pointType::setXPoint(double x)
{
xPoint = x;
}//End

void pointType::setYPoint(double y)
{
yPoint = y;
}

double pointType::getXPoint(double x)
{
return x;
}

double pointType::getYPoint(double y)
{
return y;
}```
How would I get the main.cpp be able to read the circleType.h file with the pointType classes functions? or how do I implement circleType into my program to complete it? I think I can manage the calculations and the printing if I can get it to work so I can test. Also, am I right on the functions that I might need to complete the program?

8. Member
Join Date
Jun 2009
Location
oklahoma
Posts
199

## Re: Circle and Radius Problem

You currently have xPoint and yPoint set to private inside pointType. They need to be 'protected' so that circleType can inherit it.

9. Junior Member
Join Date
Mar 2011
Posts
15

## Re: Circle and Radius Problem

I've been working on this most of the day and it has been going smoothly, I am just having one problem. As of now, I have the user entering 2 points for x and y coordinates. Which seems pretty much useless, because I assume these points are to somehow calculate the radius of the circle. Right now, I have the user inputting the radius of their own circle just so I can get some results. Everything seems to work as is, but my question is how do I implement a calculation into my code that takes the x and y coordinate entered by the user and creates a radius from it?

Here is what I have so far:

circleType.h
Code:
```class circleType: public pointType
{
public:
void printCircle() const;
double calcArea(double);
double calcCircumference(double);

circleType();
~circleType();

private:
double circleArea;
double circleCircumference;
};//End derived class circleType```
circleTypeImp.cpp
Code:
```#include <iostream>
#include <cmath>
#include "pointType.h"
#include "circleType.h"

using namespace std;

circleType::circleType()
{

}//End circleType

circleType::circleType(double)
{
}//End circleType(double)

circleType::~circleType()
{

}//End ~circleType()

{

{

double circleType::calcArea(double area)
{
const double PI = 4.0*atan(1.0);

circleArea = area;
return circleArea;
}//End calcArea

double circleType::calcCircumference(double circumference)
{
circumference = 0;
const double PI = 4.0*atan(1.0);

circumference = PI * (2 * circleRadius);

circleCircumference = circumference;
return circleCircumference;
}//End calcCircumference

void circleType::printCircle() const
{
cout << "The radius of the circle is: " << circleRadius << endl;
cout << "The area of the circle is: " << circleArea << endl;
cout << "The circumference of the circle is: " << circleCircumference << endl;
cout << endl;
}//End printCircle```
main.cpp
Code:
```#include <iostream>
#include "pointType.h"
#include "circleType.h"

using namespace std;

int main()
{
pointType p;
circleType c;

double x;
double y;
double area;
double circumference;

cout << "Follow the instructions to determine the values of your circle!" << endl;
cout << endl;
cout << "Enter the point for x: ";
cin >> x;
cout << "Enter the point for y: ";
cin >> y;
cout << endl;

p.setXPoint(x);
p.setYPoint(y);

p.getXPoint(x);
p.getYPoint(y);

p.printPoint();

cout << endl;

c.calcArea(area);
c.calcCircumference(circumference);
c.printCircle();

system("pause");
return 0;
}//End main```
pointType.h
Code:
```class pointType
{
public:
void printPoint() const;
void setXPoint(double);
void setYPoint(double);
double getXPoint(double);
double getYPoint(double);

pointType();
pointType(double x, double y);
~pointType();
protected:
double xPoint;
double yPoint;
};//End base class pointType```
pointTypeImp.cpp
Code:
```#include <iostream>
#include "pointType.h"

using namespace std;

pointType::pointType()
{

}//End pointType

pointType::pointType(double x, double y)
{
double xPoint;
double yPoint;
}//End pointType(double, double)

pointType::~pointType()
{

}//End ~pointType

void pointType::printPoint() const
{
cout << "The center point is at (" << xPoint << "," << yPoint << ")" << endl;
cout << endl;
}//End printPoint

void pointType::setXPoint(double x)
{
xPoint = x;
}//End setXPoint

void pointType::setYPoint(double y)
{
yPoint = y;
}//End setYPoint

double pointType::getXPoint(double x)
{
return x;
}//End getXPoint

double pointType::getYPoint(double y)
{
return y;
}//End getYPoint```

If anyone can offer any guidance, that would be much appreciated, but as it stands with the radius given by the user, it works, I just want to find a way to turn the coordinates given by the user into a radius if possible!

10. Elite Member
Join Date
Oct 2006
Location
Sweden
Posts
3,649

## Re: Circle and Radius Problem

If you want to calculate the radius you need to have the user to input two points (i.e. two x-y pairs). One that's the center of the circle and one that's a point somewhere on the circle.

Having those points you can easily calculate the radius using Pythagoras' theorem http://en.wikipedia.org/wiki/Pythagorean_theorem

11. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: Circle and Radius Problem

I'm just going to echo what others have said. That is a horrible and completely inappropriate use of inheritance. Your instructor is leading you seriously astray.

12. Junior Member
Join Date
Mar 2011
Posts
15

## Re: Circle and Radius Problem

Thanks for the input S_M_A, I am going to finish up when I get out of work.

I keep hearing this is a bad use of inheritance, most likely the book. The examples and explanations in the book are somewhat vague most of the time as well, just tryin' to push through with what I got :P

13. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: Circle and Radius Problem

Thanks for the input S_M_A, I am going to finish up when I get out of work.

I keep hearing this is a bad use of inheritance, most likely the book. The examples and explanations in the book are somewhat vague most of the time as well, just tryin' to push through with what I got :P
If it's a book, get a new book. Seriously.

14. Elite Member Power Poster
Join Date
Oct 2007
Location
Seattle, WA
Posts
10,895

## Re: Circle and Radius Problem

Proper use of inheritance is relatively simple: if you can logically say that Type1 IS-A Type2, eg "Ford IS-A Car", then it makes sense to use public inheritance, deriving Ford from Car. (Deriving Ford from AlienFromASmallPlanetInTheVicinityOfBetelgeuse would also work.)

If you can't make the IS-A relation, then you need to consider carefully whether inheritance is appropriate. It may be that private inheritance is still just fine----which might well be the case here----but not public inheritance. The distinction between private inheritance and composition is largely a matter of interface policy.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•