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

杭州高端网站建设公司自己做网站需要学什么软件下载

杭州高端网站建设公司,自己做网站需要学什么软件下载,免费制作企业微商城,网站权重不够高 导致目录 1.定义友元运算符重载函数的语法形式 2.双目运算符重载 3.单目运算符重载 1.定义友元运算符重载函数的语法形式 (1)在类的内部,定义友元运算符重载函数的格式如下: friend 函数类型 operator 运算符(形参表&a…

目录

1.定义友元运算符重载函数的语法形式

2.双目运算符重载

3.单目运算符重载


1.定义友元运算符重载函数的语法形式

        (1)在类的内部,定义友元运算符重载函数的格式如下:

friend 函数类型 operator 运算符(形参表)
{函数体
}

        (2) 在类中,声明友元运算符重载函数原型的格式如下:

class X
{...firend 函数类型 operator 运算符(形参表);...
};

        (3) 在类外,定义友元运算符重载函数的格式如下:

函数类型 operator 运算符(形参表)
{函数体
}

        若友元运算符重载函数重载的是双目运算符,则参数表中有两个操作数;若重载的是单目运算符,则参数表中只有一个操作数。下面予以介绍。

2.双目运算符重载

        双目运算符有两个操作数,通常在运算符的左右两侧,例如3 + 5,24 > 12 等。下面是用友元运算符重载函数进行复数运算的例子。

#include <iostream>using namespace std;class Complex
{
public:Complex(double r = 0, double i = 0);void print();friend Complex operator+(Complex& a, Complex& b); // 声明运算符 + 重载函数friend Complex operator-(Complex& a, Complex& b); // 声明运算符 - 重载函数friend Complex operator*(Complex& a, Complex& b); // 声明运算符 * 重载函数friend Complex operator/(Complex& a, Complex& b); // 声明运算符 / 重载函数
private:double real; // 复数实部double imag; // 复数虚部
};Complex::Complex(double r, double i) // 构造函数
{real = r;imag = i;
}Complex operator+(Complex& a, Complex& b)
{Complex temp;temp.real = a.real + b.real;temp.imag = a.imag + b.imag;return temp;
}Complex operator-(Complex& a, Complex& b)
{Complex temp;temp.real = a.real - b.real;temp.imag = a.imag - b.imag;return temp;
}Complex operator*(Complex& a, Complex& b)
{Complex temp;temp.real = a.real * b.real - a.imag * b.imag;temp.imag = a.real * b.imag + a.imag * b.real;return temp;
}Complex operator/(Complex& a, Complex& b)
{Complex temp;double t;t = 1 / (b.real * b.real + b.imag * b.imag);temp.real = (a.real * b.real + a.imag * b.imag) * t;temp.imag = (b.real * a.imag - a.real * b.imag) * t;return temp;
}void Complex::print()
{cout << real;if (imag > 0)cout << "+";if (imag != 0)cout << imag << 'i' << endl;
}int main()
{Complex A1(2.3, 4.6), A2(3.6, 2.8), A3, A4, A5, A6; // 定义6个Complex类的对象A3 = A1 + A2; // 复数相加A4 = A1 - A2; // 复数相减A5 = A1 * A2; // 复数相乘A6 = A1 / A2; // 复数相除A1.print(); // 输出复数A1A2.print(); // 输出复数A2A3.print(); // 输出复数相加结果A3A4.print(); // 输出复数相减结果A4A5.print(); // 输出复数相乘结果A5A6.print(); // 输出复数相除结果A6return 0;
}

        程序运行结果如下:

3.单目运算符重载

        单目运算符只有一个操作数,如-a, &b, !c, ++p等。

        以下是用友元函数重载单目运算符“-”。

#include <iostream>using namespace std;class Coord
{
public:Coord(int x1 = 0, int y1 = 0){x = x1;y = y1;}friend Coord operator-(Coord &obj); // 声明单目运算符 - 重载函数void print();
private:int x, y;
};Coord operator-(Coord &obj) // 定义单目运算符 - 重载函数
{obj.x = -obj.x;obj.y = -obj.y;return obj;
}void Coord::print()
{cout << "x = " << x << ", y = " << y << endl;
}int main()
{Coord ob1(50, 60), ob2;ob1.print();ob2 = - ob1;ob2.print();return 0;
}

        程序结果如下:

        用友元函数重载单目运算符“++”。

#include <iostream>using namespace std;class Coord
{
public:Coord(int i = 0, int j = 0){x = i;y = j;}friend Coord operator++(Coord &op) // 定义单目运算符 ++ 重载函数{                                  // 采用对象引用作为函数参数++op.x;++op.y;return op;}void print(){cout << "x = " << x << ", y = " << y << endl;}
private:int x, y;
};int main()
{Coord ob(10, 20);ob.print();++ob;ob.print();return 0;
}

        程序结果如下:

 

http://www.yayakq.cn/news/845861/

相关文章:

  • 图片网站虚拟主机图文制作教程
  • 网站建设的招聘要求wordpress文章 代码块
  • wordpress适用于任何网站吗武侯区网站建设
  • 苏州做企业网站爱用建站官网
  • 百度认证官方网站网页原型图怎么画
  • 服装网站建设定制广西建设工程造价管理协会网站
  • 建设规划工程许可证在当地什么网站怎样免费建立网站
  • 建设旅游网站的意义一款简洁的个人主页源码
  • 深圳自助企业建站模版网站怎么做国际化
  • 上海做响应式网站的公司可做生物试卷的网站
  • 电商网站建设实训步骤柚子皮wordpress
  • 自己做的网站为什么访问不网页源代码解密
  • 网站开发费用属无形资产吗国外网站建设方案
  • 网站ftp管理工具网站建设实验原理
  • 单页网站开发费用深圳哪个区最好
  • 建设部网站如何登录监理工程师wordpress只能写博客吗
  • 衡水精品网站建设报价管理系统开发
  • 做网站工具 不懂代码学设计的素材网站
  • 做网站apache如何网站建设教程网
  • 做淘宝代理哪个网站好网络营销品牌策划优化
  • 上海大 小企业网站制作微信商户平台
  • 网站建设销售哪些企业需要做网站
  • 文化传播公司网站模板网站默认图片素材
  • 51制作工厂网站个人电脑可以做网站服务器
  • 北京建站公司做网站价格南昌网站建设模板技术公司
  • 大石桥网站上海网站制作优化
  • 做产品网站seo站长教程
  • 站长查询在线链接转换工具
  • 深圳网站seo优化排名公司做个网站成功案例
  • 做燕鲍翅的网站seo整站优化网站建设