当前位置:网站首页>子类调用父类构造函数的时机
子类调用父类构造函数的时机
2022-07-27 05:19:00 【Mr FF】
1 创建子类对象时,父类的构造函数先于子类构造函数被调用。(析构时,子类的析构函数先被调用)
2 如果父类有多个构造函数,在子类构造函数中若未指定,则调用父类的默认构造函数或者无参构造函数。否则,按照子类构造函数中指定的父类构造函数类型调用。请看下例
#include <iostream>
#include <string>
using namespace std;
class Weapon {
public:
Weapon()
{
cout << "Weapon constructor" << endl;
}
Weapon(int value, string name) : m_value(value), m_name(name)
{
cout << "Weapon constructor"
<< ", value is: " << m_value
<< ", name is: " << m_name
<< endl;
}
virtual ~Weapon()
{
cout << "Weapon destructor" << endl;
}
private:
int m_value;
string m_name;
};
class Sold : public Weapon {
public:
Sold(float damage) : m_damage(damage) , Weapon(10, "killer") {
cout << "Sold constructor"
<< ", damage is: "
<< m_damage
<< endl;
}
virtual ~Sold()
{
cout << "Sold destructor" << endl;
}
private:
float m_damage;
};
int main()
{
Sold sold(27.8);
return 0;
}运行结果:
Weapon constructor, value is: 10, name is: killer
Sold constructor, damage is: 27.8
Sold destructor
Weapon destructor边栏推荐
- Handler操作记录 Only one Looper may be created per thread
- 12.优化问题实战
- Gbase 8C - SQL reference 6 SQL syntax (1)
- golang中slice切片使用的误区
- 6.维度变换和Broadcasting
- Day 4.Social Data Sentiment Analysis: Detection of Adolescent Depression Signals
- pytorch模型
- 15. GPU acceleration, Minist test practice and visdom visualization
- 西瓜书学习笔记---第四章 决策树
- Cap principle
猜你喜欢

Inno setup package jar + H5 + MySQL + redis into exe

3.分类问题---手写数字识别初体验

Day 7. Towards Preemptive Detection of Depression and Anxiety in Twitter

10.梯度、激活函数和loss

4. Tensor data type and creation tensor

Day14. Using interpretable machine learning method to distinguish intestinal tuberculosis and Crohn's disease

14. Example - Multi classification problem

数字图像处理——第九章 形态学图像处理

cycleGAN解析

【好文种草】根域名的知识 - 阮一峰的网络日志
随机推荐
Unittest套件与运行器
7. Merger and division
7.合并与分割
GBASE 8C——SQL参考6 sql语法(11)
golang怎么给空结构体赋值
5. Indexing and slicing
Day14. 用可解释机器学习方法鉴别肠结核和克罗恩病
1. Introduction to pytorch
pytorch的多GPU训练的两种方式
Digital image processing Chapter 2 fundamentals of digital image
舆情&传染病时空分析文献阅读笔记
8.数学运算与属性统计
9. High order operation
Day 8.Developing Simplified Chinese Psychological Linguistic Analysis Dictionary for Microblog
Day 9. Graduate survey: A love–hurt relationship
Day 17.The role of news sentiment in oil futures returns and volatility forecasting
Day 9. Graduate survey: A love–hurt relationship
4. Tensor data type and creation tensor
Day 15. Deep learning radiomics can predict axillary lymphnode status in early-stage breast cancer
9.高阶操作