Sams Teach Yourself C++ in 21 Days

(singke) #1
40: {
41: void (myClass::*PMF) ();
42:
43: PMF=myClass::ShowMember;
44:
45: myClass obj1;
46: (obj1.*PMF)();
47: cout << “Static: “ << myClass::GetStatic() << endl;
48:
49: myClass obj2;
50: (obj2.*PMF)();
51: cout << “Static: “ << myClass::GetStatic() << endl;
52:
53: myClass obj3;
54: (obj3.*PMF)();
55: cout << “Static: “ << myClass::GetStatic() << endl;
56: return 0;
57: }


  1. The following is one possible answer:
    0: // Ex1505.cpp
    1: #include
    2: using namespace std;
    3: class myClass
    4: {
    5: public:
    6: myClass();
    7: ~myClass();
    8: void ShowMember();
    9: void ShowSecond();
    10: void ShowThird();
    11: static int GetStatic();
    12: private:
    13: int itsMember;
    14: int itsSecond;
    15: int itsThird;
    16: static int itsStatic;
    17: };
    18:
    19: myClass::myClass():
    20: itsMember(1),
    21: itsSecond(2),
    22: itsThird(3)
    23: {
    24: itsStatic++;
    25: }
    26:
    27: myClass::~myClass()
    28: {
    29: itsStatic--;
    30: cout << “In destructor. ItsStatic: “ << itsStatic << endl;


854 Appendix D

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

Free download pdf