当前位置: 首页 > 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.r43.cn/news/565.html

相关文章:

  • 建网站支持设备是什么意思今日新闻十大头条内容
  • django 做网站知名品牌营销策略
  • 便宜电商网站建设网络软文范例
  • 小熊代刷推广网站济南seo网络优化公司
  • 跨境电商网站建设开发公众号seo排名优化
  • 专门做有机食品的网站搜索引擎优化的目的是
  • 扬州网站建设外包重庆森林
  • 直接做那个视频网站seo外包杭州
  • 沈阳做网站的科技公司营销培训内容有哪些
  • java网站开发ssh实例好的网络推广平台
  • 网站浏览量怎么查看seo外链代发
  • 做选择网站app拉新项目
  • 免费行情网站推荐太原seo哪家好
  • 上海装修公司前十强排名榜韶关seo
  • wordpress地址(url)简述什么是seo及seo的作用
  • 太原怎样优化网站建设微信crm管理系统
  • 如何查看一个网站做的外链seo排名如何
  • 做电影网站用什么软件叫什么外链大全
  • 国贸做网站公司在线seo超级外链工具
  • 营利性网站备案企业为何选择网站推广外包?
  • css3动画效果网站最新域名查询ip
  • 有什么做详情页的参考网站bt磁力狗
  • 开发程序的过程主要步骤网页搜索优化seo
  • b2c的电子商务的网站建设重庆百度小额贷款有限公司
  • 做网站有哪些项目无锡网络推广平台
  • 别人做的网站爱站网注册人查询
  • 沈阳新民网站建设长尾关键词搜索网站
  • 做pc端网站服务google seo 优化
  • 汉网可以建设网站不seo排名怎么看
  • 计算机网络实验 做网站的乔拓云网站建设