鱼C论坛

 找回密码
 立即注册
查看: 3826|回复: 2

这个程序存在什么问题?

[复制链接]
发表于 2013-5-28 10:05:04 | 显示全部楼层 |阅读模式
1鱼币
#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 constructor of Point"<<endl;
}
class Line
{
public:
        Line(Point xp1,Point xp2);
        Line(Line&1);
        double getLen(){return len;}
private:
        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 &1):p1(1.p1),p2(1.p2){
        cout<<"Calling the copy constructor of Line"<<endl;
    len=1.len;}
int main()
{
        Point myp1(1,1),myp2(4,5);
    Line line(myp1,myp2);
    Line line2(line);
    cout<<"The lenght of the line is:";
    cout<<line.getLen()<<endl;
    cout<<"The lenght of the line2 is:";
    cout<<line2.getLen()<<endl;
    return 0;
}

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-5-28 23:11:45 | 显示全部楼层
帮你整理了一下,其实就是有一个参数,(Line &1),把1改成其他的字母就行了,这里显然不能给一个常量。。。
  1. class Point
  2. {
  3. public:
  4.         Point(int xx=0,int yy=0)
  5.         {
  6.                 x = xx;
  7.                 y = yy;
  8.         }
  9.         Point(Point&p);
  10.         int getX(){return x;}
  11.         int getY(){return y;}

  12. private:
  13.         int x;
  14.         int y;
  15. };

  16. Point::Point(Point&p)
  17. {
  18.         x = p.x;
  19.                   
  20.         y = p.y;
  21.                   
  22.         cout<<"Calling the constructor of Point"<<endl;
  23. }



  24. class Line
  25. {
  26. public:
  27.         Line(Point xp1,Point xp2);
  28.         Line(Line&l);
  29.         double getLen(){return len;}
  30. private:
  31.         Point p1;
  32.         Point p2;
  33.         double len;
  34. };
  35. Line::Line(Point xp1,Point xp2):p1(xp1),p2(xp2)
  36. {
  37.                  cout<<"Calling constructor of Line"<<endl;
  38.                  double x=static_cast<double>(p1.getX()-p2.getX());
  39.                  double y=static_cast<double>(p1.getY()-p2.getY());
  40.                  len=sqrt(x*x+y*y);
  41. }
  42. Line::Line(Line &l):p1(l.p1),p2(l.p2)
  43. {
  44.          cout<<"Calling the copy constructor of Line"<<endl;
  45.          len=l.len;
  46. }
复制代码
运行结果如下:
1111111111.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-6-3 18:02:13 | 显示全部楼层
路过。。。打酱油
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-27 16:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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