資源簡介
C++實戰源碼-構造方法的應用(入門級實例207).zip
代碼片段和文件信息
//?Person.cpp?:?Defines?the?entry?point?for?the?console?application.
//
#include?“stdafx.h“
#include?“iostream.h“
#include?“string.h“
class?Person?{
private:
char?m_name[10]; //定義姓名
????char?m_gender[4]; //定義性別
????int?m_age; //定義年齡
public:
Person()?{//定義沒有參數的構造方法
m_name[0]?=?‘\0‘;
m_gender[0]?=?‘\0‘;
m_age?=?0;
????????cout?<“使用無參構造方法創建對象“?<????}
????Person(char?*name?char?*gender?int?age)?{//利用構造方法初始化域
strcpy(m_namename);
strcpy(m_gendergender);
m_age?=?age;
????????cout?<“使用有參構造方法創建對象“?<????}
????char?*?getName()?{ //獲得姓名
????????return?m_name;
????}
????char?*?getGender()?{ //獲得性別
????????return?m_gender;
????}
????int?getAge()?{ //獲得年齡
????????return?m_age;
????}
};
int?main(int?argc?char*?argv[])
{
Person?*person1?=?new?Person();//創建對象
Person?*person2?=?new?Person(“明日科技“?“男“?11);//創建對象
cout?<“員工1的信息“?< cout?<“員工姓名:“?<getName()?< cout?<“員工性別:“?<
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????1458??2010-10-13?19:13??Person\Person.cpp
?????文件????????4536??2010-10-13?18:52??Person\Person.dsp
?????文件?????????537??2010-10-13?18:52??Person\Person.dsw
?????文件?????????293??2010-10-13?18:52??Person\StdAfx.cpp
?????文件?????????769??2010-10-13?18:52??Person\StdAfx.h
- 上一篇:C++實戰源碼-有序數組折半查找
- 下一篇:C++實戰源碼-上帝創世的秘密
評論
共有 條評論