匿名对象
实际上就是用来初始化用的,可以给有名对象、对象数组等初始化
友元
作用:可以让一些函数或者一些类,去访问另一个类的私有成员
种类:全局函数、类、成员函数。这些都可以当做友元
关键字:friend
常成员函数和常对象
形容常某某某、一般使用const,表达的意思是只读、不可变的。
而常成员函数内部不可以对数据进行修改操作,保证代码安全性
常对象:const修饰的对象叫做常对象、常对象的数据成员不可修改
非 常对象,既可以调用常成员函数,也可以调用非 常成员函数,优先调用非 常成员函数
常对象只能调用常成员函数。
运算符重载
运算符重载是对运算符重新定义、赋予另外一种功能,以适应不同的数据类型
可以让构造函数也参与关系符运算
运算符简单代码实现
#include <iostream>
using namespace std;
class Student
{
//使用全局函数时,由于变量是私有的,所以需要通过friend声明来实现类外访问类内的操作
friend const Student operator*(const Student &L,const Student &R);
friend const Student operator+(const Student &L,const Student &R);
friend bool operator>(const Student &L,const Student &R);
friend Student & operator+=(Student &L,const Student &R);
friend Student &operator++(Student &O);
friend ostream &operator<<(ostream &cout,const Student &s);
friend istream &operator>>(istream &cin, Student &s);
//friend const Student operator++(Student &O,int);
private:
int a;
int b;
public:
Student(){}
Student(int a,int b):a(a),b(b){}
//成员函数实现运算符加法重载 实现了构造函数也可以使用运算符参与运算
//一个文件中不能同时存在相同功能的成员函数和全局函数的运算符重载
// 所以注释掉
//const Student operator+(const Student &R) const
// {
// Student temp;
// temp.a = a + R.a;
// temp.b = b + R.b;
// return temp;
// }
//关系运算符的重载
bool operator<(const Student &R) const
{
if(a < R.a && b < R.b)
{
return true;
}
else{
return false;
}
}
//成员函数实现+=运算
// Student & operator+=(const Student &R)
// {
// a += R.a;
// b += R.b;
// return *this;
// }
//成员函数实现前自增
// Student &operator++()
// {
// ++a;
// ++b;
// return *this;
// }
//成员函数实现后自增
const Student operator++(int)
{
Student temp;
temp.a = a++;
temp.b = b++;
return temp;
}
void show(){
cout << "a = " << a << " b = " << b << endl;
}
};
//全局函数实现运算符乘法重载
const Student operator*(const Student &L,const Student &R)
{
Student temp;
temp.a = L.a * R.a;
temp.b = L.b * L.b;
return temp;
}
//全局函数实现运算符加法重载
const Student operator+(const Student &L,const Student &R)
{
Student temp;
temp.a = L.a + R.a;
temp.b = L.b + L.b;
return temp;
}
//全局函数实现关系运算符的重载
bool operator>(const Student &L,const Student &R)
{
if(L.a < R.a && L.b < R.b)
{
return true;
}
else{
return false;
}
}
//全局函数实现+=运算
Student & operator+=(Student &L,const Student &R)
{
L.a += R.a;
L.b += R.b;
return L;
}
//全局函数实现前自增运算
Student &operator++(Student &O)
{
++O.a;
++O.b;
return O;
}
//全局函数实现后自增
//const Student operator++(Student &O,int)
//{
// Student temp;
// temp.a = O.a++;
// temp .b = O.b++;
// return O;
//}
//全局函数实现插入运算符
ostream &operator<<(ostream &cout,const Student &s)
{
cout << s.a << " " << s.b << endl;
return cout;
}
//全局函数实现提取运算符
istream &operator>>(istream &cin, Student &s)
{
cin >> s.a;
cin >> s.b;
return cin;
}
int main()
{
Student s1(10,10);
Student s2(20,20);
Student s3 = s1+s2;//两个非基本数据类型进行加法运算
s3.show();
Student s4 = s1*s2;//两个非基本数据类型进行乘法运算
s4.show();
if(s3>s4)//两个非基本数据类型进行逻辑运算
{
cout << "s3>s4" << endl;
}
else
{
cout << "s3<s4" << endl;
}
s3 += s4;
s3.show();
++s4;
s4.show();
s3++;
s3.show();
Student s5;
s5 = s2++;
s2.show();
s5.show();
cout << s4 << " " << s5 << endl;
Student s6;
cin >> s6;
s6.show();
return 0;
}
效果如图: