-
成员函数重载
2020-04-07 10:02:36大部分运算符可以重载为友元函数或成员函数 友元函数不能显式调用(a.operator@(参数)) 在两个操作数类型不同的情况下,要使用友元函数重载(当然必须要有适当构造函数完成隐式的类型转化) ...大部分运算符可以重载为友元函数或成员函数
友元函数不能显式调用(a.operator@(参数))
在两个操作数类型不同的情况下,要使用友元函数重载(当然必须要有适当构造函数完成隐式的类型转化)
-
【C++】函数重载,包括非成员函数重载以及成员函数重载。举例说明
2020-07-31 12:05:30首先说明,函数重载时,必须要不同的函数,参数个数或参数类型不同。 一.非成员函数的重载 #include<iostream> using namespace std; int mul(int a,int b); double mul(double a,double b); int main() { ...首先说明,函数重载时,必须要不同的函数,参数个数或参数类型不同。
一.非成员函数的重载#include<iostream> using namespace std; int mul(int a,int b); double mul(double a,double b); int main() { int s1,s2; double s3,s4; cout<<"输入两个乘数:"; cin>> s1>>s2; cout<<"answer="<<mul(s1,s2)<<endl; cout<<"继续输入:"; cin>>s3>>s4; cout<<"answer="<<mul(s3,s4)<<endl; return 0; } int mul(int a,int b) { return a*b; } double mul(double a,double b) { return a*b; }
运行结果:
输入两个乘数:1 2
answer=2
继续输入:1.1 2.2
answer=2.42
***成员函数重载***求两个复数的和
#include<iostream> using namespace std; class complex{ private: double real,imag; public: complex();//无参构造函数 complex(double r,double i); complex(double r); complex add(complex f); void print(); }; int main() { complex k1(1.1,2.2),k2(2.2,3.3),k3(1.1),total1,total2,total3; total1=k1.add(k2); total2=k2.add(k3); total3=k3.add(k1); cout<<"total1="; total1.print(); cout<<"total2="; total2.print(); cout<<"total3="; total3.print(); return 0; } complex::complex() { real=0; imag=0; } complex::complex(double r) { real=r; } complex::complex(double r,double i) { real=r; imag=i; } complex complex::add(complex f) { complex temp; temp.real=real+f.real; temp.imag=imag+f.imag; return temp; } void complex::print() { cout<<real; if(imag>0) cout<<"+"<<imag<<"i"<<endl; else cout<<imag<<"i"<<endl; }
运行结果:
3.3+5.5i
3.3+3.3i
2.2+2.2i
在调用求和函数时,返回值为一个类对象,要理解this指针所指的对象。
-
C++ 运算符重载:成员、非成员函数重载
2020-06-19 19:41:34C++ 运算符重载运算符重载1、背景2、运算符函数重载的两种形式1、成员函数重载1、定义格式2、非成员函数重载(友元)1、定义格式3、重载原则4、参数和返回值5、成员函数重载1、双目运算符重载1、定义2、调用格式2、...C++ 运算符重载
一、运算符重载
1、背景
- C++中标准运算符(如+、—、*、/、<、>等)的操作对象只能是基本数据类型。但对于用户自定义类型(例如类),也需要类似的运算操作。这时就必须在C++中重新定义这些运算符,使它能够用于特定类型执行特定的操作。
- 运算符重载,本质上是函数重载,属于静态多态
2、运算符函数重载的两种形式
1、成员函数重载
1、定义格式
- 成员函数声明的格式:
函数类型 operator 运算符(参数表);
- 成员函数定义的格式:
函数类型 类名::operator 运算符(参数表) { 函数体; }
2、非成员函数重载(友元)
1、定义格式
- 友元函数声明的格式:
friend 函数类型 operator 运算符(参数表);
- 友元函数定义的格式:
函数类型 operator 运算符(参数表) { 函数体; }
3、重载原则
- (1) 除了类属关系运算符"."、成员指针运算符".*"、作用域运算符"::"、sizeof运算符和三目运算符"?:"以外,C++中的所有运算符都可以重载。
- (2) 重载运算符限制在C++语言中已有的运算符范围内的允许重载的运算符之中,不能创建新的运算符。
- (3) 运算符重载实质上是函数重载,因此编译程序对运算符重载的选择,遵循函数重载的选择原则。
- (4) 重载之后的运算符不能改变运算符的优先级和结合性,也不能改变运算符操作数的个数及语法结构。
- (5) 运算符重载不能改变该运算符用于内部类型对象的含义。它只能和用户自定义类型的对象一起使用,或者用于用户自定义类型的对象和内部类型的对象混合使用时。
- (6) 运算时,有数和对象的混合运算时,必须使用友元
- (7) =,(),[],->不能以友元方式重载
- (8) 二元运算符中,第一个操作数为非对象时,必须使用友元函数。如输入输出运算符<<和>>
为什么输出运算符重载不能是一个成员函数?而非得声明为友元?
原因如下:
返回值 operator运算符(参数列表){}
重载运算符时,函数声明在类内和类外是有区别的,比方说 + - * / 等需要2个操作数的运算符,当声明在类的外部时,则参数列表为2个参数,第一个参数为运算符左边的操作数,而第二个参数为操作符右边的操作数:如下
classType operator+(classType& left, classType& right);
而当函数声明在类的内部时,即为类的成员函数时,
classType operator+(classType& right );
而第一个操作数就是调用该操作符的对象的引用,第二个操作数是传进来的参数,所以,如果要重载<<运算符,一般写法是这样的
ostream& operator<<(ostream& os, const classType& obj);
则第一个参数是该运算符的第一个操作数,然而,却不是类对象,
所以当该类运算符重载时写在类的内部时,又为了访问类内除public外的其它变量或函数,
则应当声明为友元函数:friend ostream& operator<<(ostream& os, const classType& obj);
所以,为什么有些运算符的重载要声明为友元函数,是类似的。
运算符重载原则表:
运算符 建议使用方式 一元运算符 成员函数 = ( ) [ ] -> 必须是成员函数 += -= /= *= ^= &= != %= >>= <<= 成员函数 所有其它二元运算符, 例如: –,+,*,/ 友元函数 << >> 友元函数 4、参数和返回值
-
参数:
当参数不会被改变,一般按const引用来传递(若是使用成员函数重载,函数也为const). -
返回数值:
- 如果返回值可能出现在=号左边, 则只能作为左值, 返回非const引用。
- 如果返回值只能出现在=号右边, 则只需作为右值, 返回const型引用或者const型值。
- 如果返回值既可能出现在=号左边或者右边, 则其返回值须作为左值, 返回非const引用。
5、成员函数重载
定义格式
<函数类型> operator <运算符>(<参数表>) { <函数体> }
1、双目运算符重载
1、定义
双目运算符(或称二元运算符)是C++中最常用的运算符。双目运算符有两个操作数,通常在运算符的左右两侧,
+,-,*,/,%,<,>,>=,<=,==,!=,<<,>>,&,^,|,&&,||,=
如3+5,a=b,i<10等
注意:
双目运算符重载为类的成员函数时,函数只显式说明一个参数,该形参是运算符的右操作数,因为成员函数用this指针隐式地访问了类的一个对象,它充当了运算符函数最左边的操作数。2、调用格式
<对象名>.operator <运算符>(<参数>) //obj1.operator<运算符>(OBJ obj2) 等价于 <对象名><运算符><参数> //obj1<运算符>obj2
例如 a+b 等价于 a.operator+(b)
2、单目运算符重载
1、定义
单目运算符是指运算所需变量为一个的运算符,即在运算当中只有一个操作数,又叫一元运算符。
逻辑非运算符:!、按位取反运算符:~、自增自减运算符:++, - - 等
2、调用格式
- 前置单目运算符重载为类的成员函数时,不需要显式说明参数,即函数没有形参。因为成员函数用this指针隐式地访问了类的一个对象,它充当了运算符函数最左边的操作数。
<运算符> obj 相当于 obj.operator<运算符>(); 例如: ++a 相当于 a.operator++();
- 后置单目运算符重载为类的成员函数时,函数要带有一个整型形参。
obj <运算符> 相当于 obj.operator<运算符>(int); 例如: a++ 相当于 a.operator++(int);
6、非成员函数(友元)重载
1、定义
通常我们都将其声明为友元函数,因为大多数时候重载运算符要访问类的私有数据,如果不声明为类的友元函数,而是通过在此函数中调用类的公有函数来访问私有数据会降低性能。所以一般都会设置为类的友元函数,这样我们就可以在此非成员函数中访问类中的数据了。
2、定义形式
声明的格式:
friend 函数类型 operator 运算符(参数表);
定义的格式:
friend 函数类型 operator 运算符(参数表){ 函数体; }
3、调用格式
- 双目运算符 B重载后,
obj1 运算符 obj2 等同于 operator 运算符(obj1,obj2 )
- 前置单目运算符 B重载后,
运算符 obj 等同于 operator 运算符(obj )
- 后置单目运算符 ++和–重载后,
obj 运算符 等同于 operator 运算符(obj,0 )
举例
//前置++运算符重载 friend 函数类型 & operator++(类类型 &); //后置++运算符重载 friend 函数类型 & operator++(类类型 &,int);
4、重载原则:
- 函数的形参代表依自左至右次序排列的各操作数。
- 参数个数=原操作数个数(后置++、–除外)
- 至少应该有一个自定义类型的参数。
- 后置单目运算符 ++和–的重载函数,形参列表中要增加一个int,但不必写形参名。
- 如果在运算符的重载函数中需要操作某类对象的私有成员,可以将此函数声明为该类的友元。
7、 +和 -运算符的重载实例
//https://blog.csdn.net/zhuzhaoming1994/article/details/80371779 class Point { private: int x; public: Point(int x1) { x=x1;} Point(Point& p) { x=p.x;} const Point operator+(const Point& p);//使用成员函数重载加号运算符 friend const Point operator-(const Point& p1,const Point& p2);//使用友元函数重载减号运算符 }; const Point Point::operator+(const Point& p) { return Point(x+p.x); } const Point operator-(const Point& p1,const Point& p2) { return Point(p1.x-p2.x); }
结果:
Point a(1); Point b(2); a+b; //正确,调用成员函数 a-b; //正确,调用友元函数 a+1; //正确,先调用类型转换函数,把1变成类类型,之后调用成员函数 a-1; //正确,先调用类型转换函数,把1变成类类型,之后调用友元函数 1+a; //错误,调用成员函数时,第一个操作数必须是对象,因为第一个操作数还有调用成员函数的功能 1-a; //正确,先类型转换 后调用友元函数
8、++和–运算符的重载实例
//https://blog.csdn.net/zhuzhaoming1994/article/details/80371779 class Point { private: int x; public: Point(int x1) { x=x1;} Point& operator++();//成员函数定义自增 /*函数返回临时变量,不能出现在等号的左边(临时变量不能做左值),函数的结果只能做右值,则要返回一个const类型的值*/ const Point operator++(int x); //后缀可以返回一个const类型的值,这里引入一个虚参数int x,x可以是任意整数。 friend Point operator--(Point& p);//友元函数定义-- friend const Point operator--(Point& p,int x);//后缀可以返回一个const类型的值 }; Point Point::operator++()//++obj { x++; return *this; } const Point Point::operator++(int x)//obj++ { Point temp = *this; this->x++; return temp; } Point operator--(Point& p)//--obj { p.x--; return p; //前缀形式(--obj)重载的时候没有虚参,通过引用返回*this 或 自身引用,也就是返回变化之后的数值 } const Point operator--(Point& p,int x)//obj-- { Point temp = p; p.x--; return temp; // 后缀形式obj--重载的时候有一个int类型的虚参, 返回原状态的拷贝 }
Point a(1); Point b(2); a++;//隐式调用成员函数operator++(0),后缀表达式 ++a;//隐式调用成员函数operator++(),前缀表达式 b--;//隐式调用友元函数operator--(0),后缀表达式 --b;//隐式调用友元函数operator--(),前缀表达式 cout<<a.operator ++(2);//显式调用成员函数operator ++(2),后缀表达式 cout<<a.operator ++();//显式调用成员函数operator ++(),前缀表达式 cout<<operator --(b,2);//显式调用友元函数operator --(2),后缀表达式 cout<<operator --(b);//显式调用友元函数operator --(),前缀表达式
9、重载输入输出操作符<< >>实例
class Point { private: int x; public: Point(int x1) { x=x1;} friend ostream& operator<<(ostream& cout,const Point& p);//使用友元函数重载<<输出运算符 friend istream& operator>>(istream& cin,Point& p);//使用友元函数重载>>输出运算符 }; ostream& operator<<(ostream& cout,const Point& p) { cout<<p.x<<endl; return cout; } istream& operator>>(istream& cin,Point& p) { cin>>p.x; return cin; }
-
语法:
重载方式:只能使用友元函数重载 且 使用三个引用& -
函数名:
输出流: operator<<(参数表) 输入流:operator>>(参数表)
-
参数表:两个参数均用引用&
输出流: 必须是两个参数:对输出流ostream& 和 对象 第一个操作数cout,定义在文件iostream中,是标准类类型ostream的对象的引用。 如:ostream& cout,const Point& p 输入流:必须是两个参数:对输入流ostream& 和 对象 第一个操作数是cin,定义在文件iostream,实际上是标准类类型istream的对象的引用 如:instream& cin,const Point& p
-
函数调用:
输出流: 显式调用:cout<<对象 隐式调用: operator<<(cout,对象) 输入流:显式调用:cin>>对象 隐式调用: operator>>(cin,对象)
返回类型:返回类型固定 + 使用返回函数引用(满足连续输出)
输出流: 返回ostream& 如:ostream& operator<<(ostream& cout,const Point& p) 输入流:返回:istream& 如:istream& operator>>(istream& cin,Point& p)
10、类型转换运算符重载
类型转换函数的作用是将一个类的对象转换成另一类型的数据。(C++中没有返回类型的函数有3个,构造函数、析构函数、类型转换函数。)
- 1、必须是成员函数,不能是友元函数 ,因为转换的主体是本类的对象。不能作为友元函数或普通函数。
- 2、没有参数(操作数是什么?)
- 3、不能指定返回类型(返回值的类型是由函数名中指定的类型名来确定)
- 4、函数原型:
operator 类型名( ) { 实现转换的语句 }
举例:
类型转换运算符,只要你把XXX对象隐式或者显式转换为T对象时,它都会被自动调用。//https://www.cnblogs.com/renyuan/p/6555172.html #include<iostream> using namespace std; //类型转换运算符重载,只要你把XXX对象隐式或者显式转换为T对象时,它自动被调用 template<class T> class Transfer { public: Transfer(int arg):i(arg){} operator T() const { return i; } private: int i; }; int main() { Transfer<double> t(3); //double d =static_cast<double>(t);//显示转换 double d = t;//隐式转换 cout<<d; getchar(); return 0; }
11、类成员访问运算符 -> 重载
类成员访问运算符( -> )可以被重载,它被定义用于为一个类赋予"指针"行为。运算符 -> 必须是一个成员函数。如果使用了 -> 运算符,返回类型必须是指针或者是类的对象。
#include<iostream> using namespace std; class DBHelper//动态分配内存更加方便 { public: DBHelper() { cout << " DBHelper()...." << endl; } ~DBHelper() { cout << " ~DBHelper()" << endl; //Close(); } void Open() { cout << " Open()...." << endl; } void Query() { cout << " Query()...." << endl; } void Close() { cout << " Close()...." << endl; } }; class DB { public: DB() { db_ = new DBHelper;//利用类对象确定性析构的特性 } ~DB() { delete db_;//利用类对象确定性析构的特性,将所包装的对象销毁掉 } DBHelper* operator->()//重载指针运算符,返回的是DBHelper对象 { return db_; } private: DBHelper* db_; }; int main() { DB db; db->Open(); db->Query(); db->Close(); return 0; }
12、string类实例(重载综合说明)
String.h
#ifndef _STRING_H_ #define _STRING_H_ #include<iostream> using namespace std; class String { public: String(const char* str=""); String(const String& other); String& operator=(const String& other); String& operator=(const char* str); bool operator!() const; char& operator[](unsigned int index); const char& operator[](unsigned int index) const; friend String operator+(const String& s1, const String& s2); //String operator+(const String& s1, const String& s2); String& operator+=(const String& other); friend ostream& operator<<(ostream& os, const String& str); friend istream& operator>>(istream& is, String& str); ~String(void); void Display() const; private: String& Assign(const char* str); char* AllocAndCpy(const char* str); char* str_; }; #endif // _STRING_H_
String.cpp
#pragma warning(disable:4996) #include "String.h" #include <string.h> //#include <iostream> //using namespace std; String::String(const char* str) { str_ = AllocAndCpy(str); } String::String(const String& other) { str_ = AllocAndCpy(other.str_); } String& String::operator=(const String& other) { if (this == &other) return *this; /*delete[] str_; str_ = AllocAndCpy(other.str_); return *this;*/ return Assign(other.str_); } String& String::operator=(const char* str) { /*delete[] str_; str_ = AllocAndCpy(str); return *this;*/ return Assign(str); } String& String::Assign(const char* str) { delete[] str_; str_ = AllocAndCpy(str); return *this; } char& String::operator[](unsigned int index) { return const_cast<char&>(static_cast<const String&>(*this)[index]); //return str_[index]; } const char& String::operator[](unsigned int index) const { return str_[index]; } bool String::operator!() const { return strlen(str_) != 0; } char* String::AllocAndCpy(const char* str) { int len = strlen(str) + 1; char* newstr = new char[len]; memset(newstr, 0, len); strcpy(newstr, str); return newstr; } String operator+(const String& s1, const String& s2) { //int len = strlen(s1.str_) + strlen(s2.str_) + 1; //char* newchar = new char[len]; //memset(newchar, 0, len); //strcpy(newchar, s1.str_); //strcat(newchar, s2.str_); //String tmp(newchar);//return String(newchar);内部构造函数分配空间,newchar没办法销毁 //delete newchar; //return tmp; String str = s1; str += s2; return str; } String& String::operator+=(const String& other) { int len = strlen(str_) + strlen(other.str_) + 1; char* newchar = new char[len]; memset(newchar, 0, len); strcpy(newchar, str_); strcat(newchar, other.str_); delete str_; str_ = newchar; return *this; } //cout 类型就是ostream,第一个参数是ostream,不是对象自身,所以选择友元方式重载 //返回值还是ostream&,保证接下来的对象还能够被继续输出, //例如 cout << str << endl;//cout << str返回值还是ostream,所以可以继续输出<< endl ostream& operator<<(ostream& os, const String& str) { os << str.str_; return os; } istream& operator >> (istream& is, String& str) { char tmp[1024]; cin >> tmp; str = tmp; return is; } void String::Display() const { cout << str_ << endl; } String::~String() { delete[] str_; }
main.cpp
#include "String.h" //#include<iostream> // //using namespace std; int main() { String s1("abcdefg"); //s1.Display(); char ch = s1[2]; //cout << ch << endl; s1[2] = 'A'; //s1.Display(); const String s2("abcdefgh"); //s2[2] = 'C'; //s2.Display(); String s3 = "xxx";//String(const char* str="");前面不能加explicit,需要隐式转化 String s4 = "yyy"; String s5 = s3 + s4; s5.Display(); //如果以函数方式重载,不允许,因为成员函数隐含的一个参数是对象自身,不能够以"aaa"任意字符串开头 String s6 = "aaa" + s3;//不予许"bbb" + "aaa".因为String operator+(const String& s1, const String& s2)。 s6.Display(); s3 += s4; s3.Display(); cout << s3 << endl; String s7; cin >> s7; cout << s7 << endl; return 0; }
参考
1、https://blog.csdn.net/zgl_dm/article/details/1767201
2、https://blog.csdn.net/insistGoGo/article/details/6626952
3、https://blog.csdn.net/zhuzhaoming1994/article/details/80371779
4、https://www.cnblogs.com/xiaokang01/p/9166745.html
5、https://www.cnblogs.com/renyuan/p/6555172.html -
成员函数重载运算符和友元函数重载运算符的比较
2016-02-23 12:04:271.对双目运算符而言,成员函数重载运算符的函数参数表中只有一个参数,而用友元函数重载运算符函数参数表中含有两个参数。 对单木运算符来说,成员函数重载运算符的函数参数表中没有参数,而用友元函数重载运算符...1.对双目运算符而言,成员函数重载运算符的函数参数表中只有一个参数,而用友元函数重载运算符函数参数表中含有两个参数。
对单木运算符来说,成员函数重载运算符的函数参数表中没有参数,而用友元函数重载运算符函数参数表中含有一个函数。这个问题要搞清楚,有一个this指针的问题。。。
2.双目运算符一般可以用友元函数重载和成员函数重载,但有一种情况只可以用友元函数重载。
即:双目运算符左边的变量是一个常量,而不是对象!!!这点很重要的额。
书上推荐的一般经验:
1.对于单目运算符,建议选择成员函数;
2.对于运算符“=,(),[],->”只能作为成员函数;
3.对于运算符“+ =,-=,/=,*=,&=,!=,~=,%=,<<=,>>=”建议重载为成员函数;
4.对于其他运算符,建议重载为友元函数。
对于class中将operator函数定义为friend主要有以下考虑:1.friend function是对外公开的,而class method是属于对象的,有些情况调用不方便2.对某些需要两个参数的operator function,定义friend比较方便,如下例中operator <<3.所有class method必须有匹配的左值类型进行调用而friend则无需这样,只要能隐式转化成当前类型就可以调用该函数,因此如下例构造函数没有定义为explicit的,可以进行隐式转化,就可以在不同类型间运算。下面的例子可以很好的说明定义为friend function的好处.
#include <iostream.h>
class point
{
int x;
int y;
public:
point(int vx=0){x=vx;y=0;}
point(int vx,int vy):x(vx),y(vy){}
friend point operator +(point p1,point p2);
friend ostream & operator <<(ostream &output,point &p1);
};
point operator +(point p1,point p2)
{
point p;
p.x=p1.x+p2.x;
p.y=p1.y+p2.y;
return p;
}
ostream & operator <<(ostream &output,point &p1)
{
output<<p1.x<<'+'<<p1.y<<'i'<<endl;
return output;
}
int main()
{
point p1(1,2),p2(5,6);
point p3,p4;
p3=p1+p2;
p4=1+p2;//如果定义为class method,编译将出错!类提供了相应的构造函数,且非explicit
cout<<p1<<p2;
cout<<p3<<p4;
return 0;
}
FROM: http://blog.163.com/arthur0808@126/blog/static/17120709720109163859518/
-
c++ 运算符重载之成员函数重载
2016-03-30 13:35:34c++ 运算符重载之成员函数重载 -
【C++学习】| (01)运算符重载(非成员函数重载和成员函数重载
2019-08-14 10:59:55分为成员操作符重载(number function)和非成员操作符重载(non-number functon),并不是所有的操作符都可以进行非成员函数操作符重载的,具体可以参考官网:http://www.cplusplus.com/doc/tutorial/templates/ ... -
只能使用成员函数重载的运算符
2019-07-03 22:24:18只能使用成员函数重载的运算符有:=、()、[]、-> 只能使用成员函数重载是为了防止出现:1=x,1[x],1->x,1(x)这样的合法语句 -
类成员函数重载问题
2020-10-14 21:18:31类成员函数重载问题 #include <iostream> using namespace std; class A { private: int a; int b; public: void AA(int a,int b)//一 {} void AA(int a,int b) const// 二 { a=1; b=1; } void ... -
类成员函数重载与函数指针
2018-10-07 14:52:01在有成员函数重载的情况下该如何使用函数指针呢 class l { public: void func(); void func(int, int); }; void l::func() { cout << "func()" << endl; } void l::func... -
(P22)运算符重载:运算符重载 ,成员函数重载 ,非成员函数重载 ,运算符重载规则
2020-09-13 22:44:21文章目录 实现一个复数类 03:27 -
【C++】成员函数重载二元和一元运算符
2017-09-22 09:37:28重载双目运算符时,运算符函数中应该具有两个参数,若运算符函数作为类的成员函数(当运算符...对双目运算符而言,成员函数重载运算符的函数参数表中只有一个参数,而用友元函数重载运算符函数参数表中含有两个参数。 成 -
const成员函数重载
2019-09-18 12:38:12除了常规的函数重载 在函数后加上一个const也可以实现重载,即使参数列表完全相同 调用时,根据对象的const性选择重载函数 -
C++基础知识复习--运算符重载(非成员函数重载和成员函数重载)
2017-03-09 14:33:37//非成员函数重载 Complex operator -(Complex C1, Complex C2) { Complex Temp; Temp.Vector_x= C1.getVector_x()- C2.getVector_x(); Temp.Vector_y = C1.getVector_y() - C2.getVector_y(); return Temp; } //... -
c++中成员函数重载
2021-02-21 11:11:27函数重载简单说是一个函数实现多个功能,函数名称相同但函数的参数不同或参数类型不同。 int add(int a,int b); float add(float a,float b); int add(int a,int b,int c); 以上三个函数函数名称相同,但参数... -
PTA 习题:使用成员函数重载复数类的运算符+
2020-05-11 17:22:56使用成员函数重载复数类的运算符+ 类Complex声明了一个复数类,有两个数据成员realPart(代表复数的实部)和imgPart(代表复数的虚部),并定义了成员函数实现了重载运算符“+”以实现两个复数对象的相加操作。成员... -
C++之类成员函数重载学习总结
2020-08-12 00:11:00一、类中的函数重载:1、函数重载的温习:函数重载的本质为相互独立的不同函数C++中通过函数名和函数参数确定函数调用无法直接通过函数名得到函数重载函数的入口地址函数重载必然发生在同一个作用... -
用友元函数重载乘法,用成员函数重载除法
2018-12-21 00:04:42#include<iostream> using namespace std; class Complex { private: int real;//记录实部; ... Complex(){}//构造函数并对其赋初值为0;...//重载构造两int型函数; friend ... -
C++ 重载运算符 运算符重载函数作为类成员函数 重载运算符+
2017-09-20 19:27:31用运算符重载函数作为类成员函数的方法重载运算符+ 下面的例子来自于课本: #include using namespace std; class Complex { public: Complex() //无参构造函数 { real = 0; imag = 0; } Co -
用 成员函数 重载++运算符
2011-07-30 21:23:34// 用成员函数重载 ++ 运算符 // 前缀运算符重载,定义一个不带参数的的重载函数, // 后缀运算符重载,必须定义一个带有单个参数的重载函数.But why?? #include using namespace std; class Coordinates //