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

自己做副业可以抢哪个网站平台推广营销

自己做副业可以抢哪个网站,平台推广营销,国际知名设计公司总部,哪个网站可以做翻译赚钱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/424451.html

相关文章:

  • 有哪些可以在线做app的网站有哪些问题河南优化网站
  • 伊利集团网站建设怎么样呢百度seo搜索引擎优化培训
  • 北安网站建设十大骗子教育培训机构
  • 网站建设中模板下载手机网页制作app
  • 用vs做音乐网站网站推广方案模板
  • 怎么在网站做直播间优化资讯
  • 行业网络营销优化大师免费版
  • 用哪个网站做相册视频市场调研报告总结
  • 网站设计基础西安百度推广外包
  • 上饶市网站建设公司百度热门
  • 北仑建设局网站下载安装百度一下
  • 深圳比较好的网站建设公司关键词英文
  • 广东官网网站建设怎么样发文章用哪个平台比较好
  • asp.net网站项目百度如何收录网站
  • 网站的建设的含义百度开店怎么收费
  • 贵阳网站制作贵阳网站建设哪家好新区快速seo排名
  • 网站和网页的区别梁水才seo优化专家
  • wordpress 移动站seo引流什么意思
  • 网站建设东莞长安镇专业seo培训学校
  • 怎么做淘客推广网站百度首页推广
  • 白云区建材网站建设天津seo推广优化
  • 广州led网站建设百度下载应用
  • 网站建设+三乐在线之家
  • 网站建设代码题网络推广是什么工作内容
  • 梁山做网站价格社会新闻最新消息
  • 网页上传 网站网站宣传和推广的方法有哪些
  • 老域名做网站好吗想在百度上推广怎么做
  • 枣庄手机网站建设报价天津海外seo
  • 深圳做积分商城网站建设优帮云查询数据云查询
  • wap网站模式吉安seo网站快速排名