这里写自定义目录标题
今天复习的是运算符的重载,运算符的重载可以使我们自定义的类型更像是一个内置类型,可以便捷的使用 +,-,*,/,++,–,<<,>>等符号直接进行操作。
我实现的是一个复数类型的重载
Complex.h 里边是复数类的函数声明
#pragma once
#include<iostream>
class Complex
{
public:
Complex(): _real(0.0),_virtual(0.0) { }; //构造函数
Complex(double r, double i) : _real(r), _virtual(i) //构造函数
{
std::cout << "构造函数被调用" << std::endl;
}
Complex(const Complex& other):_real(other._real),_virtual(other._virtual) //拷贝构造函数
{
std::cout << "拷贝构造函数被调用"<<std::endl;
}
Complex(Complex&& other)noexcept :_real(other._real), _virtual(other._virtual) //移动拷贝构造函数
{
std::cout << "移动拷贝构造函数被调用" << std::endl;
}
virtual ~Complex() {
std::cout << "析构函数被调用" << std::endl;
}
double get_real()const; //访问实部
void set_real(double x);//设置实部
double get_virtual()const; //访问虚部
void set_virtual(double y); //设置虚部
void print_complex()const; //打印函数
//运算符重载
//返回类型 operator 运算符符号 (参数列表)
// {
// // 运算符实现
// }
//注意 函数声明后边带 const 表示不改变函数的成员变量
Complex operator + (const Complex& x)const; //重载+
Complex operator - (const Complex& x)const; //重载-
Complex operator * (const Complex& x)const; //重载*
Complex operator / (const Complex& x)const; //重载/
Complex& operator = (const Complex& x); //重载=
Complex& operator ++ (); //前置
Complex operator ++(int);//后置++
Complex& operator -- ();//前置--
Complex operator --(int);//后置--
friend std::ostream & operator << (std::ostream & os, const Complex& x); //友元函数 使用ostream 重载 <<
friend std::istream& operator >> (std::istream& is, Complex& x); //友元函数 使用istream 重载 <<
private:
double _real;
double _virtual;
};
Complex.cpp
#include "Complex.h"
#include<iostream>
double Complex::get_real()const { return _real; }
void Complex::set_real(double x) { _real = x; }
double Complex::get_virtual()const { return _virtual; }
void Complex::set_virtual(double y) { _virtual = y; }
void Complex::print_complex() const{ std::cout << _real << "+" << _virtual<<"i"<<std::endl; }
Complex Complex:: operator + (const Complex& x) const { return Complex(_real+x.get_real() , _virtual+x.get_virtual() ); }
Complex Complex:: operator - (const Complex& x)const{ return Complex(_real - x.get_real(), _virtual - x.get_virtual()); }
//复数*运算公式 (a + bi)(c + di) = (ac - bd) + (ad + bc)i
Complex Complex:: operator * (const Complex& x)const{
return Complex(_real * x._real- _virtual * x._virtual,_real*x. _virtual +_virtual* x._real);
}
//复数的除法公式 (a + bi) / (c + di) = [(ac + bd) + (bc - ad)i] / (c² + d²),确保分母不为零。
Complex Complex:: operator / (const Complex& x)const {
double denominator = x._real * x._real + x._virtual * x._virtual;
if (denominator == 0) {
std::cerr << "Error: Division by zero!" << std::endl;
return Complex(_real,_virtual);
}
return Complex((_real * x._real + _virtual * x._virtual)/denominator, (_virtual * x._real - _real * x._virtual)/denominator);
}
// 每一个对象都有一个this指针,通过*this可以返回当前对象,这样可以不用出现临时对象
Complex& Complex::operator = (const Complex& x) {
if(this != &x)
{
_real = x._real;
_virtual = x._virtual;}
return *this;
}
Complex& Complex::operator ++ ()//前置++ ++a
{
_real++;
_virtual++;
return *this;
}
//Complex Complex::operator ++(int)//后置++ a++
//{
// Complex tmp(*this);
// _real++;
// _virtual++;
// return tmp;
//}
Complex Complex::operator ++(int)//后置++ a++
{
return Complex(_real++,_virtual++);
}
Complex& Complex::operator -- ()//前置++ ++a
{
_real--;
_virtual--;
return *this;
}
Complex Complex::operator --(int)//后置++ a++
{
return Complex(_real--, _virtual--);
}
std::ostream& operator<<(std::ostream& os, const Complex& x)
{
os << x._real;
if (x._virtual >= 0) {
os << " + " << x._virtual << "i";
}
else {
os << " - " << -x._virtual << "i";
}
return os;
}
std::istream& operator >> (std::istream& is, Complex& x)
{
is >> x._real >> x._virtual;
return is;
}
test.cpp
#include"Complex.h"
#include<iostream>
using namespace std;
int main()
{
Complex a(1.0, 2.0);
a.print_complex() ;
Complex c = a + a;
c.print_complex();
Complex d = c - a;
d.print_complex();
Complex e = a * a;
e.print_complex();
Complex f = a / a;
f.print_complex();
Complex g(a);
g.print_complex();
d = a++;
d.print_complex();
a.print_complex();
e = ++a;
e.print_complex();
a.print_complex();
cout << a << endl;
cin >>e;
cout << e << endl;
return 0;
}
如果我的后置++定义为
Complex Complex::operator ++(int)//后置++ a++
{
Complex tmp(*this);
_real++;
_virtual++;
return tmp;
}

这个涉及到 拷贝构造函数和移动拷贝构造函数,可以去我前边的文章看–》》拷贝构造和移动拷贝构造
450

被折叠的 条评论
为什么被折叠?



