C++子类对象—子类对象的构造和析构
来源:互联网 发布:医院网络瘫痪应急预案 编辑:程序博客网 时间:2024/04/27 16:59
对象在使用之前,始终是要经历“构造”这个过程的。在第15章,我们了解到当一个对象的成员数据是另一个对象的时候,就先运行成员对象的构造函数,再运行父对象的构造函数。但是继承的出现,会引入子类的构造函数。这时候,这些构造函数的运行顺序又是怎样的呢?
//node.h和linklist.h同程序17.3.2
//stack.h
#include "linklist.h"
class Stack:private Linklist//私有继承链表类
{
public:
bool push(int i,char c);
bool pop(int &i,char &c);
void show();
Stack(int i,char c);
Stack();
};
Stack::Stack(int i,char c):Linklist(i,c)//将子类构造函数的参数传递给父类的构造函数
{
cout <<"Stack constructor with parameter is running..." <<endl;
}
Stack::Stack()//子类构造函数
{
cout <<"Stack constructor is running..." <<endl;
}
bool Stack::push(int i,char c)
{
while (pcurrent->next!=NULL)
pcurrent=pcurrent->next;
return Insert(i,c);
}
bool Stack::pop(int &i,char &c)
{
while (pcurrent->next!=NULL)
pcurrent=pcurrent->next;
i=pcurrent->idata;
c=pcurrent->cdata;
return Delete();
}
void Stack::show()
{
Show();
}
//main.cpp
#include <iostream>
#include "stack.h"
int main()
{
Stack ss(1,'4');//调用带参数的构造函数
cout <<"Stack ss constructed" <<endl;
ss.show();
Stack zz; //调用不带参数的构造函数
cout <<"Stack zz constructed" <<endl;
zz.show();
return 0;
}
运行结果:
Node constructor is running...
Linklist constructor is running...
Stack constructor with parameter is running...
Stack ss constructed
1 4
Node constructor is running...
Linklist constructor is running...
Stack constructor is running...
Stack zz constructed
0 0
Linklist destructor is running...
Node destructor is running...
Linklist destructor is running...
Node destructor is running...
这个程序中有三个类,其中Stack类是Linklist类的子类,Node类的对象是Linklist类的成员数据。根据程序的运行结果,我们可以确定,父类的成员对象仍然是最先构造的,接着是运行父类的构造函数,最后运行子类的构造函数。也就是说子类对象是在父类对象的基础上扩展而成的。
另外,如果我们希望把子类的构造函数的参数传递给父类的构造函数时,可以在子类的构造函数定义中用以下格式调用父类的构造函数:
子类名::构造函数名(参数表):父类名(参数表)
如程序17.4.1就是用上述方法实现子类和父类的构造函数参数传递。这样的方法不仅使子类对象的初始化变得简单,并且使子类和父类的构造函数分工明确,易于维护。
//node.h和linklist.h同程序17.3.2
//stack.h
#include "linklist.h"
class Stack:private Linklist
{
public:
bool push(int i,char c);
bool pop(int &i,char &c);
void show();
Stack(int i,char c);
Stack();
~Stack();//析构函数
};
Stack::Stack(int i,char c):Linklist(i,c)
{
cout <<"Stack constructor with parameter is running..." <<endl;
}
Stack::Stack()
{
cout <<"Stack constructor is running..." <<endl;
}
Stack::~Stack()
{
cout <<"Stack destructor is running..." <<endl;
}
bool Stack::push(int i,char c)
{
while (pcurrent->next!=NULL)
pcurrent=pcurrent->next;
return Insert(i,c);
}
bool Stack::pop(int &i,char &c)
{
while (pcurrent->next!=NULL)
pcurrent=pcurrent->next;
i=pcurrent->idata;
c=pcurrent->cdata;
return Delete();
}
void Stack::show()
{
Show();
}
//main.cpp
#include <iostream>
#include "stack.h"
int main()
{
Stack zz;
cout <<"Stack zz constructed" <<endl;
zz.show();
return 0;
}
运行结果:
Node constructor is running...
Linklist constructor is running...
Stack constructor is running...
Stack zz constructed
0 0
Stack destructor is running...
Linklist destructor is running...
Node destructor is running...
子类对象的构造
讨论子类对象的构造,就是在讨论子类对象的生成方式。它是先生成父类对象的成员,再对其进行扩展呢,还是先生成子类对象的成员,然后再对其进行补充?我们还是修改一下程序17.3.2,用事实来解决这个问题:(程序17.4.1)//node.h和linklist.h同程序17.3.2
//stack.h
#include "linklist.h"
class Stack:private Linklist//私有继承链表类
{
public:
bool push(int i,char c);
bool pop(int &i,char &c);
void show();
Stack(int i,char c);
Stack();
};
Stack::Stack(int i,char c):Linklist(i,c)//将子类构造函数的参数传递给父类的构造函数
{
cout <<"Stack constructor with parameter is running..." <<endl;
}
Stack::Stack()//子类构造函数
{
cout <<"Stack constructor is running..." <<endl;
}
bool Stack::push(int i,char c)
{
while (pcurrent->next!=NULL)
pcurrent=pcurrent->next;
return Insert(i,c);
}
bool Stack::pop(int &i,char &c)
{
while (pcurrent->next!=NULL)
pcurrent=pcurrent->next;
i=pcurrent->idata;
c=pcurrent->cdata;
return Delete();
}
void Stack::show()
{
Show();
}
//main.cpp
#include <iostream>
#include "stack.h"
int main()
{
Stack ss(1,'4');//调用带参数的构造函数
cout <<"Stack ss constructed" <<endl;
ss.show();
Stack zz; //调用不带参数的构造函数
cout <<"Stack zz constructed" <<endl;
zz.show();
return 0;
}
运行结果:
Node constructor is running...
Linklist constructor is running...
Stack constructor with parameter is running...
Stack ss constructed
1 4
Node constructor is running...
Linklist constructor is running...
Stack constructor is running...
Stack zz constructed
0 0
Linklist destructor is running...
Node destructor is running...
Linklist destructor is running...
Node destructor is running...
这个程序中有三个类,其中Stack类是Linklist类的子类,Node类的对象是Linklist类的成员数据。根据程序的运行结果,我们可以确定,父类的成员对象仍然是最先构造的,接着是运行父类的构造函数,最后运行子类的构造函数。也就是说子类对象是在父类对象的基础上扩展而成的。
另外,如果我们希望把子类的构造函数的参数传递给父类的构造函数时,可以在子类的构造函数定义中用以下格式调用父类的构造函数:
子类名::构造函数名(参数表):父类名(参数表)
如程序17.4.1就是用上述方法实现子类和父类的构造函数参数传递。这样的方法不仅使子类对象的初始化变得简单,并且使子类和父类的构造函数分工明确,易于维护。
子类对象的析构
在第15章中介绍析构函数的时候,我们就说它的运行顺序往往是和构造函数的运行顺序相反的。那么使用了继承之后,是否依然是这样的规律呢?我们继续修改程序17.4.1,尝试验证我们的猜想。//node.h和linklist.h同程序17.3.2
//stack.h
#include "linklist.h"
class Stack:private Linklist
{
public:
bool push(int i,char c);
bool pop(int &i,char &c);
void show();
Stack(int i,char c);
Stack();
~Stack();//析构函数
};
Stack::Stack(int i,char c):Linklist(i,c)
{
cout <<"Stack constructor with parameter is running..." <<endl;
}
Stack::Stack()
{
cout <<"Stack constructor is running..." <<endl;
}
Stack::~Stack()
{
cout <<"Stack destructor is running..." <<endl;
}
bool Stack::push(int i,char c)
{
while (pcurrent->next!=NULL)
pcurrent=pcurrent->next;
return Insert(i,c);
}
bool Stack::pop(int &i,char &c)
{
while (pcurrent->next!=NULL)
pcurrent=pcurrent->next;
i=pcurrent->idata;
c=pcurrent->cdata;
return Delete();
}
void Stack::show()
{
Show();
}
//main.cpp
#include <iostream>
#include "stack.h"
int main()
{
Stack zz;
cout <<"Stack zz constructed" <<endl;
zz.show();
return 0;
}
运行结果:
Node constructor is running...
Linklist constructor is running...
Stack constructor is running...
Stack zz constructed
0 0
Stack destructor is running...
Linklist destructor is running...
Node destructor is running...
根据运行结果,我们可以确认:使用了继承之后,析构函数的运行顺序依然恰好与构造函数的运行顺序相反。
原文:http://see.xidian.edu.cn/cpp/biancheng/view/92.html
0 0
- C++子类对象—子类对象的构造和析构
- C++【子类的构造和析构】
- Java 子类构造对象
- 黑马程序员--java中父类和子类都有构造方法时,子类对象的初始化过程
- 【面向对象】Java子类的构造函数
- 子类的构造,析构和拷贝
- 5.3 子类和对象
- 关于基类指针指向子类对象,构造函数和析构函数调用的顺序的问题。
- 基类子类子对象的构造与析构顺序
- 关于子类对象的构造函数和父类构造函数的执行顺序
- 类和子类对象的转换
- [C++]子类的构造函数
- Java 构造器之访问子类对象的实例变量
- java 子类对象的实例化 调用构造函数先后
- 子类继承父类时,创建对象,构造方法的调用问题
- 子类继承父类,那么子类实例化时对象调用的是父类还是子类的构造函数?
- 继承和派生--包含子对象的子类的构造函数
- 《C++面向对象》在C++中子类继承和调用父类的构造函数方法
- win7系统右下角没有音量的系统图标
- Android中的Intent详细讲解
- iOS:remove all subviews
- 2013 CocoaChina微信精选之技术汇
- JS时间比较
- C++子类对象—子类对象的构造和析构
- Lucene学习总结之三:Lucene的索引文件格式(1)
- first order system analysis 自控原理 一阶系统的matlab分析
- Cocos2dx 像素碰撞检测
- JVM内存结构和6大区域
- 随心小记
- android ActionBar-PullToRefresh 下拉刷新的使用方法
- unix 网络编程学习(1)time服务器
- Eclipse中10个最有用的快捷键组合