鱼C论坛

 找回密码
 立即注册
查看: 1989|回复: 5

多参构造函数的对象怎么作为其他函数的参数

[复制链接]
发表于 2016-4-19 18:01:09 | 显示全部楼层 |阅读模式

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

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

x
  1. #include <iostream>
  2. #include <cstring>
  3. #include<math.h>
  4. using std::cout;
  5. using std::endl;
  6. class Point{
  7. private:
  8.       double x, y;
  9. public:
  10.       Point(double xx, double yy)
  11.       {
  12.               x=xx;
  13.               y=yy;
  14.           }
  15.       double getX()
  16.       {
  17.               return x;
  18.           }
  19.       double getY()
  20.       {
  21.               return y;
  22.           }
  23. };
  24. class Triangle{
  25. private:
  26.       Point a, b, c;
  27. public:
  28.       Triangle():a(5.8,3.3),b(3.3,4.2),c(6.6,7.8)
  29.       {}
  30.       Triangle(double x1,double y1,double x2,double y2,double x3,double y3):a(x1,y1),b(x2,y2),c(x3,y3)
  31.       {}
  32.       Triangle(Point p1,Point p2,Point p3){}//这边要怎么初始化?
  33.       double getSideA()
  34.       {
  35.               double sidea;
  36.               sidea=sqrt((b.getX()-c.getX())*(b.getX()-c.getX())+(b.getY()-c.getY())*(b.getY()-c.getY()));
  37.               return sidea;
  38.           }
  39.       double getSideB()
  40.       {
  41.               double sideb;
  42.               sideb=sqrt((a.getX()-c.getX())*(a.getX()-c.getX())+(a.getY()-c.getY())*(a.getY()-c.getY()));
  43.               return sideb;
  44.           }
  45.       double getSideC()
  46.       {
  47.               double sidec;
  48.               sidec=sqrt((b.getX()-a.getX())*(b.getX()-a.getX())+(b.getY()-a.getY())*(b.getY()-a.getY()));
  49.               return sidec;
  50.           }
  51.       double getArea()
  52.       {
  53.               double s,area,x,y,z;
  54.               x=getSideA();
  55.               y=getSideB();
  56.               z=getSideC();
  57.               s=getSideA()+getSideB()+getSideC();
  58.               area=sqrt(s*(s-x)*(s-y)*(s-z));
  59.               return area;
  60.           }
  61.       double getPeri()
  62.       {
  63.               double s;
  64.               s=getSideA()+getSideB()+getSideC();
  65.               return s;
  66.           }
  67. };
  68. int main()
  69. {
  70.         Triangle T1(1.7,8.8,4.3,2.2,6.6,7.7),T2;
  71.         cout<<"T1的面积为:"<<T1.getArea()<<endl;
  72.         cout<<"T1的周长为:"<<T1.getPeri()<<endl;
  73.         cout<<"T2的面积为:"<<T2.getArea()<<endl;
  74.         cout<<"T2的周长为:"<<T2.getPeri()<<endl;
  75.         return 0;
  76. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-4-20 09:45:08 | 显示全部楼层
不清楚你想问什么,Triangle(Point p1,Point p2,Point p3){a=p1;b=p2;c=p3;}?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-20 17:44:47 | 显示全部楼层
LeoChou 发表于 2016-4-20 09:45
不清楚你想问什么,Triangle(Point p1,Point p2,Point p3){a=p1;b=p2;c=p3;}?

是的,但是这样编译会出问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-20 17:48:13 | 显示全部楼层
LeoChou 发表于 2016-4-20 09:45
不清楚你想问什么,Triangle(Point p1,Point p2,Point p3){a=p1;b=p2;c=p3;}?

因为Point对象的构造函数是多参的,覆盖了无参的构造函数,这样就不能直接Point p1,Point p2,Point p3直接作为Triangle函数的形参。编译会报错。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-4-21 09:11:23 | 显示全部楼层
那就再写一个Point的无参构造函数或者为有参构造函数添加默认值Point(double xx=0, double yy=0) 。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-21 11:31:36 | 显示全部楼层
LeoChou 发表于 2016-4-21 09:11
那就再写一个Point的无参构造函数或者为有参构造函数添加默认值Point(double xx=0, double yy=0) 。

原来默认值是这样用的啊,谢谢大神啊。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-22 09:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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