1. How to compile and link the test.cpp file
g++ test -o test.cpp
2. Basics of class in c++
(1)派生类也无法看到基类中的私有成员函数
(2)继承时,派生类中与基类同名的成员函数将会覆盖基类中的成员函数,参数列表不同的同名函数也会被覆盖
(3)静态成员函数属于类而不属于任何对象
(4)当基类中声明了虚成员函数时,如果派生类中的成员函数有与该虚函数同名、同参数且返回值类型相同,则该派生类成员函数自动成为虚函数,可以在运行过程中实现多态;但要注意:虚函数只能被基类指针调用或基类引用调用时,才能实现动态绑定
(5)类模板就是指类中的数据类型可以任意指定,其定义方式就是在普通类定义前加上template’<‘class T’>'并在类定义中将具体的类型名用T来代替,模板的作用就是在同一种类中使用不同的数据或对象类型,进而实现多态
3. 变量名前或后的“_”符号的意义
About the names for data members you can use any name. Using the “_” is the practice in OpenFOAM in particular and C++ in general to differentiate the data members from the local variables.
Some developers use CAPS letter, it is really up to you and your preference.
const
is C++ qualifier to prevent the myODE class to modify the value of the passed variable to the constructor.
4. If your class contains a const member variable, or a reference, you have to use an initalizer list.
className::className(const A, const B) : constantVariable1_(A),constantVariable2_(B)
{}
5. 左值与右值
左值和右值都是针对表达式而言的,左值是指表达式结束后依然存在的持久对象,右值是指表达式结束时就不再存在的临时对象
6. 友元类
友元类中的成员函数可以访问本类中的所有私有成员
7. 嵌套类对象的初始化
当一个类中含有其它类的对象时,需要在构造函数和复制构造函数中用初始化列表对这些嵌套对象优先进行初始化,也别忘了对本类的成员变量进行初始化。
example: 在Line类中使用Point类的对象
构造函数实现 Line::Line(Point xp1, Point xp2) : p1(xp1), p2(xp2) {}
复制构造函数实现 Line::Line(Line &l) : p1(l.p1), p2(l.p2) {}
由于一般使用的是复制型初始化,所以不要求被包含的对象的类中进行自定义相应的复制构造函数,默认的复制构造函数就够用了。
8. break与continue的区别
break是跳出循环
continue是结束本次循环