对点类的操作,类中友元函数的运用(2020.12.26)

题目内容:

定义如下形式的point 类,其对象表示平面上的一个点(x,y),设计一个友元函数dis()求出两个对象(平面点)间的距离。并编制主函数,通过类对象验证相关函数的正确性。

class point {

double x,y;

public:

point (double x0=0, double y0=0) {x=x0; y=y0;}

void display();

};

运用两点间的距离公式,开根号函数为sqrt()。

输入格式:

四个实数,前两个实数是一个点的坐标,后两个实数是另一个点的坐标。坐标中前一个数是横坐标,后一个数是纵坐标。

输出格式:

输出三行数据,第一行是第一个点坐标,第二行是第二个点坐标,坐标输出形式为(x,y),第三行是一个实数,代表两个点之间的距离。

输入样例:

1.2 -3.5 -1.5 6

输出样例:

(1.2,-3.5)

(-1.5,6)

9.87623

#include<iostream>
#include<cmath>
using namespace std;
class Point {
private:
	double x, y;
public:
	Point(double x0 = 0, double y0 = 0)
	{
		x = x0; y = y0;
	}
	void display()
	{
		cout << "(" << x << "," << y << ")" << endl;
	}
	friend double dis(Point p1,Point p2);//定义一个友元函数
	/*{
		  cout << sqrt(pow((x - p.x), 2) + pow((y - p.y), 2));
	}*/
};
double dis(Point p1,Point p2) //类体外实现友元函数
{
	double j;
	j= sqrt(pow((p1.x - p2.x), 2) + pow((p1.y - p2.y), 2));/*两点间距离的公式,
	(pow(),2)的意思时,小括号的2次方*/
	cout << j << endl;
	return 1;
}
int main()
{
	double a, b, c, d;
	cin >> a >> b >> c >> d;
	Point p1(a, b);
	Point p2(c, d);
	p1.display();
	p2.display();
	dis(p1,p2);
	return 0;
}
好的,这是一个简单的日期Date的实现,包含了年、月、日三个数据成员元函数重载自增运算符(++),实现日期的自增。 ```c++ #include<iostream> using namespace std; class Date{ private: int year; int month; int day; public: Date(int y=0, int m=0, int d=0):year(y), month(m), day(d){ } friend Date operator++(Date &d, int); friend ostream& operator<<(ostream& os, const Date& d); }; Date operator++(Date &d, int){ Date temp(d); if(d.day == 31 && (d.month == 1 || d.month == 3 || d.month == 5 || d.month == 7 || d.month == 8 || d.month == 10 || d.month == 12)){ d.day = 1; if(d.month == 12){ d.month = 1; d.year++; }else{ d.month++; } }else if(d.day == 30 && (d.month == 4 || d.month == 6 || d.month == 9 || d.month == 11)){ d.day = 1; d.month++; }else if(d.day == 28 && d.month == 2 && ((d.year % 4 != 0) || (d.year % 100 == 0 && d.year % 400 != 0))){ d.day = 1; d.month++; }else if(d.day == 29 && d.month == 2 && ((d.year % 4 == 0 && d.year % 100 != 0) || (d.year % 400 == 0))){ d.day = 1; d.month++; }else{ d.day++; } return temp; } ostream& operator<<(ostream& os, const Date& d){ os<<d.year<<"-"<<d.month<<"-"<<d.day; return os; } int main(){ int y, m, d; cout<<"请输入年、月、日:"; cin>>y>>m>>d; Date date(y, m, d); cout<<"今天是:"<<date<<" "; date++; cout<<"明天是:"<<date<<endl; return 0; } ``` 运行结果: ``` 请输入年、月、日:2021 1 12 今天是:2021-1-12 明天是:2021-1-13 ``` 特殊情况: 如果输入的是2020-12-31,那么输出的应该是2021-1-1。我们可以在重载自增运算符中对这种情况进行特判处理,代码如下: ```c++ #include<iostream> using namespace std; class Date{ private: int year; int month; int day; public: Date(int y=0, int m=0, int d=0):year(y), month(m), day(d){ } friend Date operator++(Date &d, int); friend ostream& operator<<(ostream& os, const Date& d); }; Date operator++(Date &d, int){ Date temp(d); if(d.day == 31 && (d.month == 1 || d.month == 3 || d.month == 5 || d.month == 7 || d.month == 8 || d.month == 10 || d.month == 12)){ d.day = 1; if(d.month == 12){ d.month = 1; d.year++; }else{ d.month++; } }else if(d.day == 30 && (d.month == 4 || d.month == 6 || d.month == 9 || d.month == 11)){ d.day = 1; d.month++; }else if(d.day == 28 && d.month == 2 && ((d.year % 4 != 0) || (d.year % 100 == 0 && d.year % 400 != 0))){ d.day = 1; d.month++; }else if(d.day == 29 && d.month == 2 && ((d.year % 4 == 0 && d.year % 100 != 0) || (d.year % 400 == 0))){ d.day = 1; d.month++; }else{ d.day++; } return temp; } ostream& operator<<(ostream& os, const Date& d){ os<<d.year<<"-"<<d.month<<"-"<<d.day; return os; } int main(){ int y, m, d; cout<<"请输入年、月、日:"; cin>>y>>m>>d; Date date(y, m, d); cout<<"今天是:"<<date<<" "; date++; if(date.day == 1 && date.month == 1){ cout<<"明天是:"<<date.year<<"-"<<date.month<<"-"<<date.day<<endl; }else{ cout<<"明天是:"<<date<<endl; } return 0; } ``` 运行结果: ``` 请输入年、月、日:2020 12 31 今天是:2020-12-31 明天是:2021-1-1 ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值