### Classes (and not getting them)

I've been throwing things at this problem since Wednesday and I don't even know what I'm doing anymore. I figure that when a programming problem reduces you to tears, it's probably time to get help...

So my main issue here is classes. I could write a long and distinctly not elegant solution to the problem without classes, but I'm supposed to use one. Can someone please explain to me how I would use a class in the problem below? Explain it to me like you're explaining it to a five-year-old--I'm entirely lost :(

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152`` ``````//Write a program that includes a definition for a class named Point that stores and manipulates the location of a point on a plane [integers only]. You will need to declare and implement the following member functions: //i) A member function set that sets the private data after an object of this class is created //ii) A member function to move the point by an amount along the vertical and horizontal directions as specified by the first and second arguments //iii) A member function to mirror the point on either the x or y axis or both by using one or more parameters to determine which of these three options to apply //iv) Two functions to retrieve the current coordinates of the point //Your program will prompt a user for a set of points, first asking how many points //will be in the set, then reading in the points. The program then offers the user the following menu until the user exits: //A) enter a new set of points [will restart from the begining] //B) move a particular point along the x axis, y axis, or x and y axis //C) mirror a particular point along the x axis, y axis, or x and y axis //D) output all of the points in their current state //E) exit //ALGORITHM: you must include an algorithm at the top of your program which is a 'recipe' for how you will approach the problems of the program to make them work properly in code //COMMENTS: you must include comments in the program to describe variable declarations, methods/functions, loops, and any equations or vital calculations //GRADING: //Algorithm 0-10 points //Comments 0-10 points //Class Usage 0-30 points //Meets Requirements in Description 0-30 points //Free of Errors based on testing multiple iterations 0-20 points //So, I'll be needing: // A class, Point // A class member x that holds data pertaining to X // A class member y that holds data pertaining to Y // A variable for how far X needs to move (moveX)--or do I just replace the point entirely? // A variable for how far Y needs to move (moveY) // A function to multiply X, Y, or both by -1 (for mirroring) // A variable for the number of points to be dealt with (n) // A variable for the menu selection (menuSel)--where you can choose 1 (enter new points), 2 (move point), 3 (mirror point), 4 (output current points), or 5 (exit) // Note to self, functions go in Public and variables go in Private #include using namespace std; class Point //creates a class Point { public: void move (int); //this will be the function to move a point along one or both axis void mirror (int); //this will be the function to mirror a point along one or both axis int getX(int); //this is an accessor function to get X values int getY(int); //this is an accessor function to get Y values int xOne; int yOne; int xTwo; int yTwo; private: int x; //holds data pertaining to X1 int y; //holds data pertaining to Y1 }; int Point::getX() { return x; } int Point::getY() { return y; } void move (int x, int y) //creates the definition of the function move, to move a point along one or both axes { cout << "Where would you like to move the point to along the x axis? "; //asks user what they would like to change the x-coordinate of the point to cin >> x; //reads in a new x-coordinate cout << "Where would you like to move the point to along the y axis? "; //asks user what they would like to change the y-coordinate of the point to cin >> y; //reads in a new y-coordinate Point::xOne = x; Point::yOne = y; } void mirror (int x, int y) //creates the definition of the function mirror, to mirror the point across one or both axes. { char choice; //initializes a variable to hold the user's choice 'y' or 'n' cout << "Would you like to mirror the point across the x axis? Y/N "; //asks user if they would like to mirror a point across the x axis cin >> choice; //reads in user's choice, y or n switch (choice) //if user inputs Y or y, the point will be mirrored across the x-axis. If the user inputs N or n, the point will not be mirrored. If the user inputs anything else, the point will not be mirrored. { case'Y': case'y': x * -1; Point.x = x; break; case'N': case'n': break; default: break; } cout << "Would you like to mirror the point across the y axis? Y/N "; //asks the user if they would like to mirror a point across the y axis cin >> choice; //reads in user's choice, 'y' or 'n' switch (choice) //if user inputs Y or y, the point will be mirrored across the y-axis. If the user in puts N or n, the point will not be mirrored. If the user inputs anything else, the point will not be mirrored. { case'Y': case'y': y * -1; Point.y = y; break; case'N': case'n': break; default: break; } } int main () { int n; //number of points int menuSel; cout << "Enter the number (between 1 and 10) of points you wish to input: "; //prompts user to input the number of points they wish to enter cin >> n; //reads in the number from the user for (int i = 0; i < n; i++) //this loop should allow you to enter n points { cout << "\n Enter a point's X coordinate. "; //prompts user to input the x coordinate of a point cin >> Point::x1; //reads data into an array of X values cout << "\n Enter a point's Y coordinate. "; //prompts user to input the y coordinate of a point cin >> ; //reads data into an array of Y values } cout << "Please choose an action from the selections below:" << endl << "1. Enter a new set of points" << endl << "2. Move a particular point along the X-axis, Y-axis, or both." << endl << "3. Mirror a particular point around the X-axis, Y-axis, or both." << endl << "4. Output all points at their current location." << endl << "Exit the program."; cin >> menuSel; switch (menuSel) { case 1: return 1; break; case 2: //CODE FOR MOVING POINTS break; case 3: //CODE FOR MIRRORING POINTS case 4: cout << "(" << Point::xOne << ", " << Point::yOne << ")"; case 5: return 0; default: cout << "Invalid menu selection! Exiting program."; exit (1); }``````

You may have noticed from the jumbled state of that that I don't have any idea what I'm doing anymore.

But my main issue is definitely classes, especially private members.

I'm sorry I sound so stupid!
You never told us what the problem was. If theres an error make sure to tell us the error. I have no idea what im looking for.

Edit: ok i think i found the problem lol. A Point object is never instantiated. in main() do this:
`Point nameOfPointObject;`

Then when references nameOfPointsObject's variables, use `nameOfPointObject.membervariable` and `nameOfPointObject.memberFunction()`

I also notice your missing code for cases 2 and 3. Fix the code to use a Point object first though and repost it or edit the OP.
Last edited on
Mainly the problem is that I don't understand classes at all.

I don't know how to be able to use private members for anything, including in the functions that I need to use them in.

The errors I've been getting are that "Point::xOne member is inaccessable" (which makes sense, because it's a private member, but obviously there's a way to work with it or there wouldn't be a point to their existance) and, where I'm trying to make functions, "Declaration is incompatible with int Point::getX()"

EDIT: what is "instantiated"?

But thank you, I'll try to do...something...with that.
Last edited on
Right, for private members use setters and getters. You have some get methods, now make some methods that look like
 ``1234`` ``````void setX( int num ) { x = num; }``````

edit: also, main should return 0; at the very end.

edit again: By instantiated i mean created. Your class is like a blueprint and the object is the house. You never 'made' a house anywhere in your code.
Last edited on
I'm sorry, but I don't understand DX

What I don't know is how to let the user input as many points as they want, and have everything stored somewhere where it can be manipulated individually. I can sort of imagine how you would do it if you told the user to input up to ten points or whatever--you'd have ten variables. But how do you get variables to store user-inputted data when there's going to be an unknown amount of user-inputted data?
I would like to know if you understand the part about instantiated objects? That is very important to this project.

As for a dynamic list, you can use an std::vector. [url]http://www.cplusplus.com/reference/vector/vector/[/url]

But i would like to see the code you have right now to see if you are correctly using an object.
I think I understood, yes, but apparently that doesn't mean much with me.

Here's what I have now (a friend came and gave me some hints, but I'm still getting a whole slew of errors)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147`` ``````#include using namespace std; class Point //creates a class Point { Point (int x, int y); public: void move (int); //this will be the function to move a point along one or both axis void mirror (int); //this will be the function to mirror a point along one or both axis int getX(int); //this is an accessor function to get X values int getY(int); //this is an accessor function to get Y values void setX(int); void setY(int); int xOne; int yOne; int xTwo; int yTwo; private: int x; //holds data pertaining to X1 int y; //holds data pertaining to Y1 }; int Point::getX(int)/*these "()" need something inside them. what did you put inside them in your class? ^^^ */ { return x; } int Point::getY(int)/*ditto to the above hint :D */ { return y; } void Point::setX(int) { x = x; } void Point::setY(int) { y = y; } void move (int x, int y) //creates the definition of the function move, to move a point along one or both axes { cout << "Where would you like to move the point to along the x axis? "; //asks user what they would like to change the x-coordinate of the point to cin >> x; //reads in a new x-coordinate cout << "Where would you like to move the point to along the y axis? "; //asks user what they would like to change the y-coordinate of the point to cin >> y; //reads in a new y-coordinate Point xOne = x;/*this might need to change*/ //you only need to use "Class name"::"function name" when making the function like you did in lines 60 and 65 :) Point yOne = y;/*this might need to change*/ //when refrencing here, you only need to use the name of the int from your class like "yOne=y" n.n } void mirror (int x, int y) //creates the definition of the function mirror, to mirror the point across one or both axes. { char choice; //initializes a variable to hold the user's choice 'y' or 'n' cout << "Would you like to mirror the point across the x axis? Y/N "; //asks user if they would like to mirror a point across the x axis cin >> choice; //reads in user's choice, y or n switch (choice) //if user inputs Y or y, the point will be mirrored across the x-axis. If the user inputs N or n, the point will not be mirrored. If the user inputs anything else, the point will not be mirrored. { case'Y': case'y': { x*-1; Point.x = x; break; } case'N': case'n': { break; } default: { break; } } cout << "Would you like to mirror the point across the y axis? Y/N "; //asks the user if they would like to mirror a point across the y axis cin >> choice; //reads in user's choice, 'y' or 'n' switch (choice) //if user inputs Y or y, the point will be mirrored across the y-axis. If the user in puts N or n, the point will not be mirrored. If the user inputs anything else, the point will not be mirrored. { case 'Y': case 'y': /* your breaks are broken D: try treating the body of your cases as though they were if's. wrap them up safe and sound with "{}" :3 */ { y*-1; Point.y = y; break; } case 'N': case 'n': { break; } } } int main () { int n; //number of points int menuSel; cout << "Enter the number (between 1 and 10) of points you wish to input: "; //prompts user to input the number of points they wish to enter cin >> n; //reads in the number from the user for (int i = 0; i < n; i++) //this loop should allow you to enter n points { cout << "\n Enter a point's X coordinate. "; //prompts user to input the x coordinate of a point cin >> Point.xOne ; //reads data into the first slot for X cout << "\n Enter a point's Y coordinate. "; //prompts user to input the y coordinate of a point cin >> Point.yOne; //reads data into an array of Y values } cout << "Please choose an action from the selections below:" << endl << "1. Enter a new set of points" << endl << "2. Move a particular point along the X-axis, Y-axis, or both." << endl << "3. Mirror a particular point around the X-axis, Y-axis, or both." << endl << "4. Output all points at their current location." << endl << "Exit the program."; cin >> menuSel; switch (menuSel) { case 1: { return 1; break; } case 2: { //CODE FOR MOVING POINTS break; } case 3: { //CODE FOR MIRRORING POINTS } case 4: { cout << "(" << Point.xOne << ", " << Point::yOne << ") \n"; } case 5: { return 0; break; } default: cout << "Invalid menu selection! Exiting program."; exit (1); } return 0; }``````

I've heard about vectors, but we didn't even mention them in class, so it seems odd that they'd involved in the last project.

A lot of my errors are "a nonstatic member reference must be relative to a specific object" and there are a couple that say that " "initializing" cannot convert from int to Point," as well as some "Point::y is inaccessable" sorts of errors--probably has something to do with me not knowing what to set x = in the set function.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170`` ``````#include using namespace std; class Point //creates a class Point { Point (int xParam, int yParam)// Constructor, sets x and y to xParam and yParam. { setX(xParam); setY(yParam); } public: void move (int); //this will be the function to move a point along one or both axis void mirror (int); //this will be the function to mirror a point along one or both axis int getX(); //this is an accessor function to get X values int getY(); //this is an accessor function to get Y values void setX(int param); void setY(int param); int xOne; int yOne; int xTwo; int yTwo; private: int x; //holds data pertaining to X1 int y; //holds data pertaining to Y1 }; int Point::getX()// Getters do not usually take parameters. They are simply used to access a private variable. { return x; } int Point::getY()// Getters do not usually take parameters. They are simply used to access a private variable. { return y; } void Point::setX(int parameter)// Your paremeter needs a name. { x = parameter; // Set x to the given parameter } void Point::setY(int parameter) { y = parameter;// Set x to the given parameter } // I made the parameters references because you probably want to change the values of the variables that are passed in. If we do not make these references then whatever happens in this // function will not change anything outside of this function. A Point object is also passed in. The alternative to this is making the Point object global. void move(int& x, int& y, Point& point) //creates the definition of the function move, to move a point along one or both axes { cout << "Where would you like to move the point to along the x axis? "; //asks user what they would like to change the x-coordinate of the point to cin >> x; //reads in a new x-coordinate cout << "Where would you like to move the point to along the y axis? "; //asks user what they would like to change the y-coordinate of the point to cin >> y; //reads in a new y-coordinate point.setX( x ); // Use the Point class's setX function to set the X value of the point object. point.setY( y ); // ^^ } void mirror (int x, int y) //creates the definition of the function mirror, to mirror the point across one or both axes. { char choice; //initializes a variable to hold the user's choice 'y' or 'n' cout << "Would you like to mirror the point across the x axis? Y/N "; //asks user if they would like to mirror a point across the x axis cin >> choice; //reads in user's choice, y or n switch (choice) //if user inputs Y or y, the point will be mirrored across the x-axis. If the user inputs N or n, the point will not be mirrored. If the user inputs anything else, the point will not be mirrored. { case'Y': case'y': { x *= -1; // Fixed this. It now assigns x to the value of x multiplied by -1. This line is equal to x = x * -1; Point.x = x; // This is incorrect but i am not clear on what you want to happen. If you mirror it will the original disappear? break; } case'N': case'n': { break; } default: { break; } } // I do not understand why there are two of these? cout << "Would you like to mirror the point across the y axis? Y/N "; //asks the user if they would like to mirror a point across the y axis cin >> choice; //reads in user's choice, 'y' or 'n' switch (choice) //if user inputs Y or y, the point will be mirrored across the y-axis. If the user in puts N or n, the point will not be mirrored. If the user inputs anything else, the point will not be mirrored. { case 'Y': case 'y': /* your breaks are broken D: try treating the body of your cases as though they were if's. wrap them up safe and sound with "{}" :3 */ { y*-1; Point.y = y; break; } case 'N': case 'n': { break; } } } int main () { int n; //number of points int menuSel; cout << "Enter the number (between 1 and 10) of points you wish to input: "; //prompts user to input the number of points they wish to enter cin >> n; //reads in the number from the user Point points[n]; // An array of Points of size n. for (int i = 0; i < n; i++) //this loop should allow you to enter n points { // temporary variables used to hold input. int x = 0; int y = 0; cout << "\n Enter a point's X coordinate. "; //prompts user to input the x coordinate of a point cin >> x; cout << "\n Enter a point's Y coordinate. "; //prompts user to input the y coordinate of a point cin >> y; points[i].setX(x);// Set the x value points[i].setY(y);// Set the y value } cout << "Please choose an action from the selections below:" << endl << "1. Enter a new set of points" << endl << "2. Move a particular point along the X-axis, Y-axis, or both." << endl << "3. Mirror a particular point around the X-axis, Y-axis, or both." << endl << "4. Output all points at their current location." << endl << "Exit the program."; cin >> menuSel; switch (menuSel) { case 1: { return 1; // Not sure what your trying to do here break; } case 2: { //CODE FOR MOVING POINTS break; } case 3: { //CODE FOR MIRRORING POINTS } case 4: { cout << "(" << Point.xOne << ", " << Point::yOne << ") \n"; } case 5: { return 0; break; } default: cout << "Invalid menu selection! Exiting program."; exit (1); } return 0; }``````

Alright i fixed everything i saw. It hasnt been tested though so you should test it. I did notice a problem though. You are going to need to prompt the user on which point he wants to move or edit. Feel free to ask about anything i added or changed.

I woudl suggest making the move and mirror functions apart of the Point class so you can eventually do something like

 ``123456`` ``````which point would you like to edit? 4 is entered Would you like to move(1) or mirror(2)? 1 is entered points[4].move();``````
Last edited on
Topic archived. No new replies allowed.