首页 > 代码库 > C++拷贝函数的小结,关于变量的作用域等---ShinePans
C++拷贝函数的小结,关于变量的作用域等---ShinePans
#include <iostream> using namespace std; class circle { private: double r0; public: circle(double r) { r0 = r; cout << r << "调用了球体的初始化的函数\n"; } circle(const circle &t){ r0 = t.r0; cout << "r=" << r0 << "调用了球体的复制的函数\n"; } //double get_rr(){return r;} double getArea(){ cout << "球体的表面积:" << 4 * 3.14*r0*r0; return 4 * 3.14*r0*r0; } }; class Rectangle { private: double length; double width; double height; public: Rectangle(double a, double b, double c){ length = a; width = b; height = c; cout << length << "," << width << "," << height << ",调用了长方体的初始化的函数\n"; } Rectangle(Rectangle &n){ length = n.length; width = n.width; height = n.height; cout << height << "调用了长方体的复制函数\n"; } double getArea(){ return (2 * length*width + 2 * length*height + 2 * width*height); } }; class Whole { private: circle p1; Rectangle p2; double all_area; public: Whole(circle xp1, Rectangle xp2); }; Whole::Whole(circle xp1, Rectangle xp2) :p2(xp2), p1(xp1){ double area1 = 2 * p1.getArea(); cout << "两个球体的表面积是:" << area1 << endl; double area2 = p2.getArea(); cout << "长方体的表面积是:" << area2 << endl; all_area = area1 + area2; cout << "输出总的表面积:" << all_area << endl; } /*Whole::Whole(Whole &p):p1(p.p1),p2(p.p2){ all_area=p.area1+p.area2; } /*Whole::Whole(Whole &p4):p(p4.p1),p(p4.p2){ cout<<"输出总的表面积:"; area=p4.area; }*/ int main(){ double m; cout << "请输入m的值:"; cin >> m; double rr = m / 2; circle xp1(rr); Rectangle xp2(2 * m, m, m); Whole(xp1, xp2); system("pause"); return 0; }
对用拷贝函数,有一个很重要的容易错误的地方:
在类中定义 一个变量时:
如我在 class circle中定义一个 private : double r,如果下面全部使用r ,那么程序将会识别错误,会使用 没有初始化的r,所以最好定义r0,后面传入的使用r,
下面是使用r的代码:
#include <iostream> using namespace std; class circle { private: double r; public: circle(double r) { r=r; cout<<r<<"调用了球体的初始化的函数\n"; } circle(circle & t){ r=t.r; cout<<"r="<<r<<"调用了球体的复制的函数\n"; } //double get_rr(){return r;} double getArea(){ cout<<"球体的表面积:"<<4*3.14*r*r; return 4*3.14*r*r; } }; class Rectangle { private: double length; double width; double height; public: Rectangle(double a,double b,double c){ length=a; width=b; height=c; cout<<length<<","<<width<<","<<height<<",调用了长方体的初始化的函数\n"; } Rectangle(Rectangle &n){ length=n.length; width=n.width; height=n.height; cout<<height<<"调用了长方体的复制函数\n"; } double getArea(){ return (2*length*width+2*length*height+2*width*height); } }; class Whole { private: circle p1; Rectangle p2; double all_area; public: Whole(circle xp1,Rectangle xp2); }; Whole::Whole(circle xp1,Rectangle xp2):p2(xp2),p1(xp1){ double area1=2*p1.getArea(); cout<<"两个球体的表面积是:"<<area1<<endl; double area2=p2.getArea(); cout<<"长方体的表面积是:"<<area2<<endl; all_area=area1+area2; cout<<"输出总的表面积:"<<all_area<<endl; } /*Whole::Whole(Whole &p):p1(p.p1),p2(p.p2){ all_area=p.area1+p.area2; } /*Whole::Whole(Whole &p4):p(p4.p1),p(p4.p2){ cout<<"输出总的表面积:"; area=p4.area; }*/ int main(){ double m; cout<<"请输入m的值:"; cin>>m; double rr=m/2; circle xp1(rr); Rectangle xp2(2*m,m,m); Whole(xp1,xp2); return 0; }
运行结果:
是错误的
C++拷贝函数的小结,关于变量的作用域等---ShinePans
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。