Question

In: Computer Science

C++ // Rectangle.cpp is the Rectangle class function implementation file. #include "Rectangle.h" /******************************************************************* * Rectangle::setLength *...

C++

// Rectangle.cpp is the Rectangle class function implementation file.
#include "Rectangle.h"

/*******************************************************************
 *                    Rectangle::setLength                         *
 * If the argument passed to the setLength function is zero or     *
 * greater, it is copied into the member variable length, and true *
 * is returned. If the argument is negative, the value of length   *
 * remains unchanged and false is returned.                        *
 *******************************************************************/
bool Rectangle::setLength(double len)
{
        bool validData = true;

   if (len >= 0)               // If the len is valid
      length = len;            // copy it to length
   else
      validData = false;       // else leave length unchanged
   
   return validData;
}

/******************************************************************
 *                    Rectangle::setWidth                         *
 * If the argument passed to the setWidth function is zero or     *
 * greater, it is copied into the member variable width, and true *
 * is returned. If the argument is negative, the value of width   *
 * remains unchanged and false is returned.                       *
 ******************************************************************/
bool Rectangle::setWidth(double w)
{
        bool validData = true;

   if (w >= 0)                 // If w is valid
      width = w;               // copy it to width
   else
      validData = false;       // else leave width unchanged
   
   return validData;
}

/**************************************************************
 *                     Rectangle::getLength                   *
 * This function returns the value in member variable length. *
 **************************************************************/
double Rectangle::getLength()
{
        return length;
}

/**************************************************************
 *                     Rectangle::getWidth                    *
 * This function returns the value in member variable width.  *
 **************************************************************/
double Rectangle::getWidth()
{
        return width;
}

/*******************************************************************
 *                        Rectangle::getArea                       *
 * This function calculates and returns the area of the rectangle. *
 *******************************************************************/
double Rectangle::getArea()
{
        return length * width;
}

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// Rectangle.h

#ifndef RECTANGLE_H
#define RECTANGLE_H

class Rectangle
{
public:
Rectangle();
bool setLength(double len);
bool setWidth(double w);
double getLength();
double getWidth();
double getArea();
private:
// Declaring variables
double length;
double width;
};
#endif

================================

// Rectangle.cpp


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


Rectangle::Rectangle()
{
this->length=0;
this->width=0;

}
/*******************************************************************
* Rectangle::setLength *
* If the argument passed to the setLength function is zero or *
* greater, it is copied into the member variable length, and true *
* is returned. If the argument is negative, the value of length *
* remains unchanged and false is returned. *
*******************************************************************/
bool Rectangle::setLength(double len)
{
bool validData = true;

if (len >= 0) // If the len is valid
length = len; // copy it to length
else
validData = false; // else leave length unchanged

return validData;
}

/******************************************************************
* Rectangle::setWidth *
* If the argument passed to the setWidth function is zero or *
* greater, it is copied into the member variable width, and true *
* is returned. If the argument is negative, the value of width *
* remains unchanged and false is returned. *
******************************************************************/
bool Rectangle::setWidth(double w)
{
bool validData = true;

if (w >= 0) // If w is valid
width = w; // copy it to width
else
validData = false; // else leave width unchanged

return validData;
}

/**************************************************************
* Rectangle::getLength *
* This function returns the value in member variable length. *
**************************************************************/
double Rectangle::getLength()
{
return length;
}

/**************************************************************
* Rectangle::getWidth *
* This function returns the value in member variable width. *
**************************************************************/
double Rectangle::getWidth()
{
return width;
}

/*******************************************************************
* Rectangle::getArea *
* This function calculates and returns the area of the rectangle. *
*******************************************************************/
double Rectangle::getArea()
{
return length * width;
}

==============================

// main.cpp

#include <iostream>
using namespace std;
#include "Rectangle.h"
int main()
{
double length,width;
Rectangle r;
while(true)
{
cout<<"Enter Length :";
cin>>length;
if(!r.setLength(length))
{
cout<<"** Invalid.Must be >=0 **"<<endl;
}
else
{
break;
}
}
  
while(true)
{
cout<<"Enter Width :";
cin>>width;
if(!r.setWidth(width))
{
cout<<"** Invalid.Must be >=0 **"<<endl;
}
else
{
break;
}
}
  
cout<<"Length "<<r.getLength()<<endl;
cout<<"Width "<<r.getWidth()<<endl;
cout<<"Area of Rectangle :"<<r.getArea()<<endl;
  
return 0;
}

====================================

Output:

=====================Could you plz rate me well.Thank You


Related Solutions

c++ E2b: Design a class named Rectangle to represent a rectangle. The class contains:
using c++E2b: Design a class named Rectangle to represent a rectangle. The class contains:(1) Two double data members named width and height which specifies the width and height of the rectangle .(2) A no-arg constructor that creates a rectangle with width 1 and height 1.(3) A constructor that creates a rectangle with the specified width and height .(4) A function named getArea() that returns the area of this rectangle .(5) A function named getPerimeter() that returns the perimeter of this...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full specification of the class is: A data member counter of type int. An data member named counterID of type int. A static int data member named nCounters which is initialized to 0. A constructor that takes an int argument and assigns its value to counter. It also adds one to the static variable nCounters and assigns the (new) value of nCounters to counterID. A...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to create a rectangle with user-specified height and width. 4. Method getArea() that returns...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 0 in the default constructor. A non-argument constructor method to create a default rectangle. Another constructor method to create a rectangle with user-specified height and width. Method getArea() that returns the area. Method...
In C++ Consider the binary search tree implementation in file bintree.cp a)Add to the TreeNode class...
In C++ Consider the binary search tree implementation in file bintree.cp a)Add to the TreeNode class a pointer to the parent node. Modify the insert and erase functions to properly set those pointers. b)Then define a TreeIterator class that contains a pointer to a TreeNode and has member functions get and next. i)The iterator’s get member function should return the data value of the node to which it points. ii)The iterator’s next member function should find the next element in...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of complex Class The complex class presents the complex number X+Yi, where X and Y are real numbers and i^2 is -1. Typically, X is called a real part and Y is an imaginary part of the complex number. For instance, complex(4.0, 3.0) means 4.0+3.0i. The complex class you will design should have the following features. Constructor Only one constructor with default value for Real...
C++ file=.class definitions.h Define a Fraction class with num and den as its private data. Include...
C++ file=.class definitions.h Define a Fraction class with num and den as its private data. Include a constructor to initialize the fraction to 0/1, a copy constructor, a destructor, and overloading functions to overload the assignment operator =, the comparison operators <, >, ==, !=, arithmetic operators +, +=, -, -=, *, *=, /, /=, as well as friend functions (non-member) to overload << and >> to output and input a fraction (see book example). Also, include a private member...
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle....
PUT IN JAVA PROGRAMMING The Rectangle class: 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 a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area of...
Complete the task below C++ This header file declares the Comp315Array Class **************************/ //Include the libraries...
Complete the task below C++ This header file declares the Comp315Array Class **************************/ //Include the libraries #include <iostream> //Allow the use of cin and cout //Declare namespace std for using cin and cout using namespace std; //Class declaration class Comp315Array{ //Public members public: /**************** Default Constructor Task: assign 0 to the array size *********/ Comp315Array(); /**************** Constructor Task: Define an int array of size n *********/ Comp315Array(int n); /***************** getSize method Task: return the array size ******************/ int getSize(); /*****************...
I want to indent this c++ program #include<iostream> using namespace std; class Rectangle{ private: double width;...
I want to indent this c++ program #include<iostream> using namespace std; class Rectangle{ private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; double getPerimeter() const; bool isSquare() const; }; void Rectangle::setWidth(double w){ width = w; } void Rectangle::setLength(double l){ length = l; } double Rectangle::getWidth() const{ return width; } double Rectangle::getLength() const{ return length; } // Added Method definitions double Rectangle::getArea() const{ return (width * length); } double Rectangle::getPerimeter() const{...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT