- , :
+, -, *, /, %, ^, &, |, ~,!, =, <, >,+=, [ ], ->, (), new, delete.
- . - :
_ _::operator#(_){
/* */ }
operator#(a,b) a#b. # . >>, <<, -. , . -, , -, . - this. :
// +, = ++ coord. // 27
#include <iostream.h>
#include <conio.h>
class Coord {
int x, y; //
public:
Coord(int i=0, int j=0) { x = i; y = j; }
void getXY(int &i, int &j) { i = x; j = y; }
Coord operator+(Coord ob2);
Coord operator=(Coord ob2);
Coord operator++(); //
};
Coord Coord::operator+(Coord ob2) // +
{ Coord temp;
temp.x = x + ob2.x; // temp.x=this->x+ob2.x
temp.y = y + ob2.y; // temp.y=this->y+ob2.y
return temp;
}
Coord Coord::operator=(Coord ob2) // =
{ x = ob2.x; // this->x=ob2.x
y = ob2.y; // this->y=ob2.y
return *this;// ,
}
Coord Coord::operator++() // ++,
{ x++; y++;
return *this;
}
int main(){
Coord o1(10, 10), o2(5, 3), o3;
int x, y;
o3 = o1 + o2; // . operator+()
o3.getXY(x, y);
cout << "(o1 + o2) X: " << x << ", Y: " << y << "\n";
o3 = o1; //
o3.getXY(x, y);
cout << "(o3 = o1) X: " << x << ", Y: " << y << "\n";
++o1; //
o1.getXY(x, y);
cout << "(++o1) X: " << x << ", Y: " << y << "\n";
while(!kbhit());
return 0;
}
:
(1+2) X:15,y:13
(3=1) X:10, y:10
(++o1) X:11, Y:11
++ - this.
&& , << .
#include <iostream.h> // 28
#include <conio.h>
class Set {
char *pl; // char
public:
Set(char *pl)//
{pi=new char[strlen(pl)+1];strcpy(pi,pl);}//
Set &operator &&(Set &); // &&-
friend ostream &operator<<(ostream &stream,Set &ob);//<<
~Set(){delete [] pi;}//
};
Set& Set::operator &&(Set &s) // A=A^B
{int l=0;
for (int j=0;pi[j]!=0;j++)
for (int k=0;s.pi[k]!=0;k++)
if (pi[j]==s.pi[k]) {
pi[l]=pi[j];l++;break;}
pi[l]=0;
return *this;
}
ostream &operator<<(ostream &stream, Set &ob) {
stream << ob.pi << '\n'; /* */
return stream;
}
int main(){
Set s1("1f2bg5e6"),s2("abcdef");
Set s3=s2;
cout<<(s1&&s2)<<endl;// fbe
cout<<s3<<endl;// abcdef
while(!kbhit());
return 0;
}