473,395 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Very lost

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
6 1655

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
"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
* 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

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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Allcomp | last post by:
Hello, I have seen something really strange in VB6 If I do a Int ( (5 * 1.2)) , I receive the value 5, but I should receive 6? Is this a bug or something really "normal". I can see that if I...
8
by: Ondine | last post by:
Hi I have a client running an Access 2000 database on a small network with 3 pc's. Two of the laptop pcs have a data replica, which they use when not connected to the network, the 'server'...
6
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence...
2
by: michele | last post by:
Hi, i'm using a datagrid control in my webform, with AutoGenerateColumns=false; it work very well if EnableViewState=true; including paging and sorting, but this cause a big performance issue, so...
0
by: Nick | last post by:
I have a situation wherein i have a text box, that has abc() being executed whenever the lost focus event is triggered. However, there is an exceptional case where, if a button click causes the...
9
by: Elliot Rodriguez | last post by:
WinXP Pro Let me preface this by saying I have developed with the .NET IDE since its release, and I consider myself reasonably savvy with it. I have a medium sized form with about 120 controls...
2
by: maxkumar | last post by:
Hi, I am running a ASP.NET 1.1 site on Win Server 2003 with IIS 6.0. The website has been running for about 1.5 years now. In the past, we used to have random cases of session variables getting...
2
by: trickyidiot | last post by:
I don't even know if this is possible - I'm still fairly new to stored procedures. I have 4 tables I need to compile data from: saved_searches saved_homes printed_homes member_info ...
56
by: mdh | last post by:
As I begin to write more little programs without the help of the exercises, little things pop up that I need to understand more fully. Thus, below, and although this is not the exact code, the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.