Date: May 18, 2012
Time: 08:30-10:30
Open book; turn off computer & mobile phone
// Inheritance
#include <iostream>
class CBox
{
public:
int m_Length;
int m_Width;
int m_Height;
CBox(int lv = 1, int wv = 2, int hv = 3)
: m_Length(lv), m_Width(wv), m_Height(hv) {}
int Volumn() { return m_Length * m_Width * m_Height; }
};
class CCandyBox: public CBox
{
public:
char m_Contents[10];
CCandyBox(const char *str = "Candy")
{
strcpy(m_Contents, str);
}
CCandyBox(int lv, int wv, int hv, char *str)
: CBox(lv, wv, hv)
{
strcpy(m_Contents, str);
}
};
int main()
{
CCandyBox myCandyBox(2, 3, 4, "Chocolate");
CCandyBox myBox;
std::cout << sizeof(myCandyBox) << std::endl;
std::cout << myBox.Volumn() << std::endl;
return 0;
}
// Size of a binary tree
#include <iostream>
using std::cout;
using std::endl;
class CBinaryTree
{
public:
int value;
CBinaryTree* left;
CBinaryTree* right;
CBinaryTree(int n, CBinaryTree* p1 = NULL,
CBinaryTree* p2 = NULL):
value(n), left(p1), right(p2) {}
};
int main()
{
CBinaryTree G(0);
CBinaryTree F(6);
CBinaryTree E(9);
CBinaryTree D(0);
CBinaryTree C(1, &F, &G);
CBinaryTree B(9, &D, &E);
CBinaryTree A(2, &B, &C);
cout << sizeof(A) << endl;
return 0;
}
// Recursively output a binary tree
#include <iostream>
using std::cout;
using std::endl;
class CBinaryTree
{
public:
int value;
CBinaryTree* left;
CBinaryTree* right;
CBinaryTree(int n, CBinaryTree* p1 = NULL,
CBinaryTree* p2 = NULL):
value(n), left(p1), right(p2) {}
void Print()
{
if (left) left->Print();
cout << value;
if (right) right->Print();
}
};
int main()
{
CBinaryTree G(0);
CBinaryTree F(6);
CBinaryTree E(9);
CBinaryTree D(0);
CBinaryTree C(1, &F, &G);
CBinaryTree B(9, &D, &E);
CBinaryTree A(2, &B, &C);
C.Print();
return 0;
}
// Friend function
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
class CBinaryTree
{
friend ostream& operator<<(ostream& os, CBinaryTree bt)
{
bt.Print();
return os;
}
public:
int value;
CBinaryTree* left;
CBinaryTree* right;
CBinaryTree(int n, CBinaryTree* p1 = NULL,
CBinaryTree* p2 = NULL):
value(n), left(p1), right(p2) {}
void Print()
{
cout << value;
if (left) left->Print();
if (right) right->Print();
}
};
int main()
{
CBinaryTree G(0);
CBinaryTree F(6);
CBinaryTree E(9);
CBinaryTree D(0);
CBinaryTree C(1, &F, &G);
CBinaryTree B(9, &D, &E);
CBinaryTree A(2, &B, &C);
cout << A << endl;;
return 0;
}
// Overloading the increment operator
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
using std::ostream;
class Word
{
private:
public:
string word;
Word(): word("A") {} ;
Word& operator++()
{
word.insert(0, "B");
return *this;
}
const Word operator++(int)
{
Word w = *this;
word.append("C");
return w;
}
void Print()
{ cout << word << endl; }
friend ostream& operator<<(ostream& os, Word a) // P.569
{ os << a.word;
return os;
}
};
int main()
{
Word a;
++a;
a++;
cout << ++a;
cout << a++;
cout << endl << a;
return 0;
}
// Size of an object with static members
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
class MyView
{
public:
char type[12];
MyView* nextView;
static MyView* firstView;
static MyView* lastView;
MyView(const char* str)
{
strcpy(type, str);
if (firstView == NULL)
firstView = lastView = this;
else
{
lastView->nextView = this;
lastView = this;
}
nextView = NULL;
}
};
MyView* MyView::firstView = NULL;
MyView* MyView::lastView = NULL;
int main()
{
MyView view1("Bar Chart");
MyView view2("Line Chart");
MyView* p;
for (p=view1.firstView; p; p = p->nextView)
cout << p->type << endl;
cout << sizeof(MyView) << endl;
return 0;
}
// Virtual function
#include <iostream>
using std::cout;
using std::endl;
class Window // Base class
{
public:
virtual void Create() // virtual function
{ cout <<"Base class Window"<< endl; }
};
class CommandButton : public Window
{
public:
void Create()
{ cout<<"Derived class Command Button"<< endl; }
};
int main()
{
Window *x, *y;
x = new Window;
x->Create();
y = new CommandButton;
y->Create(); // y is a pointer to the base class type
return 0;
}
// Pointer to the base class#include <iostream>
using std::cout;
using std::endl;
class Window // Base class
{
public:
void Create() // This is not declared virtual
{ cout <<"Base class Window"<< endl; }
};
class CommandButton : public Window
{
public:
void Create()
{ cout<<"Derived class Command Button"<< endl; }
};
int main()
{
Window *x, *y;
x = new Window;
x->Create();
y = new CommandButton;
y->Create(); // y is a pointer to the base class type
return 0;
}
// typedef
#include <iostream>
#include <vector>
#include <list>
using std::cout;
using std::endl;
using std::vector;
using std::list;
typedef list<int>::iterator IT;
class CMyList: public list<int>
{
public:
CMyList(IT i1, IT i2)
{
for (IT iter = i1; iter != i2; iter++)
push_back(*iter);
}
void Print()
{
for (IT iter = begin(); iter != end(); iter++)
cout << *iter << ' ';
cout << endl;
}
};
int main()
{
int phone[] = { 0, 4, 9, 2, 9, 1, 0, 9, 6, 0 };
vector<int> number(phone, phone+sizeof(phone)/sizeof(phone[0]) );
list<int> data(number.begin() + 4, number.end());
CMyList data2(data.begin() + 2, data.end());
data2.Print();
return 0;
}
// Vector Capacity
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
int main()
{
vector<int> a;
a.reserve(6);
for (int i=3; i<18; i++)
a.push_back(5);
cout << a.size() << '\t'
<< a.capacity() << '\n';
return 0;
}