西安石油大学C++上机实验 上机二:友元和运算符重载程序设计(2学时)

上机二:友元和运算符重载程序设计(2学时)

实验目的:

了解友元函数的概念。

掌握友元函数的定义和使用方法。

了解运算符重载的概念和使用方法。

掌握几种常用的运算符重载的方法。

实验内容

编写一个时间类,实现时间的加、减运算并输出。

上机内容

1.练习友元函数的定义和运算符重载的方法;

2.测试章节例题,设计运算符重载的复数类,实现常用复数运算操作,分别重载为类的成员函数和重载为类的友元函数,并编写一个测试程序进行测试。

(4)编辑头文件。在弹出的 Complex.h 头文件中添加 Complex 类的代码,
完成如下。

步骤 a:声明程序设计所需要包含的头文件:
#include <iostream.h>
步骤 b:根据题目要求:定义复数类:Complex 类:
class Complex
{
    
    
……
};
步骤 c:定义类中成员变量:
private:
double real; //实部
double image; //虚部
步骤 d:将加法运算符重载为类的友元函数:
friend Complex operator+(const Complex &x,const Complex &y); //类内函
数说明
23
inline Complex operator+(const Complex &x,const Complex &y); //类外函
数实现 
{
    
    
return Complex(x.real+y.real,x.image+y.image);
}
步骤 e:将减法运算符重载为类的友元函数:
friend Complex operator-(const Complex &x,const Complex &y); //类内函数
说明
inline Complex operator-(const Complex &x,const Complex &y) //类外函
数实现 
{
    
    
return Complex(x.real-y.real,x.image-y.image);
}
步骤 f:定义成员函数:
构造函数:
Complex(double x=0, double y=0):real(x),image(y){
    
    } 
析构函数:
~ Complex (){
    
    }
将乘法运算符重载为类的成员函数:
Complex operator*(const Complex &x) const; //类内函数声明
inline Complex Complex::operator*(const Complex &x) const //类外函数实{
    
    
return Complex(real*x.real-image*x.image,real*x.image+image*x.real);
}
将除法运算符重载为类的成员函数:
Complex operator/(const Complex &x) const; //类内函数声明
inline Complex Complex::operator/(const Complex &x) const //类外函数实现 
24
{
    
    
double m;
m=x.real*x.real+x.image*x.image;
 return Complex((real*x.real+image*x.image)/m,
(image*x.real-real*x.image)/m);
}
输出成员函数:
void Show(); //类内函数声明
void Complex::Show() //类外函数实现 
{
    
    
cout<<"("<<real<<","<<image<<"i)"<<endl;
}6)添加源文件代码。在 Complextest.cpp 文件中添加主程序代码,详细代
码如下:
#include "Complex.h" //包含 Complex.h 头文件
void main()
{
    
    
Complex a(1,2),b(2,3),c,d,e,f; 
c=a+b; 
cout<<"两个复数之和为:"<<endl;
c.Show(); 
d=a-b; 
cout<<"两个复数之差为:"<<endl;
d.Show(); 
 e=a*b; 
cout<<"两个复数的乘积为:"<<endl;
e.Show();
26
 f=a/b; 
cout<<"两个复数的除法运算结果为:"<<endl;
f.Show();
}

在这里插入图片描述
在这里插入图片描述

实验内容

编写一个时间类,实现时间的加、减运算并输出。

#include <iostream>
#include <iomanip>
using namespace std;

class Time {
    
    
	private://定义时间类中的私有对象hour minute second
		int hour;
		int minute;
		int second;

	public:
		Time(int h = 0, int m = 0, int s = 0) : hour(h), minute(m), second(s) {
    
    };
		Time(const Time &other) : hour(other.hour), minute(other.minute), second(other.second) {
    
    };
		friend istream &operator>>(istream &input, Time &other) {
    
    
			input >> other.hour >> other.minute >> other.second;
			return input;
		}
		friend ostream &operator<<(ostream &output, const Time &other) {
    
    
			output << setw(2) << setfill('0') << other.hour << ":" << setw(2) << setfill('0') << other.minute << ":" << setw(
			           2) << setfill('0') << other.second << endl;
			return output;
		}
		const Time operator++(int) {
    
    
			Time tmp(*this);
			int x = hour * 3600 + minute * 60 + second;
			x++;
			hour = x / 3600;
			x %= 3600;
			minute = x / 60;
			second = x % 60;
			return tmp;
		}
		const Time operator--(int) {
    
    
			Time tmp(*this);
			int x = hour * 3600 + minute * 60 + second;
			x--;
			hour = x / 3600;
			x %= 3600;
			minute = x / 60;
			second = x % 60;
			return tmp;
		}
		Time operator+=(const Time &other) {
    
    
			second += other.second;
			minute += other.minute;
			hour += other.hour;
			if (second >= 60) {
    
    
				minute += second / 60;
				second %= 60;
			}
			if (minute >= 60) {
    
    
				hour += minute / 60;
				minute %= 60;
			}
			if (hour >= 24) {
    
    
				hour %= 24;
			}
			return *this;
		}
		Time operator-=(const Time &other) {
    
    
			if (second < other.second) {
    
    
				second += 60;
				second -= other.second;
				minute--;
			} else {
    
    
				second -= other.second;
			}
			if (minute < other.minute) {
    
    
				minute += 60;
				minute -= other.minute;
				hour--;
			} else {
    
    
				minute -= other.minute;
			}
			if (hour < other.hour) {
    
    
				hour += 24;
				hour -= other.hour;
			} else {
    
    
				hour -= other.hour;
			}

			return *this;
		}
		Time &operator++() {
    
    
			int x = hour * 3600 + minute * 60 + second;
			x++;
			hour = x / 3600;
			x %= 3600;
			minute = x / 60;
			second = x % 60;
			return *this;
		}
		Time &operator--() {
    
    
			int x = hour * 3600 + minute * 60 + second;
			x--;
			hour = x / 3600;
			x %= 3600;
			minute = x / 60;
			second = x % 60;
			return *this;
		}
};

int main() {
    
    
	Time t1, t2;
	cin >> t1;
	cin >> t2;
	cout << (t1 += (t2++));
	cout << (t1 -= t2);
	cout << ++t2;
	cout << (t2 += (t1--));
	cout << --t1;
	cout << (t2 -= t1);
}

思考题

分析总结上述程序设计实现的不足之处,并思考复数类其他运算符(如+=,==等)重载如何实现

猜你喜欢

转载自blog.csdn.net/shaozheng0503/article/details/130156293
今日推荐