c++ 实验三
#include<iostream> using namespace std; class rectangle { public: rectangle(float l, float w); float area(); private: float length, width; }; rectangle::rectangle(float l, float w) { length = l; width = w; } float rectangle::area() { return length * width; } int main() { float length, width; cout << "Enter the length and width of the rectangle:"; cin >> length >> width; rectangle a(length, width); cout << a.area() << endl; return 0; }
#include<iostream> using namespace std; class Complex { public: Complex(float re, float im); Complex(float re); void add(Complex &c) { real += c.real; }; void show() { cout << real << (imaginary>0?'+':'-') << imaginary << "i" << endl; }; private: float real, imaginary; }; Complex::Complex(float re, float im) { real = re; imaginary = im; }; Complex::Complex(float re) { real = re; imaginary = 0; }; int main(){ Complex c1(3,5); Complex c2 = 4.5; c1.add(c2); c1.show(); return 0; }