Point类的实现——C++学习笔记

点类的定义

point.h:

//point.h

#ifndef _POINT_              
#define _POINT_

//点类的定义
class Point {
public:
	//默认构造
	Point();
	//构造函数
	Point(double, double);
	//拷贝构造
	Point(const Point&);
	//获取点x,y坐标
	double getX();					
	double getY();
	//设置点坐标
	void setPoint(double, double);
	//重载赋值运算
	void operator=(const Point&);
	//打印点坐标			
	void show();									

private:
	double x, y;
};
#endif  

point.cpp:

//point.cpp

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

using namespace std;

//点类的实现
Point::Point() {									//默认构造
	this->x = 0;
	this->y = 0;
}
Point::Point(double x, double y) {					//构造函数
	this->x = x;
	this->y = y;
}
Point::Point(const Point& p){						//拷贝构造
	this->x = p.x;
	this->y = p.y;
}							
	
double Point::getX() { return x; }					//获取点x坐标
double Point::getY() { return y; }					//获取点y坐标

void Point::setPoint(double x, double y){			//设置点坐标

	this->x = x; 
	this->y = y;
}

void Point::operator=(const Point& p) {				//重载赋值运算
	this->x = p.x;
	this->y = p.y;
}

void Point::show(){									//打印点坐标
	cout << "(" << x << "," << y << ")"<<endl;
}

主函数:

//main
#include<iostream>
#include "point.h"
using namespace std;

int main() {

	Point a;
	Point b(1.2, 2.3);
	Point c = b;

	a = c;
	double m = 8;

	b.setPoint(5, m);

	double x = b.getX(), y = b.getY();
	cout << x << "  " << y << endl;
	a.show();
	a.show();
	c.show();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

渊鱼L

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

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

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

打赏作者

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

抵扣说明:

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

余额充值