21xrx.com
2024-03-29 16:33:26 Friday
登录
文章检索 我的文章 写文章
C++程序将两个数字相加
2021-07-08 15:06:28 深夜i     --     --
C + +

两个数字相加的C++程序。

C++编程代码

#include <iostream>


using namespace std;

int main()
{
   int a, b, c;
   
   cout << "Enter two integers to add\n";
   cin >> a >> b;

   c = a + b;
   cout <<"Sum of the numbers: " << c << endl;
   
   return 0;
}

使用类的 C++ 加法程序

#include <iostream>


using namespace std;

class Mathematics {
  int x, y;

public:
  void input() {
    cout << "Input two integers\n";
    cin >> x >> y;
  }

  void add() {
    cout << "Result: " << x + y;
  }

};

int main()
{
   Mathematics m; // Creating an object of the class

   m.input();
   m.add();

   return 0;
}

我们创建了具有两个函数 input 和 add 的数学类。 函数 input 用于从用户处获取两个整数,函数 add 执行加法并显示结果。 同样,您可以创建更多函数来进行减法、乘法、除法。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复