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

有网络网站打不开怎么回事网络推广方法大全

有网络网站打不开怎么回事,网络推广方法大全,如何给网站做关键字,可以做行程的网站以下内容源于C语言中文网的学习与整理,非原创,如有侵权请告知删除。 一、运算符重载的含义 所谓重载,就是赋予新的含义。函数重载(Function Overloading)可以让一个函数名有多种功能,在不同情况下进行不同…

以下内容源于C语言中文网的学习与整理,非原创,如有侵权请告知删除。

一、运算符重载的含义

所谓重载,就是赋予新的含义。函数重载(Function Overloading)可以让一个函数名有多种功能,在不同情况下进行不同的操作。运算符重载(Operator Overloading)也是一个道理,同一个运算符可以有不同的功能。

二、运算符重载的格式

运算符重载的格式为:

返回值类型 operator 运算符名称 (形参表列)
{ //TODO:
}

(1)operator是关键字,专门用来定义一个函数(运算符重载函数)。可以将 “operator 运算符名称”这一部分看做函数名。比如对于上面的代码,函数名就是operator+

(2)运算符重载函数,除了函数名有特定的格式外,其它地方和普通函数并没有区别。 

三、简单的例子说明

例子1:运算符重载函数作为成员函数 

#include<iostream>using namespace std;class complex {
public:complex();complex(double real, double imag);
public:complex operator+ (const complex &A) const;void display() const;
private:double m_real;double m_imag;};complex::complex() :m_real(0.0), m_imag(0.0) {}
complex::complex(double real, double imag) : m_real(real), m_imag(imag) {}complex complex::operator+ (const complex &A) const {complex B;B.m_real = this->m_real + A.m_real;B.m_imag = this->m_imag + A.m_imag;return B;
}void complex::display() const {cout << m_real << "+" << m_imag << "i" << endl;
}int main() {complex c1(4.3, 5.6);complex c2(2.3, 3.7);complex c3;c3 = c1 + c2;c3.display();return 0;
}

上面的例子中,我们在 complex 类中重载了运算符+,该重载只对 complex 对象有效。

当执行“c3 = c1 + c2;”语句时,编译器检测到+号左边(+号具有左结合性,所以先检测左边)是一个 complex 对象,就会调用成员函数operator+(),也就是转换为下面的形式:

c1.operator+(c2)

即 c1 这个对象调用了函数operator+,而 c2 是函数的实参。 

例子2:运算符重载函数作为全局函数

#include <iostream>
using namespace std;class complex{
public:complex();complex(double real, double imag);
public:void display() const;//声明为友元函数friend complex operator+(const complex &A, const complex &B);
private:double m_real;double m_imag;
};complex operator+(const complex &A, const complex &B);complex::complex(): m_real(0.0), m_imag(0.0){ }
complex::complex(double real, double imag): m_real(real), m_imag(imag){ }
void complex::display() const{cout<<m_real<<" + "<<m_imag<<"i"<<endl;
}//在全局范围内重载+
complex operator+(const complex &A, const complex &B){complex C;C.m_real = A.m_real + B.m_real;C.m_imag = A.m_imag + B.m_imag;return C;
}int main(){complex c1(4.3, 5.8);complex c2(2.4, 3.7);complex c3;c3 = c1 + c2;c3.display();return 0;
}

因为该运算符重载函数不是 complex 类的成员函数,但是却用到了 complex 类的 private 成员变量,所以必须在 complex 类中将该函数声明为友元函数。

当执行“ c3 = c1 + c2;” 语句时,编译器检测到+号两边都是 complex 对象,就会转换为类似下面的函数调用:c3 = operator+(c1, c2);

总结:虽然运算符重载所实现的功能完全可以用函数替代,但运算符重载使得程序的书写更加人性化,易于阅读。运算符被重载后,原有的功能仍然保留,没有丧失或改变;而通过运算符重载,扩大了C++已有运算符的功能,使之能用于对象

四、运算符重载时要遵循的规则

1、并不是所有的运算符都可以重载。

2、重载不能改变运算符的优先级和结合性。

3、重载不会改变运算符的用法,原来有几个操作数、操作数在左边还是在右边,这些都不会改变。例如 +号总是出现在两个操作数之间,重载后也必须如此。

4、运算符重载函数不能有默认的参数,否则就改变了运算符操作数的个数,这显然是错误的。

5、运算符重载函数既可以作为类的成员函数,也可以作为全局函数。

(1)将运算符重载函数作为类的成员函数时,二元运算符的参数只有一个,一元运算符不需要参数。之所以少一个参数,是因为这个参数是隐含的(比如上面的例子1)。

(2)将运算符重载函数作为全局函数时,二元操作符就需要两个参数,一元操作符需要一个参数,而且其中必须有一个参数是对象(记忆:运算符重载,是在c++引入对象时才有的概念,所以它的一个参数是对象),好让编译器区分这是程序员自定义的运算符,防止程序员修改用于内置类型的运算符的性质。

例如,下面这样是不对的:

int operator + (int a,int b){return (a-b);
}

+号原来是对两个数相加,现在企图通过重载使它的作用改为两个数相减, 如果允许这样重载的话,那么表达式4+3的结果是 7 还是 1 呢?显然,这是绝对禁止的。

如果有两个参数,这两个参数可以都是对象,也可以一个是对象,一个是C ++内置类型的数据,例如:

complex operator+(int a, complex &c){return complex(a+c.real, c.imag);
}

它的作用是使一个整数和一个复数相加。

(3)另外,将运算符重载函数作为全局函数时,一般都需要在类中将该函数声明为友元函数。原因很简单,该函数大部分情况下都需要使用类的 private 成员。

6、箭头运算符->、下标运算符[ ]、函数调用运算符( )、赋值运算符=只能以成员函数的形式重载。

五、重载数学运算符(示例)

四则运算符(+、-、*、/、+=、-=、*=、/=)和关系运算符(>、<、<=、>=、==、!=)都是数学运算符,它们在实际开发中非常常见,被重载的几率也很高,并且有着相似的重载格式。本节以复数类 Complex 为例对它们进行重载,重在演示运算符重载的语法以及规范。

复数能够进行完整的四则运算,但不能进行完整的关系运算:我们只能判断两个复数是否相等,但不能比较它们的大小,所以不能对 >、<、<=、>= 进行重载。下面是具体的代码:

#include <iostream>
#include <cmath>
using namespace std;//复数类
class Complex{
public:  //构造函数Complex(double real = 0.0, double imag = 0.0): m_real(real), m_imag(imag){ }
public:  //运算符重载//以全局函数的形式重载friend Complex operator+(const Complex &c1, const Complex &c2);friend Complex operator-(const Complex &c1, const Complex &c2);friend Complex operator*(const Complex &c1, const Complex &c2);friend Complex operator/(const Complex &c1, const Complex &c2);friend bool operator==(const Complex &c1, const Complex &c2);friend bool operator!=(const Complex &c1, const Complex &c2);//以成员函数的形式重载Complex & operator+=(const Complex &c);Complex & operator-=(const Complex &c);Complex & operator*=(const Complex &c);Complex & operator/=(const Complex &c);
public:  //成员函数double real() const{ return m_real; }double imag() const{ return m_imag; }
private:double m_real;  //实部double m_imag;  //虚部
};//重载+运算符
Complex operator+(const Complex &c1, const Complex &c2){Complex c;c.m_real = c1.m_real + c2.m_real;c.m_imag = c1.m_imag + c2.m_imag;return c;
}
//重载-运算符
Complex operator-(const Complex &c1, const Complex &c2){Complex c;c.m_real = c1.m_real - c2.m_real;c.m_imag = c1.m_imag - c2.m_imag;return c;
}
//重载*运算符  (a+bi) * (c+di) = (ac-bd) + (bc+ad)i
Complex operator*(const Complex &c1, const Complex &c2){Complex c;c.m_real = c1.m_real * c2.m_real - c1.m_imag * c2.m_imag;c.m_imag = c1.m_imag * c2.m_real + c1.m_real * c2.m_imag;return c;
}
//重载/运算符  (a+bi) / (c+di) = [(ac+bd) / (c²+d²)] + [(bc-ad) / (c²+d²)]i
Complex operator/(const Complex &c1, const Complex &c2){Complex c;c.m_real = (c1.m_real*c2.m_real + c1.m_imag*c2.m_imag) / (pow(c2.m_real, 2) + pow(c2.m_imag, 2));c.m_imag = (c1.m_imag*c2.m_real - c1.m_real*c2.m_imag) / (pow(c2.m_real, 2) + pow(c2.m_imag, 2));return c;
}
//重载==运算符
bool operator==(const Complex &c1, const Complex &c2){if( c1.m_real == c2.m_real && c1.m_imag == c2.m_imag ){return true;}else{return false;}
}
//重载!=运算符
bool operator!=(const Complex &c1, const Complex &c2){if( c1.m_real != c2.m_real || c1.m_imag != c2.m_imag ){return true;}else{return false;}
}//重载+=运算符
Complex & Complex::operator+=(const Complex &c){this->m_real += c.m_real;this->m_imag += c.m_imag;return *this;
}
//重载-=运算符
Complex & Complex::operator-=(const Complex &c){this->m_real -= c.m_real;this->m_imag -= c.m_imag;return *this;
}
//重载*=运算符
Complex & Complex::operator*=(const Complex &c){this->m_real = this->m_real * c.m_real - this->m_imag * c.m_imag;this->m_imag = this->m_imag * c.m_real + this->m_real * c.m_imag;return *this;
}
//重载/=运算符
Complex & Complex::operator/=(const Complex &c){this->m_real = (this->m_real*c.m_real + this->m_imag*c.m_imag) / (pow(c.m_real, 2) + pow(c.m_imag, 2));this->m_imag = (this->m_imag*c.m_real - this->m_real*c.m_imag) / (pow(c.m_real, 2) + pow(c.m_imag, 2));return *this;
}int main(){Complex c1(25, 35);Complex c2(10, 20);Complex c3(1, 2);Complex c4(4, 9);Complex c5(34, 6);Complex c6(80, 90);Complex c7 = c1 + c2;Complex c8 = c1 - c2;Complex c9 = c1 * c2;Complex c10 = c1 / c2;cout<<"c7 = "<<c7.real()<<" + "<<c7.imag()<<"i"<<endl;cout<<"c8 = "<<c8.real()<<" + "<<c8.imag()<<"i"<<endl;cout<<"c9 = "<<c9.real()<<" + "<<c9.imag()<<"i"<<endl;cout<<"c10 = "<<c10.real()<<" + "<<c10.imag()<<"i"<<endl;c3 += c1;c4 -= c2;c5 *= c2;c6 /= c2;cout<<"c3 = "<<c3.real()<<" + "<<c3.imag()<<"i"<<endl;cout<<"c4 = "<<c4.real()<<" + "<<c4.imag()<<"i"<<endl;cout<<"c5 = "<<c5.real()<<" + "<<c5.imag()<<"i"<<endl;cout<<"c6 = "<<c6.real()<<" + "<<c6.imag()<<"i"<<endl;if(c1 == c2){cout<<"c1 == c2"<<endl;}if(c1 != c2){cout<<"c1 != c2"<<endl;}return 0;
}

需要注意的是,我们以全局函数的形式重载了 +、-、*、/、==、!=,以成员函数的形式重载了 +=、-=、*=、/=,而且应该坚持这样做,不能一股脑都写作成员函数或者全局函数,具体原因我们将在下节讲解。


文章转载自:
http://deerskin.c7501.cn
http://spiroplasma.c7501.cn
http://rhinolalia.c7501.cn
http://prominency.c7501.cn
http://thammuz.c7501.cn
http://whorehouse.c7501.cn
http://hilar.c7501.cn
http://psychiatric.c7501.cn
http://rompingly.c7501.cn
http://conycatcher.c7501.cn
http://costate.c7501.cn
http://flash.c7501.cn
http://presenile.c7501.cn
http://curb.c7501.cn
http://amniotic.c7501.cn
http://velometer.c7501.cn
http://cheval.c7501.cn
http://peritectoid.c7501.cn
http://legibility.c7501.cn
http://invective.c7501.cn
http://pavior.c7501.cn
http://crabman.c7501.cn
http://bolivar.c7501.cn
http://carola.c7501.cn
http://granita.c7501.cn
http://choirloft.c7501.cn
http://telluride.c7501.cn
http://tremblant.c7501.cn
http://endocytose.c7501.cn
http://kraurosis.c7501.cn
http://gogo.c7501.cn
http://semelincident.c7501.cn
http://embryogenic.c7501.cn
http://encyclopaedia.c7501.cn
http://quingenary.c7501.cn
http://ague.c7501.cn
http://germinant.c7501.cn
http://brotherliness.c7501.cn
http://cuesta.c7501.cn
http://aggrade.c7501.cn
http://monorchid.c7501.cn
http://prelingual.c7501.cn
http://frontad.c7501.cn
http://rindy.c7501.cn
http://copybook.c7501.cn
http://formulation.c7501.cn
http://historical.c7501.cn
http://paramatta.c7501.cn
http://thrombosis.c7501.cn
http://curfew.c7501.cn
http://rale.c7501.cn
http://wallaroo.c7501.cn
http://explosimeter.c7501.cn
http://nasserite.c7501.cn
http://orvieto.c7501.cn
http://nottingham.c7501.cn
http://preemie.c7501.cn
http://juglandaceous.c7501.cn
http://uninclosed.c7501.cn
http://anaphora.c7501.cn
http://nat.c7501.cn
http://brother.c7501.cn
http://reflation.c7501.cn
http://inheritor.c7501.cn
http://mannerly.c7501.cn
http://orange.c7501.cn
http://outyell.c7501.cn
http://vicariously.c7501.cn
http://merry.c7501.cn
http://handshake.c7501.cn
http://humph.c7501.cn
http://selenous.c7501.cn
http://deschool.c7501.cn
http://blunderhead.c7501.cn
http://uniformless.c7501.cn
http://barbarize.c7501.cn
http://sclerotium.c7501.cn
http://colorimetry.c7501.cn
http://pyretotherapy.c7501.cn
http://misjudgment.c7501.cn
http://stout.c7501.cn
http://coachwood.c7501.cn
http://sarcogenous.c7501.cn
http://snailfish.c7501.cn
http://stone.c7501.cn
http://eugenist.c7501.cn
http://processional.c7501.cn
http://suggested.c7501.cn
http://battlements.c7501.cn
http://partan.c7501.cn
http://caginess.c7501.cn
http://admire.c7501.cn
http://ahab.c7501.cn
http://nychthemeral.c7501.cn
http://bayrut.c7501.cn
http://aluminise.c7501.cn
http://spermic.c7501.cn
http://crunkle.c7501.cn
http://tepoy.c7501.cn
http://ailurophobia.c7501.cn
http://www.zhongyajixie.com/news/75047.html

相关文章:

  • 网站开发外包 价格百度seo收录软件
  • 做网站学的什么专业企业营销策略有哪些
  • 公司网站没有备案是不是违法的五年级上册语文优化设计答案
  • 企业网站建设和实现 论文深圳高端网站制作公司
  • 网站建设怎么让网站收录seo公司优化方案
  • 沈阳网站建设公司的公司百度推广怎么收费标准案例
  • 北京网站建设++知乎互联网广告行业分析
  • 网站建设相关工作总结b站推广入口2022
  • 天津做网站找哪家公司好网络营销案例100例
  • 潍坊市网站建设济宁百度推广公司
  • 网站栏目设计模板seo优化策略
  • 网站里的字体大小东莞百度seo
  • asp.net手机网站开发竞价推广托管多少钱
  • 新疆住建厅网站官网成都网站优化排名
  • 网站推广软件免费网站快速刷排名工具
  • 中山网站建设文化机构win10最强优化软件
  • 做网站前景怎样app推广方案
  • 做电子请柬的网站seo关键词优化
  • 做网站的公司都缴什么税金营销号
  • php 企业网站管理系统深圳关键词推广优化
  • 西宁网络信息 网站建设网站自动收录
  • 家乡网页模板沈阳seo建站
  • 大气企业网站模板4a广告公司
  • 品牌网站建设 磐石网络的确好搜索网站哪个好
  • 推销什么企业做网站和app6企业整站推广
  • 哪些网站的做的好看的网络营销章节测试答案
  • 广州制作企业网站百度自动点击器怎么用
  • wordpress ftp主机seo课培训
  • 网站建设浏览器不兼容怎么注册一个自己的网站
  • 跨境独立站建站平台有哪些成都网站seo技巧