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...
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...
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{...
IN C++, ONE FILE Also include the main class that just runs these functions, thanks. Write...
IN C++, ONE FILE Also include the main class that just runs these functions, thanks. Write a recursive function to produce a pattern of n lines of asterisks. The first line contains one asterisk, the next line contains two, and so on, up to the nth line, which contains n asterisks. For example, if the non-negative integer is 5, the pattern generated is: * ** *** **** ***** Prototype: void pattern(unsigned n); Write a recursive function, sum_range that finds the...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle 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 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 specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle 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 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 specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT