ccbsd3 :cpp % cat -n mannew.h 1 //mannew.h -- by tsaiwn@csie.nctu.edu.tw 2 //設計兩個 class: Animal 和 從 Animal 擴充來的 mankind 3 //Interface file for "mankind" and "Animal" classes 4 class Animal { 5 int pv1; 6 float pv2; 7 protected: 8 int pt1[5]; 9 public: 10 Animal(int = 38); // default parameter, 若沒寫則compiler自動填38 11 float pb1; 12 int pb2[9]; 13 void talk(void); 14 }; 15 /// 16 class mankind:public Animal{ // mankind 最好第一字要大寫! 17 char * pv3; 18 public: 19 mankind(char * ="NoName"); // Constructor 20 ~mankind( ); // Destructor 21 int pb3[8]; 22 void talk(void); 23 }; ccbsd3 :cpp % cat -n manlib.cpp 1 //manlib.cpp -- Implementation file for "Animal" and "mankind" 2 // 實作 Animal 和 mankind 這兩個 class 3 // -- by tsaiwn@csie.nctu.edu.tw 4 #include 5 #include 6 using namespace std; 7 #include "mannew.h" 8 Animal::Animal(int x){ 9 pv1=x; 10 pv2=45.67; 11 this->pb1=135.246; 12 for (int i=0; i<9; i++) pb2[i]=i+1; 13 //pb2={ 1,2,3,4,5,6,7 ,8,9}; 14 cout<<" Animal shows up\n"; 15 }; 16 void Animal::talk(void) 17 { 18 cout << " Animal talk, pv1=" << dec <pv3 << " is dying \n"; 29 } 30 void mankind::talk(void) 31 { 32 // cout << " mankind talk, pv1=" << pv1 <<"\n"; 33 // cout << " mankind talk, pb2=" << pb2 <<"\n"; 34 // cout << " mankind talk, pb2=" << Animal::pb2 <<"\n"; 35 cout << " mankind talk, pb2[0]=" << pb2[0] <<"\n"; 36 cout << " mankind talk, pv3=" << pv3 <<"\n"; 37 cout << " mankind talk, pb3[3]=" << pb3[3] <<"\n"; 38 }; ccbsd3 :cpp % g++ -c manlib.cpp ccbsd3 :cpp % ar r libanim.a manlib.o ar: creating libanim.a ( -c 是說 compile only, 此會生出 manlib.o) (如果你願意, 可將之做成 library, 則 manlib.cpp 就不用給人家 見下一頁最後; 給 manlib.o 也可以不用給 manlib.cpp; 但 .h 檔要給) ccbsd3 :cpp % cat -n mannew.cpp 1 //mannew.cpp --test Animal, mankind; by tsaiwn@csie.nctu.edu.tw 2 // g++ mannew.cpp manlib.cpp; ./a.out 3 #include 4 #include 5 using namespace std; 6 #define call 7 #include "mannew.h" 8 /// 9 Animal aa1(123),aa2(456); 10 mankind * mm1, *mm2; 11 void onlyasub(void) 12 { mankind nobody; 13 cout << " Now in routine onlyasub\n"; 14 } 15 main(){ 16 aa1.talk(); 17 cout << "Welcome to C++\n"; 18 call onlyasub(); 19 mm1= new mankind("Chang-3"); 20 mm2= new mankind("Lee-4"); 21 cout << "mm1->pb1=" << mm1->pb1 << "\n"; 22 cout << " (Let mm1 talk)\n"; 23 mm1->talk(); 24 delete mm2; 25 cout << " (and then Let mm1 talk by Animal method)\n"; 26 mm1->Animal::talk(); 27 return(0); 28 } ccbsd3 :cpp % g++ mannew.cpp manlib.cpp ccbsd3 :cpp % g++ mannew.cpp manlib.o ccbsd3 :cpp % ./a.out | cat -n 1 Animal shows up 2 Animal shows up 3 Animal talk, pv1=123=0x7b 4 pv2=45.67 5 pb1=135.246 6 pb2[6]=7 7 Welcome to C++ 8 Animal shows up 9 mankind NoName appears 10 Now in routine onlyasub 11 %%% mankind NoName is dying 12 Animal shows up 13 mankind Chang-3 appears 14 Animal shows up 15 mankind Lee-4 appears 16 mm1->pb1=135.246 17 (Let mm1 talk) 18 mankind talk, pb2[0]=1 19 mankind talk, pv3=Chang-3 20 mankind talk, pb3[3]=0 21 %%% mankind Lee-4 is dying 22 (and then Let mm1 talk by Animal method) 23 Animal talk, pv1=38=0x26 24 pv2=45.67 25 pb1=135.246 26 pb2[6]=7 ccbsd3 :cpp % exit 如何在Unix上造library? very simple, 例如: g++ -c manlib.cpp (翻譯出 manlib.o) ar r libanim.a manlib.o (archive 造出 Library libanim.a ) ranlib libanim.a (使它 link 時較快) 阿這樣用: g++ mannew.C -L. -lanim ( -L. 表示要在 current working directory 找 library, -lanim 表示要找的 library 名為 libanim.a ) (For more information, see "man ar" and "man ranlib")