Date: June 22nd, 2012
Time: 08:30-09:30
Open book; turn off computer & mobile phone
// Copy Constructor
#include <iostream>
using std::cout;
using std::endl;
class CBox // Class definition at global scope
{
public:
// Constructor definition
explicit CBox(double lv, double bv = 2.0, double hv = 3.0)
{
m_Length = lv; // Set values of
m_Width = bv; // data members
m_Height = hv;
}
CBox(CBox& initB)
{
m_Length = initB.m_Length;
m_Width = initB.m_Width;
m_Height = initB.m_Height;
}
private: // What would happen if data members are private?
double m_Length; // Length of a box in inches
double m_Width; // Width of a box in inches
double m_Height; // Height of a box in inches
};
int main()
{
CBox box1(5.0);
CBox box2(box1);
CBox box3 = box2;
cout << box3.Volume() << endl;
return 0;
}
// Virtual Function
#include <iostream>
using std::cout;
using std::endl;
class Window // Base class
{
public:
void Create()
{ cout <<"Base class Window" << endl; }
};
class CommandButton : public Window
{
public:
virtual void Create() // "Try to Override a C++ function"
{ cout<<"Derived class Command Button" << endl; }
};
int main()
{
Window *x, *y;
x = new Window();
x->Create();
y = new CommandButton();
y->Create();
}
// Destructor
#include <iostream>
using std::cout;
using std::endl;
class CBox // Class definition at global scope
{
public:
// Constructor definition
explicit CBox(double lv, double bv = 2.0, double hv = 3.0)
{
m_Length = lv; // Set values of
m_Width = bv; // data members
m_Height = hv;
}
CBox(CBox& initB)
{
m_Length = initB.m_Length;
m_Width = initB.m_Width;
m_Height = initB.m_Height;
}
~CBox()
{
cout << "Destroy a " << m_Length << " x "
<< m_Width << " x " << m_Height << " box.\n";
}
double m_Length; // Length of a box in inches
double m_Width; // Width of a box in inches
double m_Height; // Height of a box in inches
};
int main()
{
CBox box1(5.0);
CBox box2(1.0, 2.0);
CBox box3 = box2;
box3.m_Length = 3.0;
return 0;
}