C++:This指针示例

本文展示了C++中一个名为Point的类的实现,包括构造函数、成员函数(SetPoint、GetPoint、GetX、GetY)以及对象的创建和使用。在主函数中,创建了Point对象并演示了如何设置和获取点的坐标。
摘要由CSDN通过智能技术生成
#pragma once
#include <iostream>
using namespace std;
class Point
{
public:
	Point();		//构造函数
	Point(float x, float y);//构造函数
	void SetPoint(float x, float y);
	Point GetPoint();
	float GetX();
	float GetY();
	~Point();
private:
	float x, y;
};

#include "stdafx.h"
#include "Point.h"


Point::Point()
{
}

Point::Point(float x, float y)//构造函数
{
	this->x = x;
	this->y = y;
	
}
void Point::SetPoint(float x, float y)
{
	this->x = x;
	this->y = y;
}
Point Point::GetPoint()
{
	Point Rpoint;		//返回的点
	Rpoint.x = this->x;
	Rpoint.y = this->y;
	return Rpoint;
}
float Point::GetX()
{
	return this->x;
}
float Point::GetY()
{
	return this->y;
}
Point::~Point()
{
}
// thisPointer_example.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "Point.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	Point p(2.2f, 3.3f);			//此时构造函数中的this指针指向对象p
	cout << "点坐标是:" << p.GetPoint().GetX() << ", " << p.GetPoint().GetY() << endl;
	cout << "点坐标是:" << p.GetX() << ", " << p.GetY() << endl;
	p.SetPoint(3.5f, 4.2f);
	cout << "点坐标是:" << p.GetX() << ", " << p.GetY() << endl;
	getchar();
	return 0;
}

运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值