#include #include using namespace std; class Sharer { public: Sharer(): myId(++counter) { counter2++; } /*Sharer(){ myId = ++counter; counter2++; } */ static string ToUpper(string s){ for(auto & x: s) { x = static_cast(toupper(x)); } return s; } /* static void ToUpper() { name = ToUpper(name); } */ static void ToUpper(Sharer & s) { s.name = ToUpper(s.name); return; } void Name(string n){name = n;} string Name(void) const { return name;}; int ID(void) const { return myId; } static int Counter(void) { return counter; } static int Counter2(void) { return counter2; } const static int SOME_CONSTANT = 100; private: string name; static int counter; static inline int counter2 = 10; const int myId; }; // need to do this once. // Should be in the implementation file. // note we do not use static here. int Sharer::counter = 0; int main() { /* const int foo; foo = 7; */ // called before an instance of the class is created! cout << Sharer::ToUpper("hello world") << endl; cout << "We have created " << Sharer::Counter() << " sharers." << endl; cout << "Counter2 is " << Sharer::Counter2() << "." << endl; cout << "And the constant is " << Sharer::SOME_CONSTANT << "." << endl; cout << endl; //Sharer::counter++; Sharer a; cout << "I am sharer " << a.ID() << endl; cout << "We have created " << Sharer::Counter() << " sharers." << endl; cout << "The constat is still " << a.SOME_CONSTANT << "." << endl; cout << endl; a.Name("howdie"); Sharer::ToUpper(a); cout << a.Name() << endl; cout <<" a's id = " << a.ID() << endl; Sharer b,c,d; cout <<" b's id = " << b.ID() << endl; cout <<" c's id = " << c.ID() << endl; cout <<" d's id = " << d.ID() << endl; cout << endl; cout << "We have created " << Sharer::Counter() << " sharers." << endl; cout << "We have created " << a.Counter() << " sharers." << endl; cout << "We have created " << b.Counter() << " sharers." << endl; cout << "We have created " << c.Counter() << " sharers." << endl; cout << "We have created " << d.Counter() << " sharers." << endl; cout << endl; cout << "Counter2 is " << Sharer::Counter2() << "." << endl; cout << "Counter2 is " << a.Counter2() << "." << endl; cout << "Counter2 is " << b.Counter2() << "." << endl; cout << "Counter2 is " << c.Counter2() << "." << endl; cout << "Counter2 is " << d.Counter2() << "." << endl; return 0; }