文章目录
通过time和date类的实现,练习++ --运算符重载
本章的语法讲解可参考:C++4:常引用&单目运算符++重载&const用法
1.A function member, SubOneSecond()
通过成员函实现时间-1s
Problem description:
Finish the class of Cmytime。
There are three function members:
Show()
int Set(int h,int m,int s)
int Set(int h,int m,int s)
if 0<=h<=23, 0<=m,s<=59, then set the clock data members the new value
and return 1;
else do nothing and return 0.
void SubOneSecond();
substract one second to the time.
后缀代码:
//StudybarCommentBegin
int main(void) {
int h,m,s;
cin>>h>>m>>s;
Cmytime t1;
t1.Set(h,m,s);
t1.Show();
cout<<endl<<t1.Set(24,0,0)<<"\n";
t1.Show();
t1.SubOneSecond();
cout<<endl;
t1.Show();
return 0;
}
//StudybarCommentEnd
---------------------------------------------------------------------
Sample input:
23 25 38
Sample Output:
23:25:38
0
23:25:38
23:25:37
Answer:
#include<iostream>
using namespace std;
class Cmytime
{
public:
//Cmytime(int , int , int );
Cmytime();
int Set(int , int , int );
void Show();
void SubOneSecond();
private:
int h, m, s;
};
//不得不说这个老学吧是真的zz,楞是空构造函数编译通过了
//Cmytime::Cmytime(int hour=0, int minute=0, int second=0) {
// h = hour;
// m = minute;
// s = second;
//}
Cmytime::Cmytime()
{
}
int Cmytime::Set(int hour, int minute, int second) {
if (hour >= 0 && hour<=23) {
if (minute >= 0 && minute<=59) {
if (second >= 0 && second<=59) {
h = hour;
m = minute;
s = second;
return 1;
}
}
}
return 0;
}
void Cmytime::Show()
{
cout << h << ':' << m << ':' << s;
}
void Cmytime::SubOneSecond()
{
if (s == 0)
{
s = 59;
if (m == 0)
{
m = 59;
if (h == 0) h = 23;
else h--;
}
else m--;
}
else s--;
}
2.-- operator 1
“- -”运算符的前缀重载
Problem description:
finish the class of Cmytime。
There are three function members:
Show()
int Set(int h,int m,int s)
int Set(int h,int m,int s)
1、if 0<=h<=23, 0<=m,s<=59, then set the clock data members the new value
and return 1;
else do nothing and return 0.
operator--()
substract one second to the time.
!!!即在第一题基础上将--前缀运算符(--x)重载为SubOneSecond()函数。
后缀代码:
//StudybarCommentBegin
int main(void) {
int h,m,s;
cin>>h>>m>>s;
Cmytime t1;
t1.Set(h,m,s);
t1.Show();
cout<<endl<<t1.Set(24,0,0)<<"\n";
t1.Show();
--t1;
cout<<endl;
t1.Show();
return 0;
}
//StudybarCommentEnd
---------------------------------------------------------------------
Sample input:
23 25 38
Sample Output:
23:25:38
0
23:25:38
23:25:37
Answer:
#include<iostream>
using namespace std;
class Cmytime
{
public:
//Cmytime(int , int , int );
Cmytime();
int Set(int , int , int );
void Show();
void operator--();
private:
int h, m, s;
};
//
//Cmytime::Cmytime(int hour=0, int minute=0, int second=0) {
// h = hour;
// m = minute;
// s = second;
//}
Cmytime::Cmytime()
{
}
int Cmytime::Set(int hour, int minute, int second) {
if (hour >= 0 && hour<=23) {
if (minute >= 0 && minute<=59) {
if (second >= 0 && second<=59) {
h = hour;
m = minute;
s = second;
return 1;
}
}
}
return 0;
}
void Cmytime::Show()
{
cout << h << ':' << m << ':' << s;
}
//此处的--运算符重载并无返回值,直接对对象进行操作即可
//下例中--存在返回值
void Cmytime::operator--()
{
if (s == 0)
{
s = 59;
if (m == 0)
{
m = 59;
if (h == 0) h = 23;
else h--;
}
else m--;
}
else s--;
}
3.operator–() and operator–(int)
“- -”运算符的前缀及后缀重载,没有编译意义但有语法意义,用于区分前缀重载
Problem description:
finish the class of Cmytime。
There are three function members:
Show()
int Set(int h,int m,int s)
int Set(int h,int m,int s)
1、if 0<=h<=23, 0<=m,s<=59, then set the clock data members the new value
and return 1;
else do nothing and return 0.
operator--()
and
operator--(int)
substract one second to the time.
!!!即在第一题基础上将--运算符(--x)以及(x--)重载为SubOneSecond()函数。
后缀代码:
//StudybarCommentBegin
int main(void) {
int h,m,s;
cin>>h>>m>>s;
Cmytime t1,t2,t3;
t1.Set(h,m,s);
t1.Show();
cout<<endl<<t1.Set(24,0,0)<<"\n";
t1.Show();
t2=--t1;
t3=t1--;
cout<<endl;
t1.Show();
cout<<endl;
t2.Show();
cout<<endl;
t3.Show();
return 0;
}
//StudybarCommentEnd
---------------------------------------------------------------------
Sample input:
23 25 38
Sample Output:
23:25:38
0
23:25:38
23:25:37
Answer:
#include<iostream>
using namespace std;
class Cmytime
{
public:
//Cmytime(int , int , int );
Cmytime();
int Set(int , int , int );
void Show();
Cmytime &operator--();
Cmytime operator--(int);
private:
int h, m, s;
};
//Cmytime::Cmytime(int hour=0, int minute=0, int second=0) {
// h = hour;
// m = minute;
// s = second;
//}
Cmytime::Cmytime(){}
int Cmytime::Set(int hour, int minute, int second) {
if (hour >= 0 && hour<=23) {
if (minute >= 0 && minute<=59) {
if (second >= 0 && second<=59) {
h = hour;
m = minute;
s = second;
return 1;
}
}
}
return 0;
}
void Cmytime::Show()
{
cout << h << ':' << m << ':' << s;
}
//--------------------------------------------------
//此例实现的--操作,对应整型类已有的相同操作
Cmytime &Cmytime::operator--()
{
if (s == 0)
{
s = 59;
if (m == 0)
{
m = 59;
if (h == 0) h = 23;
else h = h - 1;
}
else m = m - 1;
}
else s = s - 1;
return *this;
//------------返回值处取this内容,且返回引用-------------------------
}
Cmytime Cmytime::operator--(int)
{
Cmytime tmp = *this;
if (s == 0)
{
s = 59;
if (m == 0)
{
m = 59;
if (h == 0) h = 23;
else h = h - 1;
}
else m = m - 1;
}
else s = s - 1;
return tmp;
}
//tmp用于寄存原来的对象,局部变量不能做引用返回----------------------
4.output the next date
构造函数的另一种写法
Problem description:
input “year month day” ,output the next date
后缀代码:
//StudybarCommentBegin
int main(void)
{
using std::cin;
using std::cout;
using std::endl;
date D1,D2;
int year;
int month;
int day;
cin>>year>>month>>day; //shu ru di yi ge ri qi
D1=date(year,month,day);
D2=D1++;
cout<<D1.year()<<" "<<D1.month()<<" "<<D1.day()<<endl;
cout<<D2.year()<<" "<<D2.month()<<" "<<D2.day()<<endl;
return 0;
}
//StudybarCommentEnd
---------------------------------------------------------------------
Sample Input
1992 6 30
Sample Output
1992 7 1
1992 6 30
Sample Input
2001 12 31
Sample Output
2002 1 1
2001 12 31
Answer:
#include<iostream>
using namespace std;
bool leap(int);
int month_days(int, int);
class date
{
public:
date(int year = 0, int month = 0, int day = 0) : yy(year), mm(month), dd(day) {};
~date();
date operator++(int);//后缀++
int year();
int month();
int day();
private:
int yy, mm, dd;
};
date::~date()
{
}
date date::operator++(int)
{
date tmp = *this;
if (dd < month_days(mm, yy)) dd++;
else if (dd == month_days(mm, yy))
{
dd = 1;
if (mm != 12) mm++;
if (mm == 12) { mm = 1; yy++; }
}
return tmp;
}
int date::year()
{
return yy;
}
int date::month()
{
return mm;
}
int date::day()
{
return dd;
}
bool leap(int x)
{
if ((x % 400 == 0) || (x % 4 == 0 && x % 100 != 0)) return true;
else return false;
}
int month_days(int x,int year)
{
int days = 0;
if (x == 2)
{
if (leap(year)) days = 29;
else if (!leap(year)) days = 28;
}
else if (x == 1|| x == 3|| x == 5|| x == 7|| x == 8|| x == 10|| x == 12)
days = 31;
else if (x == 4|| x == 6|| x == 9|| x == 11)
days = 30;
return days;
}
后缀代码:
//StudybarCommentBegin
int main(void)
{
using std::cin;
using std::cout;
using std::endl;
date D1,D2;
int year;
int month;
int day;
cin>>year>>month>>day; //shu ru di yi ge ri qi
D1=date(year,month,day);
D2=D1++;
cout<<D1.year()<<" "<<D1.month()<<" "<<D1.day()<<endl;
cout<<D2.year()<<" "<<D2.month()<<" "<<D2.day()<<endl;
return 0;
}
//StudybarCommentEnd