c++ 基本题

来源:百度知道 编辑:UC知道 时间:2024/06/04 04:19:55
实现并测试一个Time类。Time类的每个对象都表示一个特定的时间,即Time类中包含3个整型数据分量分别存储时(hour)、分(minute)和 秒(second)。Time类中要包括构造函数,存取各个分量和完整时间的操作,成员函数advance(int h,int m,int s)用于调整时间,成员函数reset(int h,int m,int s)用于重置时间,print()函数输出时间。

2. 定义一个整数集合类IntSet,包括以下成员函数:

IntSet():类构造函数,根据需要可定义多个构造函数

Empty():清空该整数集合;

IsEmpty():判断整数集合是否为空;

IsMemberOf():判断某个整数是否在该整数集合内;

Add():增加一个整数到整数集合中

Sub():从整数集合中删除一个整数元素

IsEqual():判断两个集合是否相等

Intersection():求两个整数集合的交集

Merge():求两个整数集合的并集

Print():依次打印该整数集合

注意:整数集合中不允许有相同元素存在。

3. 编写一个程序,其中有一个简单的串类String,包含设置字符串、返回字符串长度及内容等功能。另有一个具有编辑功能的串类EditString,它的基类是String,在其中设置一个光标,使其能支持在光标处的插入、替换和删除等编辑功能。

4. 定义一个三维点类,重载下列运算符:

赋值运算符=;

比较运算符==和!=;

乘法运算符*,实现两个点的点积;

前缀与后缀自增运算符++;

插入运算符<<与提取运算符>>;

5. 某人喜欢饲养宠物。假定他拥有的放置宠物的窝的数目是固定的。请设计一个程序,使得某人可以饲养任意种类任意数目的宠物。要求能够知道现在

第一个:差不多的
#include <iostream.h>

class Clock
{
public:
Clock(int = 0, int = 0, int =0);
Clock operator+(int);
Clock operator+=(int);
Clock operator++(); // prefix ++
Clock operator++(int); // postfix ++

private:
int iHour;
int iMinute;
int iSecond;

friend ostream& operator<<(ostream&, const Clock&);
};

Clock::Clock(int h, int m, int s) : iHour(h), iMinute(m), iSecond(s)
{
}

Clock Clock::operator+(int v)
{
int s = iSecond + v;
int m = iMinute + s / 60;
s %= 60;
int h = iHour + m / 60;
m %= 60;
h %= 24;

return Clock(h, m , s);
}

Clock Clock::operator+=(int v)
{
*this = *this + v; // *this = (*this).operator+(v)
return *this;
}

Clock Clock::operator++()
{
*this += 1;
return *this;
}

Clock Clock::operator++(in