Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12372207
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: C/C++

2015-10-22 11:38:31

一、CPP构造函数的核心知识点

二、几个关键知识点

1、普通基类定义了有参数的构造函数,派生类的构造函数必须给基类传参数使基类实现初始化。

2、派生类含有用const定义的数据成员,也要在参数列表中初始化。

3、参数列表的初始化顺序是先入基类,再到派生类;类内是根据数据定义的先后顺序初始化,与在列表中的先后顺序是没有必然关系的。

案例测试代码:


  1. #include "stdafx.h"
  2. #include
  3. using namespace std;
  4. class Vehicle
  5. {
  6. public:
  7.     //Vehicle(int width,int height)
  8.     Vehicle(int baseData):Height(baseData),Width(Height+baseData)
  9.     {
  10.         cout<<"Vehicle's Width=" }
  11. public:
  12.     int Width;
  13.     int Height;
  14. };
  15. class Bus : public Vehicle
  16. {
  17. public:
  18.     Bus(int baseData,float price):Vehicle(baseData+SEQ_NUM),MAX_SPEED(100),mSeatCount(MAX_SPEED/5)
  19.     {
  20.         this->Price = price;
  21.         cout<<"Bus's Price=" <<",mSeatCount=" <<",MAX_SPEED="< <<",SEQ_NUM="< < }
  22. public:
  23.     float Price;
  24. private:
  25.     int mSeatCount;
  26.     const int MAX_SPEED;
  27.     static const int SEQ_NUM = 1001; //只有静态常量整型数据成员才能在类中初始化
  28. };

  29. int _tmain(int argc, _TCHAR* argv[])
  30. {
  31.     Bus* myBus = new Bus(2,250000.0);
        cout<<"MyBus's Width="<Width<<",Height="<Height<<",Price="<Price<     getchar();
        return 0;
  32. }
  33. }




参考网址:

http://blog.163.com/xychenbaihu@yeah/blog/static/132229655201243110342683/

http://www.cnblogs.com/BlueTzar/articles/1223169.html

http://blog.csdn.net/zhaojinjia/article/details/8785912

阅读(2777) | 评论(0) | 转发(0) |
0

上一篇:C#的new和CPP的new

下一篇:do_initcalls 的原理

给主人留下些什么吧!~~