您的位置:首页 > 编程语言 > C语言/C++

C++数组和类

2013-11-03 11:32 211 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: