CSC210 (C++ Object Oriented Programming) Name: ______
Lab 2: Deriving a Cylinder Class & Sphere Class Signature: ______
Due Date: June 13, 2018 (has neither given nor received aid on this program)
Purpose: The purpose of this lab is to familiarize students with inheritance and two overriding functions. Chapter 11 of our textbook has excellent examples, and this lab will call on these techniques from Object Oriented Design (OOD).
Problem:
1. Example 10.8 p.689-691 has been modified. Please use my website for an updated circleType base class.
2. Derive a CylinderType class and a SphereType class from the base CircleType class. The cylinder derived class should have only two private members: double height & string name. The sphere class should have only one private member: string name. The cylinder constructor has two default parameters, and the sphere constructor has only one default parameter. Each derived class has a destructor. Each overrides the base class’ print(ostream&) function (which accepts a file object), an area() function that redefines the cylinder’s and sphere’s surface area. Each derived class separately defines a volume() function, an accessor ("getter") function for the height, and a mutator ("setter") function for setHeight(). Be sure to find the correct surface area for each 3D object, including both top & bottom circle base areas. Your sphere & cylinder function definitions must reuse the CircleType formulas such as CircleType area() and circumference(). The algorithms and examples of other derived classes will be discussed in our lessons. Heavily document your C++ source code with copious comments!
3. For all the derived objects, their constructor will call their own print(ostream&) function to echo each object’s calculations to both the console and file. Also, as good programming practice, keep your main function “clean”.
4. Without reading any data call a function to: First, test the circle, cylinder, and sphere default constructors. Second, test the setRadius for each of these default objects using 3.7775. Print each object’s calculations with the new values. Third, test the cylinder’s and sphere’s setHeight() using 7.333333333333. Again, print each object’s calculations with these new values. (I used a client function called defaultObjects().
5. For reading in the data, use an input file named cccInput.txt. Your client main program cccTest.cpp should read in the dimensions (radius and height) of 7 pairs of data, construct these with each 2 parameter constructor using the popular “while not end-of-file” loop. These objects should process and print their outputs to a file (echoed to the console) in neat right-justified columns with headings, including the radius, circumference, height, area, and volume of each derived cylinder and sphere. Use the constant M_PI found in <cmath>. To do this you will have to uncheck setting the compiler to C++11. You must use your instructor’s same data sample (ccsInput.txt) found on the website, for the first 7 inputs as well as the identically formatted output in the example to demonstrate the correct solutions to 3 decimal places. Additionally, choose 7 more inputs on your own. Note: your name is centered in the "by" line. Also, you should use the same vertical spacing as in my example output. Very important: Please define the volume and area in each derived class function using already defined base functions, specifically as circleType::area() as an example. Putting M_PI * radius * radius in these derived class function definitions shows a lack of understanding the usefulness of inheritance and its reusability. This misuse of the overridden function definitions will receive a penalty.
6. Helpful cylinder formulas used: volume = Π radius2 ● height
area = 2 Π radius2 + circumference ● height
7. Helpful sphere formulas used: volume = 4.0 /3.0 Π radius3
area = 4.0 Π radius2
8. Please email me each (do not zip) of these 9 files via the TCC email account to:
ccsTest.cpp (contains the main, title, defaults, and fileProcess functions)* (using at least these functions)
CylinderType.cpp (contains the derived class implementation)
SphereType.cpp (contains the derived class implementation)
CircleType.cpp (contains the base class implementation)
CylinderType.h (header file)
SphereType.h (header file)
CircleType.h (header file)
ccsInput.txt (input file with radius and height data for 7 cylinders and spheres)
ccsOutput.txt (contains your program’s output report with appropriate title and your name in the by line, centered)
9. On or before the due date, attach & email these 9 files to . Put Lab2 CSC210 in the subject line.
10. Place this signed specification sheet on top. Use your C++ editor to print hard copies of all 4 .cpp files with line #s, input and output .txt files (without line #s), and the completed answers to homework Ch.11 Exercises: 1 through 8, and exercise #12. Staple all printouts below this specification sheet. Put the homework, last. Staple these hard copies together and submit at the beginning of class.
Checklist (for hard copies):
Staple your required items in this order: a) this signed specification sheet, b) all four .cpp source code files with the ccsTest.cpp program first, c) your instructor’s ccsInput.txt file, d) your ccsOuput.txt (created by your program),
and e) your 9 homework answers (handwritten or typed).
Please, seek only my help (if necessary) in this lab assignment. Please ask very many questions in class!
Header files:
#ifndef H_CircleType
#define H_CircleType
#include <fstream>
using namespace std;
class CircleType
{
public:
CircleType(double r = 1.0);
//Constructor with one default parameter.
//Radius is set according to the parameter.
//Postcondition: radius = r or 1.0 if not specified
double getRadius() const;
//Function to return the radius.
//Postcondition: The value of radius is returned.
double area() const;
//Function to return the area of a circle.
//Postcondition: Area is calculated and returned.
double circumference() const;
//Function to return the circumference of a circle.
//Postcondition: Circumference is calculated and returned.
void print(ostream& ) const;
// function to print the radius, area, and circumference
// of the object (in one complete line)
void setRadius(double);
//Function to set the radius.
//Postcondition: if (r >= 0) radius = r;
// otherwise radius = 1;
private:
double radius;
string name;
};
#endif
#ifndef H_CylinderType
#define H_CylinderType
#include "CircleType.h"
#include <fstream>
using namespace std;
class CylinderType : public CircleType
{
public:
CylinderType(double rad = 1.0, double heit = 1.0);
//Constructor with two default parameters. Client assigned.
//Postcondition: if parameters given: radius = r; height = h;
// without parameters: radius = 1.0; and height = 1.0;
// name is assigned "Cylinder"
~CylinderType();
//Destructor destroys objects made, returns memory to the heap
double area() const;
//Function to return the surface area of a cylinder.
//Overrides the circle's area function.
//Postcondition: Surface area is calculated and returned.
double volume() const;
//Function to return the volume of a cylinder.
//Postcondition: Volume is calculated and returned.
void print(ostream& ) const;
// function to override circle's print
// displays the radius, surface area, and volume
double getHeight() const;
// set the height of the cylinder
string getName() const;
// gets the name of the object
void setHeight(double);
// set the height of the cylinder
private:
double height;
string name;
};
#endif
#ifndef H_SphereType
#define H_SphereType
#include "CircleType.h"
#include <fstream>
using namespace std;
class SphereType : public CircleType
{
public:
SphereType(double rad = 1.0);
//Constructor with one default parameter. Client assigned.
//Postcondition: if parameters are given: radius = r
// without parameters: radius = 1.0
// name is assigned "Sphere"
~SphereType();
//Destructor destroys any objects made, returns memory to the heap
double area() const;
//Function to return the surface area of a sphere
//Overrides the circle's area function.
//Postcondition: Surface area is calculated and returned.
double volume() const;
//Function to return the volume of a sphere
//Postcondition: Volume is calculated and returned.
void print(ostream& ) const;
// function to override circle's print
// displays the radius, height, surface area, and volume
double getHeight() const;
// Function to return the height of a sphere (really the same as diameter)
// Postcondition: height (diameter) is calculated and returned
string getName() const;
// gets the name of the object
void setHeight(double);
// sets the height (diameter) of a sphere
// by setting the circle’s radius to half the sphere’s height
private:
string name;
};
#endif
Your mandatory input file (found on my website and Y: drive) must be used. (You will append 7 more data lines):
2.5035 7.114
5.81 12.240
25.33 8.099
7.002 0.388
18.29 1.964
46.73 6.627
0.8949 19.754
(plus your additional 7 data lines)
My output file (except for your centered name, your format and spacing should be identical):
C I R C L E, C Y L I N D E R, S P H E R E C A L C U L A T I O N S
by Jeff Goldstein
Object Name Radius Circumf Height Area Volume
------
2D Circle 1.000 6.283 3.142
Cylinder 1.000 6.283 1.000 12.566 3.142
Sphere 1.000 6.283 2.000 12.566 4.189
2D Circle 3.777 23.735 44.829
Cylinder 3.777 23.735 1.000 113.393 44.829
Sphere 3.777 23.735 7.555 179.316 225.789
Cylinder 3.777 23.735 7.333 263.713 328.746
Sphere 3.667 23.038 7.333 168.948 206.492
Cylinder 2.503 15.730 7.114 151.283 140.074
Sphere 2.503 15.730 5.007 78.760 65.725
Cylinder 5.810 36.505 12.240 658.921 1298.026
Sphere 5.810 36.505 11.620 424.192 821.518
Cylinder 25.330 159.153 8.099 5320.328 16324.942
Sphere 25.330 159.153 50.660 8062.695 68076.023
Cylinder 7.002 43.995 0.388 325.122 59.762
Sphere 7.002 43.995 14.004 616.104 1437.987
Cylinder 18.290 114.919 1.964 2327.579 2064.043
Sphere 18.290 114.919 36.580 4203.754 25628.886
Cylinder 46.730 293.613 6.627 15666.322 45463.033
Sphere 46.730 293.613 93.460 27441.094 427440.779
Cylinder 0.895 5.623 19.754 116.105 49.700
Sphere 0.895 5.623 1.790 10.064 3.002
(plus your additional computations computed from your input data)