简体   繁体   中英

composition of classes in c++

This is an example of the composition of classes. But my code doesn't show a correct answer. This program must calculate the length of the line segment in the coordinates of the two ends of the line segment. I don't know what to do in the main function. this code consists of two classes.

#include <cmath>
#include <iostream>
using namespace std;

class point {
 private:
  float x;
  float y;

 public:
  void setpoint(float abscissa, float ordinate) {
    x = abscissa;
    y = ordinate;
  }
  float getx() { return x; }

  float gety() { return y; }
};

class LineSegment {
 public:
  float length() {
    result = sqrt(pow(a.getx() - b.getx(), 2) + pow(a.gety() - b.gety(), 2));
    return result;
  }

  void displayMessage() { cout << result; }

 private:
  float result;
  point a;
  point b;
};

int main() {
  point a;
  float q, s;
  cout << "Enter two numbers for first point:\n";
  cin >> q >> s;
  a.setpoint(q, s);
  point b;
  float e, r;
  cout << "Enter two numbers for second point:\n";
  cin >> e >> r;
  a.getx();
  a.gety();
  LineSegment pt;
  pt.length();
  pt.displayMessage();

  return 0;
}

enter image description here

The a and b local variables of your main() function are unrelated to the a and b members of class LineSegment . You should give LineSegment a constructor by which you can convey the endpoints to it, or at minimum provide a method or methods by which the endpoints can be set after the fact. You must not attempt to compute the segment length before its endpoints are set.

Neither the member a nor the member b are initialized for pt . You need to initialize them or they are initialized using the default constructor of point which happens to not do any initialization resulting in undefined behavior.

You could eg pass the point s to the constructor of LineSegment to fix this:

class LineSegment {
public:
    LineSegment(const point& p1, const point& p2)
        : a(p1), b(p2)
    {}
...
};

...

LineSegment pt {a, b};

...

Note: I recommend adding a prefix to member variables. m_ is one option (ie you'd use m_a and m_b as member variable names). This way you avoid confusion like this and also avoid shadowing of variables assuming this is the only kind of variable using this prefix.


Edit: You also never call setpoint on b in main ; you need to do this before passing the points to the constructor in the above snippet.

Here is your code, touched up to work:

#include <cmath>
#include <iostream>
// using namespace std;  // CHANGED: Bad practice

class point {
 private:
  float x = 0.0f;  // CHANGED: Add default member initialization
  float y = 0.0f;

 public:
  point() = default;  // CHANGED: Add default constructor
  point(int abscissa, int ordinate) : x(abscissa), y(ordinate) {}  // ADDED
  void setpoint(float abscissa, float ordinate) {
    x = abscissa;
    y = ordinate;
  }
  float getx() const { return x; }  // CHANGED: Mark getters as const

  float gety() const { return y; }
};

class LineSegment {
 public:
  // CHANGED: Add constructor so you can actually build a LineSegment
  LineSegment(point one, point two) : a(one), b(two) {}
  // CHANGED: Made a one-liner
  float length() const {
    return sqrt(pow(a.getx() - b.getx(), 2) + pow(a.gety() - b.gety(), 2));
  }

  // void displayMessage() const { std::cout << result; }  // CHANGED: Not
  // needed

 private:
  // float result;  // CHANGED: Does not need to stored
  point a;
  point b;
};

int main() {
  float q, s;
  std::cout << "Enter two numbers for first point:\n";
  std::cin >> q >> s;
  point a(q, s);  // Can now directly initialize

  float e, r;
  std::cout << "Enter two numbers for second point:\n";
  std::cin >> e >> r;
  point b(e, r);  // CHANGED: Actually put values into b
  // a.getx();  // CHANGED: These do nothing
  // a.gety();
  LineSegment pt(a, b);  // CHANGED: Actually put data into pt
  std::cout << "\nLine Length: " << pt.length() << '\n';  // CHANGED: Make use
                                                          // of functions now
                                                          // available

  return 0;
}

Your biggest issues were not initializing your objects correctly. point b was never given the values e and r , and LineSegment pt was never given any point s.

Making those small changes and your code works as expected. Just using a simple example of (0, 0) and (1, 1) provides output of 1.41421 , which is root-2, which is correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM