Telescoping constructor: see Effective Java 2nd Edition Item 2

If you want to use telescoping constructor but your compiler does not support c++11 standards, you can use default parameter instead, see code snippets 'main.cpp', 'Box.h', 'Box.cpp'.

If your compiler supports c++11 standards, the syntax of telescoping is shown in code snippet 'Box2.h' and 'Box2.cpp', and you might need to use '-std=c++11' parameter when you are compiling your source code.

For now, I'd like to use default parameter.

main.cpp

 #include "Box.h"

 int main()
{
Box box;
box.print();
Box box2();
box2.print();
Box box3(, 'A');
box3.print();
Box box4(, 'A', 1.88);
box4.print();
return ;
}

Box.h

 #ifndef BOX_H
#define BOX_H class Box
{
public:
Box(const int &_a = , const char &_b = 'M', const double &_c = 0.99);
~Box();
void print(); private:
int a;
char b;
double c;
}; #endif // BOX_H

Box.cpp

 #include "Box.h"

 #include <iostream>

 using namespace std;

 Box::Box(const int &_a, const char &_b, const double &_c)
: a(_a), b(_b), c(_c)
{ } Box::~Box()
{ } void Box::print() {
cout << "a = " << a << ",b = " << b << ",c = " << c << endl;
}

Box2.h

 #ifndef BOX_H
#define BOX_H class Box
{
public:
Box(const int &_a, const char &_b, const double &_c);
Box(const int &_a, const char &_b);
Box(const int &_a);
Box();
~Box();
void print(); private:
int a;
char b;
double c;
}; #endif // BOX_H

Box2.cpp

 #include "Box.h"

 #include <iostream>

 using namespace std;

 Box::Box(const int &_a, const char &_b, const double &_c)
: a(_a), b(_b), c(_c)
{ } Box::Box(const int &_a, const char &_b)
: Box(_a, _b, 0.99)
{ } Box::Box(const int &_a)
: Box(_a, 'M')
{ } Box::Box()
: Box()
{ } Box::~Box()
{ } void Box::print() {
cout << "a = " << a << ",b = " << b << ",c = " << c << endl;
}

最新文章

  1. DataList删除操作
  2. [Machine-Learning] 熟悉Matlab
  3. socket.io 中文手册 中文文档
  4. 基站查询接口,基站查询API
  5. Algorithm | Tree traversal
  6. Spring中bean的scope详解
  7. SpringMVC学习系列(10) 之 异常处理
  8. JavaWeb 输出九九乘法表,三角形,菱形
  9. excel导入数据库
  10. HDU1297 Children’s Queue (高精度+递推)
  11. php的setcookie
  12. C#数据库连接字符串
  13. Eclipse plugin web site 发布和版本更新
  14. ArticleRemoveDelDialog【基于AlertDialog的回收删除对话框】
  15. Java安装以及环境变量配置
  16. tab 切换实现方法
  17. ef 数据库连接字符串加密
  18. sublime使用技巧总结
  19. insert into on duplicate key update
  20. GRE协议

热门文章

  1. 【分块】bzoj2120 数颜色
  2. iOS 获取自定义cell上按钮所对应cell的indexPath.row的方法
  3. 【FTP】java FTPClient 文件上传内容为空,文件大小为0
  4. C#基础之 派生类
  5. 解决小米手机Android Studio安装app 报错的问题It is possible that this issue is resolved by uninstalling an existi
  6. python的dict如何排序
  7. WebHelper-SessionHelper、CookieHelper、CacheHelper、Tree
  8. IP地址后面斜杠加具体数字详解
  9. linux导入so文件
  10. 蓝点通用管理系统V13版发布了!