Answer to Question #203737 in C++ for python

Question #203737

Write the definition for a class called Rectangle that has floating point data members

length and width. The class has the following member functions:

void setlength(float)r

void setwidth(float)

float perimeter()

float area()

void show()

int sameArea(Rectangle) that has one parameter of type Rectangle. sameArea returns 1 if the

two Rectangles have the same area, and returns 0 if they don't.

1. Write the definitions for each of the above member functions.

2. Write main function to create two rectangle objects. Set the length and width of the first

rectangle to 5 and 2.5. Set the length and width of the second rectangle to 5 and 18.9. Display

each rectangle and its area and perimeter.


3. Check whether the two Rectangles have the same area and print a message indicating the result.

Set the length and width of the first rectangle to 15 and 6.3. Display each Rectangle and its area

and perimeter again. Again, check whether the two Rectangles have the same area and print a

message indicating the result.


1
Expert's answer
2021-06-08T23:46:42-0400


#include <iostream>


using namespace std;
class Rectangle{
    private:
        float length;
        float width;
    public:
        void setlength(float l){
            length=l;
        }
        void setwidth(float w){
            width=w;
        }
        float perimeter(){
            return (2*(length+width));
        }
        float area(){
            return (length*width);
        }
        void show(){
            cout<<"\nLength: "<<length;
            cout<<"\nWidth: "<<width;
            cout<<"\nPerimeter: "<<perimeter();
            cout<<"\nArea: "<<area()<<endl;
        }
        
        static int sameArea(Rectangle r1, Rectangle r2){
            if (r1.area()==r2.area())
                return 1;
            else
                return 0;
        }
};
int main()
{
    Rectangle rec1,rec2;
    rec1.setlength(5);
    rec1.setwidth(2.5);
    rec2.setlength(5);
    rec2.setwidth(18.9);
    cout<<"\n\t\tRectangle 1\n";
    rec1.show();
    cout<<"\n\t\tRectangle 2\n";
    rec2.show();


    int s=Rectangle::sameArea(rec1,rec2);
    if (s==1)
        cout<<"\nThe two reactangles are the same\n";
    else if (s==0)
        cout<<"\nThe two reactangles are NOT the same.\n";
        
    rec1.setlength(15);
    rec1.setwidth(6.3);
    cout<<"\n\t\tRectangle 1\n";
    rec1.show();
    
    int s2=Rectangle::sameArea(rec1,rec2);
    if (s2==1)
        cout<<"\nThe two reactangles are the same\n";
    else if (s2==0)
        cout<<"\nThe two reactangles are NOT the same.\n";
    
    return 0;
}

output



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog