📄 p9_4.cpp
字号:
/*******************************************
* p9_4.cpp *
* 重载->为类的成员函数 *
*******************************************/
#include<iostream>
using namespace std;
class Complex //复数类定义
{
private:
double real; //复数实部
double image; //复数虚部
public:
Complex(double real=0.0,double image=0.0) //构造函数
{
this->real=real,this->image=image;
}
void display()
{
cout<<"("<<real<<","<<image<<")"<<endl;
}
};
class PComplex //复数指针类定义
{
private:
Complex * PC;
public:
PComplex(Complex * PC=NULL)
{
this->PC=PC;
}
Complex * operator->()
{
static Complex NullComplex(0,0); //避免指针为空
if(PC==NULL)
{
return &NullComplex;
}
return PC;
}
};
void main()
{
PComplex P1; //指针未初始化
P1->display(); //显示预先定义的(0,0)
Complex C1(100,200);
P1=&C1; //指针未初始化
P1->display(); //显示有效数据
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -