/////////////////////////////////////////////////////// // // public.cpp // // Started 1/16/97 by Jeff Ondich // Last modified: 1/16/97 // // For these lab exercises, I have put class interfaces // (the "class DopeyClass {...};" statement) and // implementations (the actual code for the member // functions) in one file along with main(). This // is for your convenience in transferring the files // from the Web site to your account. Normally, // however, the class interface should go in a // header file (such as DopeyClass.h), the implementation // should go in a .cpp file (DopeyClass.cpp), and the // main program should go in a separate .cpp file. // // 1. Read the program. Note that several lines of // code are commented out. I'll refer to those // lines below. As it stands, what do you expect // the program to do? Try it and see if you're right. // // 2. As a tiny exercise in creating classes of your // own, add a member function "CubeNumber()" to // DopeyClass, and test it in main(). // // 3. Uncomment the lines labeled D in main(). // What do you expect to happen? Recompile and see. // What happened and why? // // 4. Leave lines D uncommented, and change the // "private" in the DopeyClass interface to "public". // Recompile and run. Did it go okay? // // 5. What happens if you just remove the "public" and the // "private" from the DopeyClass interface? Are the // member data and functions public by default or // private by default? // // 6. Often classes have a few member functions that // are simple--one-liners, essentially. It can // be convenient to define these functions directly // in the interface. To see how this is done, // uncomment line B in the interface, and completely // remove both line A and lines C. Recompile, re-run. // //////////////////////////////////////////////////////// #include /////////////////////////////////////////////////////// // Here's the interface for DopeyClass. A DopeyClass // object holds one integer. You use the constructor // to set the integer initially. After that, you can // look at the number (GetNumber()), and you can // square it (SquareNumber()). That's all. Dumb. /////////////////////////////////////////////////////// class DopeyClass { private: int mNumber; public: DopeyClass( int N ); int GetNumber( void ); // Line A void SquareNumber( void ); // Commented-out line B: // int GetNumber( void ) { return( mNumber ); } }; /////////////////////////////////////////////////////// // The next three function definitions constitute the // implementation of DopeyClass. Yeeha! /////////////////////////////////////////////////////// DopeyClass::DopeyClass( int N ) { mNumber = N; } int DopeyClass::GetNumber( void ) // Lines C { // C return( mNumber ); // C } // C void DopeyClass::SquareNumber( void ) { mNumber = mNumber * mNumber; } /////////////////////////////////////////////////////// // The main program /////////////////////////////////////////////////////// int main( void ) { DopeyClass myDopeyObject( 3 ); cout << "Here's the number: " << myDopeyObject.GetNumber() << endl; myDopeyObject.SquareNumber(); cout << "Here's the number now: " << myDopeyObject.GetNumber() << endl; // Commented-out lines D: // myDopeyObject.mNumber = myDopeyObject.mNumber + 33; // cout << "Here's the number now: " << myDopeyObject.GetNumber() << endl; return( 0 ); }