#include<iostream> using namespace std; class Rectangle{ public: void attribute(float newL,float newW); void area(); private: int length,width; }; void Rectangle::attribute(float newL,float newW){ length=newL; width=newW; } void Rectangle::area(){ int area; area=length*width; cout<<"面积是:"<<area<<endl; } int main() { float a,b; cout<<"输入长:"<<endl; cin>>a; cout<<"输入宽:"<<endl; cin>>b; Rectangle hahaha; hahaha.attribute(a,b); hahaha.area(); return 0; }
#include<iostream> using namespace std; class Complex{ public: Complex(double a,double b); Complex(double a); void add(Complex &p); void show(); private: double real,imaginary; }; Complex::Complex(double a,double b){ real=a; imaginary=b; } Complex::Complex(double a){ real=a; } void Complex::add(Complex &p){ real=real+p.real; imaginary=imaginary+p.imaginary; } void Complex::show(){ cout<<real<<"+"<<imaginary<<"i"<<endl; } int main() { Complex c1(3,5); Complex c2(4.5); c1.add(c2); c1.show(); return 0; }