Date: April 8th, 2011
Time: 08:30-10:10
Open book; turn off computer & mobile phone
(10%) Determine whether the following code has syntax erros or not. If it is correct, predict its output. If it is incorrect, point out the
mistake(s).
#include <iostream> // Accessing Control in a Class
#include <cstring>
using std::cout;
using std::endl;
class CBox
{
CBox(int lv, int wv, int hv):
m_Length(lv), m_Width(wv), m_Height(hv){}
int Volume() const
{ return m_Length * m_Width * m_Height; }
int m_Length;
int m_Width;
int m_Height;
};
int main()
{
CBox myBox(4, 8, 3);
cout << myBox.Volume() << endl;
return 0;
}
#include <iostream> // static data member
#include <cstring>
using std::cout;
using std::endl;
class CMessage
{
private:
char* pmessage; // Pointer to object text string
public:
static int objectCount;
// Constructor definition
CMessage(const char* text = "Default message")
{
pmessage = new char[strlen(text) + 1]; // Allocate space for text
strcpy(pmessage, text); // Copy text to new memory
cout << ++objectCount << " : " << pmessage << endl;
}
~CMessage()
{ delete[] pmessage;
objectCount--;
}
};
int CMessage::objectCount = 0;
void f1()
{
CMessage msg1("Sunday");
CMessage msg2("Monday");
}
void f2()
{
CMessage msg2("Tuesday");
CMessage msg3("Wednesday");
}
int main()
{
f1(); f2();
return 0;
}
#include <iostream> // Assume the default pack size is 8 bytes in Visual C++
using std::cout;
using std::endl;
class Node
{
Node* left;
short value;
Node* right;
};
class C2
{
Node* left;
double value;
Node* right;
};
int main()
{
cout << "Node: " << sizeof(Node) << endl;
cout << "C2: " << sizeof(C2) << endl;
return 0;
}
#include <iostream>
using std::cout;
using std::endl;
int gcd(int a, int b)
{
if (b == 0) return a;
else return gcd(b, a % b);
}
class CRational
{
public:
int numerator; // 分子
int denominator; // 分母
CRational(int p=0, int q=1) : numerator(p), denominator(q)
{ if (q==0)
{ cout << "Error! The denominator cannot be 0.\n";
exit(1);
}
}
void Print()
{ Reduce();
cout << numerator;
if (denominator != 1)
cout << "/" << denominator;
cout << endl;
}
void Reduce()
{ int i = gcd(numerator, denominator);
numerator /= i;
denominator /=i;
}
};
int main()
{
CRational a(12,8);
CRational b(3,6);
a.Print(); b.Print();
return 0;
}
#include <iostream>
using std::cout;
using std::endl;
int gcd(int a, int b)
{
if (b == 0) return a;
else return gcd(b, a % b);
}
class CRational
{
public:
int numerator; // 分子
int denominator; // 分母
CRational(int p=0, int q=1) : numerator(p), denominator(q)
{ if (q==0)
{ cout << "Error! The denominator cannot be 0.\n";
exit(1);
}
}
void Print() const // This line is different. Does it matter?
{ Reduce();
cout << numerator;
if (denominator != 1)
cout << "/" << denominator;
cout << endl;
}
void Reduce()
{ int i = gcd(numerator, denominator);
numerator /= i;
denominator /=i;
}
};
int main()
{
CRational a(12,8);
CRational b(3,6);
a.Print(); b.Print();
return 0;
}
#include <iostream> // Inheritance
#include <cstring>
using std::cout;
using std::endl;
class CBox
{
public:
CBox(int lv = 2, int wv = 3, int hv = 5):
m_Length(lv), m_Width(wv), m_Height(hv){}
private:
int m_Length;
int m_Width;
int m_Height;
};
class CCandyBox: CBox
{
public:
char m_Contents[10];
CCandyBox(char* str = "Candy") // Constructor
{ strcpy(m_Contents, str); }
int Volume() const
{ return m_Length * m_Width * m_Height; }
};
int main()
{
CCandyBox myCandyBox;
cout << myCandyBox.Volume() << endl;
return 0;
}
#include <iostream> // Operator overloading
using std::cout;
using std::endl;
class CRational
{
public:
int numerator; // 分子
int denominator; // 分母
CRational(int p=0, int q=1)
: numerator(p), denominator(q)
{
if (q==0)
{
cout << "Error! The denominator cannot be 0.\n";
exit(1);
}
}
void Print()
{
cout << numerator;
if (denominator != 1)
cout << "/" << denominator;
cout << endl;
}
CRational& operator=(const CRational x)
{
if (this == &x)
return *this;
numerator = x.numerator;
denominator = x.denominator;
return *this;
}
};
int main()
{
CRational a;
CRational b(3);
CRational c(1,7);
(a = b) = c; a.Print();
a = b = c; a.Print();
return 0;
}
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
class CBox
{
public:
CBox(int lv = 2, int wv = 3, int hv = 5):
m_Length(lv), m_Width(wv), m_Height(hv){}
int Volume() const
{ return m_Length * m_Width * m_Height; }
int m_Length;
int m_Width;
int m_Height;
};
class CCandyBox: protected CBox
{
public:
char m_Contents[10];
CCandyBox(char* str = "Candy") // Constructor
{ strcpy(m_Contents, str); }
};
int main()
{
CCandyBox myCandyBox;
cout << myCandyBox.Volume() << endl;
return 0;
}