×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

C++ program to find the surface area of shapes using class

Learn, how can we find the surface area of shapes using the class and object approach? Submitted by Shubh Pachori, on September 04, 2022

Problem statement

Find the surface area of shapes using the class and object approach in C++.

Example:

Enter Sphere's radius : 23
Surface area of Sphere : 6650.29

Enter Cylinder's radius : 32
Enter Cylinder's height : 32
Surface area of Cylinder : 12873.1

Enter Cube's side : 32
Surface area of Cube : 6144

Enter Cuboid's length : 34
Enter Cuboid's height : 43
Enter Cuboid's breadth : 12
Surface area of Cuboid : 2794

C++ code to find the surface area of shapes using the class and object approach

#include <iostream>
using namespace std;

// create a class
class Shape {
  // protected data member
 protected:
  float surface_area;
};

// class Sphere inheriting class Shape
class Sphere : public Shape {
  // protected data member
 protected:
  float radius;

  // public member functions
 public:
  // get() function to input 
  // attributes of the class
  void get() {
    cout << "Enter Sphere's radius : ";
    cin >> radius;
  }

  // Surface_area() function to calculate 
  // surface_area of the class
  double Surface_area() {
    // calculating surface area
    surface_area = (4 * 22 * radius * radius) / 7;

    // returning surface_area
    return surface_area;
  }
};

// class Cylinder inheriting class Shape
class Cylinder : public Shape {
  // protected data members
 protected:
  float radius, height;

  // public member functions
 public:
  // get() function to input attributes 
  // of the class
  void get() {
    cout << "Enter Cylinder's radius : ";
    cin >> radius;

    cout << "Enter Cylinder's height : ";
    cin >> height;
  }

  // Surface_area() function to calculate 
  // surface_area of the class
  double Surface_area() {
    // calculating surface_area
    surface_area =
        ((2 * 22 * radius * height) / 7) + ((2 * 22 * radius * radius) / 7);

    // returning surface_area
    return surface_area;
  }
};

// class Cube inheriting class Shape
class Cube : public Shape {
  // protected data member
 protected:
  float side;

  // public member functions
 public:
  // get() function to input attributes 
  // of the class
  void get() {
    cout << "Enter Cube's side : ";
    cin >> side;
  }

  // Surface_area() function to calculate 
  // surface_area of the class
  double Surface_area() {
    // calculating Surface_area
    surface_area = 6 * side * side;

    // returning surface_area
    return surface_area;
  }
};

// class Cuboid inheriting class Shape
class Cuboid : public Shape {
  // protected data members
 protected:
  float length, height, breadth;

  // public member functions
 public:
  // get() function to input attributes 
  // of the class
  void get() {
    cout << "Enter Cuboid's length : ";
    cin >> length;

    cout << "Enter Cuboid's height : ";
    cin >> height;

    cout << "Enter Cuboid's breadth : ";
    cin >> breadth;
  }

  // Surface_area() function to calculate 
  // Surface_area of the class
  double Surface_area() {
    // calculating Surface_area
    surface_area =
        (2 * (length * breadth) + (breadth * height) + (length * height));

    // returning surface_area
    return surface_area;
  }
};

int main() {
  // create a object
  Sphere S;

  // float type variable to store surface_area
  float sphere;

  // calling get() function to input radius of sphere
  S.get();

  // calling Surface_area() function to 
  // calculate Surface_area of the sphere
  sphere = S.Surface_area();

  cout << "Surface area of Sphere : " << sphere << endl;
  cout << endl;

  //----------------------

  // create a object
  Cylinder Cy;

  // float type variable to store Surface_area
  float cylinder;

  // calling get() function to input 
  // radius and height of cylinder
  Cy.get();

  // calling Surface_area() function to
  // calculate Surface_area of the cylinder
  cylinder = Cy.Surface_area();

  cout << "Surface area of Cylinder : " << cylinder << endl;
  cout << endl;

  //---------------------------

  // create a object
  Cube Cu;

  // float type variable to store Surface_area
  float cube;

  // calling get() function to input sides of cube
  Cu.get();

  // calling Surface_area() function to 
  // calculate Surface_area of the cube
  cube = Cu.Surface_area();

  cout << "Surface area of Cube :" << cube << endl;
  cout << endl;

  //--------------------------------

  // create a object
  Cuboid C;

  // float type variable to store Surface_area
  float cuboid;

  // calling get() function to input length,
  // breadth and height of cuboid
  C.get();

  // calling Surface_area() function to 
  // calculate Surface_area of the cuboid
  cuboid = C.Surface_area();

  cout << "Surface area of Cuboid : " << cuboid << endl;
  cout << endl;

  return 0;
}

Output

Enter Sphere's radius : 10
Surface area of Sphere : 1257.14

Enter Cylinder's radius : 11
Enter Cylinder's height : 12
Surface area of Cylinder : 1590.29

Enter Cube's side : 13
Surface area of Cube : 1014

Enter Cuboid's length : 14
Enter Cuboid's height : 15
Enter Cuboid's breadth : 16
Surface area of Cuboid : 898

Explanation

In the above code, we have created a parent class Shape, and a protected float type data member surface­_area to calculate and store the surface area of various shapes. A base class Sphere inherits the parent class Shape and its elements and this base class has two public member functions get() and Surface­_area(), to store attributes and to calculate the surface area of the shape. Same as above there are three more base classes Cylinder, Cube and Cuboid with two public member functions get() and Surface_area() to store the attributes and to calculate the surface area of the shape.

In the main() function, we are creating objects of these base classes S of class Sphere, Cy of class Cylinder, Cu of class Cube and C of class Cuboid, then initialize some float variables to store the surface area of particular shapes and then calling functions to perform operations like to input attributes and to calculate the surface area of the shapes. In the end, returning results.

C++ Class and Object Programs (Set 2) »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.