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

本地网站有什么可以做宁波seo外包服务商

本地网站有什么可以做,宁波seo外包服务商,国内网站用django做的,网站开发与制作C【类和对象】 1.运算符重载2.赋值运算符重载3.日期类的实现 1.运算符重载 (1).C规定类类型运算符使用时,必须转换成调用运算符重载。 (2).运算符重载是具有特殊名字的函数,名字等于operator加需要使用的运算符,具有返回类型和参数列表及函数…

C++【类和对象】

  • 1.运算符重载
  • 2.赋值运算符重载
  • 3.日期类的实现

1.运算符重载

(1).C++规定类类型运算符使用时,必须转换成调用运算符重载。
(2).运算符重载是具有特殊名字的函数,名字等于operator加需要使用的运算符,具有返回类型和参数列表及函数体。
(3).重载运算符函数的参数个数和该运算符作用的运算对象的个数保持一致,一元运算符有一个参数,二元运算符有两个参数,二元运算符左边的传给第一个参数,右边的传给第二个参数。
(4).如果一个重载运算符函数是成员函数,那么它的第一个函数参数是this指针,所以显示出来就少一个参数。
(5).运算符重载以后,其优先级和结合性与对应的内置类型运算符保持⼀致。
(6).不能通过连接语法中没有的符号来创建新的操作符:比如operator@
(7).
在这里插入图片描述这五个运算符不能重载。
(8).C++为了区分前置++和后置++运算符重载函数,规定前置为operator++(),后置++为operator++(int)

#include<iostream>
using namespace std;
class Date
{
public:Date(int year, int month, int day){_year = year;_month = month;_day = day;}bool operator == (const Date& d){return _year == d._year&& _month == d._month&& _day == d._day;}//d1-d2
int	operator-(const Date& d);
//日期-天数
Date operator-(int day);
private:int _year;int _month;int _day;};
int main()
{Date d1(2024, 3, 20);Date d2(2025, 3, 18);if (d1 == d2){cout << "相等" << endl;}elsecout << "不相等" << endl;return 0;
}

2.赋值运算符重载

赋值运算符重载是一个默认成员函数(不能重载到全局),用于完成两个已经存在的对象直接的拷贝赋值。
赋值运算符重载的特点:
(1).赋值运算符重载是一个函数重载,规定重载必须为成员函数,赋值运算重载的参数建议写成const 当前类类型引用,否则会传值传参会有拷贝。
(2).有返回值,且建议写成当前类类型引⽤,引⽤返回可以提高效率,有返回值目的是为了支持连续赋值场景。
(3).没有显式实现时,编译器会自动生成⼀个默认赋值运算符重载,默认赋值运算符重载行为跟默认拷贝构造函数类似,对内置类型成员变量会完成值拷贝/浅拷贝(⼀个字节⼀个字节的拷贝),对自定义类型成员变量会调用他的赋值重载函数。
(4).如果一个类显示实现析构并释放了资源,就需要我们显示实现赋值运算符重载,否则就不需要。

#include<iostream>
using namespace std;
class Date
{
public:Date(int year, int month, int day){_year = year;_month = month;_day = day;}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;}void Print(){cout << _year << "-" << _month << "-" << _day << endl;}
private:int _year;int _month;int _day;
};
int main()
{Date d1(2004,8,18);Date d2(d1);d1.Print();d2.Print();return 0;
}

3.日期类的实现

//Date.h
#include<iostream>
using namespace std;
class Date
{
public:Date(int year, int month, int day){_year = year;_month = month;_day = day;}int GetMonthDay(int year,int month){int monthDayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){return 29;}elsereturn monthDayArray[month];}Date& operator+=(int day);Date operator+(int day);Date& operator-=(int day);Date operator-(int day);// ++d1 ->d1.operator++()Date& operator++();// d1++ ->d1.operator++(1);Date operator++(int);Date& operator--();Date operator--(int);bool operator<(const Date& d);bool operator<=(const Date& d);bool operator>(const Date& d);bool operator>=(const Date& d);bool operator==(const Date& d);bool operator!=(const Date& d);int operator-(const Date& d);void Print();
private:int _year;int _month;int _day;
};
//Date.cpp
#include"Date.h";
Date& Date::operator+=(int day)
{_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);++_month;if (_month == 13){_year++;_month = 1;}}return *this;
}// d1 + 100
Date Date::operator+(int day)
{Date tmp(*this);tmp += day;return tmp;
}void Date::Print()
{cout << _year << "-" << _month << "-" << _day << endl;
}// ++d1
Date& Date::operator++()
{*this += 1;return *this;
}// d1++
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}// d1 < d2
bool Date::operator<(const Date& d)
{if (_year < d._year){return true;}else if (_year == d._year&& _month < d._month){return true;}else if (_year == d._year&& _month == d._month&& _day < d._day){return true;}else{return false;}
}bool Date::operator==(const Date& d)
{return _year == d._year&& _month == d._month&& _day == d._day;
}// d1 <= d2
bool Date::operator<=(const Date& d)
{return *this < d || *this == d;
}bool Date::operator>(const Date& d)
{return !(*this <= d);
}bool Date::operator>=(const Date& d)
{return !(*this < d);
}
//test.cpp
#include"Date.h"void TestDate1()
{Date d1(2025, 3, 9);d1.Print();Date d2 = d1 + 100;d2.Print();d1.Print();Date d3 = d1 += 100;d1.Print();d3.Print();
}void TestDate2()
{Date d1(2025, 3, 9);Date ret1 = ++d1;//Date ret1 = d1.operator++();d1.Print();ret1.Print();Date d2(2025, 3, 9);Date ret2 = d2++;//Date ret2 = d2.operator++(10000);d2.Print();ret2.Print();
}int main()
{TestDate2();return 0;
}

在这里插入图片描述

http://www.dtcms.com/wzjs/223215.html

相关文章:

  • 免费空间做淘宝客网站seo实战密码电子版
  • 2018做网站前景如何佛山做网站的公司哪家好
  • 做网站需要的公司软件开发培训机构去哪个学校
  • 自助建站推广一个新的app如何推广
  • 网站建设实训日志如何制作小程序
  • 潍坊地区网站制作流量宝官网
  • 网站安全维护怎么做兰州网站seo服务
  • 论坛类网站开发报价网络营销好找工作吗
  • 五星级酒店网站建设中国疫情今天最新消息
  • 怎么做网站排名会更好传媒公司
  • 网站开发技术的简历天津seo排名
  • 濮阳做公司网站外贸推广代理
  • 网站建设商务通什么意思淘宝直通车推广怎么做
  • 怎么用ps做网站首页图片尺寸百度竞价一个月5000够吗
  • 河北网站建设口碑好网页模板建站系统
  • 广州外贸型网站建设百度指数分析
  • 深圳专业网站建设平台网页谷歌seo排名优化服务
  • auxer可以做网站嘛百度电脑版官网
  • 外贸网站建设服务平台网络营销软文范例300
  • 青岛高端网站开发公司百度新闻发布平台
  • 山东做网站费用如何免费引流推广
  • 无锡市建设安全监督网站个人博客网站搭建
  • 2019做网站seo行不行自己接单的平台
  • vs网页设计教程广东seo推广贵不贵
  • 开发门户网站多少钱百度收录网址提交
  • 网站开发 法律申明宁波seo网络推广代理公司
  • html个人网站设计商丘seo
  • 记事本做的网站链接怎么装饰广州seo网站推广
  • 专业网站设计制作费用专业的推广公司
  • 南宁智慧园区网站建设今日最新头条新闻条