使用一个类Line来描述线段,使用Point类的对象来表示端点,使Line类包括Point类的两个对象p1和p2,作为其数据成员。Line类具有计算线段长度的功能,在构造函数中实现
c++:使用一个类Line来描述线段,使用Point类的对象来表示端点,使Line类包括Point类的两个对象p1和p2,作为其数据成员。Line类具有计算线段长度的功能,在构造函数中实现
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- CSDN专家-深度学习进阶 2022-04-10 12:02关注
#include<iostream> #include<cmath> using namespace std; class Point { public: Point(int xx = 0, int yy = 0) {//构造函数 x = xx; y = yy; } Point(Point& p);//复制构造函数 int getX() { return x; } int getY() { return y; } private: int x, y; }; Point::Point(Point& p) {//复制构造函数的实现 x = p.x; y = p.y; cout << "Calling the copy constructor of Point" << endl; } //类的组合 class Line { public: Line(Point xp1, Point xp2); Line(Line& l); double getLen() { return len; } private: Point p1, p2;//Point类的对象p1,p2 double len; }; //组合类的构造函数 Line::Line(Point xp1, Point xp2) :p1(xp1), p2(xp2) { cout << "Calling constructor of Line" << endl; double x = static_cast<double>(p1.getX() - p2.getX()); double y = static_cast<double>(p1.getY() - p2.getY()); len = sqrt(x * x + y * y); } //组合类的复制构造函数 Line::Line(Line& l) :p1(l.p1), p2(l.p2) { cout << "Calling the copy constructor of Line" << endl; len = l.len; } //主函数 int main() { Point myp1(1, 1), myp2(4, 5);//建立Point类的对象 Line line(myp1, myp2);//建立Line类的对象 Line line2(line);//利用复制构造函数建立一个新对象 cout << "The length of the line is:"; cout << line.getLen() << endl; cout << "The length of the line2 is:"; cout << line2.getLen() << endl; return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录
悬赏问题
- ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
- ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
- ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
- ¥20 神经网络Sequential name=sequential, built=False
- ¥16 Qphython 用xlrd读取excel报错
- ¥15 单片机学习顺序问题!!
- ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
- ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
- ¥15 相敏解调 matlab
- ¥15 求lingo代码和思路