This assignment is an introduction to the C++ classes and optionally File I/O for bonus points. We will learn the basics of class design, and we will put it to use creating a couple of more advanced classes.
Collaboration:
As with most laboratory assignments in this course this laboratory assignment is to performed by an individual student. You can help each other learn by reviewing assignment materials, describing to each other how you are approaching the problem, and helping each other with syntax errors
Specification: Part A: More Classes
Task 1: Circle Class:
Submit as File: circle.cpp
Notes:
You will want to add an overloaded == operator to the public section of the Point class using the following:
Once you have completed that you should be able to do the following with any Point related objects.
if(P1 == P2) cout << “Points are equal” << endl; else cout << “Points are not equal” << endl;
You may want to copy the line.cpp file from lab8 to circle.cpp. The circle class is quite similar to the line class. It uses two point objects just like the line class.
Create an object to implement a “circle” class which allows the programming to store a circle object. The object should use the “point” class developed previously. You will be given the center point and one point on a circle. The object should have at least two constructors, appropriate set/get functions, and overloaded I/O functions. It should also include functions that return the proper value for the following:
1. Determine if the object is a circle.
Point test: If both points are the same, a circle cannot be formed. This should return a bool.
2. Calculates the radius using the line distance formula
3. Calculates the diameter.
4. Calculates the area of a circle.
5. Calculates the circumference of a circle.
6. Overload (cin) operator that lets the user provide inputs to construct a Circle. Refer Laboratory 8 for this purpose.
7. Overload << (cout) operator that lets the user display the Circle and its properties (1-5 specifications above).Refer Laboratory 8 for this purpose.
8. Overload == operator (public ) that compares two Circle objects and returns either true or false. IT returns true if both circles are the same (same
center and radius) and false otherwise. To compare them, both circles have to be valid (Use Step 1). If using “point” class to build circles, assume P1 is the center.
Task 2: Triangle class:
Submit as File: triangle.cpp
Note:
You will want to copy the Circle class implementation to triangle.cpp, rename the class to Triangle, and add another Point object to store the third point of a triangle. After that is complete add a third point to both cin/cout overloaded operators. Remove or modify any remaining circle related functions/methods.
Design a C++ class to implement a “triangle” - which allows the program to store a triangle object. The object should use the “point” class developed previously. The object should have at least two constructors, appropriate set/get functions. It should also include functions that return the proper value for the following:
1. Determines if your object is a triangle. Use the collinearity test described at the end of this document.
This should return a bool
2. Calculates the area of a triangle.
3. Calculates the perimeter of the triangle.
4. Determines if the triangle is an equilateral triangle.
5. Overload (cin) operator that lets the user provide inputs to construct a
Triangle. Refer Laboratory 8 for this purpose.
6. Overload << (cout) operator that lets the user display the Triangle and its properties (1-4 specifications above). Refer Laboratory 8 for this purpose.
7. Overload == operator (public ) that compares two triangle objects and returns either true or false. IT returns true if both triangles are the same (same points) and false otherwise. To compare them, both triangles have to be valid (Use Step 1).
Task 3: Quadrilateral Class:
Submit as File: quad.cpp
Create an object to implement a Quadrilateral class which allows the programming to store a quadrilateral object. The object can use either the “point” or “line” class developed previously. The object should have at least two constructors, appropriate set/get functions. It should also include functions that return the proper value for the following:
1. Determine if your object can be a quadrilateral. 4 points can form a quadrilateral, when no three points lie on the same line. Use the collinearity test described at the end of this document.
This should return a bool.
2. Calculate the area of the Quadrilateral. A shoelace formula to compute this is posted below.
3. Overload (cin) operator that lets the user provide inputs to construct a Quadrilateral. Refer Laboratory 8 for this purpose.
4. Overload << (cout) operator that lets the user display the Quadrilateral and its properties (1-3 above specifications). Refer Laboratory 8 for this purpose.
Submit separate source files for all the class implementations above. You can test your classes and functionality in main() whenever required.
Part B (Optional) File I/O:
Submit as File: shape-info.cpp
You are also provided text file - shapes.txt which contains coordinate(s) information about the shapes. For example, the shape information in the text file can look like below (find the meaning in comments - only in this document):
Assume that, 3 (x,y) pairs (or 6 numbers) MAY form a Triangle, 4 pairs MAY form a Quadrilateral and 2 pairs MAY form a circle (one being the center and the other will be a point on the circumference). Any line with different number of coordinates is invalid input.
In this task you will be reading text from shapes.txt file and writing into another file shapes-info.txt.
Steps to implement file I/O:
1. Create an ifstream object and open the input text file shapes.txt. Also create an ofstream object and open the text file shapes-info.txt.
2. Read a single line from input file using input stream object into a temporary string. Taking hint from breakup_string.cpp (requires sstream header file), break up the temporary string into a numbers (coordinates).
3. Determine if number of coordinates are sufficient to form any of the shapes. If not, print out the invalidity in the output file and repeat step 2 (one example is shown below).
4. If from step 3 the number of coordinates are valid, construct the specific shape.
5. If the shape can indeed be created (collinearity test for Triangle or Quadrilateral and point test for circle), using ofstream object, print all the calculations into the output text file (output examples are shown below).
6. Repeat steps 2-5 until end-of-file (eof()) is encountered in the input stream.