讓我們復(fù)習(xí)一下person類:
class person
{
private:
char birthday[20];
char sex[10];
char name[20];
public:
person() { }
person(person & p)
{
birthday = p.birthday;
strcpy(sex,p.sex);
}
person(char b[], char s[])
{
birthday = b;
sex = s;
}
void set_name(char n[])
{
strcpy(name,n);//strcpy,沒忘吧?復(fù)制char類型要用這個(gè),不要寫成"name = n;"!
}
char* get_name()
{
return *name;
}
void speak()
{
cout 《 "我在牙牙學(xué)語!" 《 endl;
}
void speak(char a[])
{
cout 《 "我在說:" 《 a 《 endl;
}
};
這個(gè)類很別扭:
char birthday[20];
生日是時(shí)間,不是字符,
我們可以寫個(gè)時(shí)間類:
class time
{
int year,month,day;
time(int y,int m,int d)
{
year = y;
month = m;
day = d;
}
}
相關(guān)推薦:
C++備考之如何學(xué)習(xí)C++與面向?qū)ο螅侯惻c對(duì)象 等考C++備考之如何學(xué)習(xí)C++與面向?qū)ο螅褐剌d C++備考之如何學(xué)習(xí)C++與面向?qū)ο螅簶?gòu)造函數(shù)與&