继续上一篇博客,讲一讲类中的各种重载;上一篇博客中,已经把每种面向对象的语言,都可以自定义对类的操作,也即是对类的各种运算符的重载,例如你可以自定义加号,减号、自加自减符号两边的意义。
- 单目加减法±±的重载
#include <iostream>
#include <string>
using namespace std;
class A27{
public:
int a27=0;
A27(int a){this->a27=a;};
A27 operator++(){ //前缀单目运算符
++this->a27;
A27 _a(this->a27);
return _a;
}
A27 operator++(int){ //后缀单目运算符
A27 _a(this->a27);
++this->a27;
return _a;
}
};
int main(){
A27 a(2);
A27 b = a++;
cout<<a.a27<<endl;
cout<<b.a27<<endl;
A27 c(2);
A27 d = ++c;
cout<<c.a27<<endl;
cout<<d.a27<<endl;
}
//3
//2
//3
//3
- 双目运算符±*/的重载:
class A27{
public:
int a27=0;
A27(int a){this->a27=a;};
A27 operator+(A27 &a){
A27 b27(0);
b27.a27 = this->a27+a.a27;
return b27;
}
A27 operator-(A27 &a){
A27 b27(0);
b27.a27 = this->a27-a.a27;
return b27;
}
A27 operator*(A27 &a){
A27 b27(0);
b27.a27 = this->a27*a.a27;
return b27;
}
A27 operator/(A27 &a){
A27 b27(0);
b27.a27 = this->a27/a.a27;
return b27;
}
};
int main(){
A27 a(2);
A27 b(4);
A27 c = a+b;
cout<<c.a27<<endl;
c = a*b;
cout<<c.a27<<endl;
c = a-b;
cout<<c.a27<<endl;
c = a/b;
cout<<c.a27<<endl;
}
//6
//8
//-2
//0
- 重载关系运算符:(同理,小于号、逻辑等于号也可以这样处理)
class A27{
public:
int a27=0;
A27(int a){this->a27=a;};
bool operator >(const A27& a){
if (a.a27<this->a27){
return true;
}
else{
return false;
}
}
};
int main(){
A27 a(2);
A27 b(4);
bool c = b>a;
cout<<c<<endl;
}
//1 (bool为真)
- 赋值运算符“=”号重载
class A27{
public:
int a27=0;
A27(int a){this->a27=a;};
void operator=(const A27 &a){
this->a27 = a.a27;
}
};
int main(){
A27 a(2);
A27 b = a;
cout<<b.a27<<endl;
}
// 2
- ()重载(即使得类直接可以被调用)
class A27{
public:
int a27=0;
A27(int a){this->a27=a;};
A27 operator()(int a, int b, int c){
A27 b27(0);
b27.a27 = this->a27+a+b+c;
return b27;
}
};
int main(){
A27 a(2);
cout<<a.a27<<endl;
A27 b = a(1,2,3);
cout<<b.a27<<endl;
}
//2
//8
- []运算符重载
class A27{
public:
int a27=0;
int b27[10];
A27(){
register int i;
for(int i=0;i<10;i++) {
b27[i]=i;
}
};
int operator[](int i){
if (i<10){
return b27[i];
}else{
return this->b27[0];
}
}
};
int main(){
A27 a;
cout<<a[2]<<endl;
}
// 2
- 输入输出重载
class A27{
private:
int a27=0;
int b27[10];
public:
A27(){
register int i;
for(int i=0;i<10;i++) {
b27[i]=i;
}
};
A27(int a){this->a27=a;};
friend ostream &operator<<( ostream &output,
const A27 &a )
{
output << "a27 : " << a.a27 << endl;
return output;
}
friend istream &operator>>( istream &input, A27 &a )
{
input >> a.a27;
return input;
}
};
int main(){
A27 a(0);
cout<<a<<endl;
cin>>a;
cout<<a<<endl;
}
//0
//>>1
//1
重载输入输出是友元方式实现的,下面再讲讲友元:
6. 友元函数:友元并不是成员函数,以关键字friend进行区分,它可以访问类中的私有成员。其破坏了封装和引用,但是方便了开发,下面是一个友元的例子:
class point{
public:
point(int x, int y){
this->a = x;
this->b = y;
}
friend float dist(point &a, point &b);
private:
int a;
int b;
};
float dist(point &a, point &b){
return a.a+b.b;
};
int main(){
point p1(1,2), p2(3,4);
cout<<dist(p1, p2)<<endl;
}
//5
可见,友元函数可以取得类中私有变量并进行运算。
友元可以定义在内部,也可以定义在类体外部。无论定义在内部还是外部,都是直接调用友元函数名,不需要加"类."。
7. 友元类:友元类可以访问另一个类的似有变量,但另一个类不能访问友元类:
class A{ //A加入申明后,B可以访问A
friend class B;
private:
int x=0;
};
class B{
private:
A a;
public:
void seta(){
a.x = 1; //可以取得a的隐私数据
}
void printx(){
cout<<a.x<<endl;
}
};
int main(){
B b;
b.seta();
b.printx();
}
需要在被访问的类中声明友元类的名字。
总结一下,这篇笔记讲了C++的各种重载(还有->成员访问重载没讲)以及友元;之后如果有机会,继续讲讲C++的多线程、多进程、web套接字编程、vector(讲了vector之后,才能讲->重载)等等。希望能帮到大家;看完了01和02两篇笔记,应付C++期末考基本是没问题的了~
有错误、问题欢迎向我反映讨论~yixiaolan@foxmail.com