4-11
#include<iostream> using namespace std; class Rectangle{ public: Rectangle(float length,float width); private: float l; float w; }; Rectangle::Rectangle(float length,float width){ l=length; w=width; } int main(){ float l; float w; cout<<"请输入长:"; cin>>l; cout<<"请输入宽:"; cin>>w; cout<<"面积为:"<<l*w<<endl; return 0; }
4-20
#include<iostream> using namespace std; class Complex{ float c,d; public: Complex(float a=0,float b=0){c=a;d=b;} void add(Complex b); void show(); }; void Complex::add(Complex b){ c+=b.c; d+=b.d; } void Complex::show(){ cout<<"c="<<c<<"+"<<d<<"i"<<endl; } int main(){ Complex c1(3,5); Complex c2=4.5; c1.add(c2); c1.show(); return 0; }