科目名稱:程式設計 | 開課系所:資訊工程 學系 | 考試日期 | 2014.6.3 | ||
系所別: |
年級: |
學號: |
姓名: |
考試時間 | 18:10-20:00 |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 |
(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).
// Overloading the Addition Operator (P.459)
#include <iostream>
using std::cout;
using std::endl;
class CData
{
public:
int* pdata;
CData(int v = 0)
{ pdata = new int(v); }
CData Add(CData d)
{ return CData(*pdata = *d.pdata); }
CData operator+(CData d)
{ return Add(d); }
void Print()
{ cout << *pdata << endl; }
};
int main()
{
CData a(5), b(3);
CData c;
c = a + b;
a.Print();
c.Print();
return 0;
}
(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).
// Searching Strings (P.523)
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
int o(string a, string b)
{
int c = 0, p = 0;
while ( a.find(b, p) <= a.length() )
{
p = a.find(b, p) + 1 ;
c++;
}
return c;
}
int main()
{
string a("NCCU");
string b("C");
cout << o(a, b) << endl;
cout << o(a, b+"U") << endl;
cout << o(a, "NCNU") << endl;
return 0;
}
// The Size of a Vector (P.655)
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main()
{
vector<int> a(10);
int b = 5;
int c = 27;
cout << a.size() << endl;
a.push_back(b);
a.push_back(c);
cout << a.size() << endl;
return 0;
}