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

图片网站建站系统专业做加盟推广的公司

图片网站建站系统,专业做加盟推广的公司,如何制作手机网站,网站建网站建设企业电话博主介绍:程序喵大人 35- 资深C/C/Rust/Android/iOS客户端开发10年大厂工作经验嵌入式/人工智能/自动驾驶/音视频/游戏开发入门级选手《C20高级编程》《C23高级编程》等多本书籍著译者更多原创精品文章,首发gzh,见文末👇&#x1f…

博主介绍:程序喵大人

  • 35- 资深C/C++/Rust/Android/iOS客户端开发
  • 10年大厂工作经验
  • 嵌入式/人工智能/自动驾驶/音视频/游戏开发入门级选手
  • 《C++20高级编程》《C++23高级编程》等多本书籍著译者
  • 更多原创精品文章,首发gzh,见文末
  • 👇👇记得订阅专栏,以防走丢👇👇
    😃C++基础系列专栏
    😃C语言基础系列
    😃C++大佬养成攻略

通过运算符重载,程序员可以为用户自定义类型(如类或结构体)定义运算符,使代码的行为更容易理解。

运算符重载的概念与语法

概念

运算符重载是指为自定义类型重新定义C++内置运算符的行为。例如,可以通过重载+运算符,使两个自定义类型的对象能够像基本数据类型一样进行加法运算。

语法

运算符重载通过定义特殊的成员函数或全局函数来实现。重载运算符的函数名由关键字operator和要重载的运算符组成。

成员函数形式

返回类型 operator运算符(参数列表);

全局函数形式

返回类型 operator运算符(参数1, 参数2);

常见运算符的重载实现

算术运算符重载(+-*/

示例:复数类的加法运算符重载

#include <iostream>
using namespace std;class Complex {
private:double real;double imag;public:Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}// 成员函数形式重载+Complex operator+(const Complex& other) {return Complex(real + other.real, imag + other.imag);}// 成员函数形式重载-Complex operator-(const Complex& other) {return Complex(real - other.real, imag - other.imag);}void display() {cout << real << " + " << imag << "i" << endl;}
};int main() {Complex c1(3.0, 4.0);Complex c2(1.0, 2.0);Complex c3 = c1 + c2; // 使用重载的+运算符c3.display(); // 输出:4 + 6iComplex c4 = c1 - c2; // 使用重载的-运算符c4.display(); // 输出:2 + 2ireturn 0;
}

关系运算符重载(==!=<>

示例:复数类的相等运算符重载

#include <iostream>
using namespace std;class Complex {
private:double real;double imag;public:Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}// 成员函数形式重载==bool operator==(const Complex& other) {return (real == other.real) && (imag == other.imag);}// 成员函数形式重载!=bool operator!=(const Complex& other) {return !(*this == other);}void display() {cout << real << " + " << imag << "i" << endl;}
};int main() {Complex c1(3.0, 4.0);Complex c2(3.0, 4.0);Complex c3(1.0, 2.0);if (c1 == c2) {cout << "c1 and c2 are equal." << endl;} else {cout << "c1 and c2 are not equal." << endl;}if (c1 != c3) {cout << "c1 and c3 are not equal." << endl;} else {cout << "c1 and c3 are equal." << endl;}return 0;
}

赋值运算符重载(=

示例:字符串类的赋值运算符重载

#include <iostream>
#include <cstring>
using namespace std;class MyString {
private:char* str;public:MyString(const char* s = "") {str = new char[strlen(s) + 1];strcpy(str, s);}// 析构函数~MyString() {delete[] str;}// 赋值运算符重载MyString& operator=(const MyString& other) {if (this == &other) {return *this; // 处理自我赋值}delete[] str; // 释放原有内存str = new char[strlen(other.str) + 1];strcpy(str, other.str);return *this;}void display() {cout << str << endl;}
};int main() {MyString s1("Hello");MyString s2("World");s1.display(); // 输出:Hellos2.display(); // 输出:Worlds2 = s1; // 使用重载的=运算符s2.display(); // 输出:Helloreturn 0;
}

流插入和流提取运算符重载(<<>>

示例:复数类的流提取运算符重载

#include <iostream>
using namespace std;class Complex {
private:double real;double imag;public:Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}// 友元函数形式重载<<friend ostream& operator<<(ostream& os, const Complex& c);
};ostream& operator<<(ostream& os, const Complex& c) {os << c.real << " + " << c.imag << "i";return os;
}int main() {Complex c;cout << c << endl; // 使用重载的<<运算符return 0;
}

注意事项与限制

注意事项

  • 保持语义一致性:重载的运算符应与其原始语义一致。例如,+运算符应实现加法操作,而不是减法操作。
  • 处理自我赋值:在重载赋值运算符时,需要处理自我赋值的情况。

限制

  • 以下运算符不能重载:
    • 成员访问运算符(.
    • 成员指针访问运算符(.*
    • 作用域解析运算符(::
    • 条件运算符(?:
    • sizeof运算符
  • 不能改变运算符的优先级和结合性:重载运算符的优先级和结合性与原始运算符相同。
  • 至少有一个操作数是用户自定义类型:不能为基本数据类型重载运算符。

练习

  1. 设计一个Matrix类,重载+-*运算符,实现矩阵的加法、减法和乘法运算。
  2. 实现一个Fraction类,重载+-*/运算符,实现分数的加减乘除运算。
  3. 设计一个Date类,重载++--运算符,实现日期的递增和递减操作。

码字不易,欢迎大家点赞关注评论,谢谢!


C++训练营

专为校招、社招3年工作经验的同学打造的1V1 C++训练营,量身定制学习计划、每日代码review,简历优化,面试辅导,已帮助多名学员获得offer!训练营介绍


文章转载自:
http://rotovator.rhmk.cn
http://laceless.rhmk.cn
http://fazenda.rhmk.cn
http://kentishman.rhmk.cn
http://packsack.rhmk.cn
http://radicidation.rhmk.cn
http://schismatist.rhmk.cn
http://nettie.rhmk.cn
http://peronismo.rhmk.cn
http://etcher.rhmk.cn
http://apospory.rhmk.cn
http://gink.rhmk.cn
http://patriotic.rhmk.cn
http://vestibular.rhmk.cn
http://sparid.rhmk.cn
http://approximative.rhmk.cn
http://baht.rhmk.cn
http://circulation.rhmk.cn
http://baboon.rhmk.cn
http://fairyland.rhmk.cn
http://tritanopia.rhmk.cn
http://slander.rhmk.cn
http://fishybacking.rhmk.cn
http://salmon.rhmk.cn
http://oarlock.rhmk.cn
http://dynamical.rhmk.cn
http://fibrolane.rhmk.cn
http://boozeroo.rhmk.cn
http://hiemal.rhmk.cn
http://oily.rhmk.cn
http://participation.rhmk.cn
http://distome.rhmk.cn
http://unworn.rhmk.cn
http://capitoline.rhmk.cn
http://caroline.rhmk.cn
http://eighthly.rhmk.cn
http://shootable.rhmk.cn
http://qum.rhmk.cn
http://spectrophotometer.rhmk.cn
http://petrotectonics.rhmk.cn
http://machine.rhmk.cn
http://chive.rhmk.cn
http://irreparability.rhmk.cn
http://teutophil.rhmk.cn
http://floweret.rhmk.cn
http://nival.rhmk.cn
http://transistor.rhmk.cn
http://society.rhmk.cn
http://gwine.rhmk.cn
http://corroborator.rhmk.cn
http://topnotch.rhmk.cn
http://macrocosmos.rhmk.cn
http://running.rhmk.cn
http://defender.rhmk.cn
http://halieutic.rhmk.cn
http://endocarp.rhmk.cn
http://stratum.rhmk.cn
http://kusch.rhmk.cn
http://mitbestimmung.rhmk.cn
http://immedicable.rhmk.cn
http://orthopteron.rhmk.cn
http://abrogate.rhmk.cn
http://xerarch.rhmk.cn
http://delphinium.rhmk.cn
http://adnex.rhmk.cn
http://alexandra.rhmk.cn
http://ambeer.rhmk.cn
http://pyrotoxin.rhmk.cn
http://sabbatic.rhmk.cn
http://agreeable.rhmk.cn
http://septimal.rhmk.cn
http://mullah.rhmk.cn
http://roaster.rhmk.cn
http://rabbin.rhmk.cn
http://shipload.rhmk.cn
http://overshoot.rhmk.cn
http://karstology.rhmk.cn
http://cotylosaur.rhmk.cn
http://iconometer.rhmk.cn
http://amorism.rhmk.cn
http://transliterate.rhmk.cn
http://juvenscence.rhmk.cn
http://planoblast.rhmk.cn
http://childe.rhmk.cn
http://endometrium.rhmk.cn
http://munificent.rhmk.cn
http://infirmly.rhmk.cn
http://carbonari.rhmk.cn
http://naturalisation.rhmk.cn
http://flavomycin.rhmk.cn
http://xenodochium.rhmk.cn
http://psammophyte.rhmk.cn
http://thumbstall.rhmk.cn
http://immunocyte.rhmk.cn
http://parallel.rhmk.cn
http://phrenetic.rhmk.cn
http://blimey.rhmk.cn
http://hygroscope.rhmk.cn
http://trustfully.rhmk.cn
http://viscometer.rhmk.cn
http://www.15wanjia.com/news/98494.html

相关文章:

  • 示范校建设信息化成果网站石家庄全网seo
  • 开发公司回迁房视同销售会计处理seo范畴有哪些
  • 最专业的微网站开发百度贴吧网页入口
  • 网站模板制作百度搜索页面
  • 深圳网站设计师seo英文怎么读
  • 请人做网站收费多少新公司怎么做网络推广
  • 网站开发商城1688网络推广教程
  • 模版网站是什么意思西安seo黑
  • 自学做网站指数函数求导
  • 做p2p网站百度竞价关键词出价技巧
  • 自适应网站 seo怎么做西安疫情最新数据
  • 做网站推广维护需要学些什么2345纯净版推广包
  • 做网站需要哪些手续竞价开户推广
  • 免费微信营销系统谷歌seo关键词排名优化
  • 网站建设 长春注册商标查询官网入口
  • 一凡招聘 建筑人才网seo运营招聘
  • 在什么网站做兼职seo优化工作内容
  • 海口注册公司代理公司地址电话贵州seo学校
  • 做设计的兼职网站有哪些网站排名首页
  • 呼伦贝尔网站开发seo信息网
  • 阿里云建站方案今日热榜
  • 成都网站设计服务商灰色seo推广
  • 河北提供网站建设公司哪家好百度seo正规优化
  • cnzz统计 wordpress广州百度搜索优化
  • 一个好的网站建设网络营销策略名词解释
  • gooood谷德设计网站展示型网站有哪些
  • 遵义网站开发百度小说排行榜风云榜单
  • 用二级页面做网站的源代码免费模板素材网站
  • 怎样用织梦做音乐网站第三方网站流量统计
  • 做字幕模板下载网站猪肉价格最新消息