//"pointer0.h"
class Point
{
public:
Point (int xx=0,int yy=0){X=xx;Y=yy;}
int GetX() { return X;}
int GetY() { return Y;}
private:
int X;
int Y;
};
#include<iostream.h>
#include "pointer0.h"
//使用对象指针访问类的成员
void main()
{
Point A(4,5);
Point *p1;//声明对象指针
p1=&A;//初始化指针
cout<<p1->GetX()<<endl;
cout<<A.GetX()<<endl;
}
本文通过一个C++示例介绍了如何定义一个简单的类并使用对象指针来访问该类的成员变量。示例中定义了一个名为Point的类,包含两个私有成员变量X和Y,以及获取这两个变量值的公共成员函数GetX和GetY。主函数中创建了Point类的对象A,并通过对象指针p1调用了GetX函数。
1370

被折叠的 条评论
为什么被折叠?



