struct Node{ int x; int y; string label; Node()=default; Node(int i, int j, stri
ID: 3803898 • Letter: S
Question
struct Node{ int x;
int y;
string label;
Node()=default;
Node(int i, int j, string l) : x(i), y(j), label(l) { } ;
string to_string () const;
bool equal_nodes(const Node&); double distance(const Node &)const;
};
-------- Node Function that needs to be programmed is down below!!!! ---------
Your job is to complete the underlined methods above (all methods, no functions) in the two structs.
----- Node function
string to_string () const; Converts a Node to a string. The const at the end means the method is guaranteed not to change the variable pointed to by this.
Explanation / Answer
The function definition for string to_string () const is as follows:
string to_string () const
{
string str="";
str+="Node information: ";
str+=" x: "+=x;
str+=" y: "+y;
str+=" Node is labelled as: "+label;
return str;
}
Description:
Here, you require toString() method of the Node structure. This method means you will write the node details as a string and will return it to the calling function. So, inside this method. to return the string variable, I first created a string variable as a empty string using "". Then I added the details to that string variable from this Node. Values of x, y and the label name is concated to the string using + operator. So, at the end, this string completely describes the Node and it is returned from this function. This is the required thing implemented.
Please comment if there is any query. Thank you. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.