补充java中没有的一些东西
1.入门
#include<iostream>
using namespace std;
int main(){
cout << "hello world"<<endl;
//按任意键继续
system("pause");
return 0;
}
//输出变量的值
int a = 10;
cout << "a: "<< a <<endl;
2.常量
//定义常量的两种方式
//1.#define宏常量:#define 常量名 常量值
//通常定义在文件上方
#define Day 7
cout << "一周共有:"<<Day<<"天"<<endl;
//2.const修饰的变量
const int month = 12;
3.整型
数据类型的目的:给变量分配合适的内存空间。
short(短整型) 2字节
int(整型) 4字节
long(长整型) windows 4字节
longlong(长长整型) 8字节
4.函数声明
#include<iostream>
using namespace std;
//函数声明作用:告诉编译器我下面要定义这个函数(可能在main后面你先别急着报错)
//函数声明
int add(int num1, int num2);
int main() {
int a = 10;
int b = 20;
int c = add(a, b);
cout << "结果:" << c << endl;
system("pause");
return 0;
}
//函数定义
int add(int num1, int num2) {
int sum = num1 + num2;
return sum;
}
5.函数的分文件编写
#include<iostream>
#include"swap.h"
using namespace std;
//函数的分文件编写
//1.创建后缀名为.h的头文件
//2.创建后缀名为.cpp的源文件
//3.在头文件中写函数的声明
//4.在源文件中写函数的定义
int main() {
swap(10, 20);
system("pause");
return 0;
}
#include<iostream>
using namespace std;
//函数声明
void swap(int a, int b);
#include"swap.h"
//定义函数
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
cout << "a= " << a << endl;
cout << "b= " << b << endl;
}