书上例题= =
#include<iostream> using namespace std; class complex { public: complex(double r, double i); complex(double r); complex(const complex &A); void add(complex &A); double getr() { return real; } double geti() { return imaginary; } void show(); private: double real; double imaginary; }; complex::complex(double r) { real=r; imaginary=0; } complex::complex(double r, double i) { real=r; imaginary=i; } complex::complex(const complex &A) { real=A.real; imaginary=A.imaginary; } void complex::add(complex &A) { real+=A.real;//这里因为用的是complex里面的,所以private 里是可以用的,一开始自己用的getr,就完全死掉了! imaginary+=A.imaginary; } inline void complex::show() { if(imaginary!=0) cout<<real<<"+"<<imaginary<<"i"<<endl;//对于实数虚数最后要判断是不是实数虚数,就开始分开来运算。 else cout<<real<<endl; } int main() { complex C(3,5); complex B=4.5;//这里按照书上的一开始 ,(如果不加const)“B=4.5”是不行的,但是B(4.5)可以实行(百度的,具体不知道怎么回事,求解) C.add(B); C.show(); return 0; }
运行结果:
书上例题:
#include <iostream> using namespace std; class juxing { public: juxing(int L=0,int W=0); int area(); juxing(juxing&A); int getL() { return length; } int getW() { return width; } private: int length; int width; }; juxing::juxing (int L, int W) { length=L; width=W; } juxing::juxing(juxing&A) { length=A.length; width=A.width; cout<<"---------"<<endl; } int juxing::area() { return length*width; } int main() { cout<<"下面,你要分别输入矩形的长和宽"<<endl; int a,b; while(cin>>a>>b) { juxing A (a,b); juxing B=A; cout<<"矩阵的长为:"<<B.getL()<<endl; cout<<"矩阵的宽为:"<<B.getW()<<endl; cout<<"这个矩形的面积为"<<B.area()<<endl; } cout<<"恕我直言,那个,矩形的长宽都是数字啊,吓几儿输数据,你走开"<<endl; return 0; }
原文:https://www.cnblogs.com/kakuiyjl/p/8747905.html