Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This program demonstrates class inheritance by creating
- * the Circle class which is inherited by the Cylinder class.
- * It contains classic geometric functions such as area,
- * surface area and volume.
- *
- * @author Francisco, Graham
- * @assignment ICS 212 Assignment 23
- * @date 4/25/2019
- */
- #include <iostream>
- using namespace std;
- /**
- * This represents the class for circles. It contains the radius of the circle
- * and it can calculate its area.
- */
- class Circle {
- /*
- Public variables and functions can be accessed outside
- the class definition, i.e. they can be used in main().
- */
- public:
- //Constant variable holding the value of Pi.
- static const double PI = 3.1415926;
- /**
- * Default constructor for the Circle class. Has no parameters.
- */
- Circle() {
- //default radius
- radius = 1.0;
- }
- /**
- * Additional constructor that allows the user to set a custom radius for
- * the circle.
- */
- Circle(double radiusParam) {
- //If input is negative, turn it to positive, if 0, set to default 1
- if (radiusParam < 0) {
- radius = radiusParam * -1.0;
- } else if (radiusParam == 0) {
- radius = 1.0;
- } else {
- radius = radiusParam;
- }
- }
- /**
- * Sets the circle's radius based on the user's input.
- */
- void setRadius(double radiusParam) {
- if (radiusParam < 0) {
- radius = radiusParam * -1.0;
- } else if (radiusParam == 0) {
- radius = 1.0;
- } else {
- radius = radiusParam;
- }
- }
- /**
- * Returns the radius of the circle.
- */
- double getRadius() const {
- return radius;
- }
- /**
- * Calculates and returns the area of the circle.
- */
- double area() {
- return PI * radius * radius;
- }
- /*
- Protected variables and functions cannot be accessed outside
- the class definition, i.e. they can't be used in main(). Although
- they are accessible by inherent classes.
- */
- protected:
- // protected data member
- double radius;
- };
- /**
- *
- */
- class Cylinder: public Circle {
- /*
- Public variables and functions can be accessed outside
- the class definition, i.e. they can be used in main().
- */
- public:
- /**
- * Default constructor.
- * Constructs a Square object with 0 length and width.
- */
- Cylinder(double radiusParam, double heightParam): Circle(radiusParam) {
- //If input is negative, turn it to positive, if 0, set to default 1
- if (heightParam < 0) {
- height = heightParam * -1.0;
- } else if (heightParam == 0) {
- height = 1.0;
- } else {
- height = heightParam;
- }
- }
- /**
- * Sets the Cylinder's height based on the user's input.
- */
- void setHeight(double heightParam) {
- //If input is negative, turn it to positive, if 0, set to default 1
- if (heightParam < 0) {
- height = heightParam * -1.0;
- } else if (heightParam == 0) {
- height = 1.0;
- } else {
- height = heightParam;
- }
- }
- /**
- * Returns the radius of the circle.
- */
- double getHeight() const {
- return height;
- }
- /**
- * Calculates and returns the surface area of the cylinder.
- */
- double area() {
- return (2 * PI * radius * height) + (2 * PI * radius * radius);
- }
- /**
- * Calculates and returns the volume of the cylinder.
- */
- double volume() {
- return PI * radius * radius * height;
- }
- /*
- Protected variables and functions cannot be accessed outside
- the class definition, i.e. they can't be used in main(). Although
- they are accessible by inherent classes.
- */
- protected:
- // protected data member
- double height;
- };
- int main() {
- cout << "Test Circle class default constructor and area() function:" << endl;
- Circle circle1;
- cout << "Circle circle1: radius = " << circle1.getRadius() << endl;
- cout << "area = " << circle1.area() << endl;
- cout << "\n";
- cout << "Test Circle class constructor with radius:" << endl;
- Circle circle2(-10);
- cout << "Circle circle2: radius = " << circle2.getRadius() << endl;
- cout << "Test set method, set circle2 to radius 5..." << endl;
- circle2.setRadius(5);
- cout << "radius = " << circle2.getRadius() << endl;
- cout << "\n";
- cout << "Test Cylinder class given radius and height:" << endl;
- Cylinder cylinder1(20, -30);
- cout << "Cylinder cylinder1: radius = " << cylinder1.getRadius();
- cout << "; height = " << cylinder1.getHeight() << endl;
- cout << "surface area = " << cylinder1.area() << endl;
- cout << "volume = " << cylinder1.volume() << endl;
- cout << "\n";
- cout << "Test set method, set cylinder1 to radius 40 and height 50..." << endl;
- cylinder1.setRadius(40);
- cylinder1.setHeight(50);
- cout << "radius = " << cylinder1.getRadius() << "; height = " << cylinder1.getHeight() << endl;
- cout << "surface area = " << cylinder1.area() << endl;
- cout << "volume = " << cylinder1.volume() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement