Date: June 22nd, 2011
Time: 14:10-15:30
Open book; turn off computer & mobile phone
// Accessing the members of a class
#include <iostream>
using std::cout;
using std::endl;
class CComplex
{
double real;
double imaginary;
CComplex(double a = 0.0, double b = 0):
real(a), imaginary(b)
{ }
CComplex operator+(CComplex w)
{ return CComplex(real - w.real, imaginary - w.imaginary); }
void Show()
{
if (real != 0.0) cout << real;
if (imaginary < 0.0)
cout << imaginary << "i";
else if
(imaginary > 0.0)
cout << "+" << imaginary << "i";
cout << "\n";
}
};
int main()
{
CComplex c1(6,2);
CComplex c2(2,1);
CComplex c3 = c2 + c1;
c3.Show(); // "cout << 3.0" will display "3".
}
// Inheritance
#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();
}
#include <iostream> // Overloading the Increment Operator
using std::cout;
using std::endl;
class CChar
{
private:
char ch;
public:
CChar(char c): ch(c) {}
void Show() { cout << ch << endl; }
CChar& operator++() { ch++; return *this; } // Prefix increment operator
const CChar CChar::operator++(int) // Postfix increment operator
{ CChar c = *this;
ch--;
return c;
}
};
int main()
{
CChar choice('D');
choice++;
choice.Show();
return 0;
}
void CSketcherView::OnDraw(CDC* pDC)
{
CSketcherDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
pDC->Arc(20,20, 180,180, 180,100, 100,60);
pDC->MoveTo(180,100);
pDC->LineTo(100,100);
pDC->LineTo(100,180);
}
void CSketcherView::OnDraw(CDC* pDC)
{
CTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
const int j = 0;
const int k = 400;
const int level = 5;
square(pDC, CPoint(j, j), CPoint(j, k), CPoint(k, k), CPoint(k, j), level);
}
Assume that in the SketcherView class, we defined two public member functions:
CPoint middle(CPoint p1, CPoint p2)
{
return CPoint( (p1.x+p2.x)/2, (p1.y+p2.y)/2 );
}
void square(CDC* pDC, CPoint p1, CPoint p2, CPoint p3, CPoint p4, int n)
{
pDC->MoveTo(p1);
pDC->LineTo(p2);
pDC->LineTo(p3);
pDC->LineTo(p4);
pDC->LineTo(p1);
if (n>1)
square(pDC, middle(p1, p2), middle(p2, p3), middle(p3, p4), middle(p4, p1), n-1);
}
void CSketcherView::OnDraw(CDC* pDC)
{
CTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
const int j = 0;
const int k = 300;
const int level = 3;
box(pDC, k/3, k, k*2/3, k, level);
}
Assume that in the SketcherView class, we defined a public member function:
void box(CDC* pDC, int x1, int y1, int x2, int y2, int n)
{
int length;
if (y1 == y2)
{
length = x2 - x1;
pDC->Rectangle(x1, y1 - length, x2, y2);
if (n>1)
{
box(pDC, x1, y1-length/3, x1, y1-length*2/3, n-1);
box(pDC, x1 + length/3, y1 - length, x1 + length*2/3, y1 - length, n-1);
box(pDC, x2, y2-length*2/3, x2, y2-length/3, n-1);
}
}
else // (x1 == x2)
{
length = y1 - y2;
pDC->Rectangle(x1, y1, x2 - length, y2);
if (n>1)
{
box(pDC, x1-length/3, y1, x1-length*2/3, y1, n-1);
box(pDC, x1-length, y1-length/3, x1-length, y1-length*2/3, n-1);
box(pDC, x2-length*2/3, y2, x2-length/3, y2, n-1);
}
}
}