一、类的默认成员函数
二、构造函数
class Date
{
public:
// 1.⽆参构造函数
Date()
{
_year = 1;
_month = 1;
_day = 1;
}
// 2.带参构造函数
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
// 3.全缺省构造函数
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
void Print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
上面的代码就是三种构造函数的写法,要注意的是第二种带参构造函数不是默认构造函数,只有不传实参就可以调用的构造函数才是默认构造函数,一般在写构造函数时建议写成全缺省的构造函数。
三、析构函数
#include<iostream>
using namespace std;
typedef int STDataType;
class Stack
{
public:
Stack(int n = 4)
{
_a = (STDataType*)malloc(sizeof(STDataType) * n);
if (nullptr == _a)
{
perror("malloc申请空间失败");
return;
}
_capacity = n;
_top = 0;
}
~Stack()
{
cout << "~Stack()" << endl;
free(_a);
_a = nullptr;
_top = _capacity = 0;
}
private:
STDataType* _a;
size_t _capacity;
size_t _top;
};
// 两个Stack实现队列
class MyQueue
{
public:
//编译器默认⽣成MyQueue的析构函数调⽤了Stack的析构,释放的Stack内部的资源
// 显⽰写析构,也会⾃动调⽤Stack的析构
/*~MyQueue()
{}*/
private:
Stack pushst;
Stack popst;
};
int main()
{
Stack st;
MyQueue mq;
return 0;
}
在上面的代码中,定义了两个类,一个是Stack,一个是MyQueue,在Stack类中用malloc动态开辟了一段空间,所以在Stack类的析构函数要自己写去释放动态开辟的空间,而MyQueue类的析构函数不用写,因为自定义类型会调用自己的构造函数和析构函数,MyQueue是用两个栈实现的,析构时会调用栈的析构函数。
四、拷贝构造函数
#include<iostream>
using namespace std;
class Date
{
public:
Date(int year = 2024, int month = 7, int day = 21)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
void Print()
{
cout << "" << _year << "/" << _month << "/" << _day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
Date d2=d1;
d1.Print();
cout << endl;
d2.Print();
return 0;
}
可以看到,两个对象的年月日都是一样的,说明拷贝成功。
而没传引用传的是值的话,就会报错。 所以拷贝构造函数一定要传引用。
五、赋值运算符重载
5.1 运算符重载
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
Date(int year = 2024, int month = 7, int day = 21)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
int GetMonthDay(int year, int month)
{
assert(year > 0 && month < 13);
int month_day[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) && month == 2)
{
return 29;
}
return month_day[month];
}
Date& operator+=(int day)
{
_day += day;
while ((_day - GetMonthDay(_year, _month)) > 0)
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month == 13)
{
++_year;
_month = 1;
}
}
return *this;
}
void Print()
{
cout << "" << _year << "/" << _month << "/" << _day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
d1 += 100;
d1.Print();
return 0;
}
上面所实现的运算符重载是求一个日期多少天后的日期是多少号。
结果:
5.2 赋值运算符重载
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
Date(int year = 2024, int month = 7, int day = 21)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
int GetMonthDay(int year, int month)
{
assert(year > 0 && month < 13);
int month_day[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) && month == 2)
{
return 29;
}
return month_day[month];
}
Date& operator+=(int day)
{
_day += day;
while ((_day - GetMonthDay(_year, _month)) > 0)
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month == 13)
{
++_year;
_month = 1;
}
}
return *this;
}
void Print()
{
cout << "" << _year << "/" << _month << "/" << _day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2024, 7, 21);
Date d2(2024, 7, 23);
//赋值重载
d2 = d1;
//拷贝构造
Date d3(d1);
d2.Print();
cout << endl;
d3.Print();
return 0;
}
结果:
无论是拷贝构造还是赋值重载,他们的结果都是一样的,只是复制重载是两个已知对象进行的操作,而拷贝构造是用一个已知的对象去初始化另一个对象。
5.3 const成员函数
将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数
隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
Date operator+(int day) const;
这便是const成员函数的声明。
const成员函数的特点:
1.const成员函数内只能读取类的数据成员,无法修改类的数据成员,否者编译器会报错。
2.const成员函数内,不能调用其他非const成员函数
5.4 日期类的实现
日期类的实现,主要就是利用了运算符的重载,下面我们来实现几个基本功能
1.获取月的日期:
int Date::GetMonthDay(int year, int month)
{
assert(year > 0 && month < 13);
int month_day[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))&& month == 2)
{
return 29;
}
return month_day[month];
}
这里较为简单的方法就是通过数组来返回月的天数,需要注意的便是判断闰年。
2.日期+=(计算多少天后的日期):
Date& Date::operator+=(int day)
{
_day += day;
while ((_day - GetMonthDay(_year, _month)) > 0)
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month == 13)
{
++_year;
_month = 1;
}
}
return *this;
}
先加上天数,如果天数大于月的天数,月数进一位,一直循环,直到天数小于月的天数,需要注意月数如果等于13年数要进一位。
3.日期-= (计算多少天前的日期):
Date& Date::operator-=(int day)
{
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
跟+=的逻辑差不多,先减去天数,再判断天数是否小于0,小于0就月减1,天数再加上该月的天数,直到天数大于0。
4. 两个日期相减(计算两个日期的相差天数)
int Date::operator-(const Date& d) const
{
int flag = 1;
int n = 0;
Date max = *this;
Date min = d;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
while (min != max)
{
++min;
++n;
}
return n * flag;
}
找到两个日期小的日期,让日期天数加1,同时计数器也加1,直到两个日期相等,计数器的值即为相差的天数。
后面几个日期类的运算符的重载其实可以套用前面而得运算符重载,这里便不在赘述,直接给出源码。
Date.h头文件:
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
//friend ostream& operator<<(ostream& out, const Date& d);
//friend istream& operator>>(istream& in, Date& d);
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month);
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1);
// 拷贝构造函数
// d2(d1)
Date(const Date& d);
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=(const Date& d);
// 析构函数
~Date();
// 日期+=天数
Date& operator+=(int day);
// 日期+天数
Date operator+(int day) const;
// 日期-天数
Date operator-(int day) const;
// 日期-=天数
Date& operator-=(int day);
// 前置++
Date& operator++();
// 后置++
Date operator++(int);
// 后置--
Date operator--(int);
// 前置--
Date& operator--();
// >运算符重载
bool operator>(const Date& d) const;
// ==运算符重载
bool operator==(const Date& d) const;
// >=运算符重载
bool operator >= (const Date& d) const;
// <运算符重载
bool operator < (const Date& d) const;
// <=运算符重载
bool operator <= (const Date& d) const;
// !=运算符重载
bool operator != (const Date& d) const;
// 日期-日期 返回天数
int operator-(const Date& d) const;
//检查日期是否正确
bool Check_Date();
//打印
void Print();
private:
int _year;
int _month;
int _day;
};
Date.cpp文件:
#include"Date.h"
int Date::GetMonthDay(int year, int month)
{
assert(year > 0 && month < 13);
int month_day[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))&& month == 2)
{
return 29;
}
return month_day[month];
}
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
if (!Check_Date())
{
cout << "非法日期!" << endl;
}
}
Date::~Date()
{
_year = _month = _day = 0;
}
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
bool Date::Check_Date()
{
if (_month < 1 || _month>12 || _day<1 || _day>GetMonthDay(_year, _month))
{
return false;
}
return true;
}
void Date::Print()
{
cout << "" << _year << "/" << _month << "/" << _day;
}
Date& Date::operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
Date& Date::operator+=(int day)
{
_day += day;
while ((_day - GetMonthDay(_year, _month)) > 0)
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month == 13)
{
++_year;
_month = 1;
}
}
return *this;
}
Date Date::operator+(int day) const
{
Date temp = *this;
temp += day;
return temp;
}
Date& Date::operator-=(int day)
{
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator-(int day) const
{
Date temp = *this;
temp -= day;
return temp;
}
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date Date::operator++(int)
{
Date temp = *this;
*this += 1;
return temp;
}
Date& Date::operator--()
{
*this -= 1;
return *this;
}
Date Date::operator--(int)
{
Date temp = *this;
*this -= 1;
return temp;
}
bool Date::operator>(const Date& d) const
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year)
{
if (_month > d._month)
{
return true;
}
else if (_month == d._month)
{
return _day > d._day ? true : false;
}
}
return false;
}
bool Date::operator==(const Date& d) const
{
return _year == d._year && _month == d._month && _day == d._day;
}
bool Date::operator>=(const Date& d) const
{
return *this > d || *this == d;
}
bool Date::operator<(const Date& d) const
{
return !(*this >= d);
}
bool Date::operator<=(const Date& d) const
{
return !(*this > d);
}
bool Date::operator!=(const Date& d) const
{
return !(*this == d);
}
int Date::operator-(const Date& d) const
{
int flag = 1;
int n = 0;
Date max = *this;
Date min = d;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
while (min != max)
{
++min;
++n;
}
return n * flag;
}
Test.cpp文件:
#include"Date.h"
int main()
{
Date d1(2024, 7, 21);
cout << "+=" << endl;
d1 += 100;
d1.Print();
cout << "" << endl;
Date d2(2024, 7, 21);
cout << "+" << endl;
d2 + 100;
d2.Print();
cout << "" << endl;
Date d3(2024, 7, 21);
cout << "-=" << endl;
d3 -= 100;
d3.Print();
cout << "" << endl;
Date d4(2024, 7, 21);
cout << "-" << endl;
d4 - 100;
d4.Print();
cout << "" << endl;
Date d5(2024, 7, 21);
cout << "前置++" << endl;
++d5;
d5.Print();
cout << "" << endl;
Date d6(2024, 7, 21);
cout << "后置++" << endl;
d6++.Print();
cout << "" << endl;
Date d7(2024, 7, 21);
cout << "前置--" << endl;
--d7;
d7.Print();
cout << "" << endl;
Date d8(2024, 7, 21);
cout << "后置--" << endl;
d8--.Print();
cout << "" << endl;
Date d9(2024, 7, 21);
Date d10(2024, 7, 20);
cout << ">" << endl;
bool ret=d9 > d10;
if (ret)
{
cout << "d9>d10" << endl;
}
else
{
cout << "d9<d10" << endl;
}
Date d11(2024, 7, 21);
Date d12(2024, 7, 21);
cout << "==" << endl;
bool ret1 = d11 == d12;
if (ret1)
{
cout << "d11==d12" << endl;
}
else
{
cout << "d11!=d12" << endl;
}
Date d13(2024, 7, 22);
Date d14(2024, 7, 21);
cout << ">=" << endl;
bool ret2=d13 >= d14;
if (ret2)
{
cout << "d13>=d14" << endl;
}
else
{
cout << "d13<d14" << endl;
}
Date d15(2024, 7, 22);
Date d16(2024, 7, 21);
cout << "<" << endl;
bool ret3 = d15 < d16;
if (ret3)
{
cout << "d15<d16" << endl;
}
else
{
cout << "d16<d15" << endl;;
}
Date d17(2024, 7, 22);
Date d18(2024, 7, 21);
cout << "<=" << endl;
bool ret4 = d18 <= d17;
if (ret4)
{
cout << "d18<=d17" << endl;
}
else
{
cout << "d18>d17" << endl;;
}
Date d19(2024, 7, 22);
Date d20(2024, 7, 21);
cout << "!=" << endl;
bool ret5 = d19 != d20;
if (ret5)
{
cout << "d19!=d20" << endl;
}
else
{
cout << "d19==d20" << endl;
}
Date d21(2024, 7, 21);
Date d22(2024, 4, 12);
cout << "差距天数" << endl;
int ret6 = d21 - d22;
cout << "差距天数为:" << ret6 << endl;
return 0;
}
相日期判断是否相等等运算符重载在上面代码也有,感兴趣的同学可以自己看。
六、总结
以上便是类和对象(中)的讲解,包括了类和对象最基本的东西,希望以上所讲能够对大家有所帮助,有帮助的话记得一键三连,感谢各位。