Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <string.h>
- #include <iostream>
- using namespace std;
- /* run this program using the console pauser or add your own getch, system("pause") or input loop */
- // 定義視窗類別CWin
- class CWin {
- private:
- char id, *title;
- public:
- CWin(char i = 'D', char *text = (char *)"Default window") :id(i) {
- title = new char[50];//配置可容納50個字元的記憶空間
- strcpy(title, text);//將text所指向的字串拷貝給title
- }
- void set_data(char i, char *text) {
- id = i;
- strcpy(title, text);//將text所指向的字串拷貝給title
- }
- void show(void) {
- cout << "Window " << id << ": " << title << endl;
- }
- void show_address(void) {
- cout << "Window " << id << ": " << &id << endl;
- }
- //解構元
- ~CWin() {
- delete[] title;
- title = NULL;
- }
- };
- int main(int argc, char** argv) {
- system("color f0");
- CWin win1('A', (char *)"Main window");
- CWin win2;
- cout << "win1.show() = ";
- win1.show();
- cout << "win2.show() = ";
- win2.show();
- cout << "win1.show_address() = ";
- win1.show_address();
- cout << "win2.show_address() = ";
- win2.show_address();
- win1 = win2; // 設定win1=win2
- cout << endl << "設定 win1 = win2 之後..." << endl;
- cout << "win1.show() = ";
- win1.show();
- cout << "win2.show() = ";
- win2.show();
- cout << "win1.show_address() = ";
- win1.show_address();
- cout << "win2.show_address() = ";
- win2.show_address();
- win1.set_data('B', (char *)"Hello window");
- cout << endl << "更改 win1 的資料成員之後..." << endl;
- cout << "win1.show() = ";
- win1.show();
- cout << "win2.show() = ";
- win2.show();
- cout << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment