标签:com min 泛型 赋值 创建 一个 image 二进制文件 div
C++ 结合了三个编程流派:
C++ 常用标准:
1 // myfirst.cpp -- displays a message 2 3 #include <iostream> // a PREPROCESSOR directive 4 int main() // function header 5 { // start of function body 6 using namespace std; // make definitions visible 7 cout << "Come up and C++ me some time."; // message 8 cout << endl; // start a new line 9 cout << "You won‘t regret it!" << endl; // more output 10 return 0; // terminate main() 11 } // end of function body
编译源代码:
g++ myfirst.cpp
运行二进制文件:
./a.out
注意:
大多数程序员遵循这样的 C++ 源码风格:
// an addition operation #include <iostream> int main() { using namespace std; int x1; int x2; int ans; x1 = 1; x2 = 2; ans = x1 + x2; cout << "x1 + x2 = " << ans << endl; }
编译并运行后的输出:
x1 + x2 = 3
1 // double your input number 2 #include <iostream> 3 4 int main() 5 { 6 int now = 2019; 7 int birth_year; 8 std::cout << "What‘s your birth year?" << std::endl; 9 std::cin >> birth_year; // using cin 10 std::cout << "You‘re " << 2019 - birth_year << " years old." << std::endl; 11 return 0; 12 }
编译运行后会提示输入,输入 1992,得到运算之后的输出:
What‘s your birth year? 1992 You‘re 27 years old.
使用 cmath 头文件:
1 // using sqrt 2 3 #include <iostream> 4 #include <cmath> 5 6 int main() 7 { 8 using std::cout; 9 using std::cin; 10 using std::endl; 11 using std::sqrt; 12 13 float num; 14 cout << "Give me a number: "; 15 cin >> num; 16 cout << "sqrt(" << num << ") = " << sqrt(num) << endl; 17 }
编译运行,输入 25 后输出:
Give me a number: 2 sqrt(2) = 1.41421
自定义函数需要声明函数原型:
1 #include <iostream> 2 3 using namespace std; 4 5 int add(int, int); // function prototype 6 7 int main() 8 { 9 int x1 = 3; 10 int x2 = 4; 11 int sum = add(x1, x2); 12 cout << "3 + 4 = " << sum << endl; 13 } 14 15 int add(int n1, int n2) 16 { 17 return n1 + n2; 18 }
输出如下:
3 + 4 = 7
标签:com min 泛型 赋值 创建 一个 image 二进制文件 div
原文地址:https://www.cnblogs.com/noluye/p/11482746.html