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

CD18.【C++ Dev】类和对象(9)(声明和定义分离的写法以及代码复用)

目录

1.可以将声明写在类里面,将定义写在类外面

Date.h写入声明

Date.cpp写入成员函数的定义,再包含Date.h

检查是否能正常调用成员函数

2.论代码的复用

方法1:修改operator<

方法2:复用operator<和operator==

operator!=

operator<=

operator>

写法1.<=的反面

写法2.既不<也不==

operator>=

写法1.<的反面

写法2.>或==


承接CD17.【C++ Dev】类和对象(8):赋值运算符重载文章

1.可以将声明写在类里面,将定义写在类外面

还是以日期类为例:

Date.h写入声明

#pragma once
#include <iostream>
using namespace std;
class Date
{
public:
	Date(int year = 0, int month = 0, int day = 0);
	Date(const Date& x);
	void Print();
	bool operator< (const Date& d2);

	int _year;
	int _month;
	int _day;
};

注意:缺省参数写在声明中

Date.cpp写入成员函数的定义,再包含Date.h

#include "Date.h"
Date::Date(int year , int month, int day)//指定类域
{
	_year = year;
	_month = month;
	_day = day;
}

Date::Date(const Date& x)//指定类域
{
	cout << "Date(const Date& x)" << endl;
	_year = x._year;
	_month = x._month;
	_day = x._day;
}

void Date::Print()//指定类域
{
	cout << _year << "/" << _month << "/" << _day << endl;
}

bool Date::operator< (const Date& d2)//指定类域
{
	if (_year < d2._year)
		return true;
	else if (_year == d2._year && _month < d2._month)
		return true;
	else if (_year == d2._year && _month == d2._month && _day < d2._day)
		return true;
	return false;
}

注:当声明和定义分离时,定义需要写类域,否则编译器无法区分是普通函数还是成员函数

检查是否能正常调用成员函数

main.c写入:

#include "Date.h"
int main()
{
	Date d1(2025, 3, 18);
	Date d2(2025, 3,24);
	d1.Print();
	cout << (d1 < d2) << endl;
	return 0;
}

运行结果:结果是正确的

2.论代码的复用

前面代码给出了operator<的定义,问operator>怎么写?

方法1:修改operator<

将operator<定义里面的<全部改成>,得到operator>:

bool Date::operator> (const Date& d2)
{
	if (_year > d2._year)
		return true;
	else if (_year == d2._year && _month > d2._month)
		return true;
	else if (_year == d2._year && _month == d2._month && _day > d2._day)
		return true;
	return false;
}

这样写没有什么问题,但缺点是代码过于冗长,可以复用operator<

方法2:复用operator<和operator==

先补一个operator==的代码:

bool Date::operator== (const Date& d2)
{
	return ((_year == d2._year) && (_month == d2._month) && (_day == d2._day));
}

复用的含义和修改不同,之后只需要复用operator<和operator==就可以简洁写出operator<=,operator>=,operator!=,operator>代码

operator!=

即对operator==的结果取反,需要显式使用*thi来比较*this对象和d2对象,如下:

bool Date::operator!=(const Date& d2)
{
	return !(*this == d2);
}

operator<=

即operator==和operator<任何一个为真,则operator<=返回真,如下:

bool Date::operator<=(const Date& d2)
{
	return *this < d2 || *this == d2;
}

operator>

两种写法

写法1.<=的反面
bool Date::operator>(const Date& d2)
{
	return !(*this <= d2);
}

复用分析:operator>复用了operator<=的代码,而operator<=复用了operator<和operator==的代码,为嵌套复用

写法2.既不<也不==
bool Date::operator>(const Date& d2)
{
	return !(*this < d2) && !(*this == d2);
}

显然写法1更直截了当,可读性好,写法2有点啰嗦

operator>=

写法1.<的反面
bool Date::operator>=(const Date& d2)
{
	return !(*this<d2);
}
写法2.>或==
bool Date::operator>=(const Date& d2)
{
	return *this > d2 || *this == d2;
}

下篇文章将完善日期类对象的其他运算符重载

相关文章:

  • php webshell免杀
  • Hive问题记录(1)
  • SingleMod
  • inline 配置全局参数变量
  • 深入解析 Spring Framework 5.1.8.RELEASE 的源码目录结构
  • 驱动编写-DS18B20温度传感器
  • 远场分量(平面波角谱)与倏逝波
  • 搜索旋转排序数组
  • 初见MyBatis
  • 区间端点(java)(贪心问题————区间问题)
  • 游戏中的碰撞检测算法
  • 吐血整理:Air8201如何使用LuatOS进行电源管理功能!
  • 线程控制与线程库
  • ideaIU-2023.2.5.exe install (IntelliJ_IDEA_IU_2023.2.5)
  • 解决海豚调度器跑出数据但显示状态失败(在CDH6.3.2跑离线数仓任务)
  • C#中获取字节数据的高字节和低字节
  • MyBatis-Plus LambdaQueryWrapper 详解:优雅构建类型安全的查询条件
  • 3.25学习总结 抽象类和抽象方法+接口+内部类+API
  • 常用的离散时间傅里叶变换(DTFT)对
  • 网络相关的知识总结1
  • 怎样解除拦截网站设置/站长工具查询网
  • 网站滑动效果怎么做/企业网站制作模板
  • 广东高端网站建设/重庆网站快速排名提升
  • 怎么用织梦做本地网站/深圳网络推广市场
  • 安徽股票配资网站建设/知道百度
  • 帝国cms个人网站模板/线上宣传推广方式