x = 0
y = 0
B
x = 6
y = 0
D x = 3 y = 0
C
x = 0
y = 6
O (x = 2 y = 2)
= 6
. . , ; . , -. , , . , , . , . , :
D = A + B + C;
, :
~ (A + B);
~ , .
, , , . .
- . , .
Dot Dot:: operator * (double d), , :
D = A * 2;
-
D = 2 * A;
, , -. .
, :
friend <FuncType> operator X (<Type1> <Par1>, <Type2> <Par2>);
, , .
, , .
Dot. this. , - , .
|
|
Dot.h
class Dot //
{
public:
friend Dot operator * (double m, Dot & D); //
};
Dot.cpp
#include Dot.h
//
Dot operator * (double m, Dot & D)
{
Dot T ('T', D.x*m, D.y*m);
Return T;
}
X=
C++ , :
+= -= /= *= %= <<= >>= &= ^= |=
,
int A = 5; // A
A += 2; // A
. , , . , , .
, Vec, , . ( ) , . , , .
, Vec.
Vec.h
class Vec //
{
public:
Vec operator << (double f); //
Vec operator << (int f); //
Vec& operator <<= (int f); // c
};
Vec.h
#include "Vec.h"
// , f
Vec Vec:: operator << (double f)
{
Vec T ("T"); //
T.x = x*cos (f) - y*sin (f); //
T.y = x*sin (f) + y*cos (f); //
return T; //
}
// , f
Vec Vec:: operator << (int f)
{
Vec T ("T"); //
const double pi = 4 * atan (1); // π
double F = f * pi / 180.0; //
T = *this << F; // Vec:: operator << (double)
return T; //
}
// f
Vec& Vec:: operator <<= (int f)
{
Vec T ("T"); //
T = *this << f; // Vec:: operator << (int)
*this = T; //
return *this; //
}
Main.cpp
#include "Vec.h"
Void main ()
{
Vec V ("V", 1, 0),W ("W"); //
~ (W = V << 45); //
~ (W <<= 45); //
}
:
|
|
W x = 0.707107 y = 0.707107
W x = 5.55112e-017 y = 1
,
, , . , :
B = A + AB;
- .
, Dot. Vec. . . , :
DotVec.h
class Vec; //
class Dot //
{
public:
Dot operator + (Vec & V); //
};
class Vec //
{
char name [ 3 ]; //
double x, y; //
public:
Vec (char * pName) { strncpy (name, pName, 3); name [ 2 ] = '\0'; x = 0; y = 0; }
Vec (char * pName, double X, double Y)
{ strncpy (name, pName, 3); name [ 2 ] = '\0'; x = X; y = Y; }
Vec (char * pName, Dot A, Dot B);
inline double GetX () const { return x; }
inline double GetY () const { return y; }
};
DotVec.cpp
#include DotVec.h
// -
Dot Dot:: operator + (Vec &V)
{
Dot T ('T', x + V.GetX(), y + V.GetY());
Return T;
}
Dot Vec, GetX () GetY ().
. Dot Vec. .
DotVec.h
class Vec; //
class Dot //
{
public:
friend Dot operator + (Dot & D, Vec & V); //
};
class Vec //
{
public:
friend Dot operator + (Dot & D, Vec & V); //
};
DotVec.cpp
#include DotVec.h
//
Dot operator + (Dot & D, Vec & V)
{
Dot T ('T', D.x + V.x, D.y + V.y);
Return T;
}
, : . , :
, ;
int.
( ). , .
C++ ++ . , Vec, , , . 90 90 .
, Vec.
Vec.h
class Vec //
{
public:
Vec & operator ++ (); //
|
|
Vec operator ++ (int); //
};
Vec.cpp
#include Vec.h
// -
Vec & Vec:: operator ++ () //
{
double buff = x; // x y
x = -y; y = buffer; // buffer
return *this; //
}
Vec Vec:: operator ++ (int) //
{
Vec T = *this; //
//
++ *this; //
return T; //
}
Main.cpp
#include Vec.h
Void main ()
{
char S [ 30 ];
Vec A ("A", 3, 4), B ("B"); //
CharToOem ("\t\t \n", S); cout<<S;
~ (B = ++ A); ~ A; //
CharToOem ("\t\t \n", S); cout<<S;
~ (B = A ++); ~ A; //
}
:
B x = -4 y = 3
A x = -4 y = 3
B x = -4 y = 3
A x = -3 y = -4
, . , , .
, , , .
. , , , .
Vec.h
class Vec //
{
public:
friend Vec & operator ++ (Vec & D); //
friend Vec operator ++ (Vec & D, int); //
};
Vec.cpp
#include Vec.h
//
Vec & operator ++ (Vec & V) //
{
double buff = V.x; // V.x V.y
V.x = -V.y; V.y = buffer; // buffer
return V; //
}
Vec operator ++ (Vec & V, int) //
{
Vec T = V; //
//
++ V; //
return T; //
}
Main.cpp
#include Vec.h
Void main ()
{
Dot A ('A'), B ('B'), C ('C'), D ('D'); //
Vec AB ("AB", +A, +B); //
// AB
~ (D = A + ++AB); // 90,
// A ,
// D,
// D
~ (C = B + AB); // B ,
// C,
// C
}
:
|
|