Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that uses a class rectangle in a separate header file. C++ The c

ID: 3570487 • Letter: W

Question

Write a program that uses a class rectangle in a separate header file. C++

The class has data members for length, width, area, and perimeter. The length and width can be set by the user, but the area and perimeter can not be set by the user (their values depend on the values of length and width). Include 2 other member functions that separately calculates the perimeter and area of the rectangle. Let the user be able to separately get the value of all 4 data fields. Include a constructor, that does not take any arguments, but initializes all the data fields, the length and width to 1, and the area and perimeter calculated as in the two calculate functions.

The main program file will declare a rectangle object, call the member functions to calculate the perimeter and area, and then get and print out the results.

Use the following ex5-1.cpp and create the corresponding rectangle5-1.h header file.

/*
Exercise 5-1
This program will create a rectangle object
*/
#include
#include"rectangle5-1.h"
using namespace std;

void main()
{
    rectangle51 r; // write constructor
    float len, wid;
    cout << "Enter length: ";
    cin >> len;
    r.setlength(len); // write functions
    cout << "Enter width: ";
    cin >> wid;
    r.setwidth(wid);
    r.calcarea();
    r.calcperimeter();
    cout << "Rectangle's length = " << r.getlength() << endl;
    cout << "Rectangle's width = " << r.getwidth() << endl;
    cout << "Rectangle's area = " << r.getarea() << endl;
    cout << "Rectangle's perimeter = " << r.getperimeter() << endl;
}

Explanation / Answer

rectangle51.h

#include<iostream>

using namespace std;

class rectangle51{
private :
   float length, width,area,perimeter;
   public :
   rectangle51(){
       length = 1;
           width = 1;
           area = 1*1;
           perimeter = 2(1 + 1);
       }
       void setLength(int len){
       length = len;
       }
       void setWidth(int wid){
       length = wid;
       }
       float getLength(){
       return length;
       }
       float getWidth(){
       return width;
       }
       float getArea(){
       return area;
       }
       float getPerimeter(){
       return perimeter;
       }
       void calcArea(){
       area = length*width;
       }
       void calcPerimeter(){
       perimeter = 2*(length + width);
       }
}

;


main.cpp

#include<iostream>
#include"rectangle51.h"
using namespace std;

void main()
{
rectangle51 r; // write constructor
float len, wid;
cout << "Enter length: ";
cin >> len;
r.setLength(len); // write functions
cout << "Enter width: ";
cin >> wid;
r.setWidth(wid);
r.calcArea();
r.calcPerimeter();
cout << "Rectangle's length = " << r.getLength() << endl;
cout << "Rectangle's width = " << r.getWidth() << endl;
cout << "Rectangle's area = " << r.getArea() << endl;
cout << "Rectangle's perimeter = " << r.getPerimeter() << endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote