1.
#include <iostream> using namespace std; class circle{ public: virtual double funV(double r,double h=0){ return 3.1415926*r*r; } }; class yuanZhu:public circle{ public: double funV(double r,double h=0){ return 3.1415926*r*r*h; } }; class yuanZhui:public circle{ public: double funV(double r,double h=0){ return 3.1415926*r*r*h/3; } }; int main(){ circle c,*p; yuanZhu z; yuanZhui zhui; p=&c; cout<<p->funV(100,0)<<endl; p=&z; cout<<p->funV(100,100)<<endl; p=&zhui; cout<<p->funV(100,300)<<endl; return 0; }
输出:
31415.9
3.14159e+06
3.14159e+06
2.
#include <iostream> using namespace std; class Complex{ public: double real,Image; Complex(double r=0,double I=0){ real = r; Image = I; } Complex operator-(Complex x1); Complex &operator-=(Complex x2); bool operator!=(Complex x3); friend Complex operator+(Complex x4,Complex x5); friend Complex &operator+=(Complex &x6,Complex x7); friend bool operator==(Complex x8,Complex x9); void show(){ cout<<real; if (Image<0){ cout<<"-"<<Image<<"i"<<endl; }else{ cout<<"+"<<Image<<"i"<<endl; } } }; Complex Complex::operator-(Complex x1){ Complex newX; newX.real = real - x1.real; newX.Image = Image - x1.Image; return newX; } Complex& Complex::operator-=(Complex x2){ real = real - x2.real; Image = Image - x2.Image; return *this; } bool Complex::operator!=(Complex x3) { if ((real!=x3.real)||(Image!=x3.Image)){ return true; }else{ return false; } } Complex operator+(Complex x4,Complex x5){ Complex newX; newX.real = x5.real + x4.real; newX.Image = x5.Image +x4.Image; return newX; } Complex& operator+=(Complex &x6,Complex x7){ x6.real = x6.real +x7.real; x6.Image = x6.Image + x7.Image; return x6; } bool operator==(Complex x8,Complex x9){ if ((x8.real==x9.real)&&(x8.Image==x9.Image)){ return true; }else{ return false; } } int main(){ Complex c1(1,1),c2(1,1),c3(1,1),c4(1,1),c5(1,1),c6(1,1),c7(1,1),c8(1,1),c9(1,1),c10(1,1),c11(1,1); c1=c2+c3;c1.show(); //2+2i c4+=c5;c4.show(); //2+2i c5=c6-c7;c5.show(); //0+0i c6-=c7;c6.show(); //0+0i cout<<(c8==c9)<<endl; //1 cout<<(c8!=c9)<<endl; //0 return 0; }
输出:
2+2i
2+2i
0+0i
0+0i
1
0
Process finished with exit code 0
3.定义一个三维坐标类,分别通过成员函数重载前置++和后置++运算符,通过友元函数重载前置--和后置--运算符,实现坐标值的+1和-1。main函数做测试。
#include <iostream> using namespace std; class coordinate{ public: double x,y,z; coordinate(double x1=0,double y1=0,double z1=0){ x=x1;y=y1;z=z1; } coordinate operator++(); coordinate operator++(int); friend coordinate operator--(coordinate &pp); friend coordinate operator--(coordinate &pp,int); friend ostream& operator<<(ostream &os,coordinate tt); }; /*成员函数实现++*/ coordinate coordinate::operator++() { x++;y++;z++; return *this; } coordinate coordinate::operator++(int) { coordinate temp = *this; x++;y++;z++; return temp; } /*友元函数实现++*/ coordinate operator--(coordinate &pp) { pp.x--;pp.y--;pp.z--; return pp; } coordinate operator--(coordinate &pp,int) { coordinate temp=pp; pp.x--;pp.y--;pp.z--; return temp; } ostream& operator<<(ostream& os,coordinate tt){ os<<"("<<tt.x<<","<<tt.y<<","<<tt.z<<")"<<endl; return os; } int main(){ coordinate c1(1,1,1),c2(1,1,1),c3(1,1,1),c4(1,1,1); cout<<(++c1); cout<<(c2++); cout<<(--c3); cout<<(c4--); return 0; }
4.
#include <iostream> #include <fstream> using namespace std; class student{ public: double num,csBase,cpp,cppProject; char *name; student(double n=0,char p1[50]="姓名未知",double cB=0,double cp=0,double cpP=0){ num = n;csBase=cB;cpp=cp;cppProject=cpP; char *name = new char[50]; int i=0; while(p1[i]){ *name++=p1[i++]; } } double ave(){ double ave =(csBase+cpp+cppProject)/3 ; return ave; } friend istream &operator>>(istream &is,student &s1); }; istream &operator>>(istream &is,student &s1){ cout<<"学号:";is>>s1.num; cout<<"姓名:姓名+空格:";cin.getline(s1.name,50,' '); cout<<"计算机基础:";is>>s1.csBase; cout<<"C++程序设计:";is>>s1.cpp; cout<<"C++课程设计:";is>>s1.cppProject; return is; } int main(){ student student1; cin>>student1; cout<<"平均成绩为"<<student1.ave(); return 0; }