Question

1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default...

1. Define a class Rectangle with two attributes: (This is for C++)

-length, an integer (default value 1)

-width, an integer (default value 1)

Provide the following member functions:

-default constructor

-constructor that takes parameters used to initialize the data

-get/set function for each attribute

-a function perimeter that returns the perimeter of the rectangle

-a function area that returns the area of the rectangle

b. Write a program that uses the class Rectangle to create two rectangle objects with user input dimensions. The program compares the perimeters of the two rectangles and prints to the screen the area of the rectangle with the smallest perimeter (pick arbitrarily, if they are equal).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please refer to the code screenshots

Program code to copy

#include <iostream>

using namespace std;

class Rectangle{
   public:
  
       // attributes
       int length;
       int width;
      
       // constructor
       Rectangle(){
           // setting default values
           length = 1;
           width = 1;
       }
      
       // set function for length
       void setLength(int l){
           length = l;
       }
      
       // set function for width
       void setWidth(int w){
           width = w;
       }
      
       // get function for length
       int getLength(){
           return length;
       }
      
      
       // get function for width
       int getWidth(){
           return width;
       }
      
       // function to calculate perimeter
       int perimeter(){
           int p = 2 * (length + width);
           return p;
       }
      
       // function to calculate area
       int area(){
           int a = length * width;
           return a;
       }
};


int main(){
   // declaring two rectangles
   Rectangle r1, r2;
   int l, w;
   cout<<"Enter length and width for rectangle 1\n";
   cin>>l>>w;
   r1.setLength(l);
   r1.setWidth(w);
  
   cout<<"Enter length and width for rectangle 2\n";
   cin>>l>>w;
   r2.setLength(l);
   r2.setWidth(w);
  
   // comparing perimenters and displaying area
   if(r1.perimeter() < r2.perimeter()){
       cout<<"Area of rectangle with smallest perimeter is "<<r1.area()<<'\n';
   } else {
       cout<<"Area of rectangle with smallest perimeter is "<<r2.area()<<'\n';
   }
  
   return 0;
}


Program code screenshots

Output

Add a comment
Know the answer?
Add Answer to:
1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...

    C++ Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...

  • Following the example of the Circle class in Section 8.2, design a class named Rectangle to...

    Following the example of the Circle class in Section 8.2, design a class named Rectangle to represent a rectangle. The class contains: ■ Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. ■ A no-arg constructor that creates a default rectangle. ■ A constructor that creates a rectangle with the specified width and height. ■ A method named getArea() that returns the...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in...

    NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: - Two data fields named width and height. - A constructor that creates a rectangle with the specified width and height. The default values are 1 and 2 for the width and height, respectively. - A method named getArea() that returns the area of this rectangle. - A method named...

  • Making a rectangle class in java write a simple class that will represent a rectangle. Later...

    Making a rectangle class in java write a simple class that will represent a rectangle. Later on, we will see how to do this with graphics, but for now, we will just have to use our imaginations for the visual representations ofWthe rectangles. Instance Variables Recall that an object is constructed using a class as its blueprint. So, think about an rectangle on your monitor screen. To actually draw a rectangle on your monitor screen, we need a number of...

  • Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class...

    Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class should have a zero-argument constructor that initializes each data member to 0. It should have member functions named calcPerimeter() and calcArea() that calculate the perimeter and area of a rectangle respectively, a member function setLength() and setWidth() to set the length and width, member functions getLength() and getWidth() to return the length and width, and a member function showData() that displays the rectangle’s length,...

  • Question 2) Use Point class to define another class called Rectangle which has only two data memb...

    C++ Question 2) Use Point class to define another class called Rectangle which has only two data members of type Point named UpperLeftPoint and BottomRightPoint a. Define two constructors one is default and one is initializer b. Define member functions named getLength of rectangle c. Define member functions named get Width of rectangle d. Define member functions named getArea of rectangle. Note, the values of the functions are calculated based on the two points of the rectangle e. Define member...

  • Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then...

    Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then use it in a C++ program. The program should create two Rectangle objects and find their area and perimeter. Instructions Ensure the class file named Rectangle.cpp is open in your editor. In the Rectangle class, create two private attributes named length and width. Bothlength and width should be data type double. Write public set methods to set the values for lengthand width. Write public...

  • please use c++ Write a program that contains a class Rectangle with two private double precision...

    please use c++ Write a program that contains a class Rectangle with two private double precision members iLength and iWidth, public set and get member functions for these two members, a two argument constructor that sets the length and width to any two user specified values and a void function area() that computes the area and then prints this value by inserting it into the cout output stream. Write a main function that ereates a Rectangle with a length of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT