运算符重载
赋值运算符重载
赋值运算符是用来将一个类的对象赋值给另一个同类的对象。如果没有显示的定义,编译器会提供默认的赋值运算符,将一个对象中的给个数据成员的值都赋给另一个对象。
#include <iostream>
#include <cstring>
using namespace std;
class Student
{
public:
Student(char *p)
{
name = new char[strlen(p)+1];
if(name != NULL)
{
strcpy(name,p);
}
}
~Student()
{
delete []name;
}
Student& operator=(char *p);
S#tudent& operator=(Student &s);
void show()
{
cout<<name<<endl;
}
private:
char *name;
};
Student& Student::operator=(char *p)
{
name = new char[strlen(p)+1];
if(name != NULL)
{
strcpy(name,p);
}
return *this;
}
Student& Student::operator=(Student &s)
{
if(this != &s)
{
delete name;
name = new char[strlen(s.name)+1];
if(name != NULL)
{
strcpy(name,s.name);
}
}
return *this;
}
int main()
{
Student s1("tjk"),s2("hhh");
s1.show();
s2.show();
s1 = s2;
s2 = "tjk";
s1.show();
s2.show();
return 0;
}
[]
运算符重载
不可用友元重载
<类型>operator[](int)
- 有且只有一个形参,作为下标值。
- 为了保持运算符
[]
的原义,将其参数规定为整型,并规定最小下标值为0,而函数的返回值正是对应于该参数所取值的那一个所谓“下标变量”的具体取值。 - 如果返回值为一个引用,那么“数组元素”即可用在赋值语句的左边又可用在右边。
#include <iostream>
using namespace std;
class Point
{
public:
Point(int x = 0, int y = 0)
{
_x = x;
_y = y;
}
~Point()
{
;// cout<<"destructor"<<endl;
}
int& operator[](int);
private:
int _x;
int _y;
};
int& Point::operator[](int i)
{
//需要加入越界检查
if(i == 0)
{
return _x;
}
return _y;
}
int main()
{
Point p1(5,8);
cout<<p1[0]<<endl;
p1[0] = 100;
cout<<p1[0]<<endl;
return 0;
}
()
运算符重载
只能以成员函数重载。
<类型>operator ()(参数)
#include <iostream>
using namespace std;
class Func
{
public:
Func(int a = 0,int b = 0,int c = 0)
{
_a = a;
_b = b;
_c = c;
}
int operator()(int x ,int y,int z);
private:
int _a;
int _b;
int _c;
};
int Func::operator()(int x ,int y,int z)
{
return x * _a + y * _b + z * _c;
}
int main()
{
Func f(1,2,3);
cout<<f(2,3,4)<<endl;
return 0;
}
重载IO流的输入和输出
对插入符<<
和提取符>>
进行重载。由于重载插入符<<
和提取符>>
时,左边是的参数是流,而右边的参数是类的对象,因此只能用友元函数重载。
ostream& operator << (ostream& out,<类名>&)
{
...
return out
}
输出:
class Point
{
public:
Point(int x = 0, int y = 0)
{
_x = x;
_y = y;
}
~Point()
{
;// cout<<"destructor"<<endl;
}
Point operator+(Point pt);
friend ostream& operator<<(ostream &out,Point &p);
private:
int _x;
int _y;
};
Point Point::operator+(Point pt)
{
Point temp;
temp._x = this->_x + pt._x;
temp._y = this->_y + pt._y;
return temp;
}
ostream& operator<<(ostream &out,Point &p)
{
cout<<"["<<p._x<<","<<p._y<<"]"<<endl;
return out;
}
int main()
{
Point p1(1,2);
Point p2(1,4);
p1 = p1 + p2;
cout<<p1<<p2<<endl;
return 0;
}
输入
class Point
{
public:
Point(int x = 0, int y = 0)
{
_x = x;
_y = y;
}
~Point()
{
;// cout<<"destructor"<<endl;
}
Point operator+(Point pt);
friend ostream& operator<<(ostream &out,Point &p);
friend istream& operator>>(istream &out,Point &p);
private:
int _x;
int _y;
};
Point Point::operator+(Point pt)
{
Point temp;
temp._x = this->_x + pt._x;
temp._y = this->_y + pt._y;
return temp;
}
ostream& operator<<(ostream &out,Point &p)
{
cout<<"["<<p._x<<","<<p._y<<"]"<<endl;
return out;
}
istream& operator>>(istream &in,Point &p)
{
cin>>p._x>>p._y;
return in;
}
int main()
{
Point p1(1,2);
Point p2(1,4);
Point p3;
p1 = p1 + p2;
cout<<p1<<p2<<endl;
cin>>p3;
cout<<p3;
return 0;
}