急求一道 c++编程题 分多 速来 急急急急

2024-11-27 02:34:01
推荐回答(2个)
回答1:

更新了上一个例子,希望能帮助你,如果有问题,可以继续追问我。

// CppTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include

using namespace std;

class CTime
{
private:
int hours, minutes, seconds;

public:

CTime(): hours(0), minutes(0), seconds(0)
{

}

void Set(int h,int m,int s)
{
this->hours = h;
this->minutes = m;
this->seconds = s;
}

void Print()
{
cout< }

friend int operator -(const CTime & t1, const CTime & t2 )
{
int s1 = (t1.hours - t2.hours) *3600 + (t1.minutes-t2.minutes)*60 + t1.seconds - t2.seconds;

return s1;
}
};

void main()
{
CTime t1;
t1.Set(1, 2, 3);
t1.Print();

CTime t2;
t2.Set(2, 2, 3);
t2.Print();

cout<}

回答2:

#include "stdafx.h"
#include
#include

using namespace std;

class CTime
{
private:
int hours, minutes, seconds;

public:

CTime(): hours(0), minutes(0), seconds(0)
{

}

void Set(int h,int m,int s)
{
this->hours = h;
this->minutes = m;
this->seconds = s;
}

void Print()
{
cout< }

friend int operator -(const CTime & t1, const CTime & t2 )
{
int s1 = (t1.hours - t2.hours) *3600 + (t1.minutes-t2.minutes)*60 + t1.seconds - t2.seconds;

return s1;
}
};

void main()
{
CTime t1;
t1.Set(1, 2, 3);
t1.Print();

CTime t2;
t2.Set(2, 2, 3);
t2.Print();

cout<}