12 ? ? Complex operator + (Complex B);
13 ? ? Complex operator = (Complex B);
14 };
15 Complex Complex::operator +(Complex B) { return Complex(real+B.real,image+B.image); }
16 Complex Complex::operator =(Complex B)
17 {
18 ? ? real=B.real,image=B.image;
19 ? ? cout<<"operator = calling..."<
20 ? ? return *this;
21 }
22 int main()
23 {
24 ? ? Complex A(100.0,200.0),B(-10.0,20.0),C;
25 ? ? cout<<"A=",A.display();
26 ? ? cout<<"B=",B.display();
27 ? ? C=A+B;
28 ? ? cout<<"C=A+B=",C.display();
29 ? ? C=A;
30 ? ? cout<<"C=A=",C.display();
31 }
?
2.->:
?
?
?1 #include
?2 using namespace std;
?3?
?4 class Complex
?5 {
?6 ? ? private:
?7 ? ? double real;
?8 ? ? double image;
?9 ? ? public:
10 ? ? Complex(double real=0,double image=0) { this->real=real,this->image=image; }
11 ? ? void display() { cout<<"("<
27 {
12 };
13 class PComplex
14 {
15 ? ? private:
16 ? ? Complex *PC;
17 ? ? public:
18 ? ? PComplex(Complex *PC=NULL) { this->PC=PC; }
19 ? ? Complex * operator ->()
20 ? ? {
21 ? ? ? ? static Complex NullComplex(0.0);
22 ? ? ? ? if(PC==NULL) return &NullComplex;
23 ? ? ? ? return PC;
24 ? ? }
25 };
26 int main()
28 ? ? PComplex P1;
29 ? ? P1->display();
30 ? ? Complex C1(100,200);
31 ? ? P1=&C1;
32 ? ? P1->display();
33 ? ? return 0;
34 }
?
3.[]:
?
?
?
?1 #include
?2 #include
?3 using namespace std;
?4?
?5 class String
?6 {
?7 ? ? private:
?8 ? ? char *str;
?9 ? ? int len;
10 ? ? public:
11 ? ? void showstr() { cout<<"string:"<
12 ? ? String(const char *p=NULL)
13 ? ? {
14 ? ? ? ? if(p)
15 ? ? ? ? {
16 ? ? ? ? ? ? len=strlen(p);
17 ? ? ? ? ? ? str=new char[len+1];
18 ? ? ? ? ? ? strcpy(str,p);
19 ? ? ? ? }
20 ? ? ? ? else
21 ? ? ? ? {
22 ? ? ? ? ? ? len=0;
23 ? ? ? ? ? ? str=NULL;
24 ? ? ? ? }
25 ? ? }
26 ? ? ~String()
27 ? ? {
28 ? ? ? ? if(str!=NULL) ? delete []str;
29 ? ? }
30 ? ? char &operator[](int n) { return *(str+n); }
31 ? ? const char &operator[](int n)const { return *(str+n); }
32 };
33?
34 int main()
35 {
36 ? ? String S1("0123456789abcdef");
37 ? ? S1.showstr();
38 ? ? S1[10]='A';