鱼C论坛

 找回密码
 立即注册
查看: 2471|回复: 3

求纠错,函数是可以计算线段长度(line)

[复制链接]
发表于 2021-3-22 08:42:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;

  4. class point
  5. {
  6. public:
  7.          point(int xx=0,int yy=0)
  8.         {
  9.         x=xx;
  10.         y=yy;
  11.         }
  12.         point(point &p);
  13.         int getX()  {return x;}
  14.         int getY()  {return y;}
  15. private:
  16.         int x,y;
  17. };
  18. point::point(point &p)
  19.         {
  20.         x=p.x;
  21.         y=p.y;
  22.         cout<<"calling the copy cnstructor"<<endl;
  23.         }
  24. class line {
  25.         public:
  26.                 line(point xp1,point xp2);
  27.                 line(line &1);
  28.                 double getlen() {return len;}
  29.         private:
  30.                 point p1,p2;
  31.                 double len;
  32.                 };
  33. line::line(point xp1,point xp2):p1(xp1),p2(xp2){
  34.         cout<<"calling constructing of line"<<endl;
  35.         double x=static_cast<double>(p1.getX()-p2.getX());
  36.         double y=static_cast<double>(p1.getY()-p2.getY());
  37.         len=sqrt(x*x+y*y);
  38. }
  39. line::line(line &1):p1(1,p1),p2(1,p2){
  40.         cout<<"calling the copy constructor of line"<<endl;
  41.         len=1.len;
  42. }
  43.   


  44. int main()
  45. {point myp1(1,1),myp2(4,5);
  46. line line(myp1,myp2);
  47. line line2(line);
  48. cout<<"the length of the line is :"<<line.getlen();
  49. cout<<"the length of the line 2is :"<<line.getlen();
  50. return 0;
  51. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-22 11:13:40 | 显示全部楼层
  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;

  4. class point
  5. {
  6. public:
  7.         point(int xx=0,int yy=0)
  8.         {
  9.         x=xx;
  10.         y=yy;
  11.         }
  12.         point(point &p);
  13.         int getX()  {return x;}
  14.         int getY()  {return y;}
  15. private:
  16.         int x,y;
  17. };
  18. point::point(point &p)
  19. {
  20.         x=p.x;
  21.         y=p.y;
  22.         cout<<"calling the copy cnstructor"<<endl;
  23. }
  24. class line {
  25. public:
  26.         line(point xp1,point xp2);
  27.         //c/c++变量命名规则,必须以字母或下划线开头,可以包含字母、数字、下划线
  28.         //这里用数字 1 开头作为变量名错误
  29.         line(line &l);
  30.         double getlen() {return len;}
  31. private:
  32.         point p1,p2;
  33.         double len;
  34. };
  35. line::line(point xp1,point xp2):p1(xp1),p2(xp2){
  36.         cout<<"calling constructing of line"<<endl;
  37.         double x=static_cast<double>(p1.getX()-p2.getX());
  38.         double y=static_cast<double>(p1.getY()-p2.getY());
  39.         len=sqrt(x*x+y*y);
  40. }
  41. //这里用数字 1 开头作为变量名错误,给p1,p2赋值错误
  42. line::line(line &l):p1(l.p1),p2(l.p2){
  43.         cout<<"calling the copy constructor of line"<<endl;
  44.         len=l.len;
  45. }



  46. int main()
  47. {point myp1(1,1),myp2(4,5);
  48. //变量名不要和类名相同
  49. line line1(myp1,myp2);
  50. line line2(line1);
  51. cout<<"the length of the line is :"<<line1.getlen()<<endl;
  52. cout<<"the length of the line 2is :"<<line2.getlen()<<endl;
  53. return 0;
  54. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-22 21:10:00 | 显示全部楼层
懂了,但大佬,这句程序是什么意思啊
  1. line::line(line &l):p1(l.p1),p2(l.p2){
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-23 08:44:10 | 显示全部楼层
vaety 发表于 2021-3-22 21:10
懂了,但大佬,这句程序是什么意思啊

这句是拷贝构造函数初始化成员变量啊。
line(line & l); 这是拷贝构造函数,
如果成员变量里没有对象变量(某些对象也可以用缺省的拷贝函数),没有指针变量,
是不需要重载(写)拷贝构造函数的,c++会给类一个缺省的拷贝构造函数。

  1. line::line(line &l):p1(l.p1),p2(l.p2)
  2. {
  3. }
  4. 相当于
  5. line:line(line &l)
  6. {
  7.      p1 = l.p1;
  8.      p2 = l.p2;
  9. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 18:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表