c++——友元

1.友元函数可以访问类的私有成员的函数
2.创建友元

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*(double mult) const;
	void show() const;
};

...

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;
}
//以上类的声明对*号的重载可以实现
Time A;
Time B;
A=B*7.5; //正确
A=7.5*B;//错误

在这里插入图片描述

//接下来用友元实现7.5*B的重载
#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*(double mult) const;
	friend Time operator*(double m,const Time & t);//友元函数
	void show() const;
};

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*(double mult) const
{
	Time result;
	long totalminutes = hours * mult * 60 + minutes * mult;
	result.hours = totalminutes / 60;
	result.minutes = totalminutes % 60;
	return result;
}

Time operator*(double m,const Time & t)
{
	Time result;
	long totalminutes=m * t.hours*60 + m * t.minutes;
	result.hours = totalminutes / 60;
	result.minutes = totalminutes % 60;
	return result;
	
}

void Time::show() const
{
	cout<<hours<<"小时"<<minutes<<"分"<<endl;
}

int main()
{
	Time planning;
	Time am(3,30);
	Time pm(4,35);
	Time A;
	Time B;

	cout<<"planning time is ";
	planning.show();

	cout<<"am time is ";
	am.show();

	cout<<"pm time is ";
	pm.show();

	B = am + pm;
	
	cout<<"一天耗时 am + pm =";
	B.show();

	A = 7.5*B;
	cout<<"7.5天耗时     = ";
	A.show();

	return 0;
}

运行结果:
在这里插入图片描述
3.使用友元重载<<
3-1:

#include<iostream>

using namespace std;

class Time
{
private:
	int hours;
	int minutes;
public:
	Time();
	Time(int h,int m);
	void show();
	friend void operator<<(ostream & os,const Time & t);
};

Time::Time()
{
	hours=minutes=0;
}

Time::Time(int h,int m)
{
	hours=h;
	minutes=m;
}

void Time::show()
{
	cout<<hours<<"小时"<<minutes<<"分"<<endl;
}

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


int main()
{
	Time t1;
	cout<<t1;
	t1.show();
}

运行结果:
在这里插入图片描述

3-2:

#include<iostream>

using namespace std;

class Time
{
private:
	int hours;
	int minutes;
public:
	Time();
	Time(int h,int m);
	void show();
//	friend void operator<<(ostream & os,const Time & t);
	friend ostream &  operator<<(ostream & os,Time & t);
};

Time::Time()
{
	hours=minutes=0;
}

Time::Time(int h,int m)
{
	hours=h;
	minutes=m;
}

void Time::show()
{
	cout<<hours<<"小时"<<minutes<<"分"<<endl;
	return os;
}

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

ostream & operator<<(ostream & os,Time & t)
{
	os<<t.hours<<"时|"<<t.minutes<<"分钟"<<endl;
}

int main()
{
	Time t1;
	cout<<t1;
	t1.show();
}

运行结果:
在这里插入图片描述

3-3:重载输出到一个文件夹

#include<iostream>
#include<fstream>

using namespace std;

class Time
{
private:
	int hours;
	int minutes;
public:
	Time();
	Time(int h,int m);
	void show();
//	friend void operator<<(ostream & os,const Time & t);
	friend ostream &  operator<<(ostream & os,Time & t);
};

Time::Time()
{
	hours=minutes=0;
}

Time::Time(int h,int m)
{
	hours=h;
	minutes=m;
}

void Time::show()
{
	cout<<hours<<"小时"<<minutes<<"分"<<endl;
}
/*
void operator<<(ostream & os,const Time & t)
{
	os<<t.hours<<"hours,"<<t.minutes<<"minutes"<<endl;
}*/

ostream & operator<<(ostream & os,Time & t)
{
	os<<t.hours<<"时|"<<t.minutes<<"分钟"<<endl;
	return os;
}

int main()
{
	Time t1;
	cout<<t1;
	t1.show();

	ofstream fout;
	fout.open("test.txt");
	fout<<t1;
}

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值