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...
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...
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...
using C# Write a program named RectangleArea tocalculate the area of a rectangle with a...
using C# Write a program named RectangleArea to calculate the area of a rectangle with a length of 15.8 and a width of 7.9. The program should have two classes, one is theRectangleArea holding the Main(), and the other one is Rectangle. The Rectangleclass has three members: a property of length, a property of width, and a method with a name of CalArea() to calculate the area. You can invoke the auto-provided constructor or write a self-defined constructor to initialize...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
Using the class Date that you defined in Exercise O3, write the definition of the class...
Using the class Date that you defined in Exercise O3, write the definition of the class named Employee with the following private instance variables: first name               -           class string last name                -           class string ID number             -           integer (the default ID number is 999999) birth day                -           class Date date hired               -           class Date base pay                 -           double precision (the default base pay is $0.00) The default constructor initializes the first name to “john”, the last name to “Doe”, and...
Write the definition for a generic / Template class called time that has hours and minutes...
Write the definition for a generic / Template class called time that has hours and minutes as structure. The class has the following member functions: SetTime to set the specified value in object ShowTime to display time object Sum to sum two time object & return time Write the definitions for each of the above member functions. Write main function to create three time objects. Set the value in two objects and call sum() to calculate sum and assign it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT