⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 5_18.cpp

📁 c++书籍的源代码
💻 CPP
字号:
#include <iostream.h>
 class point	//point 类的定义
{
 public:	
  point(int a=0,int b=0);//构造函数
  ~point();
  void display(void);
  void move(int xx,int yy);
  int point::*p;//成员指针
 private:	
  int x;
  int y;
};
//成员函数的实现
 point::point(int a,int b)
{
  x=a;
  y=b;
  p=&point::x; 
  cout<<"x="<<x<<" 构造函数被调用"<<endl;
}
 point::~point()
{
  cout<<"x="<<x<<" 析构函数被调用"<<endl;
}
 void point::display(void)
{
  cout<<"x="<<x<<" y="<<y<<endl;
}
 void point::move(int xx,int yy)
{
  x+=xx;
  y+=yy;
}
//主程序
 int main()
{
  point a,*p;	
  void (point::*q)(void);//定义指向成员函数的指针
  p=&a;//对象指针p指向对象a
  q=point::display;//成员指针q指向point类的成员函数display 
  a.display();//利用对象名访问对象的成员函数
  p->display();//利用对象指针访问对象的成员函数
  (a.*q)();        //利用对象名和成员指针访问对象的成员函数
  (p->*q)();        //利用对象指针和成员指针访问对象的成员函数
  cout<<a.*(a.p)<<endl;//利用对象名和成员指针访问对象的数据成员
  cout<<p->*(a.p)<<endl;//利用对象指针和成员指针访问对象的数据成员
  return(0);
}
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -