Sams Teach Yourself C++ in 21 Days

(singke) #1
49: obj3.ShowMember();
50: obj3.ShowStatic();
51: return 0;
52: }


  1. The following is one possible answer:
    0: // Ex1503.cpp
    1: #include
    2: using namespace std;
    3: class myClass
    4: {
    5: public:
    6: myClass();
    7: ~myClass();
    8: void ShowMember();
    9: static int GetStatic();
    10: private:
    11: int itsMember;
    12: static int itsStatic;
    13: };
    14:
    15: myClass::myClass():
    16: itsMember(1)
    17: {
    18: itsStatic++;
    19: }
    20:
    21: myClass::~myClass()
    22: {
    23: itsStatic--;
    24: cout << “In destructor. ItsStatic: “ << itsStatic << endl;
    25: }
    26:
    27: void myClass::ShowMember()
    28: {
    29: cout << “itsMember: “ << itsMember << endl;
    30: }
    31:
    32: int myClass::itsStatic = 0;
    33:
    34: int myClass::GetStatic()
    35: {
    36: return itsStatic;
    37: }
    38:
    39: int main()
    40: {
    41: myClass obj1;
    42: obj1.ShowMember();
    43: cout << “Static: “ << myClass::GetStatic() << endl;
    44:


852 Appendix D

32 0672327112_app_d.qxd 11/19/04 12:30 PM Page 852

Free download pdf