MC logo

Construction and Destruction II

  Practice Questions

What does the following code print?

#include <iostream>

using namespace std;

class Azure
{
    private:
        int acnt;
    public:
        Azure() { 
                acnt = 0;
                cout << "Zero" << endl;
        }
        Azure(int n) { 
                acnt = 3*n;
                cout << "a = " << n << endl;
        }
        Azure(const Azure &al) { 
                acnt = al.acnt;
                cout << "cloned " << acnt << endl;
        }
        ~Azure() { cout << "bye " << acnt << endl; }
        int get() { return acnt; }
};

void fred(Azure a)
{
        cout << "fred: " << a.get() << endl;
}
void barney(Azure &a)
{
        cout << "barney: " << a.get() << endl;
}
int main() 
{
        Azure x;
        Azure y(42);

        fred(x);
        barney(y);
}

<<Construction and Destruction I Answer Base Class Init, Virtual Functions>>