3-4 calculate the circumference and area of a rectangle

Source: Internet
Author: User

3-4 calculate the circumference and area of a rectangle

Time Limit: 1000 ms memory limit: 65536 K Through this exercise, you can understand the definition and usage of the copy constructor. Design a rectangular rect to calculate the perimeter and area of the rectangle. Class has private data member length (length), width (width), is initialized by the constructor with the default parameter value, the function prototype is: rect (double length = 0, double width = 0); define a copy constructor for it. The parameter is a common reference of the object. The prototype of the function is rect (const rect &); write the main function, create rect object R1 initialize as long and wide data, use R1 to initialize another rect object R2, and output the length, width, circumference, and area of the object respectively. Requirement: Create rect r1 (3.0, 2.0), R2 (R1), and enter

Enter two real numbers separated by a space in the middle, representing the length and width of the rectangle

Output

There are 6 rows in total;
Output the length and width of R1, the circumference of R1, the area of R1, the length and width of R2, the circumference of R2, and the area of R2 respectively. Note that a space is used between words and words.

Sample Input
56 32
Sample output
the length and width of r1 is:56,32the perimeter of r1 is:176the area of r1 is:1792the length and width of r2 is:56,32the perimeter of r2 is:176the area of r2 is:1792
Prompt

 

Input-7.0-8.0

Output

The length and width of R1 is: 0, 0

The perimeter of R1 is: 0

The area of R1 is: 0

The length and width of R2 is: 0, 0

The perimeter of R2 is: 0

The area of R2 is: 0

Sample program from Huang Jingjing



#include <iostream>using namespace std;class Rect{private:    double len;    double wid;public:    Rect(double x=0,double y=0);    Rect(const Rect &b);    const void display()    {        cout << "the length and width of r1 is:" << len << ","<< wid << endl;        cout << "the perimeter of r1 is:" << (len + wid) * 2<<endl;        cout << "the area of r1 is:" << len * wid << endl;    }    const void display1()    {        cout << "the length and width of r2 is:" << len << ',' << wid<< endl;        cout << "the perimeter of r2 is:" << (len + wid) * 2 << endl;        cout << "the area of r2 is:" << len * wid << endl;    }};Rect::Rect(double x,double y){    len=x;    wid=y;}Rect::Rect(const Rect &b){    len=b.len;    wid=b.wid;}int main(){    double x,y;    cin>>x>>y;    if(x<0||y<0)    {        x=0;        y=0;    }    Rect rect(x,y);    Rect rect1=rect;    rect.display();    rect1.display1();    return 0;}

Zookeeper

3-4 calculate the circumference and area of a rectangle

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.