Lab 7_
Lab 7_
Fall 2024
Lab 07
Instructions:
You are tasked with designing and implementing a Rectangle class in C++ to represent a
rectangle's dimensions and perform various operations on it. The class should encapsulate the
rectangle's properties and provide functionality for comparison, copying, validation, and
calculations.
Requirements:
1. Attributes:
o The class should include the following attributes:
int length: Represents the length of the rectangle.
int width: Represents the width of the rectangle.
2. Copy Constructor:
o Implement a copy constructor to create a new Rectangle object as a copy of an
existing Rectangle object.
3. Destructor:
o Implement a destructor to handle cleanup operations, ensuring the program
remains robust (even if no dynamic memory is used).
4. Overloaded Constructor with Default Values:
o Provide a constructor to initialize the Rectangle object with:
Default values (length = 1, width = 1).
Specific values provided by the user, ensuring dimensions are valid.
5. Validation:
o Ensure that rectangle dimensions are always valid:
length > 0
width > 0
o Throw appropriate exceptions for invalid dimensions.
6. Getter Functions:
o Provide getter functions (getLength and getWidth) to retrieve the dimensions of
the rectangle. Setters are not required.
7. Operator Overloading:
o Implement the following comparison operators to compare the areas of two
rectangles:
operator==: Check if the areas of two rectangles are equal.
operator!=: Check if the areas of two rectangles are not equal.
operator>: Check if the area of one rectangle is greater than the other.
operator<: Check if the area of one rectangle is smaller than the other.
operator<=: Check if the area of one rectangle is smaller than or equal to
the other.
operator>=: Check if the area of one rectangle is greater than or equal to
the other.
8. Member Functions:
o calculateArea(): Compute and return the area of the rectangle.
o calculatePerimeter(): Compute and return the perimeter of the rectangle.
9. Display Function:
o Include a member function to display the rectangle's details (length, width, area,
and perimeter).
Extended Task 1:
Design a class Rectangle to represent a rectangle's dimensions and perform operations on it.
The class should include the following attributes:
Attributes:
o int* length
o int* width
Requirements:
1. Copy Constructor:
o Implement a copy constructor to create a new Rectangle object as a copy of
another Rectangle object.
2. Destructor:
o Ensure dynamic memory for length and width is properly deallocated.
3. Overloaded Constructor with Default Values:
o Provide a constructor to initialize the Rectangle object with default values (e.g.,
length = 1 and width = 1) or specific values provided by the user.
4. Getter for Each Attribute:
o Implement getter functions for length and width. Setters are NOT required.
5. Operator Overloading: Implement the following overloaded operators to compare the
areas of two rectangles:
o operator == (Check if the areas of two rectangles are equal)
o operator > (Check if the area of one rectangle is greater than the other)
o operator != (Check if the areas of two rectangles are not equal)
o operator < (Check if the area of one rectangle is smaller than the other)
o operator <= (Check if the area of one rectangle is smaller than or equal to the
other)
o operator >= (Check if the area of one rectangle is greater than or equal to the
other)
6. Validation: Ensure that the dimensions remain valid:
o length > 0
o width > 0
Stretch Goal:
Add a member function calculatePerimeter() to return the perimeter of the rectangle and
another function calculateArea() to return its area.
Task 2:
Let’s create a class Time to work with a 12-hour formatted time having the
following attributes:
int* hours
int* minutes
int* seconds
Provide the following functions:
1 Copy Constructor
2 Destructor
3 Overloaded constructor with default values
4 Getter of each attribute (setters are NOT required)
5 operator ==
6 operator >
7 operator !=
8 operator <
9 operator <=
10 operator >=
Make sure to check that the time remains valid in all the functions that are:
A. 1 <= hours <=12
B. 0 <= minutes < 60
C. 0 <= seconds < 60
Task 3:
Let’s create a class Date to store a date having the following attributes:
int* day
int* month
int* year