Using a class variable with another class

Im stuck on using assigning a new variable with the value of another variable which is declared in a different class. For instance, the two values that I would like to trasnfer over are declared in parameters such as: Circle new=Circle(6.0, 5.0, 2.0);

Now, i would like to transfer over the 5.0 as a x value and 2.0 as a y value to calculate distance however im unsure on just how to get the parameter values over to another file, point.cpp. The variables x and y are set in point.h so those are no issue. It's only for x1 and y1



Would i pass by reference or point at? Thank you in advanced for your help!

1
2
3
4
5
6
7
8
9
10
11
12
13




  double Point::Distance(Point)     // code in point.cpp
    {
        x1 = Circle::c1.xcenter;
        y1 = ;
        x2 = x;
        y2 = y;
        double Distance = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
        return Distance;
    }
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <cmath>

struct point { double x = 0 ; double y = 0 ; };

double distance( point a, point b )
{
    const double dx = a.x - b.x ;
    const double dy = a.y - b.y ;
    return std::sqrt( dx*dx + dy*dy ) ;
}

struct circle
{
    double radius = 1 ;
    point centre ;

    circle( double r, double x, double y ) : radius{r}, centre{x,y} {}
};

double distance_between_centres( circle a, circle b )
{ return distance( a.centre, b.centre ) ; }

int main()
{
    const circle circle_a( 6.0, 5.0, 2.0 );
    const circle circle_b( 2.0, 5.8, 3.5 );
    std::cout << "distance between centres: "
              << distance_between_centres( circle_a, circle_b ) << '\n' ;
}

http://coliru.stacked-crooked.com/a/6cda2155d4384ec8
Much appreciated!! Thank you!
Topic archived. No new replies allowed.