#include<iostream.h> ---------------函数重载 #include<string.h> class student { private: int sid; char name[20]; int age; public: student() { this->sid=0; strcpy(name,""); this->age=0; } student(int a,char* pname,int age) { this->sid=a; strcpy(this->name,pname); this->age=age; } student(student& st ) { this->sid=st.sid; strcpy(this->name,st.name); this->age=st.age; } void print() { cout<<"学生的信息:"<<endl; cout<<"sid= "<<sid<<endl; cout<<"name="<<name<<endl; cout<<"age="<<age<<endl; } }; void main() { student st1; st1.print(); student st2(10023,"吴晗",25); st2.print(); student st3(st2); st3.print(); } #include<iostream.h>-------------------基本函数重载 class complex { public: int a; int b; }; class Do { public: int add(int a,int b) { return a+b; } complex add(complex c1,complex c2) { complex c; c.a=c1.a+c2.a; c.b=c1.b+c2.b; return c; } }; void main() { complex c1; complex c2; c1.a=1; c1.b=1; c2.a=2; c2.b=2; cout<<add(1,2)<<endl; //complex c=add(c1,c2); cout<<c.a<< "+"<<c.b<<"i"<<endl; } #include<iostream.h>-------------引用 class complex { public: int a; int b; }; int add(int a,int b) { return a+b; } complex add(complex c1,complex c2) { complex c; c.a=c1.a+c2.a; c.b=c1.b+c2.b; return c; } void main() { complex c1; complex c2; c1.a=1; c1.b=1; c2.a=2; c2.b=2; cout<<add(1,2)<<endl; //complex c=add(c1,c2); }