By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,994 Members | 2,036 Online
Bytes IT Community
+ Ask a Question
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 <iostream>
//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&<<endl;

cout<<"The difference between coordinateX and coordinateY is
"<<temp2<<endl;

if (coordinateX = coordinateY)
{
cout<<"X and Y are equal to eachother."<<endl;

else
cout<<"X and Y are not equal to eachother"<<endl;
}
friend point operator +(const point& coordinateX, const point&
coordinateY)

{
point temp1;
temp1.both = coordinateX.both + coordinateY;
return temp1;
}

friend point operator -(const point& coordinateX, const point&
coordinateY)

}
point temp2;
temp2.both = coordinateX.both - coordinateY;
return temp2;
}
friend bool operator == (const point& coordinateX, const point&
coordinateY)

{
return(point.coordinateX = = point.coordinateY);
}
return 0;
}

Apr 24 '06 #1
Share this Question
Share on Google+
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

<snip assignment> What I have:

#include <iostream>
//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&<<endl;
what is temp1 ??
If you have not defined namespae std then you need to associate cout /
cin with std namespace.

cout<<"The difference between coordinateX and coordinateY is
"<<temp2<<endl;

if (coordinateX = coordinateY)
{
cout<<"X and Y are equal to eachother."<<endl;

else
cout<<"X and Y are not equal to eachother"<<endl;
}
<snip friend functions defined inside main>

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 <iostream>
//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); <snip>
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

}
<snip>

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 <iostream>
//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&<<endl;

cout<<"The difference between coordinateX and coordinateY is
"<<temp2<<endl;

if (coordinateX = coordinateY)
{
cout<<"X and Y are equal to eachother."<<endl;

else
cout<<"X and Y are not equal to eachother"<<endl;
}
Problem 11: indentation.

Problem 9: Tries to solve something not asked for, and not helping in
what's been asked for.

friend point operator +(const point& coordinateX, const point&
coordinateY)

{
point temp1;
temp1.both = coordinateX.both + coordinateY;
return temp1;
}


Problem 12: local function definitions not supported by C++.

Problem 13: "both" is nowhere defined.

Problem 14: adding two different kinds of beasts.

Problem 15: assigning directly to data member of object.
Uh, I stop there -- snipped rest of code.

But essentially, if I were you I'd concentrate solely on part [A],
/ignoring/ [b] through [i] for the moment.

TRY TO GET PART [A] TO WORK, WITH NONE OF THE REST.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 24 '06 #4

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
<snip assignment>
#include <iostream>
//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);

<snip>
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

}
<snip>

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 <al***@start.no> wrote:
* Lost Student:
if (coordinateX = coordinateY)
{
cout<<"X and Y are equal to eachother."<<endl;

else
cout<<"X and Y are not equal to eachother"<<endl;
}


Problem 11: indentation.

Problem 9: Tries to solve something not asked for, and not helping in
what's been asked for.


(Another) Problem: Assignment instead of equality comparison in the 'if'
statement.

(Another + 1) Problem: Mismatched 'else' in the 'if' statement.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Apr 24 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.