Augustus Flynn
Augustus Flynn

Reputation: 45

How to pass class as a variable

I have class Point, and i'm coding Circle. Then how to pass class as variable

int main() {
   Point p1(0, 0), p2(5, 8);
   Circle c1(p1, 4), c2(p2, 3);
   int sgd = c1.getIntersection(c2);
   cout<<"intersection: "<<sgd<<endl;
}

this is class Ponit

class Point {
  private: 
    int _x;
    int _y;
  public:
    Point() {
      _x = 0;
      _y = 0;
    }

    Point(int x, int y) {
      _x = x;
      _y = y;
      return Point(_x, _y);
    }
};

Upvotes: 0

Views: 250

Answers (2)

Augustus Flynn
Augustus Flynn

Reputation: 45

I found solution for it:

class Point {
    public:
      int _x;
      int _y;
}

and in class Circle

Circle(Point x, int s);

Upvotes: 0

at this point the problem is located in the class circle, you need to modify it so it can take a point as parameter:

class Circle
...public:

    Circle(const Point& P, int s);

after that you can pass objects/instances of the class point like

Circle c1(p1, 4), c2(p2, 3);

this approach will require that the Circle class "knows" about a Point class, this is called dependencies since the Circle depends on the Point...

you can as work-around, define a Circle constructor with the 2 params too

Circle(int x, int y, int s);

Upvotes: 1

Related Questions