------------------------------------------------------------------------------
MC logo
Hidden Calls
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Safe Integer Array Class] [Safe Integer Array Implementation] [Bounds Error Demo] [Object Copy Demo] [No Copies For Me, Please] [Hidden Calls]
cdprint.cpp
#include <iostream>

using namespace std;

class Ningle {
private:
        int amt;
public:
        Ningle(int i) {
                amt = i;
                cout << "Making an " << i << "-sized Ningle." << endl;
        }
        Ningle() {
                amt = 0;
                cout << "Making a measly Ningle" << endl;
        }
        Ningle(const Ningle &other) {
                amt = other.amt;
                cout << "Copying " << amt << " of Ningle" << endl;
        }
        ~Ningle() {
                cout << "Ningle of " << amt << " is no more." << endl;
        }

        void inc(int i) {
                amt += i;
        }

        int get() { return amt; }

        void set(int i) { amt = i; }
};

Ningle * bungle(Ningle p, Ningle &q)
{
        Ningle c;
        cout << "Got Ningles " << p.get() << " and " << q.get() << endl;

        c.set(p.get() + q.get());
        cout << "That makes " << c.get() << "!" << endl;

        p.inc(1);
        q.inc(2);

        return new Ningle(44);
}

main()
{
        Ningle x(10), y(3), z;

        cout << "Calling bungle()" << endl;
        Ningle *np = bungle(x,y);
        cout << "Got a Ningle pointer worth " << np->get() << endl;
        delete np;
        cout << "But I killed it." << endl;
}

Constructors and destructors are called whenever objects are created or destroyed, sometimes in less-than-obvious places. Can you figure out what this program prints? Run it and see.