科目名稱:資訊系統 與網路導論 | 開課系所:資訊工程 學系 | 任課教師 |
吳坤熹 |
||
系所別: |
年級: |
學號: |
姓名: |
考試日期 |
2012.5.11 |
// Size of an object with static members
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
class MyView
{
public:
char type[12];
MyView* nextView;
static MyView* firstView;
static MyView* lastView;
MyView(const char* str)
{ cout << str << " view is created.\n";
strcpy(type, str);
if (firstView == NULL)
firstView = lastView = this;
else
{
lastView->nextView = this;
lastView = this;
}
nextView = NULL;
}
};
MyView* MyView::firstView = NULL;
MyView* MyView::lastView = NULL;
int main()
{
MyView view1("Bar Chart");
MyView view2("Line Chart");
MyView* p;
// for (p=view1.firstView; p; p = p->nextView)
// cout << p->type << endl;
cout << sizeof(MyView) << endl;
return 0;
}
// Overloading the increment operator
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
using std::ostream;
class Word
{
private:
public:
string word;
Word(): word("A") {} ;
Word& operator++()
{
word.insert(0, "B");
return *this;
}
const Word operator++(int)
{
Word w = *this;
word.append("C");
return w;
}
void Print()
{ cout << word << endl; }
friend ostream& operator<<(ostream& os, Word a) // P.569
{ os << a.word;
return os;
}
};
int main()
{
Word a;
++a; a.Print();
a++; a.Print();
return 0;
}