#include <iostream> using namespace std; #ifndef CCOMPLEX_H #define CCOMPLEX_H class CComplex { public: CComplex(float r = 0.0f, float i = 0.0f): real(r), imag(i) { cout << "默认构造函数调用\n"; } CComplex(const CComplex & src); virtual ~CComplex() { cout << "虚析构函数调用!\n"; } CComplex & operator[](int index); CComplex & operator=(const CComplex & src); CComplex operator*(const CComplex & src); CComplex operator+(const CComplex & src); CComplex operator-(const CComplex & src); CComplex operator/(const CComplex & src); CComplex operator++();//前置自增 CComplex operator++(int x);//后置自增 CComplex operator--();//前置自减 CComplex operator--(int x);//后置自减 CComplex operator()(float r, float i);//重载圆括号 CComplex operator()(float r);//重载圆括号 friend bool operator==(const CComplex & dest, const CComplex & src); friend bool operator!=(const CComplex & dest, const CComplex & src); friend ostream & operator<<(ostream &out, const CComplex & src); friend istream & operator>>(istream &in, CComplex & src); private: float real; float imag; }; #endif // CCOMPLEX_H #include "CComplex.h" #include <iostream> using namespace std; #include <assert.h> CComplex::CComplex(const CComplex & src) { real = src.real; imag = src.imag; cout << "复制构造函数调用\n"; } //-----------------------------重载=------------------------- CComplex & CComplex::operator=(const CComplex & src) { real = src.real; imag = src.imag; cout << "重载赋值运算符调用!\n"; return (*this); } //----------------------------- 重载* ------------------------- CComplex CComplex::operator*(const CComplex & src) { CComplex tmp(real * src.real - imag * src.imag, real * src.real + imag * src.imag); return tmp; } //----------------------------- 重载+ ------------------------- CComplex CComplex::operator+(const CComplex & src) { CComplex tmp(real+src.real, imag+src.imag); return tmp; } //----------------------------- 重载- ------------------------- CComplex CComplex::operator-(const CComplex & src) { CComplex tmp(real-src.real, imag-src.imag); return tmp; } //----------------------------- 重载/ ------------------------- CComplex CComplex::operator/(const CComplex & src) { float tmp = src.real * src.real + src.imag * src.imag; CComplex ctmp((real * src.real + imag * src.imag) / tmp, (real * src.real - imag * src.imag) / tmp); return ctmp; } //----------------------------- 重载== ------------------------- bool operator==(const CComplex & dest, const CComplex & src) { return (dest.real == src.real && dest.imag == src.imag); } //----------------------------- 重载!= ------------------------- bool operator!=(const CComplex & dest, const CComplex & src) { return !(dest.real == src.real && dest.imag == src.imag); } //----------------------------- 重载[] ------------------------- CComplex& CComplex::operator[](int index) { assert(index >= 0 && index <= 100); return this[index]; } //----------------------------- 重载<< ------------------------- ostream & operator<<(ostream &out, const CComplex & src) { out << src.real; src.imag < 0 ? (out << "-") : (out << "+"); out << src.imag << "i"; return out; } //----------------------------- 重载>> ------------------------- istream & operator>>(istream &in, CComplex & src) { in >> src.real >> src.imag; return in; } //前置自增 CComplex CComplex::operator++() { real ++; imag ++; return *this; } //后置自增 CComplex CComplex::operator++(int ) { CComplex tmp(real, imag); real ++; imag ++; return tmp; } //前置自减 CComplex CComplex::operator--() { real --; imag --; return *this; } //后置自减 CComplex CComplex::operator--(int x) { CComplex tmp(real, imag); real --; imag --; return tmp; } //重载圆括号 CComplex CComplex::operator()(float r, float i) { real = r; imag = i; return *this; } //重载圆括号 CComplex CComplex::operator()(float r) { real = r; imag = 0; return *this; } #include <iostream> using namespace std; #include <conio.h> #include "CComplex.h" int main() { CComplex c1(1, 1), c2(2, -1), c3, c4(c1); c3 = c1; cout << "c3 = c1:" << c3 << endl << endl; cout << "c4(c1) " << c4 << endl << endl; cout << "sizeof(CComplex): " << sizeof(CComplex) << endl << endl; cout << c1 + c2 << endl; cout << c1 - c2 << endl; cout << c1 * c2 << endl; cout << c1 / c2 << endl; cout << (c1 == c2) << endl; cout << (c1 != c2) << endl; cout << "c1: " << c1 << endl; cout << "++c1: " << ++c1 << endl; cout << "c1: " << c1 << endl << endl; cout << "c1++: " << c1++ << endl; cout << "c1: " << c1 << endl << endl; cout << "c1--: " << c1-- << endl; cout << "c1: " << c1 << endl << endl; cout << "--c1: " << --c1 << endl; cout << "c1: " << c1 << endl << endl; CComplex c5,c6; c5(4, 5);//测试圆括号运算符重载 cout << "c5: " << c5 << endl; c6(6); cout << "c6: " << c6 << endl; cout << "输入3个复数" << endl; CComplex c[3]; for (int i = 0; i < 3; i++) cin >> c[i]; cout << "这三个复数是:" << endl; for (int i = 0; i < 3; i++) cout << c[i] << endl; getch(); return 0; }
时间: 2025-01-21 00:06:37