C++ examples for Class:Constructor
Customizing the Copying of Instances
#include <iostream> using namespace std; class MyClass//ww w. j a v a2 s .c om { protected: static int NextAvailableID; int UniqueID; public: int SomeNumber; int GetID() { return UniqueID; } MyClass(); MyClass(int x); MyClass(const MyClass& source); }; MyClass::MyClass() { UniqueID = NextAvailableID; NextAvailableID++; } MyClass::MyClass(int x) { UniqueID = NextAvailableID; NextAvailableID++; SomeNumber = x; } MyClass::MyClass(const MyClass& source) { UniqueID = NextAvailableID; NextAvailableID++; SomeNumber = source.SomeNumber; } int MyClass::NextAvailableID; int main() { MyClass take1 = 100; MyClass take2; take2.SomeNumber = 200; MyClass take3 = take1; cout << take1.GetID() << " " << take1.SomeNumber << endl; cout << take2.GetID() << " " << take2.SomeNumber << endl; cout << take3.GetID() << " " << take3.SomeNumber << endl; return 0; }