Assign-07
Shapes – Adding Operators
PROG1385 Page 1
This set of assignments (A-04, A-07 and A-08) are meant to give you practice (once again) at developing class definitions from scratch – but in this second installment – you will be
adding some overloaded operators to your existing classes. The set of activities are broken down into a 3 stages.
• A-04 : Gets you to develop 3 classes (with inheritance and polymorphism) as well as a test harness
• A-07 : Gets you to use some operator overloading
• A-08 : Gets you to develop some template functions and exception handling
• You may choose to do any or all of these assignments with a partner
Commenting in Assign-04, Assign-07 and Assign-08
In order to give you practice creating and writing DOxygen style comments – I want you to comment these 3 assignments using DOxygen. This means that all of your class header
and method header comments must be written so that DOxygen can extract them and produce the set of online documentation. As well, I want you to also have commented all of
the different class’ data-members and constant values (if any) to also be extracted by DOxygen. You will want to revisit the DOxygen-Dog example from Module-06 to remind
yourself how this is done. One final DOxygen expectation … I want you to also have a main project page for the Shapes project. You can place whatever you want on this main
project page, but remember that the purpose of this page is to inform the reader about the project – what is the project all about? What is it modelling? Perhaps you could also tell
the reader about what underlying OOP concepts and techniques are being used in the implementation?
Please refer to the How to Create DOxygen Comments document in the DOxygen Commenting – HELP area of Assignment-04 for instructions on how to create
and do your DOxygen style commenting.
STEP 1 – Connecting to your Source Repository and Extracting your Starting Point
- Since this exercise is based upon A-04 – all you need to do is to sign-out your existing Shape code from your repository
a. Create a directory on the local machine – e.g. C:\SETRepo
b. Connect to your repository in the cloud (remembering your repository’s URL and your login credentials) using the Tortoise SVN client and perform an SVN
Checkout. What you should notice is that you have signed out your DisneyCharacter solution as well as your Shapes solution. - If you like you can erase the DisneyCharacter subdirectory as you won’t be modifying that source in this exercise
- Once you have the Shapes solution – you are ready to begin this exercise … so let’s get into the requirements …
Assign-07
Shapes – Adding Operators
PROG1385 Page 2
STEP 2 – The Requirements and Programming
In this exercise, you will take your existing classes from A-04 and add to them :
The Shape Class
• No changes
The Circle Class
• Add the following methods to this class – watch best practices !!
o An overloaded + operation
When adding one circle to another, the resultant circle will take on the colour of the left-hand operand (the LHS)
The resultant radius is the sum of the left and right operand’s radii
o An overloaded * operation
When multiplying one circle with another, the resultant circle will take on the colour of the right-hand operand (RHS)
The resultant radius is the product of the left and right operand’s radii
o An overloaded = operation
o An overloaded == operation
Which will return true if the 2 circles being compared are equal in their radius and colours values
NOTE: Since the radius value is represented by a float data-type – you’ve got to ask yourself … are float values absolutely accurate in their values? You
may want to implement some algorithm here to see if the 2 circle’s radius values are approximately equal within a small variance
The Square Class
• Add the following methods to this class – watch best practices !!
o An overloaded + operation
When adding one square to another, the resultant square will take on the colour of the left-hand operand (the LHS)
The resultant sideLength is the sum of the left and right operand’s sideLengths
o An overloaded * operation
When multiplying one square with another, the resultant square will take on the colour of the right-hand operand (RHS)
The resultant sideLength is the product of the left and right operand’s sideLengths
o An overloaded = operation
o An overloaded == operation
Which will return true if the 2 squares being compared are equal in their sideLengths and colours values
NOTE: Since the sideLength value is represented by a float data-type – you’ve got to ask yourself … are float values absolutely accurate in their values?
You may want to implement some algorithm here to see if the 2 square’s sideLength values are approximately equal within a small variance
Assign-07
Shapes – Adding Operators
PROG1385 Page 3
The myShape Main
• You can remove all of the source from this module left over from A-04
• Modify your testHarness to create the following shapes
o you can simply instantiate the objects required – no need for user input …
o Circle shape (variable named round1) with radius 5.5 cm and colour “red”
o Circle shape (variable named round2) with radius 10.5 cm and colour “blue”
o Circle shape (variable named playARound) – instantiated with the default constructor of the Circle class
o Square shape (variable named square1) with sideLength of 5 cm and colour “orange”
o Square shape (variable named square2) with sideLength of 12 cm and colour “purple”
o Square shaped (variable named playASquare) – instantiated with the default constructor of the Square class
• After creating these shapes, call the appropriate method to print out the specifics of each shape
• Then use your overloaded operators to
o Add round2 to round1 and store it in playAround (i.e. round1 + round2)
You should end up with playARound being “red” with radius of 16.0 cm
o Add square1 to square2 and store in playASquare (i.e. square2 + square1)
You should end up with playASquare being “purple” with sideLength of 17 cm)
o Print out the specifics of playARound and playASquare
o Multiply round1 by round2 and store in playARound (i.e. round1 * round2)
You should end up with a playARound being “blue” with a radius of 57.75 cm
o Multiply square2 by square1 and store in playASquare (i.e. square2 * square1)
You should end up with a playASquare being “orange” with a sideLength of 60 cm
o Print out the specifics of playARound and playASquare
o Assign round1 to playAround, and then test to see if they are equivalent
i.e. playRound = round1; followed by if(playAround == round1)
If they are – then print “Hurray !!” to the screen
If they are not equal – then print “Awww !!” to the screen
Assign-07
Shapes – Adding Operators
PROG1385 Page 4
STEP 3 – Putting your Code back into the Repository
When you have made these changes to your class definitions and mainline – you can simply save your VS Solution and commit your files.
• Start by saving all files in solution and existing Visual Studio.
• In the previous exercise – you were reminded of repository best-practices
o Where each source file needs to be committed individually with its own custom / specific comment
o So locate all of your source files (.H files and .CPP files) and commit them one-by-one
• After committing the source files – you return to the C:\SETRepo directory where you do a final commit on the Shapes folder to pick up any stray files that also need to be
committed
What to Submit
Before submitting you’re A-07 solution, please remember to run DOxygen and have it extract your project and class documentation. As we saw in the Module-06 example, this
produces all of the necessary web pages in an HTML directory. After running DOxygen and producing the output, please ZIP up the HTML folder into an HTML.ZIP file.
Regardless of whether you choose to store your class definition in a repository, please ZIP up the 7 source files [Shape.h, Circle.h, Square.h, Shape.cpp,
Circle.cpp, Square.cpp and myShape.cpp] as well as the HTML.ZIP file (with your DOxygen comments) and submit all of these files as a single submission ZIP file into the
assignment drop-box by the deadline.
• If you are doing this assignment on your own - please give your ZIP submission the filename lastName-firstInitial.zip
• e.g. if you are Sally Jones – then your ZIP should be named jones-s.zip
• If you are working with a partner then include both your names in the ZIP filename
• e.g. if Sally Jones is working with John Smith – then your ZIP should be named jones-s-smith-j.zip
• If working with a partner, only one partner needs to submit the solution
HINT – About const Accessors in this assignment
As discussed in Module-13, you'll need to create some const versions of accessors for the Shape class (and potentially the Circle and Square too - if you're following best practices).
What are const versions of accessors you ask ?? Revisit Module-13 (starting at slide 28) and read ... also see my example code from Module-13.
When your overloaded operator passes the RHS (right-hand operand) into the method as a const reference (e.g. in an overloaded binary arithmetic operator) and you need to get a
value from the object (or the object's parent) and you choose to get it through the accessor (e.g. GetColour()) - then you'll need to create a const accessor in the class you are
trying to access.
This means that you need to create two accessors in that class ... a const version of the accessor as well as a non-const version. In creating these two versions of the accessors -
you will also need to duplicate code. But you can do this safely knowing that you're doing it in the name of best practices!!