C++ Constructor Copying Instances
#include <iostream> using namespace std; class MyClass//from w w w.java 2s. c om { protected: static int myID; int newID; public: int SomeNumber; int GetID() { return newID; } MyClass(); MyClass(int x); MyClass(const MyClass& source); }; MyClass::MyClass() { newID = myID; myID++; } MyClass::MyClass(int x) { newID = myID; myID++; SomeNumber = x; } MyClass::MyClass(const MyClass& source) { newID = myID; myID++; SomeNumber = source.SomeNumber; } int MyClass::myID; 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; }