科目名稱:程式設計 | 開課系所:資訊工程 學系 | 任課教師 |
吳坤熹 |
||
系所別: |
年級: |
學號: |
姓名: |
考試日期 |
2009.4.22 |
(考試時間: 14:10-14:30)
#include <iostream>
using std::cout;
class CCup
{
public:
int water;
CCup(int volume): water(volume) {}; // Constructor
CCup& operator++ ()
{
++water;
return *this;
}
void Show()
{
cout << water << "\n";
}
};
int main()
{
CCup juice(3);
juice.Show();
++juice;
juice.Show();
}
(10%) Determine whether the following code has syntax errors or not. If it is correct, predict its output. If it is
incorrect, point out the mistake(s).
// Operator Overloading
#include <iostream>
using std::cout;
using std::endl;
class CComplex
{
public:
double real;
double imaginary;
CComplex(double a = 0.0, double b = 0):
real(a), imaginary(b)
{ }
void Show()
{
cout << real;
if (imaginary<0.0)
cout << imaginary << "i";
else if
(imaginary > 0.0)
cout << "+" << imaginary << "i";
cout << "\n";
}
};
int main()
{
CComplex c1(2,3);
CComplex c2(4,0);
CComplex c3 = c1 + c2;
c1.Show();
c2.Show(); // "cout << 3.0" will display "3".
c3.Show();
}