c++ need little help with this: You will be coding a class hierarchy composed of two shapes: rectangle, and square. Make sure all your member
c++ need little help with this:
You will be coding a class hierarchy composed of two shapes: rectangle, and square.
Make sure all your member functions are defined in the implementation section of the class. Do not use any inline class member functions.
Use the following file, Shapes.cpp, to start coding your program. You will be modifying this file by writing code for each member function given for each class and your main function. Do not modify the declaration section given for each class at all. Don't forget to modify/add comments to your code.
The Rectangle.class is the base class and contains two protected data members to store a sideA and sideB.
a. | The Rectangle class constructor will accept two arguments which will contain a value for each side of the rectangle (sideA and sideB). Assign a value to sideA and sideB using the value of the two parameters. |
b. | Code getSideA and getSideB member functions to return back the value of the associated data member. |
c. | Code the setSideA and setSideB member functions to assign the value passed to the function to the associated data member. Make sure within the member function that the value assigned is not zero or less. If the value passed to the parameter is zero or less assign the side to a default value of 1. |
d. | Code the computePerimeter member function to compute and return back the perimeter of a Rectangle. This formula is given in the hyperlink for step 1. |
e. | Code the computeArea memberr function to compute and return back the area of a Rectangle. This formula is given in the hyperlink for step 1. |
f. | Overload the increment operator to increment (add to) the value stored in sideA and sideB by one. |
g. | Overload the decrement operator to decrement (subtract from) the value stored in sideA and sideB by one. |
h. | Overload the equality operator (==) to test two Rectangle objects for equality. This function should return back true if the two Rectangle objects are equal and false if the two Rectangle objects are not equal. The two Rectangle objects are equal if both the value of the sideA data members are equal and the value of the sideB data members are equal. |
Code the Square class to inherit from the Rectangle class. The inheritance will be public. No additional data members should be added to the Square class because it will inherit the data members from the Rectangle class. Other than what is already contained in the Shapes.cpp file (see step3), no additional member functions should be added to the Square class because it will inherit or override the member functions from the Rectangle class.
a. | The setSideA member function will override this same function from the parent class. Within this function assign the value of the data members sideA and sideB to the argument of this function. This means both data members will be assigned the same value because sideA and sideB of a square are equal. Make sure within the member function that the value assigned is not zero or less. If the value passed to the parameter (the argument) is zero or less assign both sides to a default value of 1. |
b. | The setSideB member function will override this same function from the parent class. Within this function assign the value of the data members sideA and sideB to the argument of this function. This means both data members will be assigned the same value because sideA and sideB of a square are equal. Make sure within the member function that the value assigned is not zero or less. If the value passed to the parameter (the argument) is zero or less assign both sides to a default value of 1. |
c. | The Square class constructor will accept one argument which will contain a value for each side of the square (sideB will be assigned the same value as sideA). Use a base/member initialization list to call the Rectangle constructor to assign values for sideA and sideB of the Square. Keep in mind that sideA and sideB of a square are equal. |
The main function will be used to test your class hierarchy.
a. | Test the Square class first. Prompt the user to enter in the side length of a square. Instantiate a Square object using the data entered in by the user. Call the displayData function passing it the Square object to display the values for this square. |
b. | Use the overloaded increment operator with the Square object from the above step. You have to use the prefix increment operator in order for your program to compile and run. Call the displayData function passing it the Square object to display the new values for this square. |
c. | Test the Rectangle class second. Prompt the user to enter in the first side length of a rectangle (sideA). Prompt the user to enter in the second side length of a rectangle (sideB). Instantiate a Rectangle object using the data entered in by the user. Call the displayData function passing it the Rectangle object to display the values for this rectangle. |
d. | Use the overloaded decrement operator with the Rectangle object from the above step. You have to use the prefix decrement operator in order for your program to compile and run. Call the displayData function passing it the Rectangle object to display the new values for this rectangle. |
e. | Use the overloaded == operator to display a message indicating whether the Rectangle object and the Square object from the above steps are the same or different. |
Make sure your output format matches the sample output.
Sample output below:
d
This is the beginning
Testing the Square Class Enter length of side: 4 The Tength of side A 1s 4 The length of side B is 4 The perimeter is 16 The area is 16 After calling the increment operator e Tength of side A 1s 5 e Tength of side B 1s 5 The perimeter is 20 The area is 25 esting the Rectangle Class Enter length of first side: 2 Enter length of second side: 3 The Tength of side A 1s 2 The length of side B is 3 The perimeter is 10 The area is 6 After calling the decrement operator The Tength of side A 1s 1 The length of side B is 2 The perimeter is 6 The area is 2 he two objects do not have equal sidesStep by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started