C++主要集合了JAVA的面向对象的优点,结构会有些类似;而指针这类结构和基本定义方式、库又和C很类似。
Reference 来自:C++入门教程,C++基础教程(更新完毕)
非常感谢!!
第一章
1. namespace 命名空间
使用Li用户的区域下变量3种写法:
Li::fp = fopen("one.txt", "r");
using Li::fp;
fp = fopen("one.txt", "r"); //Li用户fp变量使用
using namespace Li;
fp = fopen("one.txt", "r"); //Li用户整个命名空间使用
具体使用示例:
2. 头文件
正如c语言的#include<stdio.h>
,c++也有自己专属的。
#include <cstdio>
常见的还有:
#include <iostream> //读写文件,i/o流
#include <string> //字符串
3. 输入输出(cin和cout)
包含头文件:#include <iostream>
输出:cout<<"......."<<endl;
输入:cin>>...;
//1. 分开输入
cin>>x;
...
cin>>y;
//2. 连续输入
cin>>x>>y;
//3. bool常量
flag = a > b;
cout<<"flag = "<<flag<<endl;
//4. bool+判断
if(flag){
cout<<"true"<<endl;
}else{
cout<<"false"<<endl;
}
4. 动态分配内存
单独分配:【分配1个int型的内存空间】
int *p = new int;
delete p;
连续分配一组内存:【分配10个int型的内存空间】
int *p = new int[10];
delete[] p;
*一定要手动释放,否则只能等到程序运行结束由操作系统回收。
5. inline内联函数
-对于函数体内容比较少的函数来说,调用过程花的时间空间不值得。
-在此引入inline。
-即在编译时将函数调用处用函数体替换,类似于C语言中的宏展开。
inline void swap(int *a, int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main(){
swap(&m, &n);
return 0;
}
6. 带默认参数的函数
void func(int n, float b=1.2, char c='@')