?? people.cpp
字號(hào):
//類CPeople的實(shí)現(xiàn)
#include "people.h"
#include <string >
#include <iostream>
using namespace std;
CPeople::CPeople() //缺省構(gòu)造函數(shù),給創(chuàng)建的對(duì)象的每個(gè)成員賦默認(rèn)值
{
m_nAge = 20;
m_fSalary = 3000.00f;
m_pstrName = new char[20]; //用new操作符為字符指針m_pstrName分配空間
strcpy( m_pstrName, "無名氏");
}
CPeople::CPeople(int age, float salary, char* name) //帶參數(shù)的構(gòu)造函數(shù)
{
m_nAge = age;
m_fSalary = salary;
m_pstrName = new char[20]; //用new操作符為字符指針m_pstrName分配空間
strcpy( m_pstrName, name);
}
//拷貝構(gòu)造函數(shù),用已存在的對(duì)象來創(chuàng)建新的對(duì)象
CPeople::CPeople( const CPeople& AnotherPeople)
{
m_nAge = AnotherPeople.m_nAge;
m_fSalary = AnotherPeople.m_fSalary;
m_pstrName = new char[ strlen(AnotherPeople.m_pstrName) + 1];
strcpy( m_pstrName, AnotherPeople.m_pstrName );
}
CPeople::~CPeople() //析構(gòu)函數(shù),用來釋放我們?cè)跇?gòu)造函數(shù)中動(dòng)態(tài)申請(qǐng)的內(nèi)存空間
{
if ( m_pstrName != NULL )
delete m_pstrName; //釋放m_pstrName所占用的內(nèi)存空間
}
char *CPeople::GetName()
{
return m_pstrName;
}
int CPeople::GetAge()
{
return m_nAge;
}
void CPeople::SetAge( int age )
{
if(age<=0)
cout<< "設(shè)置的年齡不合法"<<endl;
else
m_nAge = age;
}
float CPeople::GetSalary()
{
return m_fSalary;
}
void CPeople::SetSalary( float num )
{
if(num<0)
cout<<"設(shè)置的薪水?dāng)?shù)目不合法"<<endl;
else
m_fSalary = num;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -