第2章第5题
#include "2_5CInteger.h"
void main(void)
{
CInteger c(5),d(10),sum;
cout<<"c的value"<<c.GetValue()<<endl;//主意函数有括号
cout<<"c的value"<<d.GetValue()<<endl;
sum.AddValue(c,d);
cout<<"总和:"<<sum.GetValue()<<endl;
}
#ifndef _2_5CINTEGER_H_
#define _2_5CINTEGER_H_
#include <iostream>
using namespace std;
class CInteger
{
public:
CInteger(int value=0);
int GetValue(void)
{
return m_iValue;
}
CInteger AddValue(CInteger a,CInteger b);
private:
int m_iValue;
};
#endif
#include "2_5CInteger.h"
CInteger CInteger::AddValue(CInteger a,CInteger b)
{
this->m_iValue=a.m_iValue+b.m_iValue;
return (*this);
}
CInteger::CInteger(int value)
{
m_iValue=value;
}
第二章第7题
#include "2_7CScore.h"
void main(void)
{
CScore a(90),b(95),c(89),d(80);
cout<<"总成绩:"<<CScore::GetTotalScore()<<endl;
}
#ifndef _2_7CSCORE_H_
#define _2_7CSCORE_H_
#include <iostream>
using namespace std;
class CScore
{
public:
CScore(int score=0)
{
m_iscore=score;
totalScore+=score;
}
static int GetTotalScore(void)
{
return totalScore;
}
private:
int m_iscore;
public:
static int totalScore;
};
int CScore::totalScore=0;
#endif
第二章第8题
#include "2_8CTime.h"
void main()
{
CTime a,b(11,59,59);
a.Print();
b.Print();
}
#ifndef _2_8CTIME_H_
#define _2_8CTIME_H_
#include <iostream>
using namespace std;
class CTime
{
public:
CTime(int hour,int minute,int second)
{
m_iHour=hour;
m_iMinute=minute;
m_iSecond=second;
}
CTime(void)
{
m_iHour=0;
m_iMinute=0;
m_iSecond=0;
}
~CTime(){}
void Print(void)
{
cout<<m_iHour<<":"<<m_iMinute<<":"<<m_iSecond<<endl;
}
private:
int m_iHour;
int m_iMinute;
int m_iSecond;
};
#endif
习题2第4题
#include "2_4.h"
void main(void)
{
CTree one(5),two;
one.grow(5);
two.grow(6);
cout<<"one的年龄:"<<one.GetAge()<<endl;
cout<<"two的年龄:"<<two.GetAge()<<endl;
}
#ifndef _2_4_H_
#define _2_4_H_
#include <iostream>
using namespace std;
class CTree
{
public:
CTree(int iage=0)
{
m_iAge=iage;
}
void grow(int iYear)
{
m_iAge+=iYear;
}
int GetAge(void)
{
return m_iAge;
}
private:
int m_iAge;
};
#endif