程序小白天天打卡

2018/2/6

C++

 1.关于运算符的重载

  1.运算符的重载的意义是实现运算符的重新操作

  2.重载不能改变运算符运算对象(操作数)的个数

3.重载不能改变运算符的优先级别

4.重载不能改变运算符的结合性

  5.重载运算符的函数不能有默认参数

6.重载运算符必须和用户自定义的类型一起使用、

2.注意:有五个运算符不允许重载

 -.(成员访问运算符)

- .*(成员访问指针运算符)

-::(域运算符)

-sizeof(尺寸运算符)

-?;(基于三元运算的条件运算符) 

3.text:用运算符的重载实现复数的加减

/*运算符的重载*/
/*text1 实现复数的加法*/
#include<iostream>
#include<stdlib.h>
using namespace std;
class Complex
{public:
	Complex();
	Complex(double a, double b);
	Complex operator+(Complex &d);
	void print();
protected:
	double real;//实部
	double imag;//虚部

};
/*初始化*/
Complex::Complex()
{
	real = 0;
	imag = 0;
}
Complex::Complex(double a, double b)
{
	/*实部虚部赋值*/
	real = a;
	imag = b;
	
}
 Complex Complex::operator+(Complex &d)
{
	Complex num;
	num.real = real + d.real;
	num.imag = imag + d.imag;
	return num;
}
  void Complex::print()
 {
	  cout << "(" << real << "," << imag << "i" << ")" << endl;
 }
int main()
  {
	Complex c1(3, 4), c2(5, -10), c3;
	c3 = c1 + c2;//c3=c1.operator+c2,已经是重载后的加号了
	cout << "c1=";
	c1.print();
	cout << "c2=";
	c2.print();
	cout << "c1+c2=";
	c3.print();
	system("pause");

  }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值