In Cpp, Application That Calculates The Surface Area And Volume Of 2 Cylinders Using Two Sets Of Classes

#include<iostream>
#include<cmath>

using namespace std;

class ProtectedCircle{

  protected: float radius;

  public: void setRadius(float r){
    if(0 <= r) radius = r;
  }

  public: float getRadius() const{
    return radius;
  }

  public: float getPerimeter() const{
    return (2 * M_PI * radius);
  }

  public: float getArea() const{
    return (radius * radius) * M_PI;
  }

  public: ProtectedCircle(float r = 0){
    setRadius(r);
  }
};

class ProtectedCylinder: public ProtectedCircle {

  private: float height;

  public: void setHeight(float h){
    if(0 <= h) height = h;
  }

  public: float getHeight() const{
    return height;
  }

  public: float getArea() const{
	return (((radius * radius) * (2 * M_PI)) + (2 * M_PI * radius * height));
  }

  public: float getVolume() const{
    return (M_PI * (radius * radius) * height);
  }

  public: ProtectedCylinder(float r = 0, float h = 0): ProtectedCircle(r){
    setHeight(h);
  }
};

class PrivateCircle{

  private: float radius;

  public: void setRadius(float r){
    if(0 <= r) radius = r;
  }

  public: float getRadius() const{
    return radius;
  }

  public: float getPerimeter() const{
    return (2 * M_PI * radius);
  }

  public: float getArea() const{
    return (radius * radius) * M_PI;
  }

  public: PrivateCircle(float r = 0){
    setRadius(r);
  }
};

class PrivateCylinder: public PrivateCircle {

  private: float height;

  public: void setHeight(float h){
    if(0 <= h) height = h;
  }

  public: float getHeight() const{
    return height;
  }

  public: float getArea() const{
	return (((getRadius() * getRadius()) * (2 * M_PI)) + (2 * M_PI * getRadius() * height));
  }

  public: float getVolume() const{
    return (M_PI * (getRadius() * getRadius()) * height);
  }

  public: PrivateCylinder(float r = 0, float h = 0): PrivateCircle(r){
    setHeight(h);
  }
};


int main(){
  float r, h;
  cout << "Cylinder App!" << endl;
  cout << "--------------------" << endl;
  cout << "Enter radius: ";
  cin >> r;
  cout << "Enter height: ";
  cin >> h;

  ProtectedCylinder p1(r, h);
  cout << "Cylinder area: " << p1.getArea() << endl;
  cout << "Cylinder volume: " << p1.getVolume() << endl;

  cout << "Enter radius: ";
  cin >> r;
  cout << "Enter height: ";
  cin >> h; 

  PrivateCylinder p2(r, h);
  cout << "Cylinder area: " << p2.getArea() << endl;
  cout << "Cylinder volume: " << p2.getVolume() << endl;

  return 0;
}
Download

Comments

Popular posts from this blog