Question

In: Computer Science

Using basic C++( without using <Rectangle.h> ) Write the definition for a class called Rectangle that...

Using basic C++( without using <Rectangle.h> )

  1. Write the definition for a class called Rectangle that has floating point data members length and width. The class has the following member functions:
    • void setlength(float) to set the length data member
    • void setwidth(float) to set the width data member
    • float perimeter() to calculate and return the perimeter of the rectangle
    • float area() to calculate and return the area of the rectangle
    • void show() to display the length and width of the rectangle
  1. Write the definitions for each of the above member functions outside the class.
  2. Write client code (main.cpp) to create two rectangle objects. Set the length and width of the first rectangle to 5 and 2.5. Set the length and width of the second rectangle to 7.5 and 12.6. Display each rectangle and its area and perimeter
  1. Add a default constructor to your Rectangle class that initializes the length to 1 and the width to 1.
  2. Add a constructor to your Rectangle class that has two float parameters. The constructor will initialize the length to the value given in the first parameter, and the width to the value given in the second parameter.
  3. Add a member function sameArea that has one parameter of type Rectangle. sameArea returns true if the two Rectangles have the same area, and returns false if they don't.
  4. Write client code to create one Rectangle object which is not initialized, and another Rectangle object which is initialized to have length 4 and width 3. Display each Rectangle and its area and perimeter. Check whether the two Rectangles have the same area and print a message indicating the result. Set the length and width of the first rectangle to 2 and 6. Display each Rectangle and its area and perimeter again. Again, check whether the two Rectangles have the same area and print a message indicating the result.

Solutions

Expert Solution

Note : Do you want the whole code in single file ...? Could u plz tell me ...so that I will divide the divide the code...thank You

/**********************************************/
#include <iostream>
#include <iomanip>

using namespace std;

class Rectangle
{
private:
float length;
float width;

public:
Rectangle()
{
this->length = 1;
this->width = 1;
}
Rectangle(float l, float w)
{
this->length = l;
this->width = w;
}

void setLength(float l)
{
this->length = l;
}
void setWidth(float w)
{
this->width = w;
}
float perimeter()
{
return 2 * (length + width);
}
float area()
{
return length * width;
}
void show()
{
cout << "Length :" << length << endl;
cout << "Width :" << width << endl;
}

bool sameArea(Rectangle r)
{
if (area() == r.area())
return true;
else
return false;
}
};
int main()
{

// Creating instance of Rectangle class
Rectangle r1(5, 2.5), r2(7.5, 12.6);
cout << "Rectangle#1:" << endl;
r1.show();
// Displaying the area of Rectangle
cout << "Area :" << r1.area() << endl;
// Displaying the perimeter of Rectangle
cout << "Perimeter :" << r1.perimeter() << endl;
cout << "\nRectangle#2:" << endl;
r2.show();
// Displaying the area of Rectangle
cout << "Area :" << r2.area() << endl;
// Displaying the perimeter of Rectangle
cout << "Perimeter :" << r2.perimeter() << endl;

// Creating instance of Rectangle class
Rectangle r3, r4(4, 3);
cout << "Rectangle#3:" << endl;
r3.show();
// Displaying the area of Rectangle
cout << "Area :" << r3.area() << endl;
cout << "Perimeter :" << r3.perimeter() << endl;
cout << "\nRectangle#4:" << endl;
r4.show();
// Displaying the area of Rectangle
cout << "Area :" << r4.area() << endl;
cout << "Perimeter :" << r4.perimeter() << endl;

if (r3.sameArea(r4))
{
cout << "Rectangle#3 and Rectangle#4 have same area" << endl;
}
else
{
cout << "Rectangle#3 and Rectangle#4 have not same area" << endl;
}

r1.setLength(2);
r1.setWidth(6);
cout << "Rectangle#1:" << endl;
r1.show();
cout << "Area :" << r1.area() << endl;
cout << "Perimeter :" << r1.perimeter() << endl;

if (r1.sameArea(r4))
{
cout << "Rectangle#1 and Rectangle#4 have same area" << endl;
}
else
{
cout << "Rectangle#1 and Rectangle#4 have not same area" << endl;
}


return 0;
}

/**************************************************/

/**************************************************/

Output:

/**************************************************/


Related Solutions

Write the definition for a generic class called Rectangle that has data members length and width....
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 has one parameter of type Rectangle. sameArea...
Write the definition for a generic class called Rectangle that has data members length and width....
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 has one parameter of type Rectangle. sameArea...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length,...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length, width and colour as arguments. Define enough functions to make the class not abstract
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++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a...
c++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data:  Account holder’s name (string)  Account number (int)  Account type (string, check/savings/business)  Balance (double)  Interest rate (double) – store interest rate as a decimal number.  Add appropriate member functions to manipulate an object. Use a static member in the class to automatically assign account numbers. 1.2 Implement...
1. Write a class called Rectangle that maintains two attributes to represent the length and width...
1. Write a class called Rectangle that maintains two attributes to represent the length and width of a rectangle. Provide suitable get and set methods plus two methods that return the perimeter and area of the rectangle. Include two constructors for this class. One a parameterless constructor that initializes both the length and width to 0, and the second one that takes two parameters to initialize the length and width. 2. Write a java program (a driver application) that tests...
CODE must using C++ language. Write the definition of the class dayTye that implements the day...
CODE must using C++ language. Write the definition of the class dayTye that implements the day of the week in a program. The class dayType should store the day of the week as integer. The program should perform the following operations on an object of type dayType 1. set the day 2. display the day as a string - Sunday, ... Saturday 3. return the day as an integer 4. return the next as an integer 5. return the previous...
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...
C++ Task: You are to write a class called Monomial, using filenames monomial.h and monomial.cpp, that...
C++ Task: You are to write a class called Monomial, using filenames monomial.h and monomial.cpp, that will allow creation and handling of univariate monomials, as described below. Monomial Description Each monomial object has following properties: Each monomial has a coefficient and a power. Monomial power is always positive. Monomial power is always integer. Monomials cannot be added if they have different powers Class Details The single constructor for the Monomial class should have 2 parameters: an floating point coefficient value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT