Difference between revisions of "CPP/Classes/InitializationList"
From ProgrammingExamples
< CPP
(→InitializationList.cpp) |
Daviddoria (Talk | contribs) m |
||
Line 10: | Line 10: | ||
public: | public: | ||
IntFloatChar() | IntFloatChar() | ||
− | : i(), f(), c() //initializer list, set i,f,c to their default values (undefined for primitive types like int, float, char or any pointer type) by calling their default | + | : i(), f(), c() //initializer list, set i,f,c to their default values (undefined for primitive types like int, float, char or any pointer type) by calling their default constructors |
{} | {} | ||
IntFloatChar(int I, float F, char C) | IntFloatChar(int I, float F, char C) |
Latest revision as of 08:31, 1 December 2010
InitializationList.cpp
#include <iostream> class IntFloatChar{ private: int i; float f; char c; public: IntFloatChar() : i(), f(), c() //initializer list, set i,f,c to their default values (undefined for primitive types like int, float, char or any pointer type) by calling their default constructors {} IntFloatChar(int I, float F, char C) : i(I), f(F), c(C) // initializer list, set value to the passed arguments {} int& intValue(){ return i; } float& floatValue(){ return f;} char& charValue(){ return c; } }; int main(int argc, char* argv[]){ //uses initializer list to initialize data IntFloatChar crazyDataType = IntFloatChar(1,'a',0.1f); IntFloatChar whatIsThis = IntFloatChar(); return 0; }