18-ClassStatics - #include <iostream> #include...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> #include <cstring> using namespace std; // for regular member variables, each object gets // a copy of that variable as a part of the object // the functions are shared amongst the various // objects // when we apply the static keyword onto a member // variable we alter these semantics. Instead of // each object getting its own copy, one copy // is maintained, somewhat similar to what happens // to a static variable of a c-style function, the // same variable is used throughout the program void staticVarFunction() { static int count = 0; cout << count++ << endl; } void ResetStaticVarFunction(bool reset = false) { static int theCount = 0; if (reset) theCount = 0; cout << theCount++ << endl; } class Account { private: static int number; static const int NameSize = 25; char name[NameSize]; int accountNumber; Account() {} public: Account(char Name[]) { strcpy(name, Name); accountNumber = number++; } void print() { cout << "Account: " << accountNumber << "\nName: " << name << "\nPublic Static: " << pubStatic << "\nPrivate static: " << number << "\nGetting Number: " << getNumber() << endl << endl; } static int pubStatic; static int getNumber() {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

18-ClassStatics - #include &lt;iostream&gt; #include...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online