Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.21 KB | None | 0 0
  1. /**
  2.  * This program demonstrates class inheritance by creating
  3.  * the Circle class which is inherited by the Cylinder class.
  4.  * It contains classic geometric functions such as area,
  5.  * surface area and volume.
  6.  *
  7.  * @author     Francisco, Graham
  8.  * @assignment ICS 212 Assignment 23
  9.  * @date       4/25/2019
  10.  */
  11. #include <iostream>
  12. using namespace std;
  13.  
  14. /**
  15.  * This represents the class for circles. It contains the radius of the circle
  16.  * and it can calculate its area.
  17.  */
  18. class Circle {
  19.    
  20.   /*
  21.   Public variables and functions can be accessed outside
  22.   the class definition, i.e. they can be used in main().
  23.   */
  24.   public:
  25.    
  26.     //Constant variable holding the value of Pi.
  27.     static const double PI = 3.1415926;
  28.    
  29.     /**
  30.      * Default constructor for the Circle class. Has no parameters.
  31.      */
  32.     Circle() {
  33.       //default radius
  34.       radius = 1.0;
  35.     }
  36.    
  37.     /**
  38.      * Additional constructor that allows the user to set a custom radius for
  39.      * the circle.
  40.      */
  41.     Circle(double radiusParam) {
  42.       //If input is negative, turn it to positive, if 0, set to default 1
  43.       if (radiusParam < 0) {
  44.           radius = radiusParam * -1.0;
  45.       } else if (radiusParam == 0) {
  46.           radius = 1.0;
  47.       } else {
  48.           radius = radiusParam;
  49.       }
  50.     }
  51.    
  52.     /**
  53.      * Sets the circle's radius based on the user's input.
  54.      */
  55.     void setRadius(double radiusParam) {
  56.       if (radiusParam < 0) {
  57.           radius = radiusParam * -1.0;
  58.       } else if (radiusParam == 0) {
  59.           radius = 1.0;
  60.       } else {
  61.           radius = radiusParam;
  62.       }
  63.     }
  64.    
  65.     /**
  66.      * Returns the radius of the circle.
  67.      */
  68.     double getRadius() const {
  69.       return radius;
  70.     }
  71.    
  72.     /**
  73.      * Calculates and returns the area of the circle.
  74.      */
  75.     double area() {
  76.         return PI * radius * radius;
  77.     }
  78.    
  79.   /*
  80.   Protected variables and functions cannot be accessed outside
  81.   the class definition, i.e. they can't be used in main(). Although
  82.   they are accessible by inherent classes.
  83.   */
  84.   protected:
  85.     // protected data member
  86.     double radius;
  87. };
  88.  
  89. /**
  90.  *
  91.  */
  92. class Cylinder: public Circle {
  93.  
  94.   /*
  95.   Public variables and functions can be accessed outside
  96.   the class definition, i.e. they can be used in main().
  97.   */
  98.   public:
  99.    
  100.     /**
  101.      * Default constructor.
  102.      * Constructs a Square object with 0 length and width.
  103.      */
  104.     Cylinder(double radiusParam, double heightParam): Circle(radiusParam) {
  105.       //If input is negative, turn it to positive, if 0, set to default 1
  106.       if (heightParam < 0) {
  107.           height = heightParam * -1.0;
  108.       } else if (heightParam == 0) {
  109.           height = 1.0;
  110.       } else {
  111.           height = heightParam;
  112.       }
  113.     }
  114.    
  115.     /**
  116.      * Sets the Cylinder's height based on the user's input.
  117.      */
  118.     void setHeight(double heightParam) {
  119.       //If input is negative, turn it to positive, if 0, set to default 1
  120.       if (heightParam < 0) {
  121.           height = heightParam * -1.0;
  122.       } else if (heightParam == 0) {
  123.           height = 1.0;
  124.       } else {
  125.           height = heightParam;
  126.       }
  127.     }
  128.    
  129.     /**
  130.      * Returns the radius of the circle.
  131.      */
  132.     double getHeight() const {
  133.       return height;
  134.     }
  135.    
  136.     /**
  137.      * Calculates and returns the surface area of the cylinder.
  138.      */
  139.     double area() {
  140.       return (2 * PI * radius * height) + (2 * PI * radius * radius);
  141.     }
  142.    
  143.     /**
  144.      * Calculates and returns the volume of the cylinder.
  145.      */
  146.     double volume() {
  147.       return PI * radius * radius * height;
  148.     }
  149.    
  150.   /*
  151.   Protected variables and functions cannot be accessed outside
  152.   the class definition, i.e. they can't be used in main(). Although
  153.   they are accessible by inherent classes.
  154.   */
  155.   protected:
  156.     // protected data member
  157.     double height;
  158.    
  159. };
  160.  
  161. int main() {
  162.  
  163.   cout << "Test Circle class default constructor and area() function:" << endl;
  164.   Circle circle1;
  165.  
  166.   cout << "Circle circle1: radius = " << circle1.getRadius() << endl;
  167.   cout << "area = " << circle1.area() << endl;
  168.  
  169.   cout << "\n";
  170.  
  171.   cout << "Test Circle class constructor with radius:" << endl;
  172.   Circle circle2(-10);
  173.  
  174.   cout << "Circle circle2: radius = " << circle2.getRadius() << endl;
  175.   cout << "Test set method, set circle2 to radius 5..." << endl;
  176.   circle2.setRadius(5);
  177.   cout << "radius = " << circle2.getRadius() << endl;
  178.  
  179.   cout << "\n";
  180.  
  181.   cout << "Test Cylinder class given radius and height:" << endl;
  182.   Cylinder cylinder1(20, -30);
  183.  
  184.   cout << "Cylinder cylinder1: radius = " << cylinder1.getRadius();
  185.   cout << "; height = " << cylinder1.getHeight() << endl;
  186.   cout << "surface area = " << cylinder1.area() << endl;
  187.   cout << "volume = " << cylinder1.volume() << endl;
  188.  
  189.   cout << "\n";
  190.  
  191.   cout << "Test set method, set cylinder1 to radius 40 and height 50..." << endl;
  192.   cylinder1.setRadius(40);
  193.   cylinder1.setHeight(50);
  194.   cout << "radius = " << cylinder1.getRadius() << "; height = " << cylinder1.getHeight() << endl;
  195.   cout << "surface area = " << cylinder1.area() << endl;
  196.   cout << "volume = " << cylinder1.volume() << endl;
  197.  
  198.   return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement