424,994 Members | 2,036 Online Need help? Post your question and get tips & solutions from a community of 424,994 IT Pros & Developers. It's quick & easy.

Very lost

 P: n/a Hello guys Any help that you can offer would be greatly appreciated. I am nearing the end of a C++ class and am so lost. The following is my assignment and what I have so far. Please help Define a class called Point which contains the coordinates (x, y) of a point in a plane, where both x and y are two integers (positive or negative whole numbers), i.e. the Point class contains two private integer variables x and y. Then add the following methods in the class: setPoint(int x1, int y1) to assign coordinates x1 and y1 to a point object display( ) to print a point object with coordinates x and y in (x,y) format. an overloaded operator + (addition operation for two points) to add two point objects by adding the corresponding x and y coordinates respectively an overloaded operator - (subtraction operation for two points) to subtract one point object from another by subtracting the corresponding x and y coordinates respectively an overloaded unary operator - (negation operation for a point) to negate both x and y coordinates In the main program, create two points of your choice, perform the three operations defined above and display the result for each case. Bonus: define an overloaded operator * which allows a point to multiply an integer. The result is a point where x and y coordinates are multiplied by that integer. define an overloaded operator << so that a point object can be printed directly by using << operator instead of the display method. What I have: #include //using namespace std; class point { private: int point; public: int set_point(x1=5, y1=10); display(); friend point operator +(const point& coordinateX, const point& coordinateY); //Precondition: coordinateX and coordinateY have been given values. //Returns the sum of the values of coordinateX and CoordinateY. friend point operator -(const point& coordinateX, const point& coordinateY); //Precondition: coordinateX and CoordinateY have been given values. //Returns the value of coordinateX - coordinateY. friend bool operator == (const point& coordinateX, const point& coordinateY); //Precondition: coordinateX and coordinateY have been given values. //Returns true if coordinateX and coordinateY have the same value. //Otherwise returns false. }; int main() { point coordinateX(5), coordinateY(10); cout<<"The sum of coordinateX + coordinateY is "<< temp1&<
6 Replies

 P: n/a Lost Student wrote: Hello guys Any help that you can offer would be greatly appreciated. I am nearing the end of a C++ class and am so lost. That should be a cause of concern for you. The following is my assignment and what I have so far. Please help What I have: #include //using namespace std; class point { private: int point; Why is the class member and the class name same ?? I guess the class members would be: int x; int y; public: int set_point(x1=5, y1=10); Ok so you are using default parameters here. However the datatype for parameters needs to be explicitly mentioned. So, this could be: int set_point(int x1 = 5, int y1 = 10); display(); friend point operator +(const point& coordinateX, const point& coordinateY); //Precondition: coordinateX and coordinateY have been given values. //Returns the sum of the values of coordinateX and CoordinateY. friend point operator -(const point& coordinateX, const point& coordinateY); //Precondition: coordinateX and CoordinateY have been given values. //Returns the value of coordinateX - coordinateY. friend bool operator == (const point& coordinateX, const point& coordinateY); //Precondition: coordinateX and coordinateY have been given values. //Returns true if coordinateX and coordinateY have the same value. //Otherwise returns false. }; int main() { point coordinateX(5), coordinateY(10); Makes no sense. So what you could have done is to declare 2 constructors in the point class. One - default constructor which initialises the members to 0 and other which accepts the values. So your set_point should have been a constructor. Please read more about constructors. cout<<"The sum of coordinateX + coordinateY is "<< temp1&< Please have their definitions outside main. return 0; } Apr 24 '06 #2

 P: n/a "Lost Student" writesL Any help that you can offer would be greatly appreciated. I am nearing the end of a C++ class and am so lost. The following is my assignment and what I have so far. Please help Define a class called Point which contains the coordinates (x, y) of a point in a plane, where both x and y are two integers (positive or negative whole numbers), i.e. the Point class contains two private integer variables x and y. Then add the following methods in the class: setPoint(int x1, int y1) to assign coordinates x1 and y1 to a point object display( ) to print a point object with coordinates x and y in (x,y) format. an overloaded operator + (addition operation for two points) to add two point objects by adding the corresponding x and y coordinates respectively an overloaded operator - (subtraction operation for two points) to subtract one point object from another by subtracting the corresponding x and y coordinates respectively an overloaded unary operator - (negation operation for a point) to negate both x and y coordinates In the main program, create two points of your choice, perform the three operations defined above and display the result for each case. Bonus: define an overloaded operator * which allows a point to multiply an integer. The result is a point where x and y coordinates are multiplied by that integer. define an overloaded operator << so that a point object can be printed directly by using << operator instead of the display method. What I have: #include //using namespace std; class point { private: int point; It's not a good idea to use the same identifier for two different things. You have a class named point containing an int also named point. Besides there are *two* coordinates, you just typed that above, didn't you? Try something along these lines class Point { int x; int y; public: void set_point(int x1; int y1) { x = x1; y = y1;) }; Now you add some more stuff to it. public: int set_point(x1=5, y1=10); int main() { Point a, b, c; a.set_point( 5, 10); b.set_point(3, 13}; //point coordinateX(5), coordinateY(10); c = a + b; // after you do *your* magic this should work } Operator + should not be a friend, which is what you started on. Never use a friend unless there is some real or imagined pay off. The rules say to add two points so there is no possible payoff to being a friend. It just adds clutter to your code. There will be enough necessary clutter without adding to it.. I suggest the first thing you write is the display function, yu can use it to test the other functions. Apr 24 '06 #3

 P: n/a * Lost Student: Any help that you can offer would be greatly appreciated. I am nearing the end of a C++ class and am so lost. The following is my assignment and what I have so far. Please help Since you're not explaining any problem answers must necessarily be limited to (1) pointing out obvious problems, or (2) doing your homework for you. Here I'll do (1). Let's hope nobody tries to do (2), that at least they read the FAQ before they do such a disservice to you and to the group. [A] Define a class called Point which contains the coordinates (x, y) of a point in a plane, where both x and y are two integers (positive or negative whole numbers), i.e. the Point class contains two private integer variables x and y. Then add the following methods in the class: [b] setPoint(int x1, int y1) to assign coordinates x1 and y1 to a point object [C] display( ) to print a point object with coordinates x and y in (x,y) format. [D] an overloaded operator + (addition operation for two points) to add two point objects by adding the corresponding x and y coordinates respectively [E] an overloaded operator - (subtraction operation for two points) to subtract one point object from another by subtracting the corresponding x and y coordinates respectively [F] an overloaded unary operator - (negation operation for a point) to negate both x and y coordinates [G] In the main program, create two points of your choice, perform the three operations defined above and display the result for each case. [H] Bonus: define an overloaded operator * which allows a point to multiply an integer. The result is a point where x and y coordinates are multiplied by that integer. [i] define an overloaded operator << so that a point object can be printed directly by using << operator instead of the display method. What I have: #include //using namespace std; A good thing you commented out that. Try to completely avoid using-directives for namespace std. They only cause problems. class point Problem 1: not the class name specified in [A]. { private: int point; Problem 2: same name as class. Problem 3: not the member variables specified in [A]. public: int set_point(x1=5, y1=10); Problem 4: arbitrary default values, not good. display(); Problem 5: should be a 'const' member function since should not modify object. friend point operator +(const point& coordinateX, const point& coordinateY); Problem 6: should not need to be a 'friend'; the need for 'friend' indicates incomplete functionality offered by the class. //Precondition: coordinateX and coordinateY have been given values. Problem 7: this precondition indicates a missing constructor. //Returns the sum of the values of coordinateX and CoordinateY. Problem 8: This comment is visually together with the following member function, but applies to the previous one: confusing code. friend point operator -(const point& coordinateX, const point& coordinateY); Problem 6: should not need to be a 'friend'; the need for 'friend' indicates incomplete functionality offered by the class. //Precondition: coordinateX and CoordinateY have been given values. Problem 7: this precondition indicates a missing constructor. //Returns the value of coordinateX - coordinateY. Problem 8: This comment is visually together with the following member function, but applies to the previous one: confusing code. friend bool operator == (const point& coordinateX, const point& coordinateY); Problem 6: should not need to be a 'friend'; the need for 'friend' indicates incomplete functionality offered by the class. //Precondition: coordinateX and coordinateY have been given values. Problem 7: this precondition indicates a missing constructor. //Returns true if coordinateX and coordinateY have the same value. //Otherwise returns false. Problem 8: This comment applies to the previous member function: confusing code. Problem 9: Tries to solve something not asked for, and not helping in what's been asked for. }; int main() { point coordinateX(5), coordinateY(10); Problem 10: no constructor with one argument defined, and would be meaningless if defined. cout<<"The sum of coordinateX + coordinateY is "<< temp1&<

 P: n/a osmium wrote: "Lost Student" writesL Any help that you can offer would be greatly appreciated. I am nearing the end of a C++ class and am so lost. The following is my assignment and what I have so far. Please help #include //using namespace std; class point { private: int point; It's not a good idea to use the same identifier for two different things. You have a class named point containing an int also named point. Besides there are *two* coordinates, you just typed that above, didn't you? Try something along these lines class Point { int x; int y; public: void set_point(int x1; int y1) { x = x1; y = y1;) 1. You probably meant a comma ( , ) instead of semicolon ( ; ) after x1 in the function header. 2. You probably meant a closing curly braces ( } ) instead of paranthesis ( ) ) at end of function: So, I am sure this is what you meant: void set_point(int x1, int y1) { x = x1; y = y1; } Please let me know if my thinking is right or not. ;) }; Now you add some more stuff to it. public: int set_point(x1=5, y1=10); int main() { Point a, b, c; a.set_point( 5, 10); b.set_point(3, 13}; //point coordinateX(5), coordinateY(10); c = a + b; // after you do *your* magic this should work } Operator + should not be a friend, which is what you started on. Never use a friend unless there is some real or imagined pay off. The rules say to add two points so there is no possible payoff to being a friend. It just adds clutter to your code. There will be enough necessary clutter without adding to it.. I suggest the first thing you write is the display function, yu can use it to test the other functions. Apr 24 '06 #5

 P: n/a "Jaspreet" writes: class Point { int x; int y; public: void set_point(int x1; int y1) { x = x1; y = y1;) 1. You probably meant a comma ( , ) instead of semicolon ( ; ) after x1 in the function header. 2. You probably meant a closing curly braces ( } ) instead of paranthesis ( ) ) at end of function: So, I am sure this is what you meant: void set_point(int x1, int y1) { x = x1; y = y1; } Please let me know if my thinking is right or not. ;) Yes that's what I meant. It's a good thing compilers catch those kinds of things. I still put comma instead of semicolon if for statements. Apr 24 '06 #6

 P: n/a Alf P. Steinbach wrote: * Lost Student: if (coordinateX = coordinateY) { cout<<"X and Y are equal to eachother."< 