学习C++参考书目:《C++编程思想》第1卷:标准C++导引
学习C++参考链接:http://www.prglab.com/
学习C++参考视频:华文慕课-->程序设计实习
C++学习笔记01:
代码范例部分来自《C++编程思想》,还有一部分来自上述参考链接,特此声明。另外,如有错误还请大家批评指正。
- 整行输入
#include<string> string mystr; getline(cin,mystr);
- 字符串相加
#include<string> string s1="I am"; string s2("a boy") ; string s3,s4; s3=s1+" "+s2; s4+="8";
- 给字符数组赋值
#include<string> char szMyName [20]; strcpy (szMyName,"J. Soulie"); //或者: char szMyName [20]; setstring (szMyName,"J. Soulie"); //或者: char mybuffer [100]; cin.getline (mybuffer,100);
- 字符串相加
#include<string> #include<sstream> int a=0; string mystr; getline(cin,mystr); stringstream(mystr) >> a; "1024"->1024
- go to语句
lable: ...... goto lable;
- exit函数
exit(0);//正常退出 exit(其他值);//异常退出
- 函数重载
int divide (int a, int b) { return (a/b); } float divide (float a, float b) { return (a/b); } int divide (int a){ return a/3; }
- 内联函数
(1)成员函数:写在类里面 (2)全局函数:前面加上inline
- 函数参数默认值
void divide(int a,int b=2); divide(1);//当只有一个参数时,默认为divide(1,2); divide(1,3);
- 数组作为参数传递
int a[10]; //直接传输组 void b(int x[]) { cout<<x[0] ; } b(a); //或者用指针 void c(int* x) { cout<<*x; } c(a);
- 引用传递
int a=0; void b(int & x) { x++; } b(a);//此时a=1;
- 动态内存分配
int *a; a=new int; delete a; //如果创建一个数组 a=new int [5]; delete []a; //或者 int *a=new int[5]; delete []a; //将数组初始化为0 int *a=new int[5](); delete []a;
- 动态对象创建
MyClass *a=new MyClass;//Myclass为类,a为对象 //调用构造函数 MyClass *a=new Myclass(1); delete a; //创建对象数组 MyClass *a=new Myclass[100]; delete []a;
- 构造函数和析构函数
class A { int a,b; A();//默认构造函数 A(int,int);//自己创建的构造函数 A(const A&); //复制(拷贝)构造函数 ~A();//析构函数 } ; A::A(int aa,int bb) { a=aa; b=bb; } A::A(const A& rv) { a=rv.a; b=rv.b; } A::~A() { delete a; delete b; } //可以将默认构造函数和自己创建的构造函数写成一个 A::A(int aa=0,int bb=0)//默认a=0,b=0; { a=aa; b=bb; } //也可以使用构造函数初始化列表 A::A(int aa,int bb):a(aa),b(bb){ }
- 操作符重载
class CVector { public: int x,y; CVector () {}; CVector (int,int); CVector operator + (CVector);//加号运算符重载 }; CVector CVector::operator+ (CVector param) { CVector temp; temp.x = x + param.x; temp.y = y + param.y; return (temp); } //二元运算符在类内一个参数,在类外两个参数 CVector operator+(CVector pa,Cvector pb) { CVector temp; temp.x=pa.x+pb.x; temp.y=pa.y+pb.y; return(temp); }
- 类的继承
class A { }; class B:public A { //可以定义新的变量和函数; //需要定义新的构造函数和析构函数; } //理论上说,子类(drived class)继承了基类(base class)的所有成员,除了: //构造函数Constructor 和析构函数destructor //operator=() 成员 //friends //继承里面注意 隐藏实现 //继承初始化: B::B(int i):A(i) { }
- 多态、虚函数、抽象基类
//关键字virtual 的作用就是在当使用基类的指针的时候,使子类中与基类同名的成员(函数)在适当的时候被//调用。 class CPolygon { virtual int area (void) { return (0); } }; CPolygon * ppoly1 = ▭//rect为CPolygon子类对象 CPolygon * ppoly2 = &trgl;//trgl为CPolygon子类对象 cout << ppoly1->area() << endl;//调用rect.area() cout << ppoly2->area() << endl;//调用trgl.area()
- 函数模板
template <class T> T GetMax (T a, T b) { T result; result = (a>b)? a : b; return (result); } int i=5, j=6, k; long l=10, m=5, n; k=GetMax<int,int>(i,j); n=GetMax<long int,long int>(l,m); //也可以用几种不同类型 template <class T,class U> T GetMin (T a, U b) { return (a<b?a:b); } //或者 template <class T, int N> class array { T memblock [N]; };
- 类模板
template <class T> class pair { T value1, value2; pair(T a,T b) { value1=a; value2=b; } } pair<int> a(1,2); pair<float> b(1.0,2.0); //在类外定义函数时,每定义一个,前面要加一句 template <class T>
- 文件输入输出
#include<ofstream> ofstream file; file.open("1.txt")//1.txt的后面可以加好多参数 void a(const char* filename) { ofstream file; file.open(filename); } bool is_open(); //判断是否打开 file.close(); //关闭文件 file.eof(); //判断是否到达文件末尾,返回true/false //还有几个检测当前指针和改变当前指针位置函数 tellg() 和 tellp() seekg() 和 seekp()