30分钟网站建设教程视频5000人网站开发
web/
2025/10/6 22:19:52/
文章来源:
30分钟网站建设教程视频,5000人网站开发,无锡百姓网推广,深圳专门网站制作文章目录 一、类的6个默认成员函数二、日期类的实现2.1 运算符重载部分2.2 日期之间的运算2.3 整体代码1.Date.h部分2. Date.cpp部分 三. const成员函数四. 取地址及const取地址操作符重载扩展内容 总结 ヾ(๑╹◡╹)#xff89; 人总要为过去的懒惰而付出代价ヾ(๑╹◡… 文章目录 一、类的6个默认成员函数二、日期类的实现2.1 运算符重载部分2.2 日期之间的运算2.3 整体代码1.Date.h部分2. Date.cpp部分 三. const成员函数四. 取地址及const取地址操作符重载扩展内容 总结 ヾ(๑╹◡╹) 人总要为过去的懒惰而付出代价ヾ(๑╹◡╹) 一、类的6个默认成员函数
如果一个类中什么成员都没有简称为空类。
空类中并不是什么都没有,任何类在什么都不写时编译器会自动生成以下6个默认成员函数。 默认成员函数用户没有显式实现编译器会生成的成员函数称为默认成员函数
二、日期类的实现
2.1 运算符重载部分 inline不支持声明与定义分离所以成员函数要成为inline最好直接在类里面定义类里面定义成员函数的默认就是inline。代码较长的部分采用声明与定义分离的方法 1. 运算符重载部分1 此部分在Date.h类里面部分
//运算符重载部分1bool operator(const Date d);bool operator(const Date d);bool operator(const Date d){return !(*this d);}bool operator(const Date d){return !(*this d);}bool operator!(const Date d){return !(*this d);}//思路把小于的情况写出来剩下的就是大于的情况bool operator(const Date d){return *this d || *this d;//this是一个指针指向类的指针//小于和等于都已经实现过了所以就可以复用}//运算符重载部分【-】Date operator(int day) const;Date operator(int day);Date operator-(int day) const;Date operator-(int day);运算符重载部分【比较大小】能复用其他函数的时候尽可能的去复用此部分写一个小于等于那么别的函数就都可以复用内联函数声明定义不能分离(符号表里没有函数的地址所以使用的时候找不到函数定义)所以这部分代码如果想设置成inline函数就直接放到类里面。内联函数是在调用的地方被展开以空间换取时间的方法类里面的函数默认是内联函数 2. 运算符重载部分2 此部分在Date.cpp部分
//运算符重载部分2和部分
bool Date::operator(const Date d)//访问的本质上是this-_year d._year(一个是指针访问一个是类直接访问)……//_year并不是类里面的_year(仅仅是一个声明)而是比较的另一个类
{if (_year d._year _month d._month _day d._day){return true;}else{return false;}
}
bool Date::operator(const Date d)//要加上Date::(在类外面写)
{if (_year d._year|| (_year d._year _month d._month)|| (_year d._year _month d._month _day d._day)){return true;}else{return false;}
}
//运算符重载
//d12d2;因为此处运算符是d1是不变的所以不能直接对d1进行改动
Date Date::operator(int day) const//这里的返回值不能用Date因为出了函数空间就被回收了
{//复用Date ret(*this);//拷贝构造ret day;return ret;//Date ret(*this);//拷贝构造//ret._day day;//while (ret._day GetMonthDay(ret._year, ret._month))//{// ret._day ret._day - GetMonthDay(ret._year, ret._month);// ret._month;// while (ret._month 13)// {// ret._month 1;// ret._year;// }//}//return ret;
}Date Date::operator(int day)//因为出了作用域this还在所以用引用比较好【在能用引用的情况下尽可能去用引用】
{//复用//*this *this day;//return *this;if (day 0){return *this - -day;}_day day;while (_day GetMonthDay(_year, _day)){_day - GetMonthDay(_year, _day);_month;if (_month 13){_month 1;_year;}}return *this;
}Date Date::operator-(int day) const
{Date ret(*this);ret - day;return ret;
}
Date Date::operator-(int day)
{if (day 0){return *this -day;}_day - day;while (_day 0)//日期是不能有0日的{_month--;while (_month 0){_month 12;_year--;}_day GetMonthDay(_year, _month);}return *this;
}运算法重载部分【比较大小】 因为代码较长不适合用inline函数所以使用声明与定义的方法【和的代码更加容易实现】 运算符重载部分【、、-、-】d110注意这里是和-并不是和-,所以d1是不变的不要对d1直接进行改动。【利用拷贝构造】和之间和-和-之间可以相互复用【写整块代码的时候代码之间能相互复用就相互复用【即简单又很大程度上保证了代码的正确性】】【复用更优】加减法注意这里的day要求必须是正数否则减去一个负数相当于加上一个正数加上一个负数相当于减去一个正数相互复用即可【因为-复用-所以-的函数就不用考虑负数问题】 补充知识点1 Date d1(2000 9 17 Date d2 d1;//这里是拷贝构造不是赋值【因为一个已经创建的对象去初始化另一个对象就是拷贝构造】 【赋值是两个已经创建好的对象进行初始化】 补充知识点2: 写赋值重载函数和拷贝构造函数时函数的形参要加const因为既可以防止修改值又可以防止当形参是函数的返回值【具有const属性】会权限扩大导致错误【无法修改的右值】。 2.2 日期之间的运算
前置后置前置–后置–【区分是前置还是后置的函数重载】 C用无参数的代表前置有一个参数的代表后置【参数只要是int类型的就符合要求无论是整形的哪一个数字】 1. 日期之间的运算1 此部分在Date.h类里面部分 Date.h
//日期之间的运算Date operator(){*this 1;return *this;}Date operator(int){Date tmp(*this);tmp 1;return tmp;}Date operator--(){*this - 1;return *this;}Date operator--(int){Date tmp(*this);tmp - 1;return tmp;}//日期-日期int operator-(const Date d) const;2. 日期之间的运算2 此部分在Date.cpp里面部分 Date.c
//*this和d不确定哪一个日期大
int Date::operator-(const Date d) const
{int day 0;int flag 1;Date max *this;Date min d;if (*this d){max d;min *this;flag -1;}while (min ! max){min;day;}day day * flag;return day;
}2.3 整体代码
1.Date.h部分
#pragma once#include iostream
#include assert.h
using std::cin;
using std::cout;//在正式的项目中不要全展开
using std::endl;class Date
{
public://拷贝构造函数部分//判断是否是闰年bool isLeapYear(int year){if (year % 4 0 year % 100 ! 0 year % 400 0){return true;}return false;}//每一月的天数int GetMonthDay(int year, int month);//构造函数(也可以不用写)//可能会有用户输入非法日期Date(int year 1, int month 1, int day 1);//拷贝构造函数(也可以不用写),值拷贝Date(const Date d){_year d._year;_month d._month;_day d._day;}//赋值运算符重载Date operator(const Date d){if (this ! d){_year d._year;_month d._month;_day d._day;}return *this;}//不需要写析构函数没有资源清理//打印Date,在函数后面加一个const,相当于void print(const Date* const d)const对象和普通对象都可以调用此函数//如果不加const相当于void print(Date* const d),//只要不修改this就可以加上void print() const{cout _year - _month - _day endl;}//运算符重载部分【-】Date operator(int day) const;Date operator(int day);Date operator-(int day) const;Date operator-(int day);//运算符重载部分1【比较大小】bool operator(const Date d) const;bool operator(const Date d) const;bool operator(const Date d) const{return !(*this d);}bool operator(const Date d) const{return !(*this d);}bool operator!(const Date d) const{return !(*this d);}//思路把小于的情况写出来剩下的就是大于的情况bool operator(const Date d) const{return *this d || *this d;//this是一个指针指向类的指针//小于和等于都已经实现过了所以就可以复用}//日期之间的运算Date operator(){*this 1;return *this;}Date operator(int){Date tmp(*this);tmp 1;return tmp;}Date operator--(){*this - 1;return *this;}Date operator--(int){Date tmp(*this);tmp - 1;return tmp;}//日期-日期int operator-(const Date d) const;private:int _year;int _month;int _day;
};
2. Date.cpp部分
#define _CRT_SECURE_NO_WARNINGS 1
#include Date.h//构造函数部分
Date::Date(int year 1, int month 1, int day 1)
{if (year 1 (month 1 month 12) (day 1 day GetMonthDay(year, month))){_year year;_month month;_day day;}else{cout 日期非法 endl;}
}
int Date::GetMonthDay(int year, int month)
{assert(year 0 month 1 month 13);static int monthDayArray[13] { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//因为这部分空间需要频繁开辟所以就用static就可以节约时间if (month 2 isLeapYear(year)){return 29;}else{return monthDayArray[month];}
}//运算符重载部分2
bool Date::operator(const Date d) const//访问的本质上是this-_year d._year(一个是指针访问一个是类直接访问)……//_year并不是类里面的_year(仅仅是一个声明)而是比较的另一个类
{if (_year d._year _month d._month _day d._day){return true;}else{return false;}
}
bool Date::operator(const Date d) const//要加上Date::(在类外面写)
{if (_year d._year|| (_year d._year _month d._month)|| (_year d._year _month d._month _day d._day)){return true;}else{return false;}
}//运算符重载
//d12d2;因为此处运算符是d1是不变的所以不能直接对d1进行改动
Date Date::operator(int day) const//这里的返回值不能用Date因为出了函数空间就被回收了
{//复用Date ret(*this);//拷贝构造ret day;return ret;//Date ret(*this);//拷贝构造//ret._day day;//while (ret._day GetMonthDay(ret._year, ret._month))//{// ret._day ret._day - GetMonthDay(ret._year, ret._month);// ret._month;// while (ret._month 13)// {// ret._month 1;// ret._year;// }//}//return ret;
}Date Date::operator(int day)//因为出了作用域this还在所以用引用比较好【在能用引用的情况下尽可能去用引用】
{//复用//*this *this day;//return *this;if (day 0){return *this - -day;}_day day;while (_day GetMonthDay(_year, _day)){_day - GetMonthDay(_year, _day);_month;if (_month 13){_month 1;_year;}}return *this;
}Date Date::operator-(int day) const
{Date ret(*this);ret - day;return ret;
}
Date Date::operator-(int day)
{if (day 0){return *this -day;}_day - day;while (_day 0)//日期是不能有0日的{_month--;while (_month 0){_month 12;_year--;}_day GetMonthDay(_year, _month);}return *this;
}//*this和d不确定哪一个日期大
int Date::operator-(const Date d) const
{int day 0;int flag 1;Date max *this;Date min d;if (*this d){max d;min *this;flag -1;}while (min ! max){min;day;}day day * flag;return day;
}三. const成员函数
将const修饰的类成员函数称之为const成员函数const修饰类成员函数实际修饰该成员函数隐含的this指针表明在该成员函数中不能对类的任何成员进行修改。 Date类中的打印函数中 void print() const{cout _year - _month - _day endl;}打印Date,在函数后面加一个const,相当于void print(const Date* const d)const对象和普通对象都可以调用此函数如果不加const相当于void print(Date* const d),只要不修改this就可以加上constd13).print();会报错因为d13是传值返回传值返回的是临时拷贝临时拷贝具有常性。【在print没有加const的情况下】 建议成员函数中不修改成员变量的成员函数都可以加上const。【普通对象和const对象都可以调用】 不加const那么const修饰的对象就没有办法调用成员函数 1. const对象可以调用非const成员函数吗不可以权限放大不可以 2. 非const对象可以调用const成员函数吗可以权限缩小可以 3. const成员函数内可以调用其它的非const成员函数吗不可以权限放大 4. 非const成员函数内可以调用其它的const成员函数吗可以权限缩小 四. 取地址及const取地址操作符重载
这两个默认成员函数一般不用重新定义 编译器默认会生成。 Date* operator(){return this;}//取地址重载const Date* operator() const{return this;}//const取地址重载这两个运算符一般不需要重载使用编译器生成的默认取地址的重载即可只有特殊情况才需要重载比如想让别人获取到指定的内容【不想要输出地址而是别的内容】【此时才需要我们自己写】 扩展内容
流插入运算符和流提取运算符重载 d1.operator(cout);//test函数中写可以运行 coutd1;//不可以运行 void operator(std::ostream out) { out _year “- “ _month” -” _dayendl; } Date.h 类里面
//友元函数friend std::ostream operator(std::ostream out, const Date d);friend std::istream operator(std::istream in, Date d);Date.cpp
std::ostream operator(std::ostream out, const Date d)
{out d._year - d._month - d._day endl;return out;
}
std::istream operator(std::istream in, Date d)
{in d._year d._month d._day;return in;
}内置类型直接支持流插入和流提取【且自动识别类型因为函数重载】全局变量函数不要放在.h里面因为在别的文件展开的时候会出现多个符号表里导致重定义。【只有声明的时候才会链接的时候去找函数】 总结
以上就是今天的所有内容了类的默认成员函数就到此结束了。本文以及【C类和对象】类有哪些默认成员函数呢上】详细的介绍了类的默认成员函数希望给友友们带来帮助
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/88130.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!