C++——对象数组

Coordinate.h

#include "stdafx.h"
#include <iostream>


using namespace std;

class Coordinate
{
public:
	Coordinate();
	~Coordinate();

	int i_x;
	int i_y;
};

Coordinate.cpp

#include "stdafx.h"
#include <iostream>
#include "Coordinate.h"

using namespace std;

Coordinate::Coordinate()
{
	cout << "Coordinate::Coordinate()" << endl;
}
Coordinate::~Coordinate()
{
	cout << "Coordinate::~Coordinate()" << endl;
}

main.cpp

// ConsoleApplication3.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include "Coordinate.h"

int _tmain(int argc, _TCHAR* argv[])
{
	Coordinate  c[3];
	c[0].i_x = 1;
	c[0].i_y = 2;

	Coordinate *p = new Coordinate[3];
	p->i_x = 10;
	p[0].i_y = 20;

	p++;
	p->i_x = 40;
	p->i_y = 50;

	p++;
	p->i_x = 60;
	p[0].i_y = 70;

	for (int i = 0; i < 3; i++)
	{
		cout << "c[" << i << "].x = " << c[i].i_x << endl;
		cout << "c[" << i << "].y = " << c[i].i_y << endl;
	}

	for (int j = 0; j < 3; j++)
	{
		cout << "p[" << j << "].x = " << p->i_x << endl;
		cout << "p[" << j << "].y = " << p->i_y << endl;
		p--;//前边经过了两次的p++,现在p指向p+2的位置,所以要从后往前遍历
	}
	
	p++;//没有p++将会出现错误,因为执行了for循环,p指针指向的垃圾值,要将p重新指向p[0]的位置
	delete []p;
	p = NULL;

	system("pause");
	return 0;
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

guozipi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值