2021-11-01

每日100!		
			自引用指针 this

//1表示内存地址
#include
using namespace std;
class This {
int a;
public:
This(int aa){
a = aa;
}
void print(){
cout << “内存地址:” << this << endl;
cout << "a = " << a << endl;
}
};

//2 this->x = x;
class This2 {
int x,y;
public:
This2(int x = 0,int y = 0){
this->x;
this->y;
}
void judge (This2 &A){
if(this == &A){
cout << “the same” << endl;
} else{
*this = A;
}
}
void print(){
cout << “x =” << x << endl;
cout << “y =” << y << endl;
}
};

//3返回*this
class This3 {
private:
double real,imag;
public:
This3(double r = 0.0,double i = 0.0){
real = r;
imag = i;
}
This3 Add(This3 C,This3 D);
double GetR() {
return real;
}
double GetI() {
return imag;
}
};
This3 This3::Add(This3 C1,This3 C2) {
real = C1.real + C2.real;
imag = C1.imag + C2.imag;
return *this;
}

Main.cpp
#include “This.h”
#include “This2.h”
#include “This3.h”
#include

using namespace std;
int main(){
This A(1);
A.print();
cout << “------------------------------------------------------” << endl;
This2 B(4,5);
B.judge(B);
B.print();
cout << “------------------------------------------------------” << endl;
This3 C1(7.1,5.5),C2(4,4),C3;
cout << C3.Add(C1,C2).GetR() << " " << C3.Add(C1,C2).GetI() ;
return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值