📜  使用函数重载查找 3D 形状体积的基于菜单的程序

📅  最后修改于: 2022-05-13 01:55:42.791000             🧑  作者: Mango

使用函数重载查找 3D 形状体积的基于菜单的程序

给定CubeCuboidCylinder等 3D 形状的尺寸,任务是使用函数重载找到所有 3D 形状的体积。

例子:

方法:可以通过创建具有相同名称的不同函数来解决给定的问题,例如Volume但函数定义不同。对于每个形状,然后通过向它们传递不同的参数来重载所有函数,然后在 switch-case 语句的帮助下从主函数调用所有函数。



下面是使用函数重载查找不同 3D 形状的体积的 C++ 程序:

C++
// C++ program to find the volume of
// 2D Shapes using the function
// overloading
#include 
#define PI 3.14
using namespace std;
  
// Function to find volume of cube
float volume(float side)
{
    cout << "calculating volume of Cube..";
    float calculate_volume;
  
    calculate_volume
        = side * side * side;
  
    // Return the volume
    return calculate_volume;
}
  
// Function to find volume of rectangle
float volume(float length, float breadth,
             float height)
{
    cout << "calculating volume of "
         << "Rectangle..";
    int calculate_volume;
    calculate_volume
        = length * breadth * height;
  
    // Return the volume
    return calculate_volume;
}
  
// Function to find volume of cylinder
float volume(double radius,
             double height)
{
    cout << "calculating volume of"
         << " cylinder..";
    float calculate_volume;
    calculate_volume
        = PI * radius * radius * height;
  
    // Return the volume
    return calculate_volume;
}
  
// Function to swap the values of
// volume of cube and cylinder
void swapvalues(float cylinder, float cube)
{
    float third_variable;
    if (cylinder == 0 || cube == 0) {
  
        cout << "\nvalues are not assign";
        return;
    }
  
    cout << "\n\nValues before swaping";
    cout << "\nvolume of cylinder :"
         << cylinder;
    cout << "\nvolume of cube :"
         << cube;
  
    // Perform Swap Operation
    third_variable = cube;
    cube = cylinder;
    cylinder = third_variable;
  
    cout << "\n\nvalues after swaping ";
  
    cout << "\nvolume of cylinder :"
         << cylinder;
  
    cout << "\nvolume of cube :"
         << cube;
}
  
// Driver Code
int main()
{
    float height, radius, length;
    float breadth, rectangleHeight;
    float volumeCube = 0;
    float volumeRectangle = 0;
    float volumeCylinder = 0;
    float side;
  
    int choice;
  
    // Main menu
    cout << "\n\n==== MENU ====";
    cout << "\n\n1.Calculate volume "
         << "of CUBE";
    cout << "\n\n2.Calculate volume "
         << "of RECTANGLE";
    cout << "\n\n3.Calculate volume "
         << "of CYLINDER";
    cout << "\n\n4.PRESS '4' To SWAP "
         << "VOLUMES OF CUBE AND "
            "CYLINDER";
    cout << "\n\n5.Exit";
  
    while (1) {
  
        cout << "\n\nSelect your choice :";
        cin >> choice;
  
        // Switch Case
        switch (choice) {
  
        case 1:
            cout << "\nEnter the side"
                 << " of cube :";
            cin >> side;
            volumeCube = volume(side);
  
            cout << "\nVolume of cube is :"
                 << volumeCube;
            break;
  
        case 2:
            cout << "\nEnter the length :";
            cin >> length;
  
            cout << "\nEnter the height :";
            cin >> rectangleHeight;
  
            cout << "\nEnter the breadth :";
            cin >> breadth;
  
            volumeRectangle
                = volume(length, breadth,
                         rectangleHeight);
  
            cout << "\nVolume of Rectangle is :"
                 << volumeRectangle;
  
            break;
  
        case 3:
            cout << "\nEnter the Radius :";
            cin >> radius;
  
            cout << "\nEnter the height :";
            cin >> height;
  
            volumeCylinder = volume(
                radius, height);
  
            cout << "\nVolume of Cylinder is :"
                 << volumeCylinder;
            break;
  
        case 4:
  
            swapvalues(volumeCylinder,
                       volumeCube);
            break;
  
        case 5:
            exit(0);
        }
    }
}


输出:

想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解语言和 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程