#include // A transparent piggybank. class PiggyBank { int n; //the number of coins in the bank. private. public: PiggyBank(); // default constructor (the prototype only; the // definition follows after the class PiggyBank( int init_n ); // constructor with 1 arg PiggyBank( const PiggyBank& another ); // copy constructor. It clones // PiggyBanks. ~PiggyBank(); // destructor void put_coin(); // put a coin in int count(); // return the count of coins inside }; // the class definition ends here // Never forget that semicolon! // The following definitions are "out-of-class". For functions // that simple, their code ("definitions") can be given inside the class. // For larger functions, the "out-of-class" syntax is preferred. PiggyBank::PiggyBank() : n(0) { cout << "PiggyBank default constructor.\n"; cout << " Piggibank with 0 coins is created\n"; } PiggyBank::PiggyBank( int init_n ) : n( init_n ) { cout << "PiggyBank constructor with 1 argument\n"; cout << " Piggibank with " << n << " coins is created\n"; } // The copy constructor is used to clone objects. // When a PiggyBank is passed to a function by value, it is // the copy constructor that is called to make a copy of that argument. PiggyBank::PiggyBank( const PiggyBank& another ) : n( another.n ) { cout << "PiggyBank copy constructor\n"; cout << " Piggibank with " << n << " coins is cloned\n"; } // The destructor is called each time a PiggyBank object is // destroyed by the system. In particular, it is called when // an object goes out of scope (e.g. out of a block in which // it has been defined). When your program finishes, the destructors // are called for all remaining objects. PiggyBank::~PiggyBank() { cout << "Breaking a PiggyBank. "; cout << n << " coins found.\n"; // after this is printed, the space occupied by the object is freed. } void PiggyBank::put_coin() { n += 1; } int PiggyBank::count() { // the piggy is transparent, so... return n; } // For your reference, this class with no outside definitions // looks as follows: // //class PiggyBank { // int n; //the number of coins in the bank. private. // //public: // PiggyBank() : n(0) { // cout << "PiggyBank default constructor\n"; // } // // PiggyBank( int init_n ) : n( init_n ) { // cout << "PiggyBank constructor with 1 argument\n"; // } // // PiggyBank( const PiggyBank& another ) : n( another.n ) { // cout << "PiggyBank copy constructor\n"; // } // // ~PiggyBank() { // cout << "Breaking a PiggyBank. "; // cout << n << " coins found.\n"; // } // // void put_coin() { n += 1; } // // int count() { return n; } // //}; void foo( PiggyBank x ); void foo( PiggyBank x ){ // arg by value cout << "foo: got a bank with " << x.count() << " coins\n"; x.put_coin(); x.put_coin(); x.put_coin(); } void bar( PiggyBank& x ); void bar( PiggyBank& x ){ // arg by reference cout << "foo: got a bank with " << x.count() << " coins\n"; x.put_coin(); x.put_coin(); x.put_coin(); } int main(){ PiggyBank a, b; cout << " So a has " << a.count() << " coins\n"; a.put_coin(); a.put_coin(); cout << " Now a has " << a.count() << " coins\n"; PiggyBank c(10); PiggyBank arr[3]; { PiggyBank d( 50 ); cout << d.count() << endl; } cout << "\nTrying a temporary object...\n"; cout << PiggyBank(100).count() << endl; cout << "Done with the temporary object.\n\n"; cout << "Trying a pass by value...\n"; foo( b ); cout << " Now b has " << b.count() << " coins\n"; cout << "Done with the pass by value\n\n"; cout << "Trying a pass by reference...\n"; bar( b ); cout << " Now b has " << b.count() << " coins\n"; cout << "Done with the pass by reference\n\n"; cout << "Dynamic memory allocation\n\n"; int num; cout << "How many piggybanks? "; cin >> num; PiggyBank * p = new PiggyBank[ num ]; //put a coin in each for( int i=0; i < num; i++) p[i].put_coin(); //try putting 1 coin in the 1st one, two coins in the second one, //three in the third etc, so that the last one has num coins inside // now print them out for( int j=0; j < num; j++) cout << p[j].count() << " "; // why won't cout << p[j].n << " "; work here? delete [] p; }