473,385 Members | 1,780 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,385 software developers and data experts.

class to hold 2 double values representing the x and y coordinates

254 100+
i have a question from my paper, which i dont have the correct answer, so here i asking anyone who can help, provide me the appropriate answer.
-----------------------------------
Write a class called Point that holds two double values representing the x and y coordinates of a point on graph. Provide the following:

1. A suitable default constructor that can also take one or both of the 2 values as arguments. Default values should be 0.

2. Member function get_x and get_y to get each of the values.

3. Memeber function set_x and set_y to set each of the values.

4. An overloaded output operator to output the Point in the format:
(1.2, 3.4)

5. A member function "length" to return the distance of the point from the origin, i.e.(0,0). The formula is the sqrt(x2 + y2). (2 is the supercase to x and y)
-----------------------------

Here is my code that i done so far, but i could finish the function length(), and i'm not sure everything i coded is correct. So please help me.....
Expand|Select|Wrap|Line Numbers
  1. class Point {
  2. public:
  3.         Point(double x = 0, double y = 0) : x(x), y(y) {}
  4.  
  5.         double get_x() {
  6.                 return x;
  7.         }
  8.         double get_y() {
  9.                 return y;
  10.         }
  11.         void set_x(double xx) {
  12.                 x = xx;
  13.         }
  14.         void set_y(double yy) {
  15.                y = yy;
  16.         }
  17.         std::ostream& operator<< (std::ostream& os, const Point& p) {
  18.                  os << p.x << ", " << p.y;
  19.                  return os;
  20.         }
  21.         Point length( ) {
  22.                  // do something here
  23.                  return something;
  24.         }
  25. private:
  26.         double x;
  27.         double y;
  28. };
  29.  
Feb 7 '07 #1
19 2765
RedSon
5,000 Expert 4TB
The instructor has provided you with the formula for calculating the distance. If you assume that you have valid input all you have to do is take the y coordinates of "this" point and another point and use the formula to return the result.
Feb 7 '07 #2
RedSon
5,000 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. class Point {
  2. public:
  3.         Point(double x = 0, double y = 0) : x(x), y(y) {}
  4.  
  5.         double get_x() {
  6.                 return x;
  7.         }
  8.         double get_y() {
  9.                 return y;
  10.         }
  11.         void set_x(double xx) {
  12.                 x = xx;
  13.         }
  14.         void set_y(double yy) {
  15.                y = yy;
  16.         }
  17.         std::ostream& operator<< (std::ostream& os, const Point& p) {
  18.                  os << p.x << ", " << p.y;
  19.                  return os;
  20.         }
  21.         Point length( ) {
  22.                  // do something here
  23.                  return something;
  24.         }
  25. private:
  26.         double x;
  27.         double y;
  28. };
  29.  
I'll write the method signature for you:


Expand|Select|Wrap|Line Numbers
  1. Point length(Point anotherPoint) 
  2. {
  3.    //calculate using the formula
  4.    return the_result;
  5. }
  6.  
Feb 7 '07 #3
horace1
1,510 Expert 1GB
if function "length" is to return the distance of the point from the origin, i.e.(0,0) it should return a double not a Point??, e.g.
Expand|Select|Wrap|Line Numbers
  1.        double length( ) {     // ** return double??
  2.                  return sqrt( ????   );
  3. }
  4.  
also I think the operator<<
Expand|Select|Wrap|Line Numbers
  1.        friend std::ostream  &operator<<(std::ostream& os, const Point& p) {
  2.  
should be a friend if declared within the class otherwise the compiler could think it is the left shift operator<<() function
Feb 7 '07 #4
RedSon
5,000 Expert 4TB
Oh yea horace is right, it should return double not Point. But the arguments passed to the length function should be the same.

That was one of my "senior" moments.

Thats what i get for copying and pasting code.
Feb 7 '07 #5
nickyeng
254 100+
so it is like this:
Expand|Select|Wrap|Line Numbers
  1. double length() {
  2.        return sqrt(x2 + y2);
  3. }
  4.  
?

how about other member function i done , were they correct according to the question?
Feb 7 '07 #6
Ganon11
3,652 Expert 2GB
so it is like this:
Expand|Select|Wrap|Line Numbers
  1. double length() {
  2.        return sqrt(x2 + y2);
  3. }
  4.  
?

how about other member function i done , were they correct according to the question?
The formula for the distance function actually requires you to square x and y - this was the meaning of the 2s in the original writing, they just weren't superscripted. The formula should be:

Expand|Select|Wrap|Line Numbers
  1. return sqrt(x * x + y * y);
Feb 7 '07 #7
nickyeng
254 100+
oh yeah.
thanks for reminding me, Ganon11.
Feb 8 '07 #8
RedSon
5,000 Expert 4TB
so it is like this:
Expand|Select|Wrap|Line Numbers
  1. double length() {
  2.        return sqrt(x2 + y2);
  3. }
  4.  
?

how about other member function i done , were they correct according to the question?
Everything else looks to be kosher, but you should really run and test your app yourself.
Feb 8 '07 #9
nickyeng
254 100+
Everything else looks to be kosher, but you should really run and test your app yourself.
its a paper question, not real app.
there is no point to compile it and give me no error. Cos i dont know what my teacher's code would be.

thanks for the help tho.
Feb 8 '07 #10
nickyeng
254 100+
Here i have next question, related to question above i think.
----------------
Now write a class called Vector (no relationship with an STL vector) that comprises a Point and 2 strings begin the names of the graph axes. Do not use inheritance in this case. Provide the following:

1. A suitable default constructor with 2 additional argumnets for these names. Default them to "x" and "y" respectively.

2. Do not allow the names of the axes to be changed after a Vector thas been created.

3. An output operator overload to output the vector in the format:
1.2x + 3.4y

4. An operator to add together 2 Vectors returning a third Vector. Raise an exception if the 2 Vectors have different axis names.
----------------------------------------------------------

Now this time i stuck here with "...comprises a Point and ....". What does it means to you?

I know those question should be easy but sometimes it does confusing ppl with unclear definition or explanation.

any ideas?
(i think it should similar to the code i posted before.)
Feb 8 '07 #11
RedSon
5,000 Expert 4TB

Now this time i stuck here with "...comprises a Point and ....". What does it means to you?

I know those question should be easy but sometimes it does confusing ppl with unclear definition or explanation.

any ideas?
(i think it should similar to the code i posted before.)
Its talking about designing a class that has as its member data (comprises a...) Point and whatever else it says.
Feb 8 '07 #12
nickyeng
254 100+
does my code correct as following:
Expand|Select|Wrap|Line Numbers
  1. class Vector {
  2. public:
  3.     Vector(string x = "x", string x = "y"): x(x), y(y) {}
  4.  
  5.     friend std::ostream& operator<< (std::ostream& os, const Vector& v) {
  6.         os << v.x << "x + " << v.y << "y";
  7.         return os;
  8.     }
  9.  
  10.     inline Vector operator+ (const Vector& v1, const Vector& v2) {
  11.         Vector v3 = v1;
  12.         return v3 += v2;
  13.     }
  14. private:
  15.     const string x;
  16.     const string y;
  17. };
  18.  
i am not sure about question (2), is it member data like string x, string y, make them as const member so that it cannot be changed?

how about other member function i wrote above. Are they correct according to questions ?
Feb 8 '07 #13
RedSon
5,000 Expert 4TB
First you don't include a Point in your Vector and second, I don't think your + operation will do what you are expecting it to do. You will have to debug that operation *very* carefully.

Also, in the future feel free to start a new thread for any new questions. Bits are fully recyclable, environmentally friendly and cheap!
Feb 8 '07 #14
nickyeng
254 100+
First you don't include a Point in your Vector and second, I don't think your + operation will do what you are expecting it to do. You will have to debug that operation *very* carefully.

Also, in the future feel free to start a new thread for any new questions. Bits are fully recyclable, environmentally friendly and cheap!
Redson,
i'm not completely new to here, so i know when to start a new thread for any new questions. You have to see, my 2 questions in this thread is related, that's why i posted it continueously.

If they are unrelated, sure i will help this forum to get another post for C++ forum. :-)

thanks for your tips tho.

happy holiday
Feb 9 '07 #15
horace1
1,510 Expert 1GB
Here i have next question, related to question above i think.
----------------


Now this time i stuck here with "...comprises a Point and ....". What does it means to you?

I know those question should be easy but sometimes it does confusing ppl with unclear definition or explanation.

any ideas?
(i think it should similar to the code i posted before.)
I would have thought a vector would have two Points for the vector start and end or does Vector mean something else in this context?
Feb 9 '07 #16
nickyeng
254 100+
I would have thought a vector would have two Points for the vector start and end or does Vector mean something else in this context?
vector class is unrelated to STL vector. It just a simple class.

actually the answer wont be the compiled-well code. What to do is just write the code for each questions. Forget it about compiled-well mind..!

i'm not sure my code is correct, and there is no other ppl here can provide me with the better one, so i just follow my code.

thanks for the help anyway.
Feb 9 '07 #17
horace1
1,510 Expert 1GB
vector class is unrelated to STL vector. It just a simple class.

thanks for the help anyway.
I am not talking about STL vectors but about a vector in 2D coordinate space - one would have a x1, y1 start Point and a x2, y2 end Point
I am not sure what you mean by a vector?
Feb 9 '07 #18
nickyeng
254 100+
I am not talking about STL vectors but about a vector in 2D coordinate space - one would have a x1, y1 start Point and a x2, y2 end Point
I am note what you mean by a vector?
no idea. cos teacher wont say and i have the question only.

forget about this question.


End of thread.
Feb 9 '07 #19
RedSon
5,000 Expert 4TB
Vectors in the mathematical sense have an origin (x,y) a direction (maybe a slope or something to that effect) and a magnitude. You can give the origin and another point in the line of the vector and a magnitude but you can choose any other point on the vector line. Vectors don't have end points, they are rays of a certain magnitude.
Feb 9 '07 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Krzysztof Stachlewski | last post by:
I tried to run the following piece of code: Python 2.3.4 (#53, May 25 2004, 21:17:02) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> o = object() >>> o.a...
8
by: SpOiLeR | last post by:
Hello! I have a matrix class like this: class MyObject; // MyMatrix is contains MyObjects class MyMatrix { public: ...
11
by: sw | last post by:
Hi, Is it possible to insert a class <vec> which has a vector<double> member, into the vector<vec> veclist for e.g? I've been getting compilation errors when trying to insert using the vector...
1
by: John | last post by:
Hi, Maybe someone can help me with the following: "The first task by any derived class constructor is to call it’s direct or indirect base class constructor implicitly or explicitly", reads the...
3
by: Little | last post by:
Could someone help me get started on this program or where to look to get information, I am not sure how to put things together. 1. Create 4 double linked lists as follows: (a) A double linked...
1
by: Little | last post by:
Could someone help me figure out how to put my project together. I can't get my mind wrapped around the creation of the 4 double Linked Lists. Thank your for your insight. 1. Create 4 double...
1
by: Little | last post by:
Hello everyone. I am trying to do the following program and am unable to get the beginning portion to work correctly. The scanner works when I print the statements without the double linked list...
14
by: Peter Hallett | last post by:
I would like to set up a string array as a class member, or field, and then populate this array by reading in from a text file, but I cannot find the appropriate syntax. The getter and setter are...
26
by: Alexzive | last post by:
Hello there :) , I am a python newbie and need to run following code for a task in an external simulation programm called "Abaqus" which makes use of python to access the mesh (ensamble of nodes...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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.