科目名稱:程式設計 | 開課系所:資訊工程學系 | 任課教師 |
吳坤熹 |
||
系所別: |
年級: |
學號: |
姓名: |
考試日期 |
2011.3.9 |
(考試時間: 14:10-14:20)
(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>
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;
}
};
int main()
{
CRational a;
CRational b(3);
CRational c(1,7);
a.Print(); b.Print(); c.Print();
return 0;
}