// C11_6.cpp : Defines the entry point for the console application. // 指標、參考與物件的關係示範 #include "stdafx.h" #include #include // 宣告類別 ClassMate class ClassMate { char Name[30]; public: int PerID; // GetName() 函式 char *GetName() { return Name; } // SetName() 函式 void SetName(char *NewName) { strcpy(Name, NewName); } }; void main(int argc, char* argv[]) { ClassMate Person1, Person2; ClassMate *ptPerson = 0; Person1.PerID = 301; // 設定物件內容 Person1.SetName("方田宜"); Person2.PerID = 302; Person2.SetName("賓奇部"); ptPerson = &Person1; // 利用指標和參考 cout << "Person ID :" << ptPerson->PerID << " Person Name:" << ptPerson->GetName() << endl; ptPerson = &Person2; cout << "Person ID :" << ptPerson->PerID << " Person Name:" << ptPerson->GetName() << endl; #ifdef _DEBUG if (Person2.PerID == 302) { cout << "需特別處理!" << endl; } #endif // _DEBUG }