Xi'an Petroleum University C++ computer experiment on the computer II: friend and operator overloading programming (2 credit hours)

Hands-on II: Friend and Operator Overloading Programming (2 credit hours)

Purpose:

Understand the concept of friend functions.

Master the definition and usage of friend functions.

Understand the concept and use of operator overloading.

Master several commonly used methods of operator overloading.

Experimental content

Write a time class to implement the addition and subtraction of time and output it.

On-board content

1. Practice the definition of friend functions and the method of operator overloading;

2. Test chapter examples, design a complex class with operator overloading, implement common complex number operations, overload as member functions of the class and overload as friend functions of the class, and write a test program for testing.

(4) Edit the header file. Add the code of the Complex class in the pop-up Complex.h header file,
as follows.

步骤 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();
}

insert image description here
insert image description here

Experimental content

Write a time class to implement the addition and subtraction of time and output it.

#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);
}

thinking questions

Analyze and summarize the deficiencies of the above program design implementation, and think about how to implement overloading of other complex operators (such as +=, ==, etc.)

Guess you like

Origin blog.csdn.net/shaozheng0503/article/details/130156293