I just learned to use constructors. I wrote the code given below to print distance between two points but it throws a lot of errors. Please guide me

I just learned to use constructors. I wrote the code given below to print distance between two points but it throws a lot of errors. Please guide me..

#include <iostream>
#include <math.h>
using namespace std;

class distance
{
     int a, b;
public:
    int c, d;

    friend void formula(distance, distance);

    distance(int a1, int b1)
    {
        a = a1;
        b = b1;
    }
 };

void formula(distance o1, distance o2)
{
    c = o2.a - o1.a;
    d = o2.b - o1.b;
    cout<<"The distance is "<<sqrt((c*c)+(d*d)<<endl;
}

int main()
{
    distance c1(1, 2), c2(3, 4);
    formula(c1, c2);
    return 0;
} 

1 answer

  • answered 2021-05-15 18:25 Natan Ytzhaki

    In the function formula c and d are not defined. this function would know about them if you write

    o1.c = ...
    o1.d = ...