ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

友元---

2022-03-01 23:30:35  阅读:183  来源: 互联网

标签:友元 hours int 60 --- Time operator minutes


友元


1. 创建友元

有元函数的声明

创建友元函数的第一步是将其原型放在类声明中,并在原型声明前加上关键字friend:

friend Time operator*(double m, const Time & t);

该原型意味着下面两点:

  • 虽然operator*()函数是在类声明中声明的,但它不是成员函数,因此不能使用成员函数运算符来调用;
  • 虽然operator*()函数不是成员函数,但它与成员函数的访问权限相同。

友元函数重载示例

计算时间:

#ifndef __MYTIME4_H__
#define __MYTIME4_H__

#include <iostream>

using namespace std;

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;
		Time operator-(const Time &t) const;
		Time operator*(double mult) const;
		friend Time operator*(double mult, const Time &t);
		friend ostream &operator<<(ostream &os, const Time &t);
//		void Show() const;
};
#endif
#include "mytime4.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;
}

Time Time::operator-(const Time &t) const
{
	Time diff;

	int tot1, tot2;
	tot1 = hours * 60 + minutes;
	tot2 = t.hours * 60 + t.minutes;
	diff.hours = (tot1 - tot2) / 60;
	diff.minutes = (tot1 - tot2) % 60;

	return diff;
}

Time Time::operator*(double mult) const
{
	Time result;

	long totalminutes = hours*mult*60 + minutes*mult;
	result.hours = totalminutes / 60;
	result.minutes = totalminutes % 60;

	return result;
}
/*
void Time::Show() const
{
	cout << hours << " hours,  " << minutes << " minutes." << endl;
}
*/
Time operator*(double mult, const Time &t)
{
	Time result;

	long totalminutes = t.hours*mult*60 + t.minutes*mult;
	result.hours = totalminutes / 60;
	result.minutes = totalminutes % 60;

	return result;
}

ostream &operator<<(ostream &os, const Time &t)
{
	os << t.hours << " hours, " << t.minutes << " minutes." << endl;
	return os;
}

/*
Time operator*(double mult, const Time &t)
{
	return t * mult;
}
*/
#include <iostream>
#include "mytime4.h"

using namespace std;

int main(void)
{
	Time coding(4, 35);
	Time fixing(2, 47);
	Time total;
	Time Planning;
	Time diff;
	Time adjusted;

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

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

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

	diff = coding - fixing;
//	diff.Show();
	cout << diff;
	adjusted = coding * 1.5;
//	adjusted.Show();
	cout << adjusted;

	adjusted = 1.5 * coding;
//	adjusted.Show();
	cout << adjusted;

	cout << "************************" << endl;
	cout << coding << fixing;

	return 0;
}

运行结果:

➜  C++ g++ main.cpp mytime4.cpp                                                 
➜  C++ ./a.out 
coding time = 4 hours, 35 minutes.
fixing time = 2 hours, 47 minutes.
7 hours, 22 minutes.
1 hours, 48 minutes.
6 hours, 52 minutes.
6 hours, 52 minutes.
************************
4 hours, 35 minutes.
2 hours, 47 minutes.

2. 常用的友元:重载<<运算符

  • << 的第一种重载版本
void operator<<(ostream & os, const Time & t)
{
	os << t.hours << " hours, " << t.minutes << " minutes";
}
  • << 的第二种重载版本
ostream & operator<<(ostream & os, const Time & t)
{
	os << t.hours << " hours, " << t.minutes << " minutes";
	return os;
}

3. 友元和成员函数在重载运算符上的区别

非成员版本的重载运算符函数所需的形参数目与运算符使用的操作数数目相同,而成员函数所需的参数数目少一个,因为其中的一个操作数是被隐式地传递的调用对象。

加法运算符需要两个操作数。对于成员函数版本来说,一个操作数通过this指针隐式地传递,另一个操作数作为函数参数显式地传递;对于友元版本来说,两个操作数都作为参数来传递。

标签:友元,hours,int,60,---,Time,operator,minutes
来源: https://blog.csdn.net/hahahanba/article/details/123217184

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有