Home > Back-end >  Class c javascript: void (0);
Class c javascript: void (0);

Time:09-22

//ordinary functions as a class (or more) a friend function
# include
# include
using namespace std;

//the point class definition and implementation of
The class point1
{
Private:
Double x, y;//private variables
Public:
Point1 (double x=0.0, double y=0.0).//declare the constructor
Void disp ();//declare output function
Friend double short (point1 point1, p1, p2);//declare the function point's friend yuan (and not point class member function, but you can call the class members of the private or public)
};

Point1: : point1 (double x, double y)//define constructors,
* note have scope symbols{
This - & gt; X=x; This - & gt; Y=y;
}

Void point1: : disp ()//define the output function, pay attention to have scope symbol *
{
cout<" Point (" & lt; & lt; x<" , "& lt; & lt; Y<" ) ";
}


//the definition of linear class
The class line
{
Private:
Double a, b, c;
Public:
The line (double a=0.0, double b=0.0, double c=0.0).
Void disp ();
Friend double short (point1 p, line (l);
Friend double short (p1 line, line p2);


};

Line: line (double a, b double, double c)
{
This - & gt; A=a; This - & gt; b=b; This - & gt; C=c;
}

Void line: : disp ()
{
cout<" Line: "& lt; & lt; a<" X + "& lt; & lt; B<" Y + "& lt; & lt; C<"=0 ";
}
Double short (point1 point1, p1, p2)//define a friend function, without the scope of symbols
{
Return SQRT ((p1) X-ray p2) x) * (p1) X-ray p2) x) + (p1) y - p2) y) * (p1) y - p2) y));//a friend function can directly access all objects of the class members of the private or public
}

Double short (point1, p line (l)
{
Return fabs ((L.A. + p.y p.x * * l.b + l.c)/SQRT (L.A. + l.b L.A. * * l.b));
}

//the main function

Int main ()
{
Point1 p1 (0.0, 0.0);
Point1 p2 (2.0, 1.0);
The line a1 (1.0, 2.0, 3.0);
The line a2 (2.0, 3.0, 1.0);
P1. Disp ();
P2. Disp ();
A1. Disp ();
A2. Disp ();
cout<" Point to the straight line distance is: "& lt; & lt; Short (p1, a2) & lt; & lt; endl;
cout<" The distance between two points is: "& lt; & lt; Short (p1, p2) & lt; & lt; endl;
return 0;
}

Would you please tell me what the problem, my code is compiled displayed when the within this context,?????


CodePudding user response:

A basic concept, the wrong in your function
Double short (point1, p line (l)
{
Return fabs ((L.A. + p.y p.x * * l.b + l.c)/SQRT (L.A. + l.b L.A. * * l.b));
}
,
P.x, p.y are private variables, cannot directly access the
Either you put this function in a class, or to define the x, y for public
Of course, you can also define two public function to get the x, y

CodePudding user response:

Double short (point1 point1, p1, p2) access private members can be point1,
But double short (point1 p, line (l) access private members are not point1, because it is the line of friend functions, not point1 friend function,
The solution:
class line.
The class point1
{
Private:
Double x, y;//private variables
Public:
Point1 (double x=0.0, double y=0.0).//declare the constructor
Void disp ();//declare output function
Friend double short (point1 point1, p1, p2);//declare the function point's friend yuan (and not point class member function, but you can call the class members of the private or public)
friend double short (point1 p, line (l);
};
  • Related