标签:
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 class Person 5 { 6 static const unsigned LIMIT = 25;//忘了加unsigned 7 string lname; 8 char fname[LIMIT]; 9 public: 10 11 Person() 12 { 13 lname = ""; 14 fname[0] = ‘\0‘; 15 }; 16 Person(const string ln,const char *fn = "Heyyou"); 17 void Show() const 18 { 19 cout << fname << ‘ ‘ << lname << endl; 20 }; 21 void Formalshow() const 22 { 23 cout << lname << ", " << fname << endl; 24 }; 25 /* Person(const string ln, const char *fn) 26 { 27 lname = ln; 28 strcpy(fname,fn); 29 } 30 */ 31 //不能使用上面这一段,会发生重载,把它提出来函数头加上classname::就好了 32 }; 33 Person::Person(const string ln, const char *fn) 34 { 35 lname = ln; 36 strcpy(fname,fn); 37 } 38 39 int main() 40 { 41 Person one; 42 Person two("smythecraft"); 43 Person three("Dimwiddy","Sam"); 44 one.Show(); 45 one.Formalshow(); 46 cout<<endl; 47 48 two.Show(); 49 two.Formalshow(); 50 cout<<endl; 51 52 three.Show(); 53 three.Formalshow(); 54 cout<<endl; 55 }
one two three 分别会选择不同的构造函数。
标签:
原文地址:http://www.cnblogs.com/fudianheg/p/4234644.html