面向对象——构造函数1(c++)

构造函数

#include <iostream>
struct Point{					//构造点
	int x;
	int y;
	Point(int _x = 1, int _y = 1){
		this->x = _x;           // this 代表一个临时指针,通俗地说谁调用它,它就指向谁
		this->y = _y;
	}
	Point(const Point &point){
		this->x = point.x;
		(*this).y = point.y;
	}
	Point operator+(Point point){
		point.x += (*this).x;
		point.y += this->y;
		return point;
	}
	struct Line{				//构造一个线
		Point point1, point2;
		Line(Point point_1, Point point_2){
				this->point1 = point_1;
				this->point2 = point_2;
		}
	};
	void swap(){				//交换一个点的x值与y值
		int t = this->x;
		this->x = (*this).y;
		this->y = t;
		return;
	}
};
int main(){
	Point point1;
	Point point2(5);
	Point point3(-2, 5);
	Point point4 = point2;
	Point point5 = point1 + point3;
	printf("point5:(%d %d)\n", point5.x, point5.y);
	point5.swap();
	printf("point1:(%d %d)\n", point1.x, point1.y);
	printf("point2:(%d %d)\n", point2.x, point2.y);
	printf("point3:(%d %d)\n", point3.x, point3.y);
	printf("point4:(%d %d)\n", point4.x, point4.y);
	printf("point5:(%d %d)\n", point5.x, point5.y);
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值