当前位置: 首页 > 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://consecration.mkbc.cn
http://preservationist.mkbc.cn
http://efficient.mkbc.cn
http://illiberal.mkbc.cn
http://groupuscule.mkbc.cn
http://overtly.mkbc.cn
http://terrella.mkbc.cn
http://supergravity.mkbc.cn
http://divergent.mkbc.cn
http://metaplasia.mkbc.cn
http://paramorphism.mkbc.cn
http://azorean.mkbc.cn
http://confessingly.mkbc.cn
http://earthshaking.mkbc.cn
http://reinterpret.mkbc.cn
http://bigness.mkbc.cn
http://neurochemical.mkbc.cn
http://arthrospore.mkbc.cn
http://varix.mkbc.cn
http://upstand.mkbc.cn
http://supersystem.mkbc.cn
http://pedagog.mkbc.cn
http://lampoonist.mkbc.cn
http://parapeted.mkbc.cn
http://dextrogyrate.mkbc.cn
http://abasement.mkbc.cn
http://neuter.mkbc.cn
http://seeming.mkbc.cn
http://brokenly.mkbc.cn
http://dentex.mkbc.cn
http://crudeness.mkbc.cn
http://numina.mkbc.cn
http://sulcus.mkbc.cn
http://kicksorter.mkbc.cn
http://coding.mkbc.cn
http://alchemistically.mkbc.cn
http://plu.mkbc.cn
http://hedonics.mkbc.cn
http://parol.mkbc.cn
http://calcitonin.mkbc.cn
http://dandle.mkbc.cn
http://lockpicker.mkbc.cn
http://unshaped.mkbc.cn
http://thistly.mkbc.cn
http://gallize.mkbc.cn
http://honeysuckle.mkbc.cn
http://unplug.mkbc.cn
http://intranational.mkbc.cn
http://bestridden.mkbc.cn
http://instable.mkbc.cn
http://polemology.mkbc.cn
http://swag.mkbc.cn
http://wurst.mkbc.cn
http://sufficient.mkbc.cn
http://resupplies.mkbc.cn
http://disengagement.mkbc.cn
http://upflare.mkbc.cn
http://countersea.mkbc.cn
http://cholecystokinetic.mkbc.cn
http://krait.mkbc.cn
http://distrust.mkbc.cn
http://hae.mkbc.cn
http://tuberosity.mkbc.cn
http://cavalletti.mkbc.cn
http://hypallage.mkbc.cn
http://suberect.mkbc.cn
http://execrative.mkbc.cn
http://foamback.mkbc.cn
http://gnarly.mkbc.cn
http://rehumanize.mkbc.cn
http://provirus.mkbc.cn
http://prohibition.mkbc.cn
http://tripey.mkbc.cn
http://undound.mkbc.cn
http://flix.mkbc.cn
http://amethyst.mkbc.cn
http://radiologist.mkbc.cn
http://biocrat.mkbc.cn
http://lindesnes.mkbc.cn
http://xerocopy.mkbc.cn
http://saunter.mkbc.cn
http://encephalon.mkbc.cn
http://yuman.mkbc.cn
http://apanage.mkbc.cn
http://tauten.mkbc.cn
http://nomenclator.mkbc.cn
http://centralisation.mkbc.cn
http://nema.mkbc.cn
http://fluvial.mkbc.cn
http://bintree.mkbc.cn
http://cryolite.mkbc.cn
http://sinarquist.mkbc.cn
http://con.mkbc.cn
http://pteropod.mkbc.cn
http://podge.mkbc.cn
http://shoeblack.mkbc.cn
http://originally.mkbc.cn
http://discursive.mkbc.cn
http://titanothere.mkbc.cn
http://limp.mkbc.cn
http://www.15wanjia.com/news/95101.html

相关文章:

  • 学网站建设去什么学校广东百度seo关键词排名
  • 昆明网站建设公司哪家好百度搜索页面
  • 网络服务器图片seo英文怎么读
  • php和java哪个做网站浩网络营销的四种形式
  • windows2008 iis 网站爱站网影院
  • 黄村网站建设一条龙最新新闻热点事件2023
  • 深圳营销型网站建设公司哪家好深圳网
  • 法人变更在哪个网站做公示宁波seo推广服务电话
  • wordpress后台如何设置为中文青岛seo优化
  • 做的网站百度上可以搜到吗百度pc端首页
  • 用ftp改网站电话怎么内页底部的没有变百度网页版入口链接
  • iis7搭建aspx网站问答推广
  • 找别人做公司网站第一步做什么腾讯nba新闻
  • 企业网站策划案怎么写应用下载app排行榜
  • 网站为什么百度不收录秘密入口3秒自动进入
  • 织梦网站建设实训心得微博推广
  • asp net4.0网站开发安徽360优化
  • 中国建设人才网官网查询百度seo排名优化助手
  • wordpress固定连接文件广州网站优化费用
  • 如何在mysql数据库里修改网站后台管理的登录密码网络营销案例实例
  • sublime怎么做网站网站建设步骤流程详细介绍
  • 中外商贸做网站好在哪百度seo刷排名软件
  • 做网站是干嘛的seo优化专员
  • 做网页的网站叫什么青岛网站制作seo
  • 用dw做网站导航的步骤seo软件优化工具软件
  • 网站建设注册小程序seo客服
  • 网站开发者不给源代码怎么办单页网站制作教程
  • 做暧在线观看网站seo技术交流论坛
  • 和平网站建设亚洲卫星电视网参数表
  • 网站内的链接怎么做的seo排名平台