运算符重载,友元函数,重载输入输出运算符,状态成员,rand(),类的自动转换

本文介绍了一个计算时间的C++类`Time`,并详细展示了如何通过重载加法运算符实现两个`Time`对象的相加。此外,还介绍了如何使用友元函数重载`ostream`的插入运算符,以实现类对象的自定义输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

运算符重载:计算时间的运算符重载实例:

mytime0.h:

#ifndef MYTIME0_H_
#define MYTIME0_H_


class Time
{
private:
	int hours;
	int minutes;
public:
	Time();
	Time(int h, int m = 0);//构造函数重载
	void AddMin(int m);
	void AddHr(int h);
	void Reset(int h = 0, int m = 0);
	Time operator+(const Time&t)const;//在该函数中,不得修改类成员
	void Show()const;
};
#endif
file1:

#include<iostream>
#include"mytime0.h"
Time::Time()
{
	hours = minutes = 0;
}
Time::Time(int h, int m)
{
	hours = h;
	minutes = m;
}
void Time::AddMin(int m)
{
	minutes += m;
	hours += minutes / 60;
	minutes %= 60;
}
void Time::AddHr(int h)
{
	hours += h;
}
void Time::Reset(int h, int m)
{
	hours = h;
	minutes = m;
}
Time Time::operator+(const Time&t)const//重载+运算符
{
	Time sum;
	sum.minutes = minutes + t.minutes;
	sum.hours = hours + t.hours + sum.minutes / 60;
	sum.minutes %= 60;
	return sum;
}
void Time::Show()const
{
	std::cout << hours << "hours," << minutes << "minutes";
}

file2:

#include<iostream>
#include"mytime0.h"
int main()
{
	using std::cout;
	using std::endl;
	Time planning;
	Time coding(2, 4);
	Time fixing(5, 55);
	Time total;

	cout << "planning time=";
	planning.Show();
	cout << endl;

	cout << "coding time=";
	coding.Show();
	cout << endl;

	cout << "fixing time=";
	fixing.Show();
	cout << endl;

	total = coding + fixing;
	cout << "coding + fixing=";
	total.Show();
	cout << endl;

	Time morefixing(3, 28);
	cout << "more fixing time=";
	morefixing.Show();
	cout << endl;
	total = morefixing.operator+(total);
	cout << "morefixing.operator+(total)=";
	total.Show();
	cout << endl;

	return 0;
}
operator+即为函数名,当定义Time operator+(Time&t2)时,使用t1+t2时,实际上等价于t1.operator+(t2); 当定义Time operator+(Time&t1,Time&t2)时,使用t1+t2,实际上等价于operator(t1+t2)。

将两个以上的对象相加是允许的,如t1+t2+t3。

重载的限制:重载的运算符不必是成员函数,但必须至少有一个操作数是用户定义的类型,使用运算符时不能违反运算符原来的语法规则,不能修改优先级,不能重载某些运算符(详见prime plus第387页)


友元:友元函数不是成员函数,没有this指针,因此参数要有类的引用,如friend void show(Time&t)
例子:

friend Time operator*(double m,const Time&t)//类声明中的友元函数
Time operator*(double m, const Time&t)//定义
{
	Time result;
	long totalminutes = t.hours*mult * 60 * t.mult;
	result.hour = totalminutes / 60;
	result.minutes = totalminutes % 60;
	return result;
}
虽然友元函数不是成员函数,但它与成员函数的访问权限相同。

cout是ostream类的对象。

重载ostream的方法:首先先声明为友元函数,然后:

ostream& operator<<(ostream&os, const Time&t)//参数顺序不能倒
{
	os << t.hour << " hours, " << t.minutes << " minutes";//把类成员数据提取到os对象后返回
	return os;
}

重载运算符作成员函数还是非成员函数:

t1 = t2.operator+(t3);//成员函数
t1 = operator+(t2, t3);//非成员函数



强制类型转换:
Stonwt myCat
myCat = 19.2
//程序使用构造函数Stonwt(double)来创建一个临时对象,并将19.2作为初值。随后使用逐成员赋值方式
//将该临时对象内容复制到mycat
explicit Stonewt(double lbs)//禁止类似如上的隐式转换,但仍允许显式转换,如mycat=stonwt(19.2)




实现一个矩阵,其声明如下: #include "stdafx.h" #include <iostream> using namespace std; class Matrix; // 矩阵 class MatrixIndex; // 子矩阵 // 矩阵 class Matrix{ private: double *data; // 核心数据,用一维数组存储二维矩阵 int nRow; // 矩阵行数 int nColumn; // 矩阵列数 public: // 构造一个x行y列的矩阵,元素值全零 Matrix(int x, int y); // 构造一个x行y列的矩阵,当type为"zero"时元素值全零, // 为"one"时元素值全一,为"rand"时元素值全随机 Matrix(int x, int y, const char* type); // 复制构造函数 Matrix(const Matrix&m1); // 用临时数组构造矩阵,例如当输入为{1,2,3}时构造一个 // 1*3的矩阵 Matrix(std::initializer_list<int> vector); // 用子矩阵构造一个矩阵,子矩阵仅保存某矩阵的引用,此 // 处将子矩阵引用的数据进行深拷贝以构造一个全新矩阵, // 这使得子矩阵可以作为右值同矩阵一样使用 Matrix(const MatrixIndex& index); // 销毁数组data ~Matrix(); // 用矩阵构造一个子矩阵,子矩阵仅保存某矩阵的引用,例 // 如m1={1,2}且m2={3,4},则m(m1,m2)构造以m的第1,2行和 // 第3,4列构造的子矩阵,子矩阵仅保存源矩阵的引用 MatrixIndex operator()(const Matrix&m1, const Matrix&m2); // 重载等号运算符,实现m=m1 Matrix& operator=(const Matrix &m1); // 重载等号运算符,将m的所有元素更改为a Matrix& operator=(double a); // 重载等号运算符,将m更改为行向量vector,此时m中的元 // 素数目需要和vector中的元素数目相同 Matrix& operator=(std::initializer_list<double> vector); // 重载加号运算符,实现m1+m2,矩阵大小需满足运算规则 friend Matrix operator+(const Matrix &m1, const Matrix &m2); // 重载减号运算符,实现m1-m2,矩阵大小需满足运算规则 friend Matrix operator-(const Matrix &m1, const Matrix &m2); // 重载乘号运算符,实现m1*m2,矩阵大小需满足运算规则 friend Matrix operator*(const Matrix &m1, const Matrix &m2); // 重载<<运算符,实现cout<<m1打印矩阵内容 friend ostream& operator<<(ostream &out, const Matrix &m1); friend class MatrixIndex; }; // 子矩阵,其中每个元素为指向某矩阵元素的指针 class MatrixIndex{ private: const Matrix& source; // 子矩阵对应的源矩阵 const Matrix& rows; // 子矩阵对应源矩阵的行 const Matrix& columns; // 子矩阵对应源矩阵的列 public: // 用矩阵构造一个子矩阵,子矩阵仅保存矩阵source的 // 第rows行和第columns列的引用 MatrixIndex(const Matrix&source, const Matrix&rows, const Matrix& columns) :source(source), rows(rows), columns(columns){} // 重载等号运算符,实现m(...)=m1,此时左右元素数目 // 需相同,这使得子矩阵可以作为左值同矩阵一样使用 MatrixIndex& operator=(const Matrix &m1); // 重载等号运算符,将m(...)的所有元素更改为a MatrixIndex& operator=(double a); // 重载等号运算符,将m(...)更改为行向量vector,此 // 时左右元素数目需相同 MatrixIndex& operator=(std::initializer_list<double> vector); friend class Matrix; };
最新发布
06-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值