Date: June 11th, 2013
Time: 14:30-16:30
Open book; turn off computer & mobile phone
// Defining a class
#include<iostream>
using std::cout;
class CRational
{
public:
// Constructor
CRational(int p = 0, int q = 1): numerator(p), denominator(q) { }
int numerator;
int denominator;
void Print()
{ cout << numerator << '/' << denominator; }
};
int main()
{
CRational A = {1, 3};
A.Print();
return 0;
}
// Friend function
#include<iostream>
using std::cout;
class CRational
{
public:
// Constructor
CRational(int p = 0, int q = 1): numerator(p), denominator(q) { }
protected:
int numerator;
int denominator;
void Print()
{ cout << numerator << '/' << denominator; }
friend int main();
};
int main()
{
CRational A(3);
A.Print();
return 0;
}
// Copy Constructor
#include<iostream>
using std::cout;
class CRational
{
public:
int numerator;
int denominator;
// Constructor
CRational(int p = 0, int q = 1): numerator(p), denominator(q)
{ cout << "Constructor called.\n"; }
// Copy Constructor
CRational(CRational &a): numerator(1), denominator(1)
{ cout << "Copy constructor called.\n"; }
void Print()
{ cout << numerator << '/' << denominator << '\n'; }
};
int main()
{
CRational A;
A.Print();
CRational B = A;
B.Print();
return 0;
}
// Operator Overloading
#include<iostream>
using std::cout;
class CRational
{
public:
int numerator;
int denominator;
// Constructor
CRational(int p = 0, int q = 1): numerator(p), denominator(q)
{ }
// Copy Constructor
CRational(CRational &a): numerator(1), denominator(1)
{ }
CRational& operator=(CRational c)
{
// Note that passing-by-value will copy an CRational object to c,
// which will invoke the copy constructor. See pp.405-406
numerator = c.numerator;
denominator = c.denominator;
return *this;
}
void Print()
{ cout << numerator << '/' << denominator << '\n'; }
};
int main()
{
CRational A(1, 3);
CRational B;
B.Print();
B = A;
B.Print();
return 0;
}
// String Objects
#include <iostream>
#include <string>
using std::cout;
using std::string;
int main()
{
string title = "China's super-secret space city";
size_t offset = 0;
for (int i=0; i<3; i++)
offset = title.find('s', offset + 1);
title.replace(offset,
title.find(' ', offset) - offset,
"beautiful");
cout << title << '\n';
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();
}
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*2/3, k, k/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 = x1 - x2;
pDC->Rectangle(x1, y1, x2, y2 + length);
if (n>1)
{
box(pDC, x1, y1+length/3, x1, y1+2*length/3, n-1);
box(pDC, x1 - length/3, y2 + length, x1 - length*2/3, y2 + length, n-1);
box(pDC, x2, y1+length*2/3, x2, y1+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, x1-length*2/3, y2, x1-length/3, y2, n-1);
}
}
}