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 par Ameter instead, see Code Snippets ' main.cpp ', ' Box.h ', ' Box.cpp '.
If your compiler supports C++11 standards, the syntax of telescoping are shown in code snippet ' Box2.h ' and ' Box2.cpp ', and You might need to use '-std=c++11 ' parameter if you're compiling your source code.
For-now, I-d like-to-use default parameter.
Main.cpp
1#include"Box.h"2 3 intMain ()4 {5 box box;6 box.print ();7Box Box2 (1);8 box2.print ();9Box Box3 (1,'A');Ten box3.print (); OneBox Box4 (1,'A',1.88); A box4.print (); - return 0; -}
Box.h
1 #ifndef Box_h2 #defineBox_h3 4 classBox5 {6 Public:7Box (Const int&_a =0,Const Char&_b ='M',Const Double&_c =0.99);8~Box ();9 voidprint ();Ten One Private: A intA; - Charb; - DoubleC; the }; - - #endif //Box_h
Box.cpp
1#include"Box.h"2 3#include <iostream>4 5 using namespacestd;6 7Box::box (Const int&_a,Const Char&_b,Const Double&_c)8 : A (_a), B (_b), C (_c)9 {Ten One } A -box::~Box () - { the - } - - voidBox::p rint () { +cout <<"A ="<< a <<", B ="<< b <<", C ="<< C <<Endl; -}
Box2.h
1 #ifndef Box_h2 #defineBox_h3 4 classBox5 {6 Public:7Box (Const int&_a,Const Char&_b,Const Double&_c);8Box (Const int&_a,Const Char&_b);9Box (Const int&_a);Ten Box (); One~Box (); A voidprint (); - - Private: the intA; - Charb; - DoubleC; - }; + - #endif //Box_h
Box2.cpp
1#include"Box.h"2 3#include <iostream>4 5 using namespacestd;6 7Box::box (Const int&_a,Const Char&_b,Const Double&_c)8 : A (_a), B (_b), C (_c)9 {Ten One } A -Box::box (Const int&_a,Const Char&_b) -: Box (_a, _b,0.99) the { - - } - +Box::box (Const int&_a) -: Box (_a,'M') + { A at } - - Box::box () -: Box (0) - { - in } - tobox::~Box () + { - the } * $ voidBox::p rint () {Panax Notoginsengcout <<"A ="<< a <<", B ="<< b <<", C ="<< C <<Endl; -}
C + + Telescoping constructor is not supported until c++11