C++面向对象 class eight

这篇博客介绍了C++中运算符重载的使用,包括输入输出重载、加减乘除及自增自减运算符的重载,前缀和后缀++/--操作,以及对下标运算符[]的重载。同时展示了如何实现复数类和复数数组类的这些功能,并给出了相关实例代码。
摘要由CSDN通过智能技术生成
  • 几种运算符重载
  • 输入输出重载
  • 加减乘除自加自减
  • ++ - -,前缀++ - -
  • 对下标运算符重载
  • 对强制类型转换重载
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
class Complex{
public:
	Complex(int _real = 0,int _imag = 0){
		real = _real;
		imag = _imag;
	}
	friend istream& operator>>(istream&,Complex&);
	friend ostream& operator<<(ostream&, Complex&);
	Complex operator+(const Complex &another);
	Complex operator-(const Complex &another);
	Complex operator*(const Complex &another);
	Complex operator/(const Complex &another);
	Complex operator++(int);
	Complex& operator++();
	Complex& operator--();
	Complex operator--(int);
	operator double();
	void show(){
		cout<<real<<"+"<<imag<<"i"<<endl;
	}
private:
	int real,imag;
};
class ComplexArray{
public:
	friend istream& operator>>(istream&,ComplexArray&);
	friend ostream& operator<<(ostream&,ComplexArray&);
	Complex operator[](int index);
private:
int len;
Complex c[101];
};
Complex::operator double(){
	return sqrt(real*real+imag*imag);
}
Complex ComplexArray::operator[](int index){
	if(index<1||index>len) return Complex(0,0);
	return c[index];
}
Complex& Complex::operator++(){
	real++;
	imag++;
	return *this;
}
Complex& Complex::operator--(){
	real--;
	imag--;
	return *this;
}
Complex Complex::operator++(int){
	Complex sum;
	sum.real = real;
	sum.imag = imag;
	real++;
	imag++;
	return sum;
}
Complex Complex::operator--(int){
	Complex sum;
	sum.real = real;
	sum.imag = imag;
	real--;
	imag--;
	return sum;
}
istream& operator>>(istream& is,ComplexArray& ca){
	is>>ca.len;
	for(int i=1;i<=ca.len;i++)
	is>>ca.c[i];
}
ostream& operator<<(ostream& os, ComplexArray& ca){
	os<<ca.len<<endl;
	for(int i=1;i<=ca.len;i++)
	os<<ca.c[i]<<endl;
}
istream& operator>>(istream& is,Complex& c){
	is>>c.real>>c.imag;
	return is;
}
ostream& operator<<(ostream& os, Complex& c){
	cout<<c.real<<"+"<<c.imag<<"i";
	return os;
}
Complex Complex::operator+(const Complex &another){
	Complex sum;
	sum.real=real+another.real;
	sum.imag=imag+another.imag;
	return sum;
}
Complex Complex::operator-(const Complex &another){
	Complex sum;
	sum.real=real-another.real;
	sum.imag=imag-another.imag;
	return sum;
}
Complex Complex::operator*(const Complex &another){
	Complex sum;
	sum.real=real*another.real-(imag*another.imag);
	sum.imag=imag*another.real+real*another.imag;
	return sum;
}
Complex Complex::operator/(const Complex &another){
	Complex sum;
	sum.real=(real*another.real+imag*another.imag)/(another.imag*another.imag+another.real*another.real);
	sum.imag=(imag*another.real-real*another.imag)/(another.imag*another.imag+another.real*another.real);
	return sum;
}
int main()
{
	ComplexArray c;
	Complex b;
	cin>>c;
	b = c[3];
	cout<<(double)b;
//	cout<<c;
//    Complex c,b;
//	cin>>c;
//	b=c--;
//	cout<<c<<endl<<b;
//	b=--c;
//	cout<<c<<endl<<b;
//	cin>>b;
//	c=c+b;
//	cout<<c<<endl;
//	c=c-b;
//	cout<<c<<endl;
//	c=b*b;
//	cout<<c<<endl;
//	c=c/b;
//	cout<<c<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈希表扁豆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值