📄 2-1.cpp
字号:
#include <iostream>
#include <cmath>
using namespace std;
class Point /*点类定义*/
{
public: /*外部借口,公有成员函数*/
friend double Distance(Point &p,Point &q);
Point(float xx = 0,float yy = 0) /*构造函数*/
{
X=xx;
Y=yy;
cout <<"构造函数被调用Point(float xx = 0,float yy = 0)"<<endl;
}
Point(Point &p) /*拷贝构造函数*/
{
X=p.X;
Y=p.Y;
cout <<"拷贝构造函数被调用Point(Point &p)"<<endl;
}
float GetX() {return X;};
float GetY() {return Y;};
private: /*私有数据成员*/
float X,Y;
};
double Distance(Point &p,Point &q)
{
float result;
return sqrt((p.X-q.X)*(p.X-q.X)+(p.Y-q.Y)*(p.Y-q.Y));
}
//演示两点距离
/*int main()
{
Point p(0,0),q(4,3);
double d= distance (p,q);
cout << d << endl;
system("pause");
return 0;
} */
///////////////////////////////////////////////////////////////////////////////////////
class Rectangle
{
public: /*外部借口,公有成员函数*/
Rectangle(Rectangle &); /*拷贝构造函数*/
Rectangle(Point xp1,Point xp2); /*构造函数*/
double GetArea(){return area;}
private:
Point p1,p2;
double area;
};
Rectangle::Rectangle(Point xp1,Point xp2):p1(xp1),p2(xp2)
{
float l=p1.GetX()>p2.GetX()?p1.GetX()-p2.GetX():p2.GetX()-p1.GetX();
float w=p1.GetY()>p2.GetY()?p1.GetY()-p2.GetY():p2.GetY()-p1.GetY();
cout <<"构造函数被调用Rectangle(Point xp1,Point xp2)"<<endl;
area = w*l;
}
Rectangle::Rectangle(Rectangle &R):p1(R.p1),p2(R.p2)
{
area=R.area;
cout <<"拷贝构造函数被调用Rectangle(Rectangle &)"<<endl;
}
//演示矩形面积
int main()
{
float px,py,qx,qy;
char temp;
cout << "请输入两个点的坐标(px,py),(qx,qy):" << endl;
cin >>px>>py>>qx>>qy ;
Point myp1(px,py),myp2(qx,qy); /*生成对象*/
Rectangle rec(myp1,myp2);
cout << "矩形面积为:" << rec.GetArea() << endl;
system("pause");
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -