-
2022-03-10 22:48:00
一、运算符重载
概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
1.1加号运算符重载
作用:实现两个自定义数据类型相加的运算
编译器给起一个通用的名称——可以通过成员函数重载、或者通过全局函数重载
使用的的指令是:operator+
总结:1、对于内置的数据类型的表达的运算符是不可能改变的
2、不要滥用运算符重载
1.2左移运算符重载(<<)
作用:可以输出自定义数据类型(一般是使用全局运算符重载)
1.3递增运算符重载
作用:通过重载递增运算符实现自己的整型数据
1.4赋值运算符重载
C++编译器至少给一个类添加4个函数
1、默认构造函数(无参,函数体为空)
2、默认析构函数(无参、函数体为空)
3、默认拷贝构造函数,对属性进行值拷贝
4、赋值运算符opertor=,对属性进行值拷贝
如果类中属性指向堆区,做赋值操作时也会出现深浅拷贝的问题
1.5关系运算符重载
1.6函数调用运算符
函数调用运算符()也可以重载
由于重载后使用的方式非常想函数的调用,因此称为仿函数
仿函数没有固定写法非常灵活
二、继承
继承是面向对象三大特征之一,有些类鱼类之间纯在特殊的关系
我们定义这些类时,下级别的成员除了拥有上一级的共性还又自己的特性,这个时候我们就可以考虑利用继承技术减少代码的重复
2.1继承的基本语法
例如我们看到很多网站中都又公共的头部,公共的底部等但是只有但是其他的内容不同
继承好处:减少重复代码
语法:class 子类:继承方式 父类
子类 也称为 派生类 父类 也成为 基类
2.2继承的方式
继承语法:class 子类:继承方式 父类
继承的方式有三种:公共继承,保护继承,私有继承
公共继承时除了私有属性无法继承其他的属性保持不变
保护继承(protest)除了私有属性无法访问其他的属性保持不变
2.3继承中的对象模型
父类中所有非静态成员属性都会背子类继承
父类中私有成员属性时被编译器给隐藏了因此时访问不到但是确实被继承下来了
2.4继承中构造和析构函数顺序
子类继承夫欸后当创建子类对象时也会调用父类的构造函数
2.5继承同名成员处理方式
访问子类同名成员,直接访问即可
访问父类同名成员,需要加作用域
当子类与父类拥有同名的成员函数,子类会隐藏父类中的成员函数加作用域可以访问到父类中同名函数
2.6继承同名静态成员处理方法
静态成员和非静态成员出现同名处理方式
访问子类同名成员直接访问即可,访问父类同名成员需要加作用域
总结:同名静态成员处理方式和非静态处理方式一样只不过有两种访问方式(通过对象和通过类名)
静态成员函数及变量:注意类内需要声明类外初始化
2.7菱形继承
菱形继承概念:两个派生类继承同一个基类,又有某个类同时继承着两个派生类,这种继承被称为菱形继承或者砖石继承
利用虚拟继承解决菱形继承的问题(在继承之前加上关键字virtual变为虚继承)
更多相关内容 -
C++运算符重载规则详解
2020-12-31 11:11:26C++允许重载的运算符和不允许...C++运算符重载的规则 C++对运算符重载定义了如下几条规则。 1) C++不允许用户自己定义新的运算符,只能对已有的C++运算符进行重载。 例如,有人觉得BASIC中用“**“作为幂运算符很方便 -
C++运算符重载
2018-05-18 08:41:34C++运算符重载:赋予运算符另一种作用,实现自定义类型的运算C++运算符重载内容: 1.友元重载 2.类重载 3.输入输出流重载 -
C++ 运算符重载
2021-05-08 06:28:07C++ 运算符重载. 我们为什么要重载运算符, 以及重载的用途以及好处.C++ 运算符重载
概述
运算符重载 (Operator Overloading)
函数重载
重载: 将同一名字重新赋予新的含义.
函数重载: 对一个函数赋予新的含义, 使之实现新功能. 例如:int max(int x, int y); double max(double a, double b, double c);
运算符也有重载: 赋予运算符新的含义, 使之一名多用. 例如
int main() { int i = 2, j = 3; int k = i + j; string s1 = "good ", s2 = "morning"; string s3 = s1 + s2; cout << k << endl; cout << s3 << endl; return 0; } 输出结果: 5 good morning
运算符重载
通过运算符重载, 扩大了 C++ 已有运算符的作用, 使运算符能用于类对象. 使用运算符重载, 能使程序易于编写, 阅读和维护. 运算符被重载后, 其原有的功能仍然保留, 没有丧失过改变.
运算符重载实质上是函数的重载:
- 定义一个重载运算符的函数
- 需要执行被重载的运算符时, 系统就自动调用该函数, 以实现相应的运算
C++ 的运算符
- 算数运算符: +(加) -(减) *(乘) %(整除余数) ++(自加) – (自减)
- 关系运算符: >(大于) <(小于) ==(等于) >=(大于等于) <=(小于等于) !=(不等于)
- 逻辑运算符: &&(逻辑与) ||(逻辑或) !(逻辑非)
- 位运算符: <<(按位左移) >>(按位右移) &(按位与) |(按位或) ∧(按位异或) ~(按位取反)
- 赋值运算符: = 及其扩展赋值运算符
- 条件运算符: ?:
- 都好运算符: ,
- 指针运算符: *
- 引用运算符合地址运算符: &
- 求字节数运算符: sizeof
- 强制类型转换运算符: (类型) 或 类型()
- 成员运算符: .
- 指向成员的运算符:->
- 下标运算符: []
- 其他: 如函数调用运算符 ()
重载运算符的规则
- 不允许创造新的运算符, 只能对已有 C++ 运算符进行重载.
- C++ 允许重载运算符: 成员运算符(.), 成员指针访问运算符(.*), 域运算符(:😃, 求字节数运算符(sizeof), 条件运算符(?😃
- 重载不能改变运算符运算对象 (即操作数) 的个数
- 重载不能改变运算符的优先级别
- 重载不能改变运算符的结合性
- 重载运算符的函数不能有默认的参数
- 重载的运算符必须和用户定义的自定义类型的对象一起使用. 参数至少有一个是类对象或其 引用
成员函数实现 Complex 加法
Complex 类:
#ifndef PROJECT2_COMPLEX_H #define PROJECT2_COMPLEX_H class Complex { private: double real; double imag; public: Complex(); Complex(double r, double i); Complex add(Complex &c2); void display(); }; #endif //PROJECT2_COMPLEX_H
Complex.cpp:
#include <iostream> #include "Complex.h" using namespace std; Complex::Complex() : real(0), imag(0) {} Complex::Complex(double r, double i) : real(r), imag(i) {} Complex Complex::add(Complex &c2) { Complex c; c.real = real + c2.real; c.imag = imag + c2.imag; return c; } void Complex::display() { cout << "(" << real << ", "; cout << imag << "i)" << endl; }
main:
int main() { Complex c1(3, 4), c2(5, -10), c3; cout << "c1 ="; c1.display(); cout << "c2 ="; c2.display(); c3 = c1.add(c2); cout << "c1 + c2 = "; c3.display(); return 0; }
输出结果:
c1 =(3, 4i) c2 =(5, -10i) c1 + c2 = (8, -6i)
运算符重载的方法
运算符重载格式:
函数类型 operator 运算符名称 (形参流标) {对运算符的重载处理}
Complex 类:
#ifndef PROJECT4_COMPLEX_H #define PROJECT4_COMPLEX_H class Complex { private: double real; double imag; public: Complex(); Complex(double, double); void display(); Complex operator+(Complex &c2); }; #endif //PROJECT4_COMPLEX_H
Complex.cpp:
#include <iostream> #include "Complex.h" using namespace std; Complex::Complex() : real(0), imag(0) {} Complex::Complex(double r, double i) :real(r), imag(i) {} void Complex::display() { cout << "(" << real << ", "; cout << imag << "i)" << endl; } Complex Complex::operator+(Complex &c2) { Complex c; c.real = real + c2.real; c.imag = imag + c2.imag; return c; }
main:
#include <iostream> #include "Complex.h" using namespace std; int main() { Complex c1(3, 4), c2(5, -10), c3; cout << "c1 ="; c1.display(); cout << "c2 ="; c2.display(); c3 = c1 + c2; cout << "c1 + c2 = "; c3.display(); return 0; }
输出结果:
c1 =(3, 4i) c2 =(5, -10i) c3= (8, -6i)
多种实现方法
成员函数实现:
Complex Complex::operator+(Complex &c2) { Complex c; c.real = real + c2.real; c.imag = imag + c2.imag; return c; }
简化:
Complex Complex::operator+(Complex &c2){ return Complex(real +c2.real, imag + c2.image); }
友元函数实现:
Complex operator+(Complex &c1, Complex &c2){ ...... }
实现 operator+=
Complex 类:
#ifndef PROJECT4_COMPLEX_H #define PROJECT4_COMPLEX_H class Complex { private: double real; double imag; public: Complex(); Complex(double, double); void display(); Complex operator+=(const Complex &c); }; #endif //PROJECT4_COMPLEX_H
Complex.cpp:
#include <iostream> #include "Complex.h" using namespace std; Complex::Complex() : real(0), imag(0) {} Complex::Complex(double r, double i) :real(r), imag(i) {} void Complex::display() { cout << "(" << real << ", "; cout << imag << "i)" << endl; } Complex Complex::operator+=(const Complex &c) { real += c.real; // this->real += c.real; imag += c.imag; // this->imag += c.imag; return *this; }
main:
#include <iostream> #include "Complex.h" using namespace std; int main() { Complex c1(3, 4), c2(5, -10), c3; cout << "c1 ="; c1.display(); cout << "c2 ="; c2.display(); c1 += c2; cout << "c1= "; c1.display(); return 0; }
输出结果:
c1 =(3, 4i) c2 =(5, -10i) c1= (8, -6i)
三种运算符重载函数
运算符重载函数可以是类的成员函数:
- 它可以通过 this 指针自由地访问本类的数据成员. 少写一个函数的参数, 但有要求.
运算符重载函数可以是类的友元函数:
- 如果运算符左侧的操作属于 C++ 标准类型 (如 int) 或是一个其他类的对象, 则运算符重载函数不能选用成员函数. 为方便访问类的私有成员, 声明为友元函数为佳.
运算符重载函数还可以是普通函数:
- 只有极少的情况下才使用 (因普通函数一般不能直接访问类的私有成员)
成员函数实现
Complex 类:
#ifndef PROJECT4_COMPLEX_H #define PROJECT4_COMPLEX_H class Complex { private: double real; double imag; public: Complex(); Complex(double, double); void display(); Complex operator+(double d); // 成员函数实现 }; #endif //PROJECT4_COMPLEX_H
Complex.cpp:
#include <iostream> #include "Complex.h" using namespace std; Complex::Complex() : real(0), imag(0) {} Complex::Complex(double r, double i) :real(r), imag(i) {} void Complex::display() { cout << "(" << real << ", "; cout << imag << "i)" << endl; } Complex Complex::operator+(double d) { return Complex(real + d, imag); }
友元函数实现
Complex 类:
#ifndef PROJECT4_COMPLEX_H #define PROJECT4_COMPLEX_H class Complex { private: double real; double imag; public: Complex(); Complex(double, double); void display(); friend Complex operator+(Complex &c, double d); // 友元函数 }; #endif //PROJECT4_COMPLEX_H
Complex.cpp:
#include <iostream> #include "Complex.h" using namespace std; Complex::Complex() : real(0), imag(0) {} Complex::Complex(double r, double i) :real(r), imag(i) {} void Complex::display() { cout << "(" << real << ", "; cout << imag << "i)" << endl; } Complex operator+(Complex &c, double d) { return Complex(c.real + d, c.imag); }
输出结果
main:
#include <iostream> #include "Complex.h" using namespace std; int main() { Complex c1(3, 4), c2(5, -10), c3, c4; cout << "c1 ="; c1.display(); cout << "c2 ="; c2.display(); c3 = c1 + 3.14; cout << "c3= "; c3.display(); return 0; }
输出结果:
c1 =(3, 4i) c2 =(5, -10i) c3= (6.14, 4i)
重载单元运算符
单元运算符 (unary operation), 即只有一个运算量. 如: !a, -b, &c, *p, ++i, i-- 等.
例子
重载单元运算符实现分数对象的相反数.
Fraction 类:
#ifndef PROJECT4_FRACTION_H #define PROJECT4_FRACTION_H #include <iostream> using namespace std; class Fraction { private: int nume; // 分子 int deno; // 分母 public: Fraction(); Fraction(int, int); Fraction operator-(const Fraction &c); // 分数相减 Fraction operator-(); // 取反一目运算 friend ostream& operator<<(ostream &output, const Fraction &f); }; #endif //PROJECT4_FRACTION_H
Fraction.cpp:
#include "Fraction.h" Fraction::Fraction() : nume(0), deno(0) {} Fraction::Fraction(int n , int d) : nume(n), deno(d) {} Fraction Fraction::operator-(const Fraction &c) { return Fraction(nume*c.deno - c.nume*deno, deno*c.deno); } Fraction Fraction::operator-() { return Fraction(-nume, deno); } ostream& operator<<(ostream &output, const Fraction &f) { double result = (double)f.nume / f.deno; output << result << endl; return output; }
main:
#include <iostream> #include "Fraction.h" using namespace std; int main() { Fraction f1(1,3), f2(1,5), f3, f4; f3 = f1 - f2; // 分数相减 f4 = -f1; // 分数取反 cout << f3; cout << f4; return 0; }
输出结果:
0.133333 -0.333333
重载二元运算符
二元运算符 (binary operation).
- 有两个操作数, 通常在运算符的左右两侧 (例如: 3+2, 5>8, x*=3)
- 重载双目运算符时, 函数中应该有两个参数
例子
要求:
- 定义字符串类 String, 用来存放不定长的字符串
- 重载关系运算符, 用于两个字符串的比较运算
步骤:
- 定义类的 “框架”
- 完善运算符重载
String 类:
#ifndef PROJECT4_STRING_H #define PROJECT4_STRING_H #include <cstdlib> class String { private: char *p; public: String(){p=nullptr;} String(char *str); void display(); }; #endif //PROJECT4_STRING_H
String.cpp:
#include <iostream> #include <cstring> #include "String.h" using namespace std; String::String(char *str) { p = new char[sizeof(str)]; strcpy(p, str); } void String::display() { cout << p; }
main:
#include <iostream> #include "String.h" using namespace std; int main() { String s1("Hello"); String s2("China"); s1.display( ); cout<<" "; s2.display( ); cout<<endl; return 0; }
输出结果:
Hello China
重载 I/O
通过重载输入流 (input stream) 和输出流 (output stream), 我们可以用来输出用户自己定义的数据.
格式:
ostream &operator<<(ostream&, const 自定义类&); istream &operator>>(istream&,自定义类&);
插入运算符 <<
Complex 类:
#ifndef PROJECT4_COMPLEX_H #define PROJECT4_COMPLEX_H #include <iostream> using namespace std; class Complex { private: double real; double imag; public: Complex(); Complex(double, double); void display(); Complex operator+(Complex &c); friend ostream& operator<<(ostream &output, const Complex &c); }; #endif //PROJECT4_COMPLEX_H
Complex.cpp:
#include <iostream> #include "Complex.h" using namespace std; Complex::Complex() : real(0), imag(0) {} Complex::Complex(double r, double i) :real(r), imag(i) {} void Complex::display() { cout << "(" << real << ", "; cout << imag << "i)" << endl; } Complex Complex::operator+(Complex &c) { return Complex(real + c.real, imag + c.imag); } ostream &operator<<(ostream &output, const Complex &c) { output<<"("<<c.real<<" + "<<c.imag<<"i)"; return output; }
main:
#include <iostream> #include "Complex.h" using namespace std; int main() { Complex c1(2, 4),c2(6, 10),c3; c3 = c1 + c2; cout << c1 << " + " << c2 << " = " << c3 << endl; return 0; }
输出结果:
(2 + 4i) + (6 + 10i) = (8 + 14i)
提取运算符 >>
Complex 类:
#ifndef PROJECT4_COMPLEX_H #define PROJECT4_COMPLEX_H #include <iostream> using namespace std; class Complex { private: double real; double imag; public: Complex(); Complex(double, double); void display(); Complex operator+(Complex &c); friend ostream& operator<<(ostream &output, const Complex &c); friend istream& operator>>(istream &input, Complex &c); }; #endif //PROJECT4_COMPLEX_H
Complex.cpp:
#include <iostream> #include "Complex.h" using namespace std; Complex::Complex() : real(0), imag(0) {} Complex::Complex(double r, double i) :real(r), imag(i) {} void Complex::display() { cout << "(" << real << ", "; cout << imag << "i)" << endl; } Complex Complex::operator+(Complex &c) { return Complex(real + c.real, imag + c.imag); } ostream &operator<<(ostream &output, const Complex &c) { output<<"("<<c.real<<" + "<<c.imag<<"i)"; return output; } istream &operator>>(istream &input, Complex &c) { cout << "input real part and imaginary part:\n"; input >> c.real >> c.imag; return input; }
main:
#include <iostream> #include "Complex.h" using namespace std; int main() { Complex c1, c2; cin >> c1 >> c2; cout << "c1=" << c1 << endl; cout << "c2=" << c2 << endl; return 0; }
输出结果:
input real part and imaginary part: 2 4 input real part and imaginary part: 6 10 c1=(2 + 4i) c2=(6 + 10i)
总结
运算符重载使类的设计更加丰富多彩, 扩大了类的功能和使用范围. 运算符重载使得程序易于理解, 易于对对象进行操作. 有了运算符重载, 在声明了类之后, 我们就可以像使用标准类型一样来使用自己声明的类.
类的声明往往是一劳永逸的. 有了好的类, 用户在程序中就不必定义许多成员函数去完成运算和 I/O 的功能, 使主函数更加简单易读. 好的运算符重载能细心啊面向对象程序设计思想.
-
C++运算符重载友元函数实例
2016-08-17 21:37:36一个实例,实现运算符重载(成员函数和非成员函数两种方式),友元函数的使用,注意事项等,自己学习时编写的, -
c++ 运算符重载和方法重载
2021-01-08 04:30:29字符串相关操作: strcpy(s1,s2)复制s2为s1; strcmp(s1,s2)比较s1和s2字符串内容是否相同 strcat(s1,s2)拼接字符串,把s2拼接在s1的末尾 strchr(s1,ch)找到s1这个字符...const修饰的取地址运算符重载 析构 -
C++运算符重载 成员函数与友元函数详解
2020-09-05 02:44:13以下是对C++运算符重载 成员函数与友元函数进行了介绍,需要的朋友可以过来参考下 -
C++运算符重载课件
2017-09-17 15:21:04中国农业大学C++课程课件,信息与电气工程学院学生专用。 -
C++运算符重载简单举例
2022-04-12 09:39:36C++运算符重载简单举例,使用+号重载对象成员的运算。C++运算符重载,有比较多的妙用,例如,默认的“+”号,承担着整型数字的相加、浮点型数字的相加、字符串的连接,等等,但不能实现对象中的数据成员的相加。因此我们通过重载,可以实现一些特殊的效果。
我们定义一个类,类里有3个数据成员,分别为一个字符型、一个整型、一个浮点型,我们重载运算符+号,使得两个对象可以相加,具体效果为字符串连接、整型数字取和、浮点型数字取平均值,具体代码如下:
#include<iostream> using namespace std; class test { public: string name; int age; double score; test (string name, int age, double score): name(name), age(age), score(score) {} test operator + (test &A) { return test(name + A.name, age + A.age, (score + A.score) / 2); } void display() { cout << "姓名:" << name << "\t年龄:" << age << "\t成绩:" << score << endl; } }; int main() { test a("小明", 16, 85); a.display(); test b("张三", 20, 60); b.display(); test c = a + b; c.display(); test d = c.operator + (a); d.display(); }
运行结果如下图所示。
需要注意的是a.operator + (b)与a+b效果是一样的。
-
c++运算符重载
2018-10-10 15:36:20c++运算符重载教学指导,用于理解重载运算符函数的作用、函数定义及函数实现 -
C++运算符重载函数
2021-12-15 19:12:51刚学了运算符重载,先把自己目前认为正确的理解记录下来。 运算符重载为成员函数 #include <iostream> using namespace std; class Rect { public: Rect(int _w,int _h) { w=_w; h=_h; } //...刚学了运算符重载,先把自己目前认为正确的理解记录下来。
运算符重载为成员函数
#include <iostream> using namespace std; class Rect { public: Rect(int _w,int _h) { w=_w; h=_h; } //重载乘法运算 Rect operator*(int n) { cout<<"重载乘法运算"<<endl; w=w*n; h=h*n; return *this; } void show() { cout<<"w is: "<<w<<" h is: "<<h<<endl; } private: int w; int h; }; int main() { //定义矩形对象 Rect r1(5,7); /* 条件反射:把人类思维习惯写的表达式--》转换成函数调用--》可以帮助分析清楚函数该怎么写 r1*3--》r1.operator*(3) 3*r1--》3.operator*(r1)//你是来搞笑的 */ Rect r2=r1*3; r1.show(); r2.show(); }
运行结果:
重载乘法运算 w is: 15 h is: 21 w is: 15 h is: 21
这里直接在类里面进行运算符重载
main函数第九行:
Rect r2=r1*3;
r1*3可以理解为r1.operator*(3),调用对象r1中的成员函数"operator*",实参为3
运算符重载为友元函数
#include <iostream> using namespace std; class Rect { public: Rect(int _w,int _h) { w=_w; h=_h; } void show() { cout<<"w is: "<<w<<" h is: "<<h<<endl; } //把运算符重载声明成矩形类的友元函数 friend Rect operator*(int n,Rect &rect); private: int w; int h; }; //注意:operator*跟矩形类没有任何关系,就是个普普通通的函数 Rect operator*(int n,Rect &rect) { cout<<"重载乘法调用了"<<endl; rect.w=rect.w*n; rect.h=rect.h*n; return rect; } int main() { //定义矩形对象 Rect r1(5,7); /* 条件反射:把人类思维习惯写的表达式--》转换成函数调用--》可以帮助分析清楚函数该怎么写 3*r1--》3.operator*(r1)//你是来搞笑的,行不通(3不是一个对象) 正确思路:把运算符重载成Rect类的友元函数 3*r1--》operator*(3,r1) */ Rect r2=3*r1; r1.show(); r2.show(); }
运行结果:
重载乘法调用了 w is: 15 h is: 21 w is: 15 h is: 21
这里在类进行声明,运算符重载为它的友元函数,并在类的外面进行定义,多了一个Rect类的引用的形参
Rect operator*(int n,Rect &rect)
main函数第九行:
Rect r2=3*r1;
3和r1调换了一下顺序
假设如果还是像成员函数那样的写法:可以理解为 3.operator*(r1)。很明显,这个3不是类,编译时会出错。
但是如果像友元函数这种写法:可以理解为 operator*(3,r1)。这样就对了,就像我们调用一个普通的函数一样。
(END)
-
C++运算符重载和友元函数
2021-07-04 18:31:00C++运算符重载和友元函数 提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录C++初学运算符重载和友元函数一、运算符重载1.运算符重载的基本概念1.1 运算符重载定义:C++ 允许在同一作用... -
十个 C++ 运算符重载示例,看完不懂打我...
2021-11-28 22:46:47下面是一些 C++ 运算符重载示例,包括算术运算符、赋值运算符、逻辑运算符、成员运算符、关系运算符等等,这些都是使用频率较高的几个运算符重载案例。 ⭐️ 所有示例代码均存放于 GitHub: getiot/cpp-courses/... -
C++运算符重载使用方法
2020-09-26 17:36:07C++运算符重载运算符重载基本概念可重载的运算符特殊运算符运算符使用的建议加号和减号运算符重载加号运算符重载减号运算符重载前置和后置(++/--)运算符重载左移和右移运算符重载赋值运算符重载指针运算符(*,->... -
C++运算符重载 const用法
2021-10-26 14:42:54一、 所谓重载就是赋予新的含义 ...如果让运算符重载了,就可以让两个对象相加 class integer { public: integer():m_num(0){} integer(int num):m_num(num){} const integer operator+(const integer & o -
c++运算符重载简单教程
2022-01-22 16:33:11定义一个复数类,实现两个复数相加功能 #include<iostream> using namespace std; class complex{ private: int real; int image; public: complex(){} complex(int real,int image){ ... real= -
C++运算符重载相关的几个问题
2021-09-01 16:46:141. 为什么需要重载运算符: C++预定义的运算符,只能用于基本数据类型的运算:整型、实型、字符型、逻辑型等,且不能用于对象的运算。...2. C++运算符重载的基本形式: 返回值类型 + operator关键字 + 待 -
C++运算符重载---加号运算符重载
2021-02-03 14:19:36运算符重载概念:对已有的运算符重新定义,赋予其另一种功能,以适应不同的数据类型。 4.5.1加号运算符重载 作用:实现两个自定义数据类型相加的运算 实现两个对象相加 其实你自己可以写一个有这个作用的函数 //通过... -
C++运算符重载的两种方法
2021-03-07 02:44:54有两种方法可以使运算符重载:使重载运算符成为该类的成员函数。这允许运算符函数访问类的私有成员。它也 允许函数使用隐式的this指针形参来访问调用对象。使重载的成员函数成为独立分开的函数。当以这种方式重载时... -
c++ 运算符重载operator
2021-01-12 17:19:30不可重载的五个运算符 . :: * ?: sizeof ...运算符重载 本质就是重载方法,因为运算符不能直接识别复杂类型,当运算符操作复杂类型时会调用operator运算符方法 PS:运算符可以直接操作基本类型,但 -
c++运算符重载之加法(+)的运算符重载
2020-06-07 23:27:53一是成员函数的运算符重载,二是友元函数的运算符重载。并且分析这两者之间的区别,和优劣! 使用成员函数重载运算符"+": Pork Pork:: operator+(Cow &xiaoNiu){ int temp=this->weight+xiaoNiu....