Sams Teach Yourself C++ in 21 Days

(singke) #1
Answers 851

D



  1. The following is one possible answer:
    0: // Ex1502.cpp
    1: #include
    2: using namespace std;
    3: class myClass
    4: {
    5: public:
    6: myClass();
    7: ~myClass();
    8: void ShowMember();
    9: void ShowStatic();
    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: void myClass::ShowStatic()
    33: {
    34: cout << “itsStatic: “ << itsStatic << endl;
    35: }
    36: int myClass::itsStatic = 0;
    37:
    38: int main()
    39: {
    40: myClass obj1;
    41: obj1.ShowMember();
    42: obj1.ShowStatic();
    43:
    44: myClass obj2;
    45: obj2.ShowMember();
    46: obj2.ShowStatic();
    47:
    48: myClass obj3;


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

Free download pdf