运算符重载

运算符重载

重载+号

作用:实现两个自定义数据类型相加的运算

成员函数重载+号

加号运算符重载
#include<iostream>
using namespace std;
class Person{
	public:
	//1.成员函数重载+号
	Person operator+(Person &p)
	{
		Person temp;
		temp.m_A=this->m_A+p.m_A;
		temp.m_B=this->m_B+p.m_B;
		return temp; 
	} 
		int m_A;
		int m_B;
		
}; 
void test01()
{
	Person p1;
	p1.m_A=10;
	p1.m_B=10;
	Person p2;
	p2.m_A=10;
	p2.m_B=10;
	
	Person p3=p1+p2;
	cout<<p3.m_A<<" "<<p3.m_B<<endl;
}
int main()
{
	test01();
}

全局函数重载加号

#include<iostream>
using namespace std;
class Person{
	public:
		int m_A;
		int m_B;
}; 
//2.全局函数重载+号	
Person operator+(Person &p1,Person &p2)
	{
		Person temp;
		temp.m_A=p1.m_A+p2.m_A;
		temp.m_B=p1.m_B+p2.m_B;
		return temp; 
	} 
void test01()
{
	Person p1;
	p1.m_A=10;
	p1.m_B=10;
	Person p2;
	p2.m_A=10;
	p2.m_B=10;
	
	Person p3=p1+p2;
	cout<<p3.m_A<<" "<<p3.m_B<<endl;
}
int main()
{
	test01();
}

左移运算符重载

//左移运算符重载
#include<iostream>
using namespace std;
class Person{
	friend ostream & operator<<(ostream &cout,Person &p); //友元函数 用来访问私有成员
	public:
		Person(int a,int b)
		{
			m_A=a;
			m_B=b;
		}
	private:
	//利用成员函数重载 左移运算符
	//不能利用成员函数重载<<运算符,因为无法实现cout在左侧 
//	void coperator<<(cout)
//	{
//	} 
		int m_A;
		int m_B;
}; 
//只能利用全局函数重载左移运算符
ostream & operator<<(ostream &cout,Person &p)//本质 operator<<(cout,p) 简化 cout<<p
{
 	cout<<"m_A= "<<p.m_A<<" "<<"m_B= "<<p.m_B;
	return cout;	
}//链式编程思想,所以要return 一个cout 
void test01()
{
	Person p(10,10);
	cout<<p<<endl;
}
int main()
{
	test01();
}

递增运算符重载

作用:通过重载递增运算符,实现自己的整型数据

关系运算符重载

#include<iostream>
using namespace std;
class Person
{
	public:
		Person(string name,int age)
		{
			m_Name=name;
			m_Age=age;
		}
		//重载==号
		bool operator==(Person &p)
		{
			if(this->m_Name==p.m_Name&&this->m_Age==p.m_Age)
			{
				return true;
			}
			return false;
		 } 
	string m_Name;
	int m_Age;
}; 

void test01()
{
	Person p1("Tom",18);
	
	Person p2("Tom",20);
	
	if(p1==p2)
	{
		cout<<"p1和p2是相等的!"; 
	}
	else
		cout<<"p1和p2是不相等的!";
}
int main()
{
	test01();
	return 0;
}

7-14 数据的最大值问题(重载+函数模板) (50 分)

两个类如下设计:类time有三个数据成员,hh,mm,ss,分别代表时,分和秒,并有若干构造函数和一个重载-(减号)的成员函数。类date有三个数据成员,year,month,day分别代表年月日,并有若干构造函数和一个重载>(<)(大于号或者小于号)的成员函数。

要求设计一个函数模板template <\class T>\ double maxn(T x[], int len) 对int,float,time和date或者其他类型的数据,返回最大值。

主函数有如下数据成员:

int intArray[100];

double douArray[100];time timeArray[100];

date dateArray[100];

其中,hh = 3600 * ss, mm = 60 * ss, year = 365 * day, month = 30 * day,对于time和date类型,数据在转换成ss或者day后进行运算。

输入格式:

每行为一个操作,每行的第一个数字为元素类型,1为整型元素,2为浮点型元素,3为time类型,4为date类型,若为整型元素,接着输入整型数据,以0结束。若为浮点型元素,接着输入浮点型数据,以0结束。若为time型元素, 输入time型数据(hh1 mm1 ss1 hh2 mm2 ss2),以0结束。若为date型数据,输入date型数据(year1 month1 day1 year2 month2 day2),以0结束。输入-1时表示全体输入结束。

输出格式:

对每次输入,输出一个最大值。

样例输入:

1 4 5 9 3 7 0

2 4.4 5.5 6.9 3.2 2.7 0

3 18 21 22 18 20 31 18 21 49 0

4 2013 5 14 2013 5 15 2013 4 1 0

-1

样例输出:

9

6.9

18 21 49

2013 5 15

//7.14
#include<iostream>
using namespace std;
//优先声明类 
class Time;
class Date;
class Time{
	private:
		int hh;
		int mm;
		int ss;
		int total;
	public:
		Time(){}
		Time(int h,int m,int s):hh(h),mm(m),ss(s){
			this->cal_total();
			}
		void cal_total()
		{
			total=hh*3600+mm*60+ss;
		}
		int get_total()
		{
			return total;
		}
		bool operator<(Time &p)
		{
			if(this->get_total()<p.get_total())
				return true;
			return false;
		}
		bool operator>(Time &p)
		{
			if(this->get_total()>p.get_total())
				return true;
			return false;
		}
		void print()
		{
			cout<<this->hh<<" "<<this->mm<<" "<<this->ss<<endl;
		}
		
};
class Date{
	private:
		int year;
		int month;
		int day;
		int total;
	public:
		Date(){}
		Date(int y,int m,int d):year(y),month(m),day(d){
			this->cal_total();
			}
		void cal_total()
		{
			total=year*365+month*30+day;
		}
		int get_total()
		{
			return total;
		}
		bool operator<(Date &p)
		{
			if(this->get_total()<p.get_total())
				return true;
			return false;
		}
		bool operator>(Date &p)
		{
			if(this->get_total()>p.get_total())
				return true;
			return false;
		}
		void print()
		{
			cout<<this->year<<" "<<this->month<<" "<<this->day<<endl;
		}
};
template<class T>
int maxn(T x[],int len)
{
	int index=0;
	for(int i=0;i<len;i++)
	{
		if(x[i]>x[index])//上面已经定义了大于号的意义 
		{
			index=i;
		}
	}
	return index;
}	//返回的是最大值的数组下标	
int main()
{
	int intArray[100];
	double douArray[100];
	Time TimeArray[100];
	Date DateArray[100];
	int type;

	while(1)
	{	
		cin>>type;
		int count=0;
		int pos;
		if(type==-1)
			break;
		switch(type)
		{
			case 1:
				while(1)
				{
					int a;
					cin>>a;
					if(a==0)
						break;
					intArray[count++]=a;//输入操作 
				}
				pos=maxn(intArray,count);
				cout<<intArray[pos]<<endl;
				break;
			case 2:
				while(1)
				{
					double a;
					cin>>a;
					if(a==0)
						break;
					douArray[count++]=a;//输入操作 
				}
				pos=maxn(douArray,count);
				cout<<douArray[pos]<<endl;
				break;
			case 3:
				while(1)
				{
					int h,m,s;
					cin>>h;
					if(h==0)
						break;
					cin>>m>>s;
					TimeArray[count++]=Time(h,m,s);
				}
				pos=maxn(TimeArray,count);
//				cout<<TimeArray[pos].hh<<" "<<TimeArray[pos].mm<<" "<<TimeArray[pos].ss<<endl;//会说这些成员是私有,不能访问,所以可以写一个print函数放在public里去访问 
				TimeArray[pos].print();
				break;
			case 4:
				while(1)
				{
					int y,m,d;
					cin>>y;
					if(y==0)
						break;
					cin>>m>>d;
					DateArray[count++]=Date(y,m,d);
				}
				pos=maxn(DateArray,count);
				DateArray[pos].print();
				break;
		}		
	}
	
}

重载流插入和流提取运算符

  • cin和cout分别是istream和ostream类的对象

  • 形式:

    • istream &operator>>(istream&,自定义类&);
    • ostream &operator<<(ostream&,自定义类&);
  • 只能重载为友元函数

#include <iostream>
#include <string>
using namespace std;
class Dog {string name; int age, weight;string color ;
public:
  friend ostream & operator<<(ostream& out,const Dog& d)    {
    	out<<d.name<<" is a "<<d.age<<" year-old "<<d.color;
               out<<" dog who weighs "<<d.weight<<" pounds.\n";
    	return out;
    }
    friend istream& operator>>(istream& in, Dog& d)    {
    	in>>d.name>>d.age>>d.weight>>d.color;    	return in;    } 
};
int main( )
{
	Dog d1, d2;
	cin>>d1>>d2;
	cout<<d1<<d2; 
	return 0;
}

posted @ 2021-06-04 20:41  小滢小滢考第一名  阅读(92)  评论(0编辑  收藏  举报