我是靠谱客的博主 等待烤鸡,最近开发中收集的这篇文章主要介绍c++学习运算符重载,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

运算符重载
1、实验目的
·学习掌握重定义与类有关的运算符。
·把握重载运算符的时机。
·学习掌握把一个类对象转换为另一个类对象。
·学习掌握重载为成员与友元的区别及其带参数的情况。
·学习掌握值返回与引用返回的区别及应用时机。
2、实验内容
(1)
生成一个表示复数的类FS。复数的实部sb和虚部xb作为其数据成员。提供成员函数print()显示复数(格式:-3+5i或6-2i),重载“+”、“-”为FS类的成员函数,用来计算两个复数的和、差。
思路导航:
① 定义类,设计构造函数和显示函数print();
② 重载运算符“+”、“-”为类FS的成员函数。
③ 实例化FS类的2个对象,并利用重载的运算符对其进行计算。
另外,根据上述定义,试将上述的运算符重载成员函数改写成友元函数。

#include <iostream>
using namespace std;
class FS {
	public:
		FS(double r=0.0,double i=0.0) {
			real=r;
			imag=i;
//			cout<<"复数对象创建并设置初始值"<<endl;
		}
		~FS(){
//			cout<<"复数对象结束"<<endl;
		}
		void set(double r=0.0,double i=0.0) {
			real=r,imag=i;
		}
		FS operator +(FS &b);
		FS operator -(FS &b);
		FS operator *(FS &b);
		FS operator /(FS &b);
		friend ostream & operator<<(ostream &out,const FS &b);
		void out(void);
	private:
		double real;
		double imag;
};
FS FS::operator +(FS &b) {
	return(FS(real+b.real,imag+b.imag));
}
FS FS::operator -(FS &b) {
	return(FS(real-b.real,imag-b.imag));
}
FS FS::operator *(FS &b) {
	FS temp;
	temp.real=real*b.real-imag*b.imag;
	temp.imag=real*b.imag+imag*b.real;
	return(temp);
}
FS FS::operator /(FS &b) {
	FS temp;
	temp.real=(real*b.real+imag*b.imag)/(b.real*b.real+b.imag*b.imag);
	temp.imag=(imag*b.real-real*b.imag)/(b.real*b.real+b.imag*b.imag);
	return(temp);
}
ostream& operator<<(ostream &out,const FS &b){
	out<<b.real;
	if(b.imag>0)out<<"+";
	out<<b.imag<<"i"<<endl;
	return out;
}
void FS::out(){
//	cout<<real;
//	if(imag>0)cout<<"+";
//	cout<<imag<<"i"<<endl;
}
int main() {
	FS a(1,-2),b(2,1),c;
	cout<<"a=";
	a.out();
	cout<<a<<endl;
	cout<<"b=";
	b.out();
	cout<<b<<endl;
	c=a+b;
	cout<<"c=a+bnc=";
	c.out();
	cout<<c<<endl;
	c=a-b;
	cout<<"c=a-bnc=";
	c.out();
	cout<<c<<endl;
	c=a*b;
	cout<<"c=a*bnc=";
	c.out();
	cout<<c<<endl;
	c=a/b;
	cout<<"c=a/bnc=";
	c.out();
	cout<<c<<endl;
	return 0;
}

将上述的运算符重载成员函数改写成友元函数

#include <iostream>
using namespace std;
class FS {
	public:
		FS(double r=0.0,double i=0.0) {
			real=r;
			imag=i;
//			cout<<"复数对象创建并设置初始值"<<endl;
		}
		~FS() {
//			cout<<"复数对象结束"<<endl;
		}
		void set(double r=0.0,double i=0.0) {
			real=r,imag=i;
		}
		friend FS operator +(FS &a,FS &b);
		friend FS operator -(FS &a,FS &b);
		friend FS operator *(FS &a,FS &b);
		friend FS operator /(FS &a,FS &b);
		friend ostream & operator<<(ostream &out,const FS &b);
		void out(void);
	private:
		double real;
		double imag;
};
FS operator +(FS &a,FS &b) {
	return(FS(a.real+b.real,a.imag+b.imag));
}
FS operator -(FS &a,FS &b) {
	return(FS(a.real-b.real,a.imag-b.imag));
}
FS operator *(FS &a,FS &b) {
	FS temp;
	temp.real=a.real*b.real-a.imag*b.imag;
	temp.imag=a.real*b.imag+a.imag*b.real;
	return(FS(a.real*b.real-a.imag*b.imag,a.real*b.imag+a.imag*b.real));
}
FS operator /(FS &a,FS &b) {
	FS temp;
	temp.real=a.real*b.real+a.imag*b.imag;
	temp.imag=a.imag*b.real-a.real*b.imag;
	return(temp);
}
ostream& operator<<(ostream &out,const FS &b){
	out<<b.real;
	if(b.imag>0)out<<"+";
	out<<b.imag<<"i"<<endl;
	return out;
}
void FS::out() {
//	cout<<real;
//	if(imag>0)cout<<"+";
//	cout<<imag<<"i"<<endl;
}
int main() {
	FS a(2,-1),b(-1,2),c;
	cout<<"a="<<a<<endl;
	a.out();
	cout<<"b="<<b<<endl;
	b.out();
	c=a+b;
	cout<<"c=a+bnc="<<c<<endl;
	c.out();
	c=a-b;
	cout<<"c=a-bnc="<<c<<endl;
	c.out();
	c=a*b;
	cout<<"c=a*bnc="<<c<<endl;
	c.out();
	c=a/b;
	cout<<"c=a/bnc="<<c<<endl;
	c.out();
	return 0;
}

(2)
编写一个时间类,实现时间的加、减、读和输出:

#include <iostream>
using namespace std;
class Time {
	public:
		Time(int h=0,int m=0,int s=0) {
			hour=h,minute=m,second=s;
		}
		void SetTime();
		void Display();
		Time operator + (Time &a);
		Time operator - (Time &a);
		friend ostream & operator<<(ostream &out,const Time &b);
	private:
		int hour,minute,second;
};
void Time::SetTime() {
	do {
		cout<<"input hour:"<<endl;
		cin>>hour;
	} while(hour<=0||hour>24);
	do {
		cout<<"input minute:"<<endl;
		cin>>minute;
	} while(minute<0||minute>=60);
	do {
		cout<<"input second:"<<endl;
		cin>>second;
	} while(second<0||second>=60);
}
void Time::Display() {
//	cout<<hour<<":"<<minute<<":"<<second<<endl;
}
Time Time::operator + (Time &a) {
	int h,m,s;
	h=hour,m=minute,s=second;
	if(second+a.second>=60) {
		s=second+a.second-60;
		m=minute+1;
	} else s=s+a.second;
	if(m+a.minute>=60) {
		m=m+a.minute-60;
		h=hour+1;
	} else m=m+a.minute;
	if(h+a.hour>24) {
		h=h+a.hour-24;
	} else h=h+a.hour;
	return(Time(h,m,s));
}
Time Time::operator - (Time &a) {
	int h,m,s;
	h=hour,m=minute,s=second;
	if(second-a.second<0) {
		s=second-a.second+60;
		m=minute-1;
	} else s=second-a.second;
	if(m-a.minute<0) {
		m=m-a.minute+60;
		h=hour-1;
	} else m=m-a.minute;
	if(h-a.hour<0) {
		h=h-a.hour+24;
	} else h=h-a.hour;
	return(Time(h,m,s));
}
ostream & operator<<(ostream &out,const Time &b){
	out<<"("<<b.hour<<':'<<b.minute<<':'<<b.second<<")";
	return out;
}
int main() {
	Time a,b,c;
	cout<<"请输入A的时间:"<<endl;
	a.SetTime();
	cout<<"请输入B的时间:"<<endl;
	b.SetTime();
	cout<<"A的时间为:"<<a<<endl;
	a.Display();
	cout<<"B的时间为:"<<b<<endl;
	b.Display();
	cout<<"c=a+b=";
	c=a+b;
	cout<<c<<endl;
	c.Display();
	cout<<endl<<"c=a-b=";
	c=a-b;
	cout<<c<<endl;
	c.Display();
	return 0;
}

最后

以上就是等待烤鸡为你收集整理的c++学习运算符重载的全部内容,希望文章能够帮你解决c++学习运算符重载所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部