Good day, I've realized hierarchy of classes: Point -> LineSegment.
It's necessary to realize the following methods of the class: moving, stretching, rotating, turning, change on an axis. I'm trying to realize moving at a some distance on this code . What should I correct for in my code that it works?

#include <iostream.h>
#include <stdlib.h>
#include <cmath>
#include <conio.h>

// Classes
class Point
{
private:
  // Members
  double x;
  double y;

public:
  // Constructor
  Point (double _x, double _y) : x(_x), y(_y) {}
};

class LineSegment
{
private:
  // Members
  Point p1; 
  Point p2; 

public:
  // Constructor
  LineSegment (Point &_p1, Point &_p2) // Create a line from 2 points;
  : p1(_p1), p2(_p2)
  { }

  bool (LineSegment &ls) {
    int dist;
    ls.p1 = ls.p1 + dist;
    ls.p2 = ls.p2 + dist;
  }
  return true;
};

int main()
{
  Point A(1, 4), B(2, 3);
  LineSegment t1(A,B);

  cout << "Class hierarchy: Point -> Segment";

  return 0;
}

[C++ Error] ClassMethods.cpp(32): E2293 ) expected

Dani AI

Generated

A few concrete notes tied to the thread: the syntax/brace mistake that pointed out was one problem; the compiler error about operator+ stems from trying to add an int to a Point when no such overload exists (as suggested); and the dist variable in the posted code is uninitialized, which is undefined behavior. A more robust design is to give Point clear operations (translate, add/subtract points, rotate) and implement LineSegment operations as member functions that call Point operations.

Example Point helpers (different from the original post):

struct Point {
    double x = 0.0, y = 0.0;
    Point() = default;
    Point(double xx, double yy) : x(xx), y(yy) {}
    Point& operator+=(const Point& o) { x += o.x; y += o.y; return *this; }
    Point operator+(const Point& o) const { return Point(x + o.x, y + o.y); }
    Point operator-(const Point& o) const { return Point(x - o.x, y - o.y); }
    void translate(double dx, double dy) { x += dx; y += dy; }
    Point rotated(double theta) const {
        double c = std::cos(theta), s = std::sin(theta);
        return Point(x*c - y*s, x*s + y*c);
    }
};

Then LineSegment methods become straightforward, e.g. translate both endpoints, rotate about a center, or translate by a distance+angle:

class LineSegment {
    Point p1, p2;
public:
    LineSegment(const Point& a, const Point& b) : p1(a), p2(b) {}
    void translate(double dx, double dy) { p1.translate(dx,dy); p2.translate(dx,dy); }
    void translateByDistance(double dist, double angleRad) {
        double dx = dist * std::cos(angleRad);
        double dy = dist * std::sin(angleRad);
        translate(dx, dy);
    }
    void rotate(double angleRad, const Point& center = Point(0,0)) {
        p1 = (p1 - center).rotated(angleRad) + center;
        p2 = (p2 - center).rotated(angleRad) + center;
    }
};

Practical cautions: initialize variables (no uninitialized dist), prefer <iostream> and std::cout (avoid old <iostream.h>), use radians with std::sin/std::cos or convert degrees explicitly, and choose method names that reflect intent (translate, rotate) instead of overloading scalars unless the operation is well-defined. This approach keeps the implementation clear, type-safe, and easy to extend for scaling, reflecting, or rotating about arbitrary axes.

Recommended Answers

All 4 Replies

Well, this function:

bool (LineSegment &ls) {
    int dist;
    ls.p1 = ls.p1 + dist;
    ls.p2 = ls.p2 + dist;
  }

If you look at a method, in terms of some pseudocode:

function [type] [name] (parameters)
[
//... implementation

return [typy]

]

Therefore, you can think of it like this:

bool method_name (LineSegment &ls){

  //... implementation 

  return true; 
}

Just remember where to put your return, in your code, it's in the wrong place. Needs to be in the brackets.

Oh.. Thanks! I'm sitting about it for a long time and haven't noticed. After this correction

bool moving(LineSegment &ls) {
int dist;
  ls.p1 = ls.p1 + dist;
  ls.p2 = ls.p2 + dist;
return true;
}

there are following errors appeared:
[C++ Error] ClassMethods.cpp(35): E2094 'operator+' not implemented in type 'Point' for arguments of type 'int'
[C++ Error] ClassMethods.cpp(36): E2094 'operator+' not implemented in type 'Point' for arguments of type 'int'

I've tried to use friends overloading inside the class LineSegment

friend LineSegment operator+ ( const Point &, const Point & );

but it's not change nothing. What should I change for in my code?

You're trying to add an integer to a "point"-class. As this is not standard c++, the compiler is complaining that you should implement a function to teach it how to do so. If operator-overloading sounds a bit to scary for now, you could also choose to implement a addInteger member function in your point class. Something like:

class Point
{
private:
    // Members
    double x;
    double y;
public:
    // Constructor
    Point (double _x, double _y) : x(_x), y(_y) {}
    addInteger(int _i) { // do stuff }
};

Thanks for that, Nick.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.