8
C++ . . operator. operatorX, X . operator, , .
, :
: + ! * ++ -- ~ &
: + / * % << >> & ^ | = > ||! = += = /= *= %= <<= >>= &= ^= |= == >* &&.
: () [ ] new delete
: ..*::?: # ##
, : .
, , operator+, , operator+=. , . , . :
Dot A('A'), B('B'), C('C');
C=A+B; //
C=A.operator+(B); //
, new delete, :
- ;
;
;
, ;
, .
, :
;
;
;
=, (), [ ] > - .
- . :
;
;
( ).
, . :
;
.
( ).
|
|
new delete .
, . , . , , .
. - , . , -, :
<FuncType> operatorX();
<FuncType> ; X .
, . , .
, +,! ~ . : , . .
Dot.h
class Dot //
{
const char name; //
double x, y; //
public:
Dot (char Name): name (Name) { x = 0; y = 0;}
Dot (char Name, double X, double Y): name (Name) { x = X; y = Y; }
Dot & operator ~ (); //
Dot & operator + (); //
double & operator! (); //
};
Dot.cpp
#include Dot.h
// ,
Dot & Dot:: operator ~ ()
{
char S [ ] = " ";
CharToOem (S, S);
cout<<S<<name<<"\tx = "<<x<<"\ty = "<<y<<'\n';
return *this; //
}
// ,
Dot & Dot:: operator + ()
{
char S [ ] = " ";
CharToOem (S, S);
cout<<S<<name<<'\n';
cout<<"\tx = "; cin>>x;
cout<<"\ty = "; cin>>y;
return *this; //
}
//
double Dot:: operator! ()
{
return sqrt (x*x + y*y);
}
Main.cpp
#include Dot.h
Void main ()
{
Dot A ('A'); // Dot (char Name)
double d =!~+A; // "+" -
// "~" -
// "!" -
char S [ ] = " = ";
CharToOem (S, S); cout<<S<<d<<'\n';
}
:
A
x = 3
y = 4
A x = 3 y = 4
|
|
= 5
, :
friend <FuncType> operator X (<Type> <Par>);
<FuncType> ;
X ;
<Type> ;
<Par> .
, , :
friend Dot & operator ~ (Dot & D); // " "
friend Dot & operator + (Dot & D); // ""
friend double operator! (Dot & D); // " "
:
Dot & operator ~ (Dot & D)
{
char S [ ] = " "; CharToOem (S, S);
cout<<S<<A.name<<"\tx = "<<D.x<<"\ty = "<<D.y<<'\n';
Return D;
}
Dot & operator + (Dot & D)
{
char S [ ] = " "; CharToOem (S, S);
cout<<S<<D.name<<'\n';
cout<<"\tx = "; cin>>D.x; cout<<"\ty = "; cin>>D.y;
Return D;
}
double operator! (Dot & D)
{
return sqrt (D.x*D.x + D.y*D.y);
}
.
, :
(, );
;
, .
, , . , : , .
Dot , name, . .
, , . , ( ), . , this. , , :
D1 = D2 = D3;
, :
Dot.h
class Dot //
{
public:
Dot& operator = (Dot & R); //
};
Dot.cpp
#include Dot.h
//
Dot & Dot:: operator = (Dot & R)
{
if (this == &R) return *this;
x = R.x; y = R.y;
return *this;
}
, .
D = D;
, , :
Coord D, *pD; D = *D;
, .
, : , , .
|
|
operator = ()
if (this = &R) return *this;
R , .
-, :
<FuncType> operator X (<Type> <Par>);
, . , , this.
. , .
, - . , , Dot:
Dot.h
class Dot //
{
public:
Dot operator + (Dot & D); //
Dot operator - (Dot & D); //
Dot operator / (double d); //
Dot operator * (double d); //
};
Dot.cpp
#include Dot.h
//
Dot Dot:: operator + (Dot & D) //
{
Dot T ('T', x + D.x, y + D.y); //
return T; //
}
Dot Dot:: operator - (Dot & D) //
{
Dot T ('T', x - D.x, y - D.y);
Return T;
}
Dot Dot:: operator / (double d) //
{
Dot T ('T', x / d, y / d);
Return T;
}
Dot Dot:: operator * (double d) //
{
Dot T ('T', x * d, y * d);
Return T;
}
Main.cpp
#include Dot.h
Void main ()
{
Dot A ('A'), B ('B'), C ('C'), D ('D'), O ('O'); //
~ (D = (+B + +A) / 2); // A B ,
// D
//
~ (O = (+C + D * 2) / 3); // ,
//
//
double d =! (B A); //
char S [ ] =" = "; CharToOem (S, S);
cout<<S<<d<<'\n';
}
, . :