栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 实验六 多态的应用

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 实验六 多态的应用

文章目录
    • 前言
    • 一、要求
    • 二、分析
    • 三、代码
    • 四、 结果

前言

本文章是 【C++笔记】 专栏的文章,主要是C++黑马的笔记、自己的实验与课设
C++笔记 传送门

一、要求

定义Point类,有坐标x,y两个数据成员

分别用成员函数和友元函数实现对Point类重载“+”、“-”、“++”、“《”、“》”(包括前增和后增)运算符,实现对坐标值的改变

二、分析

重载运算符的格式为函数类型 operator 运算符(形参列表)

重载流插入运算符ostream& operator <<(ostream &, 自定义类&)

重载流提取运算符istream &operator>>(istream &, 自定义类&)

流插入<<和流提取>>不能定义类的成员函数,只能作为友元函数

Point operator ++()是前增,可以通过this指针自由访问本类,加上int,Point operator ++(int),变为后增

分析以上概念后,分两个源文重载“+”、“-”、“++”、 “–”,重载“+”、“-”、“++”、 “–”、“《”、“》”。都定义Point类,其中友元函数的定义格式与成员函数略有区别,成员函数比友元函数少写一个参数即*this类本身,友元函数需要自定义参数列表(Point &p0)

三、代码

提示:所有实验源码已在github整理

//成员函数对“+”、“-”、“++”、 “--”重载
#include
using namespace std;

class Point
{
private:
	double x, y;
public:
	Point() {};
	Point(double x, double y)
	{
		this->x = x;
		this->y = y;
	}
	void show()
	{
		cout << "(" << this->x << "," << this->y << ")" << endl;
	}
	Point operator + (Point &p2) 
	{
		Point p;
		p.x =x+ p2.x;
		p.y =y+ p2.y;
		return p;
	}
	Point operator - (Point &p2)
	{
		Point p;
		p.x = x - p2.x;
		p.y = y - p2.y;
		return p;
	}
	//前增
	Point operator ++()
	{
		//先加
		x++;
		y++;
		//后使用
		return *this;
	}
	//后增
	Point operator ++(int)
	{
		//先使用(备份)
		Point temp=*this;
		//后加
		++(*this);
		return temp;
	}
	//前减
	Point operator --()
	{
		x--;
		y--;
		return *this;
	}
	//后减
	Point operator --(int)
	{
		Point temp = *this;
		--(*this);
		return temp;
	}
};

int main()
{
	Point p0(1, 1);
	Point p1(1, 1);
	cout << "p0";
	p0.show();
	cout << "p1";
	p1.show();
	cout << "p0+p1=";
	Point p2 = p0 + p1;
	p2.show();
	Point p3 = p1 - p0;
	cout << "p1-p0=";
	p3.show();
	cout << "++p0:";
	(++p0).show();
	cout << "p0++:";
	(p0++).show();
	cout << "p0++后p0:";
	p0.show();
	cout << "--p1:";
	(--p1).show();
	cout << "p1--:";
	(p1--).show();
	cout << "p1--后p1:";
	p1.show();
	
	system("pause");
	return 0;
}

//友元函数对“+”、“-”、“++”、 “--”、“《”、“》”重载
#include
using namespace std;

class Point
{
private:
	double x, y;
public:
	
	Point() {};
	Point(double x, double y)
	{
		this->x = x;
		this->y = y;
	}
	void show()
	{
		cout << "(" << this->x << "," << this->y << ")" << endl;
	}
	friend Point operator + (Point &p1,Point &p2);
	friend Point operator - (Point &p1, Point &p2);
	friend Point operator ++(Point &p0);
	friend Point operator ++(Point &p0,int);
	friend Point operator --(Point &p0);
	friend Point operator --(Point &p0,int);
	friend ostream& operator <<(ostream &out, Point &p0);
	friend istream &operator>>(istream &in, Point &p0);
};
Point operator + (Point &p1, Point &p2)
{
	Point p;
	p.x = p1.x + p2.x;
	p.y = p1.y + p2.y;
	return p;
}
Point operator - (Point &p1, Point &p2)
{
	Point p;
	p.x = p1.x - p2.x;
	p.y = p1.y - p2.y;
	return p;
}
//前增
Point operator ++(Point &p0)
{
	//先加
	p0.x++;
	p0.y++;
	//后使用
	return p0;
}
//后增
Point operator ++(Point &p0,int)
{
	//先使用(备份)
	Point temp = p0;
	//后加
	p0.x++;
	p0.y++;
	return temp;
}
//前减
Point operator --(Point &p0)
{
	p0.x--;
	p0.y--;
	return p0;
}
//后减
Point operator --(Point &p0,int)
{
	Point temp = p0;
	p0.x--;
	p0.y--;
	return temp;
}

ostream &operator <<(ostream &out, Point &p0)
{
	out <<"("<< p0.x<<","<>(istream &in, Point &p0)
{
	in >> p0.x >> p0.y;
	return in;
}
int main()
{
	Point p0;
	cout << "输入p0" << endl;
	cin >> p0;//提取运算符重载
	cout << "p0="<< p0 << endl;//流插入运算符重载

	Point p1;
	cout << "输入p1" << endl;
	cin >> p1;//提取运算符重载
	cout << "p1=" << p1 << endl;//流插入运算符重载
	
	Point p3 = p0 + p1;//加法重载
	cout << "p3=p0+p1=" << p3 << endl;
	Point p4 = p0 - p1;//减法重载
	cout << "p4=p0-p1=" << p4 << endl;
	cout << "++p0:";//前置++
	(++p0).show();
	cout << "p0++:";//后置++
	(p0++).show();
	cout << "p0++后p0:";
	p0.show();
	cout << "--p1:";//前置--
	(--p1).show();
	cout << "p1--:";//后置--
	(p1--).show();
	cout << "p1--后p1:";
	p1.show();

	system("pause");
	return 0;
}
四、 结果

成员函数对“+”、“-”、“++”、 “–”重载

友元函数对“+”、“-”、“++”、 “–”、“《”、“》”重载

其它参考C++前置自增运算符和后置自增运算符的重载 趋之若鹜的博客-CSDN博客_后置自增运算符重载,cin和cout重载实现详解_code配上格子衫的博客-CSDN博客_重载cout

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/931637.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号