Mega Code Archive

 
Categories / C++ Tutorial / Class
 

Overload constructor two ways

#include <iostream> #include <new> using namespace std; class powers {   int x; public:   powers() {      x = 0;      cout << "\nno initializer\n\n";   }   powers(int n) {      x = n;      cout << "\n\ninitializer:" << x;   }   int getx() { return x; }   void setx(int i) { x = i; } }; int main() {   powers ofTwo[] = {1, 2, 4, 8, 16}; // initialized   powers ofThree[5];                 // uninitialized } initializer:1 initializer:2 initializer:4 initializer:8 initializer:16 no initializer no initializer no initializer no initializer no initializer