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

怎么做算命网站网站自然排名怎么优化

怎么做算命网站,网站自然排名怎么优化,免费做网站的平台,建设收费网站目录 四则运算符重载 左移运算符重载 递增运算符重载 赋值运算符重载 关系运算符重载 函数调用运算符重载 对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型 四则运算符重载 对自定义数据类型实现四则运算(加减乘除&…

目录

四则运算符重载

左移运算符重载

递增运算符重载

赋值运算符重载

关系运算符重载

函数调用运算符重载


对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型

四则运算符重载

        对自定义数据类型实现四则运算(加减乘除)操作,比如:自己写成员函数,实现两个对象相加属性后返回新的对象

        注意:四则运算符重载也可以进行函数重载。

        总结:

        1.对于内置的数据类型的表达式的的运算符是不可以改变的

        2.不要滥用运算符重载

#include <iostream>using namespace std;class Person
{
public:int m_a;int m_b;Person(int a, int b) :m_a(a), m_b(b){}//成员函数重载Person operator+(const Person &p){Person rp(0, 0);rp.m_a = this->m_a + p.m_a;rp.m_b = this->m_b + p.m_b;return rp;}
};
//全局函数重载
static Person operator-(const Person& p1, const Person& p2)
{Person rp(0, 0);rp.m_a = p1.m_a - p2.m_a;rp.m_b = p1.m_b - p2.m_b;return rp;
}
//函数重载
static Person operator-(const Person& p1, const int value)
{Person rp(0, 0);rp.m_a = p1.m_a - value;rp.m_b = p1.m_b - value;return rp;
}int main(void)
{Person p1(10, 10);Person p2(10, 10);Person p3(5, 5);Person p4 = p1 + p2; //Person p4 = p1.operator+(p2);cout << "p4 m_a value:" << p4.m_a << endl;cout << "p4 m_b value:" << p4.m_b << endl;Person p5 = p4 - p3; //Person p5 = operator-(p4,p3)cout << "p5 m_a value:" << p5.m_a << endl;cout << "p5 m_b value:" << p5.m_b << endl;Person p6 = p5 - 5; //Person p6 = operator-(p5,5)cout << "p6 m_a value:" << p6.m_a << endl;cout << "p6 m_b value:" << p6.m_b << endl;system("pause");return 0;
}

左移运算符重载

        输出自定义的数据类型,比如:直接输出一个对象就可以输出其成员属性

        总结:左移运算符配合友元可以实现输出自定义数据类型

#include <iostream>
#include <string>using namespace std;class Person
{friend static ostream& operator<<(ostream& cout, Person& p);
public:Person(int a, int b){m_a = a;m_b = b;}
private://通常不使用 成员函数重载 左移运算符 ,无法实现cout在左侧int m_a;int m_b;
};static ostream& operator<<(ostream &cout, Person& p) // operator<<(cout, p)
{cout << "m_a=" << p.m_a << endl;cout << "m_b=" << p.m_b << endl;return cout;
}static void test(void)
{Person p(10, 10);cout << p << endl;
}int main(void)
{test();system("pause");return 0;
}

递增运算符重载

        区分前置和后置的差别!!

#include <iostream>
#include <string>using namespace std;class MyInteger
{friend static ostream& operator<<(ostream& cout, MyInteger p);
public:MyInteger(int b){m_b = b;}//重载 后置,int 占位参数 int ,区分前置和后置MyInteger operator++(int){MyInteger temp = *this;++*this;return temp;}//重载 前置MyInteger& operator++() //返回引用是为了一直对一个数据进行操作{++m_b;return *this;}
private:int m_b;
};static ostream& operator<<(ostream& cout, MyInteger p) // operator<<(cout, p)
{cout << p.m_b;return cout;
}static void test(void)
{MyInteger p(0);cout << ++p << endl;cout << p << endl;
}static void test1(void)
{MyInteger p1(0);cout << p1++ << endl;cout << p1 << endl;
}int main(void)
{test();test1();system("pause");return 0;
}

赋值运算符重载

         c++至少给一个类添加4个函数:前三个略

        4.赋值运算符operator=,对属性进行值拷贝(注意深浅拷贝问题)

#include <iostream>
#include <string>using namespace std;class Person
{
public:Person(int b){m_age = new int(b);}~Person(){if (m_age != NULL){delete m_age;m_age = NULL;}}Person& operator=(const Person & p) //重载赋值运算符,使用深拷贝{if (m_age != NULL){delete m_age;m_age = NULL;}m_age = new int(*p.m_age);return *this;}int *m_age;
};static void test1(void)
{Person p1(12);Person p2(19);Person p3(14);p3 = p2 = p1;cout << "p1年龄为:" << *p1.m_age << endl;cout << "p2年龄为:" << *p2.m_age << endl;cout << "p3年龄为:" << *p3.m_age << endl;
}int main(void)
{test1();system("pause");return 0;
}

关系运算符重载

        重载关系运算符,让两个自定义的数据类型进行对比操作

#include <iostream>
#include <string>using namespace std;class Person
{
public:Person(string name, int age){m_name = name;m_Age = age;}bool operator==(Person& p){if (this->m_name == p.m_name && this->m_Age == p.m_Age){return true;}else {return false;}}string m_name;int m_Age;
};static void test1(void)
{Person p1("Tom", 19);Person p2("Mac", 19);if (p1 == p2){cout << "p1 == p2" << endl;}else{cout << "p1 != p2" << endl;}}int main(void)
{test1();system("pause");return 0;
}

函数调用运算符重载

  • 函数调用运算符() 也可以重载
  • 由于重载后使用的方式非常像函数的调用,故称仿函数
  • 仿函数没有固定写法,非常灵活(返回值、参数均不固定)
#include <iostream>
#include <string>using namespace std;class Myprint
{
public:void operator()(string test){cout << test << endl;}
};class MyAdd
{
public:int operator()(int a, int b){return a + b;}
};static void test1(void)
{Myprint myprint;myprint("Hello World!");//仿函数MyAdd myadd;int result = myadd(1, 1);cout << "result = " << result << endl;//匿名函数对象cout << "result = " << MyAdd()(11, 11) << endl;
}int main(void)
{test1();system("pause");return 0;
}

推荐:[C++核心编程](五):类和对象——友元(friend)

http://www.ds6.com.cn/news/3505.html

相关文章:

  • 做网站服务器权限设置汉中网络推广
  • 一级a做爰网站湖南企业竞价优化公司
  • 做网站的需要什么软件sem模型
  • linux做网站服务器吗google下载
  • tp5.1做的网站网站推广方案策划
  • 温州网站建设 温州网站制作营销方案范文
  • 品牌型网站制如何做好线上营销
  • 网站建设和管理网络口碑营销
  • 如何做商业推广网站疫情放开死亡人数最新消息
  • 做网站 宁波seo优化推广专员招聘
  • 做电子画册的网站网络营销的三大基础
  • 公司网站需要服务器吗专业搜索引擎seo服务
  • 网站的服务免费下载百度seo
  • 网站建设技重庆广告公司
  • 端端网站开发楼市最新消息
  • 建设集团网站方案设计软文推广页面
  • 如何制作手机商城网站什么是seo搜索引擎优化
  • 论述网站建设及运营流程sem是什么基团
  • 办文明网站做文明网民活动方案西安网站建设排名
  • 免费域名查询域名年龄对seo的影响
  • 做网站的公司叫什么名字好谷歌搜索引擎363入口
  • 幼儿园网站的建设需求分析推广普通话宣传周活动方案
  • 哈尔滨网站建设托管简述提升关键词排名的方法
  • 有网站建wap网上销售有哪些方法
  • 网站收录不增加城关网站seo
  • 找人做网站上线后被投诉侵权360信息流广告平台
  • 产品定制网站开发安徽做网站公司哪家好
  • 请人做网站得多少钱网站自然优化
  • 长春做网站seo百度大盘指数
  • 卧龙区2015网站建设价格关键词优化网站排名