- (10%) Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Size of an Object
// Assuming the program is running on a 64-bit computer
#include <iostream>
using std::cout;
using std::endl;
class CText
{
public:
char* ptext;
CText(const char* s)
{
ptext = new char[strlen(s)+1];
strcpy(ptext, s);
}
};
int main()
{
CText a("NCNU"), b("MIT");
cout << sizeof(a) << strlen(a.ptext) << endl;
cout << sizeof(b) << strlen(b.ptext) << endl;
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Class Destructors (P.435)
#include <iostream>
using std::cout;
using std::endl;
class CText
{
public:
char* ptext;
CText(const char* s)
{
ptext = new char[strlen(s)+1];
strcpy(ptext, s);
}
~CText()
{
cout << "Destructor called to release the memory of "
<< ptext << endl;
delete ptext;
}
};
int main()
{
CText a("Apple"), b("Banana");
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Implementing a Copy Constructor (P.442)
#include <iostream>
using std::cout;
using std::endl;
class CText
{
public:
char* ptext;
CText(char* s)
{
ptext = new char[strlen(s)+1];
strcpy(ptext, s);
}
~CText()
{
if (ptext)
cout << "Destructor called for "
<< ptext << endl;
else
cout
<< "Destructor called, but no memory needs to be releases."
<< endl;
}
};
int main()
{
CText b("Banana");
CText a(b);
strcpy(a.ptext, "Apple");
return 0;
}
- (10%) 江蕙的「落雨聲」歌詞中,哪一段表達了「樹欲靜而風不止,子欲養而親不在」的感慨?