Trouble with Class | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Trouble with Class

Does anyone have more resources for learning about class? Having a hard time understanding. Also I'm trying to display the distance between two points. Can someone explain what I'm doing wrong?

12th Dec 2018, 8:34 PM
Krista Clark
Krista Clark - avatar
3 Answers
+ 3
Line 17: double getX(); // <--- no arg Line 20: double getY(); // <--- no arg Line 29 add this: double length(); // separate private member Line 71: double Point::getX() { // removed arg return (x); } Line 75: double Point::getY() { // removed arg return (y); } Line 88: void Segment::displaySegment() { cout << "Point A: "; A.display(); cout << "Point B: "; B.display(); cout << endl; cout << "Length of the segment is: " << length() << endl; // <---- } Add this at the end of the code: double Segment::length() { // separate private member //Here is my trouble return sqrt(pow(B.getX() - A.getX(), 2.0) + pow(B.getY() - A.getY(), 2.0)); // returning the distance between the 2 points } Output: Point A: (3,4) Point B: (5,6) Length of the segment is: 2.82843 _________ https://code.sololearn.com/cu46FhYQiYu6#cpp
12th Dec 2018, 9:01 PM
Babak
Babak - avatar
+ 3
Because getters only `return` things which means there's no need to pass to them additional information. On the other hand, setters' sole responsibility is setting the private data members using input information through passing arguments to their parameters. So, to summarize Setter general form: void setterName(T param); Getter general form: T getterName();
12th Dec 2018, 9:15 PM
Babak
Babak - avatar
+ 1
Thank you. Can you explain why the arg is removed?
12th Dec 2018, 9:07 PM
Krista Clark
Krista Clark - avatar