Summary of C + + copy function, about the scope of variables and so on---shinepans

Source: Internet
Author: User

#include <iostream>using namespace Std;class circle{private:double r0;public:circle (double r) {r0 = R;cout < < R << "function called the initialization of the sphere \ n";} Circle (const circle &t) {r0 = T.r0;cout << "r=" << r0 << "Duplicate function called the sphere \ n";} Double Get_rr () {return r;} Double Getarea () {cout << "sphere surface area:" << 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 << ", the initialization of the box is called function \ n ";} Rectangle (Rectangle &n) {length = N.length;width = N.width;height = n.height;cout << height << "Copy of Box is called Count \ 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):p 2 (XP2), p1 (XP1) {Double area1 =2 * P1.getarea () cout << "Two spheres of surface area are:" << area1 << endl;double area2 = P2.getarea (); cout << "cuboid surface The product is: "<< area2 << endl;all_area = area1 + area2;cout <<" Output total surface area: "<< all_area << Endl;} /*whole::whole (Whole &p):p 1 (P.P1), p2 (p.p2) {all_area=p.area1+p.area2;} /*whole::whole (Whole &p4):p (P4.P1), P (p4.p2) {cout<< "Output total surface area:"; area=p4.area;} */int Main () {double m;cout << "Please enter the value of M:"; cin >> m;double rr = m/2;circle XP1 (RR); Rectangle XP2 (2 * m, M, m); Whole (XP1, XP2); System ("pause"); return 0;}





For a copy function, there is an important error-prone place:

When defining a variable in a class:

If I define a private:double R in class circle, then the program will recognize the error and use R without initialization, so it is best to define r0, the subsequent use of R,

Here is the code that uses r:

#include <iostream>using namespace Std;class circle{private:double r;public:circle (double r) {R=r;cout<<r << "called the initialized function of the sphere \ n";}    Circle (Circle & T) {R=T.R; cout<< "r=" <<r<< "called the copied function of the sphere \ n";} Double Get_rr () {return r;} Double Getarea () {cout<< "Sphere surface area:" <<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<< "," <

Operation Result:



It's wrong.



Summary of C + + copy function, about the scope of variables and so on---shinepans

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.