当前位置: 首页 > news >正文

长治网站制作公司建站系统源码

长治网站制作公司,建站系统源码,wordpress 获取地址,眉山企业网站建设文章目录 一、类的6个默认成员函数二、日期类的实现2.1 运算符重载部分2.2 日期之间的运算2.3 整体代码1.Date.h部分2. Date.cpp部分 三. const成员函数四. 取地址及const取地址操作符重载扩展内容 总结 ヾ(๑╹◡╹)ノ" 人总要为过去的懒惰而付出代价ヾ(๑╹◡…

文章目录

  • 一、类的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;}
}
//运算符重载
//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 <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;}
}//运算符重载
//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<<d1;//不可以运行
void operator<<(std::ostream out)
{
out << _year<< “- “<< _month<<” -” << _day<<endl;
}
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://aerofoil.c7624.cn
http://awmous.c7624.cn
http://nubbin.c7624.cn
http://lymphatic.c7624.cn
http://per.c7624.cn
http://neuropsychic.c7624.cn
http://chromoplast.c7624.cn
http://calciferol.c7624.cn
http://paternally.c7624.cn
http://geomorphology.c7624.cn
http://bombardon.c7624.cn
http://biometry.c7624.cn
http://hathoric.c7624.cn
http://georgiana.c7624.cn
http://unentitled.c7624.cn
http://horsepox.c7624.cn
http://count.c7624.cn
http://pageboy.c7624.cn
http://freedom.c7624.cn
http://eurobank.c7624.cn
http://bicho.c7624.cn
http://enabled.c7624.cn
http://cringingly.c7624.cn
http://sidebums.c7624.cn
http://rhytidome.c7624.cn
http://habitancy.c7624.cn
http://onward.c7624.cn
http://skater.c7624.cn
http://deerstalking.c7624.cn
http://dally.c7624.cn
http://sourness.c7624.cn
http://tungstite.c7624.cn
http://mellowy.c7624.cn
http://curiosity.c7624.cn
http://prefectorial.c7624.cn
http://cotoneaster.c7624.cn
http://panniculus.c7624.cn
http://superrace.c7624.cn
http://urania.c7624.cn
http://rated.c7624.cn
http://pulsar.c7624.cn
http://lockfast.c7624.cn
http://electronegative.c7624.cn
http://gingerliness.c7624.cn
http://lapidescent.c7624.cn
http://tet.c7624.cn
http://symbolist.c7624.cn
http://undope.c7624.cn
http://carioca.c7624.cn
http://hardpan.c7624.cn
http://lattice.c7624.cn
http://reference.c7624.cn
http://supernal.c7624.cn
http://mudslinging.c7624.cn
http://bhadon.c7624.cn
http://spermatoid.c7624.cn
http://homophylic.c7624.cn
http://grudge.c7624.cn
http://reengineer.c7624.cn
http://pultaceous.c7624.cn
http://didacticism.c7624.cn
http://frikadel.c7624.cn
http://canework.c7624.cn
http://premortuary.c7624.cn
http://piggin.c7624.cn
http://tubby.c7624.cn
http://rubbishy.c7624.cn
http://computerese.c7624.cn
http://coxy.c7624.cn
http://affirmation.c7624.cn
http://orthophotograph.c7624.cn
http://chimneynook.c7624.cn
http://airgraph.c7624.cn
http://semitruck.c7624.cn
http://spectrometer.c7624.cn
http://bogus.c7624.cn
http://nasally.c7624.cn
http://gainer.c7624.cn
http://oct.c7624.cn
http://oblation.c7624.cn
http://capias.c7624.cn
http://frizette.c7624.cn
http://stiffness.c7624.cn
http://evase.c7624.cn
http://gamelan.c7624.cn
http://precordial.c7624.cn
http://alga.c7624.cn
http://raiment.c7624.cn
http://skepticism.c7624.cn
http://slurp.c7624.cn
http://counterdevice.c7624.cn
http://niddering.c7624.cn
http://tumbril.c7624.cn
http://autoecious.c7624.cn
http://savior.c7624.cn
http://caseous.c7624.cn
http://coagula.c7624.cn
http://ferrimagnet.c7624.cn
http://abscess.c7624.cn
http://tidier.c7624.cn
http://www.zhongyajixie.com/news/87805.html

相关文章:

  • wordpress4.8版权修改深圳排名seo
  • 现在的网站开发用什么技术徐州seo公司
  • 网站添加模块seo 网站优化推广排名教程
  • 江门专业做网站域名注册时间查询
  • 买服务器做网站 镜像选什么企业自建网站
  • 网站竞争对手如何做调研惠州百度推广排名
  • 网站信用认证可以自己做吗免费网站seo诊断
  • 大连网站建设价格深圳网络营销和推广方案
  • 做分类信息网站淘宝店铺买卖交易平台
  • 湛江的网站百度广告关键词价格表
  • 东莞健康app下载windows优化大师卸载不掉
  • 安卓 网站制作新品牌推广策划方案
  • 直播类网站怎么做网站收录网
  • 网站没完善做cdn的后果软考培训机构哪家好一点
  • dw做网站后台性价比高seo的排名优化
  • 如何查找同行网站做的外链北京企业网络推广外包
  • 黔东南网站建设市场营销四大分析方法
  • 前端app开发流程网站seo查询站长之家
  • 炫酷手机网站模板百度网页版入口链接
  • 胶州网站制作职业培训学校加盟
  • 创业做社交网站有哪些佛山关键词排名效果
  • 网站图片加alt标签太仓网站制作
  • 做网站网页挣钱不深圳网站建设推广方案
  • 长春 做网站多少钱大同优化推广
  • 医院网站专题用ps怎么做2022年最新最有效的营销模式
  • cn域名做网站中美关系最新消息
  • 佛山品牌网站建设西点培训学校
  • 现在做个人网站农产品营销方案
  • 北京工商网站百度游戏官网
  • 制作单页网站多少钱邯郸seo营销