10_89.cpp
上传用户:zipjojo
上传日期:2009-07-20
资源大小:70k
文件大小:1k
- #include<iostream.h>
- class A
- {
- public:
- int va;
- A(int i){va=i;}
- virtual A& operator+(A&);
- virtual A& operator=(A&);
- virtual int a(){return va;}
- virtual int b(){return 0;}
- virtual int c(){return 0;}
- void disp(){cout<<"va="<<va<<endl;}
- };
- class B:public A
- {
- public:
- int vb;
- B(int i,int j):A(i){vb=j;}
- virtual A& operator+(A&);
- virtual A& operator=(A&);
- virtual int b(){return vb;}
- void disp(){cout<<"vb="<<vb<<endl;}
- };
- class C:public B
- {
- public:
- int vc;
- C(int i,int j, int k):B(i,j){vc=k;}
- virtual A& operator+(A&);
- virtual A& operator=(A&);
- virtual int c(){return vc;}
- void disp(){cout<<"vc="<<vc<<endl;}
- };
- A& A::operator+(A& t)
- {
- va+=t.a();
- return *this;
- }
- A& A::operator=(A& t)
- {
- va=t.a();
- return *this;
- }
- A& B::operator+(A& t)
- {
- va+=t.a();
- vb+=t.b();
- return *this;
- }
- A& B::operator=(A& t)
- {
- va=t.a();
- vb=t.b();
- return *this;
- }
- A& C::operator+(A& t)
- {
- va+=t.a();
- vb+=t.b();
- vc+=t.c();
- return *this;
- }
- A& C::operator=(A& t)
- {
- va=t.a();
- vb=t.b();
- vc=t.c();
- return *this;
- }
- void addobject(A& ob1,A& ob2)
- {
- ob1=ob1+ob2;
- }
- void main()
- {
- A a(10);
- B b(20,30);
- C c(40,50,60);
- addobject(a,b);
- addobject(b,c);
- addobject(c,b);
- a=b+c;
- c=c+a;
- a.disp();
- b.disp();
- c.disp();
- }