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
{
};
#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!
First order of business, get your pointType functions correct.
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.
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.
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.
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:
double radius;
};//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
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?
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.
Originally Posted by xCrusade
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.
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
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.
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?
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!
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.
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
- Brian W. Kernighan
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.
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
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
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.
Bookmarks