Difference between revisions of "CPP/Classes/InitializationList"

From ProgrammingExamples
< CPP
Jump to: navigation, search
(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[…')
 
(InitializationList.cpp)
Line 3: Line 3:
 
#include <iostream>
 
#include <iostream>
  
class Point
+
class IntFloatChar{
{
+
private:
   public:
+
   int i;
    Point(int NewValue) : a(NewValue) {}
+
  float f;
    int a;
+
  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
   Point P(2);
+
  IntFloatChar crazyDataType = IntFloatChar(1,'a',0.1f);
   std::cout << P.a << std::endl;
+
   IntFloatChar whatIsThis = IntFloatChar();  
 +
 
   return 0;
 
   return 0;
 
}
 
}
  
 
</source>
 
</source>

Revision as of 01: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;
}