目录
一、C++第一个程序常规标准写法
#include<iostream> //头文件名
using namespace std; //使用的命名空间
int main(){ //头函数
return 0; //返回值0
}
头文件名:简单来说就是我们要使用的第三方库(类似于python的import),我们调用的基本函数都包括在内
命名空间:表明命名空间,可以使我们写的代码不用加头缀,假如不用using namespace std;也是可以的,只不过在函数体内使用时,要std::cout<<***<<endl; 这样子写,代码量又增加了,命名空间可以做全局变量也可以只放在函数体内
头函数:是作为接口的函数头,C++的句法要求就是得从这个开始写,不要问为什么。
二、cout函数
在终端输出你编辑的内容,这里列举几个常见的用法
cout << "Giant fox is so cool"<<endl;
cout << "C++ zhen hao wan";
cout << "wo bu xue la"<<endl;
int fox;
fox = 33;
cout <<"输出变量时"<<fox<<endl;
cout <<"要这么写"<<endl;
这就是一个变了样子的printf语句 一样用 别看他麻烦了 其实就是麻烦了 但样子变高端了
cin函数也要说一下 可以有个互动过程 自定义值
int fox; 先定义
cin >> fox; 再输入
三、函数构建
自己定义函数
type functionname(argumentlist){
statements
}
voil是表明simon()没有返回值
用实例理解:
#include<iostream>
int giant(int);
int main() {
using namespace std;
giant(2);
return 0;
}
int giant(int n) {
using namespace std;
n = n * 2;
cout << n << endl;;
return 0;
}
#include<iostream>
void fox(int);
int main() {
using namespace std;
fox(2);
return 0;
}
void fox(int n) {
using namespace std;
n = n * 2;
cout << n << endl;
}
# 很简单就是输入一个数字 这个数字乘2 主要说明的是使用int时 需要一个return返回值 void则不需要()里面是定义传参的类型 int n 就是整型
最后看一道习题
答案如下
#include <iostream>
using namespace std;
float degrees_ch(float);
int main()
{
float degress_ce;
float degress_fe;
cout << "Please enter a Celsius value: ";
cin >> degress_ce;
degress_fe = degrees_ch(degress_ce);
cout << degress_ce << " degrees Celsius is " << degress_fe << " degrees Fahrenheit.";
return 0;
}
float degrees_ch(float degress_ce)
{
return 1.8 * degress_ce + 32.0;
}