第二章的作业

本文解析了C++编程中的几个经典习题,包括类的定义与使用、静态成员变量的应用、时间类的构造及输出等,通过实例帮助读者更好地理解和掌握C++面向对象编程的基本概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值