科目名稱:資訊系統 與網路導論 | 開課系所:資訊工程 學系 | 任課教師 |
吳坤熹 |
||
系所別: |
年級: |
學號: |
姓名: |
考試日期 |
2012.3.28 |
// Copy Constructor
#include <iostream>
using std::cout;
class CRational
{
int numerator;
int *denominator;
public:
CRational(int n, int d = 1): numerator(n)
{ denominator = new int(d); }
void Print()
{ cout << numerator << '/' << *denominator; }
};
int main()
{
CRational a(1,2);
CRational b(a);
a = CRational(1,3);
b.Print();
return 0;
}
// Operator Overloading
#include <iostream>
using std::cout;
class CRational
{
int numerator;
int denominator;
public:
CRational(int n=0, int d=1): numerator(n), denominator(d) {}
void operator=(CRational a) // What should be the return type?
{ numerator = a.numerator;
denominator = a.denominator;
}
void Print()
{ cout << numerator << '/' << denominator; }
};
int main()
{
CRational a(1,2);
CRational b;
b = a;
b.Print();
return 0;
}