C++数组和类

来源:互联网 发布:淘宝网百度一下 编辑:程序博客网 时间:2024/05/16 17:49

C++中类的引用产生了新的数组类型,对象数组和对象指针数组下面已经定义的Point类定义的一般类的对象、数组对象、对象指针数组的实例。

Point p1,p2[10];

Point *p3[5];

其中p1是一个普通对象、p2是一个对象数组,它有10个元素每个元素都是一个Point类的对象,p3[5]是一个对象指针数组。

访问对象的公有有成员函数的不同方式的例子:

#include <iostream>/* run this program using the console pauser or add your own getch, system("pause") or input loop */using namespace std;class Point{private:int X;int Y;public:Point(int xx=0,int yy=0){X=xx;Y=yy;}int GetX(){return X;}int getY(){return Y;}};int main(int argc, char** argv) {Point A(4,5);Point *p1 = &A;int(Point:: *p_GetX)() = &Point::GetX;cout<<(A.*p_GetX)()<<endl;cout<<(p1->GetX())<<endl;return 0;}


原创粉丝点击