使用类(1)

运算符重载,友元函数

// class.h

#pragma once

#include<iostream>

#ifndef _CLASS_H_
#define _CLASS_H_

class Time
{
private:
	int hours;
	int minutes;
public:
	Time();
	Time(int h, int m = 0);
	void AddMin(int m);
	void AddHour(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 n)const;
	friend Time operator*(double m, const Time& t)
	{
		return t * m;
	}
	friend std::ostream& operator<<(std::ostream& os, const Time& t);
};



#endif


// fuc.cpp

#include"class.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::AddHour(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 = t.minutes + 60 * t.hours;
	tot2 = minutes + 60 * hours;
	diff.minutes = (tot2 - tot1) % 60;
	diff.hours = (tot2 - tot1) / 60;
	return diff;
}

Time Time::operator*(double n)const
{
	Time result;
	long totalminutes = hours * 60 * n + minutes * n;  // n是用户自己输入的倍数
	result.hours = totalminutes / 60;
	result.minutes = totalminutes % 60;
	return result;
}

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


// main.cpp

#include"class.h"
using namespace std;

int main()
{
    Time aida(3, 35);
    Time tosca(2, 48);
    Time temp;
    
    cout << "Aida and Tosca:" << endl;
    cout << aida << "; " << tosca << endl;
    temp = aida + tosca;
    cout << "Aida + Tosca = " << temp << endl;
    temp = aida * 1.17;
    cout << "Aida * 1.17 = " << temp << endl;
    cout << "10.0 * Tosca: " << 10.0 * tosca << endl;

    return 0;
}

//知识点: 

//1.重载运算符语法: returnname operatorop(参数),其中op为可重载的运算符                              eg: void operator+( )

//2.在传递参数时,若引用传递和值传递效果一样的话,可以选择引用传递,因为引用传递的效率更高。

//3.在类中定义的函数将自动变为内联(inline)函数。

//4. 友元函数语法:在类中的函数原型前加上friend,且只在此处加上friend  -- 友元函数可以访问类中的private隐藏数据。

//5. friend std::ostream&operator<<(std::ostream& os,const Time& t);  -- 分析 

  (1.friend说明该函数是一个友元函数。

  (2.该函数的返回值是一个ostream流的引用,之所以在 << 运算符的重载中要返回该类型的值,是为了 << 可以连续使用 。 

  (3.该函数的第一个参数是一个ostream流的引用,且fstream是包含与ostream中的,所以也可以使用fstream创建的变量来作为参数

//6. 运算符重载后的调用:

  (1. temp = aida + tosca   本质上为 temp =  aida.operator+(tosca) ,重载运算符的左侧为调用对象,右侧为传递的参数 

  (2.cout<<temp 为 temp<<cout的友元函数形式,若不创建友元函数,则temp<<cout的本质为 temp.operator<<(cout),temp为调用对象,cout为传递的ostream流的引用参数 (可以通过创建友元函数使得一些不符合常规运用的代码变得常规)  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值