Chapter 9 More on Classes and Objects

1. Constructors(构造函数) 函数名即类名,无返回值,为对象开辟存储空间时自动调用。可使用初始化器:PlayingCard (Suits is, int ir) : suitValue (is), rankValue (ir) {    }

2. Default Constructor(缺省构造函数)无参数。

3. Destructor Declaration(析构函数)~ ClassName (   ) { … } 类名前面加~,无参数无返回值,不能被重载,对象的内存空间被释放时自动调用。

4.构造函数和析构函数调用顺序:类似栈的结构。

5.对象数组:ClassName ArrayName[ConstIntExpression];

using the default constructor

PlayingCard cardArray[52];
using constructor with parameters

PlayingCard cardArray[2]={ PlayingCard(PlayingCard::Heart,1), PlayingCard(PlayingCard::Club,2) };

 6.对象指针:The pointer variable will hold the beginning addressof an object in memory space.

this是一个指针!

7.Only the initializercan be used to specify the initial value for const dada members.

8.成员函数也可以const:DataType FunctionName(Parameter List) const;

The const member function can access the data members in its class, but can not modify their values.

9.const对象:constClassName ObjectName[(Augument List)];//or ClassName const ObjectName [(Augument List)];

All the data members of a const object are const data members.  Only the const member functionof a const object can be accessed.

10.指针变量也可const,指针指向的对象是谁不能改变,但可以改变对象的值。

11.引用也可const:DataType FunctionName(const ClassName & ReferenceName);但引用对象的值不能被改变。

12.栈区编译时分配内存,堆区运行时分配内存。

注意指针操作:ClassName *PointerName; PointerName=new ClassName;//or PointerName=new ClassName(Argument List); … delete PointerName;

13.浅拷贝与深拷贝:A shallow copy sharesinstance variables with the original. A deep copy creates new copies of the instance variables.

14.拷贝构造函数:ClassName:: ClassName(const ClassName & ObjectName) { … }注意const引用,要不然生成对象数组的时候会报错!

两种调用方式:ClassName ObjectName1; … ClassName ObjectName2(ObjectName1);//or ClassName ObjectName3=ObjectName1;

15.Static member has only one copy shared by all the instances(实例) of a class.

16.类声明中,静态成员变量仅完成了声明,并未定义和赋初始值,也不能在类内定义和赋初始值。

17.Static member functions are mainly used to access static data members.

18.调用静态成员函数:类名.函数名(参数列表)或类名::函数名(参数列表) 没有this指针。 

19.友元可以是: external function  member function of another class  a whole class 友元可以从外部访问整个类的成员。friend关键字要加在最前面。

20.类模板:Box<int>iBox(7);

猜你喜欢

转载自www.cnblogs.com/lipoicyclic/p/12761935.html