分析几个简单C++的函数,熟悉一些相关知识.
通过本程序主要熟悉一下类的定义、几种类对象的定义和声明方式 ......
分析几个简单C++的函数,熟悉一些相关知识.
通过本程序主要熟悉一下类的定义、几种类对象的定义和声明方式、构造和析构函数的调用顺序以及各变量的生命域等。代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
#include <iostream.h>
2
#include <string.h>
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
class CDemo
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
6
public:
7
CDemo(const char* str);
8
~CDemo();
9
private:
10
char name[20]; //pay attention to this kind of formt
11
};
12![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
CDemo::CDemo(const char* str)
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
15
strncpy(name, str, 20);
16
cout << "Constructor called for " << name << '\n';
17
}
18![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
CDemo::~CDemo()
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
21
cout << "Destructor called for " << name << '\n';
22
}
23![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
24
void func()
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
26
CDemo LocalObjectInFunc("LocalObjectInFunc"); // in stack
27
static CDemo StaticObject("StaticObject"); // local static
28
CDemo* pHeapObjectInFunc = new CDemo("HeapObjectInFunc"); // in heap
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
cout << "Inside func" << endl;
31![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
}
33![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
34
CDemo GlobalObject("GlobalObject"); // global static
35![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
36
void main()
37![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
38
CDemo LocalObjectInMain("LocalObjectInMain"); // in stack
39
CDemo* pHeapObjectInMain = new CDemo("HeapObjectInMain"); // in heap
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
cout << "In main, before calling func\n";
42
func();
43
cout << "In main, after calling func\n";
44![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
}
46![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
运行结果:
主要说明:
<1>注意到CDemo类中包含一个私有成员变量,该成员变量的赋值仅能通过构造函数来完成,应该学习一下这种编程方法和风格;
<2>由结果看出程序首先初始化全局变量,然后依次初始化main()函数中的类对象,然后调用局部函数fun(),并依次初始化其定义的局部类对象.注意各种类对象的析构过程,在局部函数结束后,其内部定义的局部类对象被析构,然后在主函数结束之前,将内部定义的类对象进行析构,接下来析构静态类对象,最后析构全局类对象.注意指针型类对象不进行析构.
=================================================================
拓展知识点:
<1>考虑出现继承时子类构造函数和析构函数的调用过程;
<2>对于虚构造函数如何调用;