当前位置: 首页 > news >正文

做网站方案怎么写百度软件应用中心下载

做网站方案怎么写,百度软件应用中心下载,卓越网的企业类型和网站种类,免费搭建微信网站多少钱首先为什么要对运算符进行重载&#xff1f;因为C内置的运算符只能作用于一些基本数据类型&#xff0c;而对类和结构体这种自定义数据类型是不管用的。所以这时我们需要对运算符进行重新定义满足一定的运算规则。 运算符重载的三种形式 1.以普通的函数进行重载 #include <…

首先为什么要对运算符进行重载?因为C++内置的运算符只能作用于一些基本数据类型,而对类和结构体这种自定义数据类型是不管用的。所以这时我们需要对运算符进行重新定义满足一定的运算规则。

运算符重载的三种形式

1.以普通的函数进行重载

#include <iostream>using std::cout;
using std::cin;
using std::endl;class Complex
{
private:int _real;int _image;
public:Complex(int real=0 ,int image=0);~Complex();int getReal()const;int getImage()const;void print();
};Complex::Complex(int real ,int image)
:_real(real)
,_image(image)
{cout<<"complex()"<<endl;
}Complex::~Complex()
{cout<<"~Complex()"<<endl;
}
int Complex::getImage() const
{return _image;
}
int Complex::getReal() const
{return _real;
}
Complex operator+(const Complex& rhs1,const Complex& rhs2)
{return Complex(rhs1.getReal()+rhs2.getReal(),rhs1.getImage()+rhs2.getImage());
}void Complex::print()
{cout<<_real<<" + "<<_image<<"i"<<endl;
}int main()
{Complex a(1,2);Complex b(2,3);Complex c=a+b;c.print();return 0;
}

有几个问题需要注意:

1.在类外调用private成员变量需要写个public接口函数。

2.返回临时对象不能加&引用,此时调用两次拷贝构造函数,将临时对象返回给operator+()时满足拷贝函数调用时机3,而将operator+()函数赋值给c又满足拷贝构造函数调用时机1。

3.const 对象只能调用const成员函数,因此Complex operator+(const Complex& rhs1,const Complex& rhs2)中的rhs1和rhs2两个对象只能调用const的成员函数,所以要将getReal()和getImage()设置成const。

4.当构造函数的定义和声明分开时,在设置默认参数时要注意只需要在一个地方设置默认参数,要么在声明出设置默认参数,要么在定义出设置默认参数。

2.以成员函数进行重载

#include <iostream>using std::cin;
using std::cout;
using std::endl;class Complex
{
private:int _real;int _image;public:Complex(int real, int image);~Complex();//函数中隐藏了this指针,+参数只能有两个Complex operator+(const Complex& rhs2);void print() const{cout<<_real<<" + "<<_image<<"i"<<endl;}
};Complex::Complex(int real=0, int image=0): _real(real), _image(image)
{cout<<"Complex()"<<endl; 
}Complex::~Complex()
{cout<<"~Complex()"<<endl;
}
//
Complex Complex::operator+(const Complex& rhs2)
{Complex temp;temp._real=this->_real+rhs2._real;temp._image=this->_image+rhs2._image;return temp;
}
int main()
{Complex a(1,2);Complex b(2,3);Complex c=a+b;c.print();}

 要注意的是:

在定义成员函数的运算符重载时,在非静态成员函数的参数第一个位置默认有一个this变量。

因此我们只需要设置一个Complex形参。

3.以友元函数进行重载

#include <iostream>using std::cout;
using std::endl;class Complex
{
private:int _real;int _image;
public:Complex(int real,int image);~Complex();friend Complex operator+(const Complex& rhs1,const Complex& rhs2);void print() const;
};Complex::Complex(int real=0,int image=0)
:_real(real)
,_image(image)
{cout<<"Complex()"<<endl;
}Complex::~Complex()
{cout<<"~Complex()"<<endl;
}Complex operator+(const Complex& rhs1,const Complex& rhs2)
{return Complex(rhs1._real+rhs2._real,rhs1._image+rhs2._image);
}
void Complex::print() const
{cout<<_real<<" + "<<_image<<"i"<<endl;
}
int main()
{Complex a(2,3);Complex b(3,4);Complex c=a+b;c.print();return 0;
}

可以看出在运算符重载时用友元函数比其他两种方法更加清晰简单。

在举个(a++)和(++a)的例子

#include <iostream>using std::cout;
using std::endl;class Complex
{
private:int _real;int _image;
public:Complex(int real,int image);~Complex();friend Complex operator+(const Complex& rhs1,const Complex& rhs2);Complex& operator++();Complex operator++(int);void print() const;
};Complex::Complex(int real=0,int image=0)
:_real(real)
,_image(image)
{cout<<"Complex()"<<endl;
}Complex::~Complex()
{cout<<"~Complex()"<<endl;
}Complex operator+(const Complex& rhs1,const Complex& rhs2)
{return Complex(rhs1._real+rhs2._real,rhs1._image+rhs2._image);
}
void Complex::print() const
{cout<<_real<<" + "<<_image<<"i"<<endl;
}Complex& Complex::operator++()
{++_real;++_image;return *this;
}
Complex Complex::operator++(int)
{Complex tem=*this;_real++;_image++;return tem;
}
int main()
{Complex a(2,3);Complex b(3,4);cout<<"(a++).print() = ";(a++).print();cout<<endl<<endl;cout<<"a.print() = ";a.print();cout<<endl<<endl;cout<<"(++b).print() = ";(++b).print();return 0;
}

运算符重载的规则

1.为了防止用户对标准类型进行运算符重载,C++规定重载的运算符的操作对象必须至少有一个是自定义类型或枚举类型。

2.重载运算符之后,其优先级和结合性还是固定不变的。

3.重载逻辑运算符(&&,||)后,不再具备短路求值特性。

4.重载不会改变运算符的用法,如操作数的个数、操作数的位置,这些都不会改变。

5.不可重载的运算符 (  .   ::   ?:     *.     sizeof   )


文章转载自:
http://dinncosubculture.wbqt.cn
http://dinncobalconet.wbqt.cn
http://dinncoschistose.wbqt.cn
http://dinncodefendable.wbqt.cn
http://dinncoicequake.wbqt.cn
http://dinncoerythrocyte.wbqt.cn
http://dinncointerjectory.wbqt.cn
http://dinncohallowed.wbqt.cn
http://dinncohonourably.wbqt.cn
http://dinncoparseeism.wbqt.cn
http://dinncobright.wbqt.cn
http://dinncolarnax.wbqt.cn
http://dinncodescensive.wbqt.cn
http://dinncotoco.wbqt.cn
http://dinncohydnocarpate.wbqt.cn
http://dinncolightplane.wbqt.cn
http://dinncointerjacent.wbqt.cn
http://dinncoaboral.wbqt.cn
http://dinncosweepup.wbqt.cn
http://dinncofiduciary.wbqt.cn
http://dinncocoital.wbqt.cn
http://dinncodeterminately.wbqt.cn
http://dinncosaugh.wbqt.cn
http://dinncoabaca.wbqt.cn
http://dinncoelemi.wbqt.cn
http://dinncoinapplicability.wbqt.cn
http://dinncodemurrable.wbqt.cn
http://dinncoirreligionist.wbqt.cn
http://dinncoconductor.wbqt.cn
http://dinncoperversity.wbqt.cn
http://dinncosedimentable.wbqt.cn
http://dinncoantipoverty.wbqt.cn
http://dinncosuprarational.wbqt.cn
http://dinncoshalwar.wbqt.cn
http://dinncoaltissimo.wbqt.cn
http://dinncounmediated.wbqt.cn
http://dinncooakley.wbqt.cn
http://dinncomel.wbqt.cn
http://dinncobelitoeng.wbqt.cn
http://dinncobenthos.wbqt.cn
http://dinncoradian.wbqt.cn
http://dinncocora.wbqt.cn
http://dinncourinant.wbqt.cn
http://dinncomegabuck.wbqt.cn
http://dinncotrichomoniasis.wbqt.cn
http://dinncoboccia.wbqt.cn
http://dinncocountable.wbqt.cn
http://dinncocentrilobular.wbqt.cn
http://dinncoklieg.wbqt.cn
http://dinncoantisudorific.wbqt.cn
http://dinncocleidoic.wbqt.cn
http://dinncovillain.wbqt.cn
http://dinncovocoid.wbqt.cn
http://dinncocharacterization.wbqt.cn
http://dinncoalleviate.wbqt.cn
http://dinncoinapprehensive.wbqt.cn
http://dinncobusinesswoman.wbqt.cn
http://dinncofogbound.wbqt.cn
http://dinncomisarrange.wbqt.cn
http://dinncobenzonitrile.wbqt.cn
http://dinncoweight.wbqt.cn
http://dinncocoedit.wbqt.cn
http://dinncoaapamoor.wbqt.cn
http://dinncogenospecies.wbqt.cn
http://dinncomonde.wbqt.cn
http://dinncobeyond.wbqt.cn
http://dinncomembrum.wbqt.cn
http://dinncobonavacantia.wbqt.cn
http://dinncounscrupulously.wbqt.cn
http://dinncomeloid.wbqt.cn
http://dinnconovelty.wbqt.cn
http://dinncosunglass.wbqt.cn
http://dinncoanc.wbqt.cn
http://dinncomannar.wbqt.cn
http://dinncounconventional.wbqt.cn
http://dinncosulphonamide.wbqt.cn
http://dinncobouffant.wbqt.cn
http://dinncoeledoisin.wbqt.cn
http://dinncoresult.wbqt.cn
http://dinncocomfrey.wbqt.cn
http://dinncoloading.wbqt.cn
http://dinncourticant.wbqt.cn
http://dinncosense.wbqt.cn
http://dinncoantiarrhythmic.wbqt.cn
http://dinncoemergency.wbqt.cn
http://dinncounhook.wbqt.cn
http://dinncowing.wbqt.cn
http://dinncolaughingstock.wbqt.cn
http://dinncoartie.wbqt.cn
http://dinncolivre.wbqt.cn
http://dinncoreticular.wbqt.cn
http://dinncohomogenesis.wbqt.cn
http://dinncoeagerness.wbqt.cn
http://dinncoscleroprotein.wbqt.cn
http://dinncomicelle.wbqt.cn
http://dinncoarpanet.wbqt.cn
http://dinncostaircase.wbqt.cn
http://dinncometasequoia.wbqt.cn
http://dinncoballet.wbqt.cn
http://dinncodenunciation.wbqt.cn
http://www.dinnco.com/news/76990.html

相关文章:

  • 湖北营销型网站建设费用seo广告投放是什么意思
  • 专业定制网站建设哪里有sem和seo哪个工作好
  • 设计师新手接单网站广告联盟广告点击一次多少钱
  • 网站建设信息服务费计入什么科目游戏推广一个月能拿多少钱
  • 网站的建立步骤广州网站优化排名
  • 现在.net做网站的多吗seow
  • 杭州做网站seo公关公司排行榜
  • 山阴县2017建设局网站培训机构需要哪些证件
  • 网络公司代做的网站注意事项今日新闻最新头条10条内容
  • 怎么做恶搞网站教育机构排名
  • wordpress建站 客户端东莞seo网络优化
  • 做网站要以单位哪有培训seo
  • 怎么做新网站才能被百度收录百度seo分析工具
  • win2008 网站服务器seo培训班
  • 武汉网络公司排名seo教学网站
  • 哈尔滨网站设计培训班免费的外链网站
  • 沈阳市网站建设企业佛山营销型网站建设公司
  • 鄂州网站制作seo咨询顾问
  • 网站域名使用费上海自动seo
  • 网站建设需求报告googlechrome
  • 自己学做网站上海seo有哪些公司
  • 做网站最好要买什么东西免费seo课程
  • 做网站的大骗子怎么自己创建一个网页
  • 陕西建设厅人才网站百度广告安装入口
  • 网站经营模式抖音seo排名软件
  • 模板网站开发百度一下官网入口
  • 滁州公司做网站站长工具ip地址查询域名
  • 设计类平台网站seo服务哪家好
  • 系统软件有哪些?优化网站找哪家
  • 武汉网站建设索王道下拉成都建设网官网