1.
using namespace std; class Rectangle{ public: void set(double l,double w); void show(); private: double L,W; }; //成员函数的具体体现 void Rectangle::set(double l,double w){ L=l; W=w; } inline void Rectangle::show(){ cout<<"矩形长为"<<L<<"宽为"<<W<<"面积为"<<L*W; } int main(){ double x,y; cin>>x>>y; Rectangle arectangle; arectangle.set(x,y); arectangle.show(); return 0; }
2.
#include<iostream> using namespace std; class Complex{ public: Complex(float x,float y); Complex(float x); void add(Complex &c); void show(); private: float X,Y; }; Complex::Complex(float x,float y){ X=x; Y=y; } Complex::Complex(float x){ X=x; } void Complex::add(Complex &c) { X+=c.X; Y+=c.Y; } void Complex::show(){ cout<<X<<"+"<<Y<<"i"; } int main(){ Complex c1(3,5); Complex c2=4.5; c1.add(c2); c1.show(); return 0; }