Question

// This program uses the programmer-defined Rectangle class. #include "Rectangle.cpp" #include <iostream> using namespace std; int...

// This program uses the programmer-defined Rectangle class.
#include "Rectangle.cpp"
#include <iostream>
using namespace std;
int main()
{

   Rectangle rectangle1;
   Rectangle rectangle2;

   rectangle1.setLength(10.0);
   rectangle1.setWidth(5.0);
   rectangle2.setLength(7.0);
   rectangle2.setWidth(3.0);


   cout << "Perimeter of rectangle1 is " << rectangle1.calculatePerimeter() << endl;
   cout << "Area of rectangle1 is " << rectangle1.calculateArea() << endl;
   cout << "Perimeter of rectangle2 is " << rectangle2.calculatePerimeter() << endl;
   cout << "Area of rectangle2 is " << rectangle2.calculateArea() << endl;

   return 0;
}

  1. Ensure the class file named Rectangle.cpp is open in your editor.
  2. In the Rectangle class, create two private attributes named length and width. Bothlength and width should be data type double.
  3. Write public set methods to set the values for length and width.
  4. Write public get methods to retrieve the values for length and width.
  5. Write a public calculateArea()method and a public calculatePerimeter() method to calculate and return the area of the rectangle and the perimeter of the rectangle.
  6. Open the file named MyRectangleClassProgram.cpp.
  7. In the MyRectangleClassProgram, create two Rectangle objects named rectangle1 and rectangle2 using the default constructor as you saw in MyEmployeeClassProgram.cpp.
  8. Set the length of rectangle1 to 10.0 and the width to 5.0. Set the length of rectangle2 to 7.0 and the width to 3.0.
  9. Print the value of rectangle1’s perimeter and area, and then print the value of rectangle2’s perimeter and area.
  10. Execute the program by clicking the Run button at the bottom of the screen
0 0
Add a comment Improve this question Transcribed image text
Answer #1

----------Rectangle.cpp-------

#include<iostream>

using namespace std;

class Rectangle{

    private:

       

        double length;

        double width;

       

    public:

       

        // constructor

        Rectangle()

        {

            this->length = 0.0;

            this->width = 0.0;

        }

       

        // constructor

        Rectangle(double length, double width)

        {

            this->length = length;

            this->width = width;

        }

       

        // seter method

        void setLength(double length)

        {

            this->length = length;

        }

       

        void setWidth(double width)

        {

            this->width = width;

        }

       

        // geter method

        double getLength()

        {

            return this->length;

        }

       

        double getWidth()

        {

            return this->width;

        }

        

        // calculate the area

        double calculateArea()

        {

            return this->length * this->width;

        }

       

        // calculate the perimeter

        double calculatePerimeter()

        {

            return 2.0 * ( this->length + this->width );

        }

};

-------------main.cpp------------

#include "Rectangle.cpp"

#include <iostream>

using namespace std;

int main()

{

   Rectangle rectangle1;

   Rectangle rectangle2;

   rectangle1.setLength(10.0);

   rectangle1.setWidth(5.0);

   rectangle2.setLength(7.0);

   rectangle2.setWidth(3.0);

   cout << "Perimeter of rectangle1 is " << rectangle1.calculatePerimeter() << endl;

   cout << "Area of rectangle1 is " << rectangle1.calculateArea() << endl;

   cout << "Perimeter of rectangle2 is " << rectangle2.calculatePerimeter() << endl;

   cout << "Area of rectangle2 is " << rectangle2.calculateArea() << endl;

   return 0;

}


Sample Output

Add a comment
Know the answer?
Add Answer to:
// This program uses the programmer-defined Rectangle class. #include "Rectangle.cpp" #include <iostream> using namespace std; int...
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
  • 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...

  • Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class...

    Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class and then use it in a Java program. The program should create two Rectangle objects and find their area and perimeter. Instructions Make sure the class file named Rectangle.java is open. In the Rectangle class, create two private attributes named lengthand width. Both length and width should be data type double. Write public set methods to set the values for length and width. Write...

  • 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...

  • Hello I have a question. I would like to check if the code follows this requirement....

    Hello I have a question. I would like to check if the code follows this requirement. Rectangle.h - A complete Rectangle Class including both declaration and definition appRectangle,cpp separate in two different files the class definition and implementation Code: rectangle.h // Rectangle class declaration. class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; //************************************************** // setWidth assigns a value to the width member. * //************************************************** void...

  • Find two syntax errors in the following program: #include <iostream> using namespace std; int area(int length,...

    Find two syntax errors in the following program: #include <iostream> using namespace std; int area(int length, int width = 0); int main() { int length, width; // for rectangle use both arguments cout << "Enter length and width of a rectangle" << endl; cin >> length >> width; cout << "The area is " << area(length, width) << endl; // for square, only need first argument cout << "Enter side of a square" << endl; cin >> length; cout <<...

  • C++ program. int main() {    Rectangle box;     // Define an instance of the Rectangle class...

    C++ program. int main() {    Rectangle box;     // Define an instance of the Rectangle class    double rectWidth; // Local variable for width    double rectLength; // Local variable for length    string rectColor;    // Get the rectangle's width and length from the user.    cout << "This program will calculate the area of a\n";    cout << "rectangle. What is the width? ";    cin >> rectWidth;    cout << "What is the length? ";    cin >>...

  • 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,...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: --------------------...

    Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: ------------------------------------------------------------------------------------------------------------------------------------------ main.cpp #include #include "rectangleType.h" using namespace std; // part e int main() { rectangleType rectangle1(10, 5); rectangleType rectangle2(8, 7); rectangleType rectangle3; rectangleType rectangle4; cout << "rectangle1: " << rectangle1 << endl; cout << "rectangle2: " << rectangle2 << endl; rectangle3 = rectangle1 + rectangle2;    cout << "rectangle3: " << rectangle3 << endl; rectangle4 = rectangle1 * rectangle2;    cout << "rectangle4: " << rectangle4 << endl; if (rectangle1 > rectangle2) cout << "Area...

  • The program must be in C# syntax. Write the definition for a generic class called Rectangle...

    The program must be in C# syntax. Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that...

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