Difference between revisions of "CPP/Classes/InitializationList"
From ProgrammingExamples
< CPP
Daviddoria (Talk | contribs) (Created page with '==InitializationList.cpp== <source lang="cpp"> #include <iostream> class Point { public: Point(int NewValue) : a(NewValue) {} int a; }; int main(int argc, char* argv[…') |
FirstPerson (Talk | contribs) (→InitializationList.cpp) |
||
| Line 3: | Line 3: | ||
#include <iostream> | #include <iostream> | ||
| − | class | + | class IntFloatChar{ |
| − | { | + | private: |
| − | public: | + | int i; |
| − | + | float f; | |
| − | + | char c; | |
| + | public: | ||
| + | IntFloatChar() | ||
| + | : i(), f(), c() //initializer list, set i,f,c to its default value by calling its default ctor | ||
| + | {} | ||
| + | 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(){ reutrn c; } | ||
}; | }; | ||
| − | int main(int argc, char* argv[]) | + | int main(int argc, char* argv[]){ |
| − | { | + | //uses initializer list to initialize data |
| − | + | IntFloatChar crazyDataType = IntFloatChar(1,'a',0.1f); | |
| − | + | IntFloatChar whatIsThis = IntFloatChar(); | |
| + | |||
return 0; | return 0; | ||
} | } | ||
</source> | </source> | ||
Revision as of 00:06, 29 June 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 its default value by calling its default ctor {} 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(){ reutrn 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; }