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

In C++ Create a C++ class Rectangle. The class has attributes length and width,

ID: 3596920 • Letter: I

Question

In C++

Create a C++ class Rectangle. The class has attributes length and width, with defaults to 1.0. It has member functions for calculating the perimeter and the area of a rectangle. It has setLength, setWidth, getLength, and getWidth functions for updating and accessing the length and width values. The set functions should verify that length/width are each floating-point numbers greater than 0.0 and less than 20.0. If an invalid value is encountered, the corresponding set function should return -1 to the calling function without changing the length/width values. Implement a main function that takes a data file name from command line argument. The main function reads the length and width values from this input file and use the Rectangle class to calculate the perimeter and area for each data set. (Note: the Rectangle class should not include any l/O codes. All IVOs should be done external to the Rectangle class) Source Files » Rectangle.h - contains the Rectangle class declaration. » Rectangle.cpp - contains the Rectangle class implementation. » hw04.cpp - contains the main function and any other functions for file l/Os

Explanation / Answer

Rectangle.h

#ifndef RECTANGLE_H

#define RECTANGLE_H

class Rectangle // Class Rectangle
{
public:
Rectangle(float=1, float=1); // Constructor

//set functions

double setlength( double);
double setwidth( double);

//get functions

double getlength() const;
double getwidth() const;

//calculation functions

double perimeter() const;
double area() const;

private:
float length;
float width;
}; // End of class Rectangle

Recrangle.cpp

#include "Rectangle.h"

//:: is a scope resolution operator to access the functions of class outside of the class

Rectangle::Rectangle(float L, float W) // Scope function for constructor
{
length=L;
width=W;
}
double Rectangle::area() const //calulating the area of the rectangle
{
return length*width;
}
double Rectangle::perimeter() const // calculating perimeter of rectangle
{
return (2*length)+(2*width);
}
double Rectangle::setlength (double l)
{
if (l >= 0 && l <= 20)
length = l;
return -1;
}
double Rectangle::setwidth (double w)
{

if (w >= 0 && w <= 20)
width = w;
return -1;
}
double Rectangle::getlength() const
{
return length;
}
double Rectangle::getwidth() const
{
return width;
}

hw04.cpp

#include"Rectangle.h"   

#include<iostream>

using namespace std;

int main(int argc ,char ** argv[])

{

int l,w;

//when we will execute the code we will enter the length and width seperated by whitespace in command line

l=argv[0]; //the first input will be assigned to l

w=argv[1];//the second input will be assigned to w

Rectangle first; //object of class rectangle

first.setlength(l); //this function takes the length and checks the condition for length

first.setwidth(w);//this function takes the length and checks the condition for width

first.getlength();//to print the length

first.getwidth();//to print the width

cout << "Area: " << first.area() <<endl;

cout<< "Perimeter: " << first.perimeter() << endl ;

return 0;

} // End of main()

#ifndef RECTANGLE_H

#define RECTANGLE_H

class Rectangle // Class Rectangle
{
public:
Rectangle(float=1, float=1); // Constructor

//set functions

double setlength( double);
double setwidth( double);

//get functions

double getlength() const;
double getwidth() const;

//calculation functions

double perimeter() const;
double area() const;

private:
float length;
float width;
}; // End of class Rectangle