广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >【C++类和对象】类有哪些默认成员函数呢?(下)
  • 948
分享到

【C++类和对象】类有哪些默认成员函数呢?(下)

c++开发语言 2023-09-06 10:09:56 948人浏览 泡泡鱼
摘要

文章目录 一、类的6个默认成员函数二、日期类的实现2.1 运算符重载部分2.2 日期之间的运算2.3 整体代码1.Date.h部分2. Date.cpp部分 三. 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;}}//运算符重载//d1+2=d2;因为此处运算符是+,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函数,所以使用声明与定义的方法【==和<的代码更加容易实现】
  • 运算符重载部分【+、+=、-、-=】
  • d1=10,注意:这里是+和-,并不是+=和-=,所以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 #include 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;}}//运算符重载//d1+2=d2;因为此处运算符是+,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,就可以加上const
  • (d+13).print();会报错,因为d+13是传值返回,传值返回的是临时拷贝,临时拷贝具有常性。【在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函数中写,可以运行
cout< void operator<<(std::ostream out)
{
out << _year<< “- “<< _month<<” -” << _day< }
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++类和对象】类有哪些默认成员函数呢?(上)】详细的介绍了类的默认成员函数,希望给友友们带来帮助!

来源地址:https://blog.csdn.net/m0_57388581/article/details/132270955

--结束END--

本文标题: 【C++类和对象】类有哪些默认成员函数呢?(下)

本文链接: https://www.lsjlt.com/news/396847.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 【C++类和对象】类有哪些默认成员函数呢?(下)
    文章目录 一、类的6个默认成员函数二、日期类的实现2.1 运算符重载部分2.2 日期之间的运算2.3 整体代码1.Date.h部分2. Date.cpp部分 三. const成员函数...
    99+
    2023-09-06
    c++ 开发语言
  • C++类的默认成员函数有哪些
    本文小编为大家详细介绍“C++类的默认成员函数有哪些”,内容详细,步骤清晰,细节处理妥当,希望这篇“C++类的默认成员函数有哪些”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。类的默认成员函数每个类中,如果不主动实...
    99+
    2023-06-29
  • C++类中隐藏的默认函数有哪些
    这篇文章主要介绍了C++类中隐藏的默认函数有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。Test类中隐藏的六个默认的函数class Test{public:/...
    99+
    2023-06-29
  • C++函数指针+对象指针+this指针+指向类静态和非静态成员的指针
    目录1、指向函数的指针2、对象指针3、this指针4、指向类的非静态成员的指针5、指向类的静态成员的指针 1、指向函数的指针 函数的代码在内存中的首地址,是由函数名表示的,也就是说函...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作