鱼C论坛

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

关于在一个类调用另一个类里面的数据

[复制链接]
发表于 2012-10-11 17:24:51 | 显示全部楼层 |阅读模式

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

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

x
只有最后一个函数void display()是输出三角形的坐标,周长和面积。不知道怎么调用出了三个point的x, y来计算了  去高手解答!!  其他部分没有问题   编译通过了- -  我用了接近两个小时呀  汗
  1. #include <iostream>

  2. #define PI 3.14159
  3. using namespace std;

  4. class Point
  5. {
  6. public:
  7.     Point(int xx = 0, int yy = 0)                                  //(1)带参数的缺省构造函数。如:point(int xx=0,int yy=0);
  8.     {
  9.         x = xx;
  10.         y = yy;
  11.     }
  12.     Point(Point &p);//(2)拷贝构造函数
  13.     void set_value(int xx, int yy);                                //(3)设置x,y坐标值
  14.     void get_value(int &xx, int &yy);                              //(4)取x,y坐标值,参数为两个整形量的引用,分别用于获取x,y的坐标值。
  15. private:
  16.     int x;
  17.     int y;
  18. };

  19. Point::Point(Point &p)
  20. {
  21.     x = p.x;
  22.     y = p.y;
  23. }

  24. void Point::set_value(int xx, int yy)
  25. {
  26.     x = xx;
  27.     y = yy;
  28. }

  29. void Point::get_value(int &xx, int &yy)
  30. {
  31.     xx = x;
  32.     yy = y;
  33. }                                                                  //Point类结束

  34. class Triangle
  35. {
  36. public:
  37.     Triangle(int x1 , int y1, int x2, int y2, int x3, int y3);     //(1)带参数的缺省构造函数,参数为整型x1,y1,x2,y2,x3,y3,分别是三个顶点
  38.     Triangle(Point &p1, Point &p2, Point &p3);                     //(2)带参数的构造函数,参数是三个point类对象的引用。

  39.     void set_value1(int x1, int y1);                               //(3)设置顶点p1坐标,参数为两个整型参数。
  40.     void set_value2(int x2, int y2);                               //(4)设置顶点p2坐标,参数为两个整型参数。
  41.     void set_value3(Point &p);                                     //(5)设置顶点p3坐标,参数是一个point类对象的引用。

  42.     void pointer1(int *p1, int *p2);                               //(6)取顶点1坐标,参数为两个整型变量的指针,即将两个坐标值放入两个指针指向的变量中。
  43.     void pointer2(int *p1, int *p2);                               //(7)取顶点2坐标,参数为两个整型变量的引用,即将两个坐标值赋值给个引用变量。
  44.     void pointer3(Point &p);                                       //(8)取顶点3坐标,参数是一个point类对象的引用。

  45.     void display();                                                //(9)输出三角形的三个顶点的坐标、周长和面积。
  46. private:
  47.     Point point1;
  48.     Point point2;
  49.     Point point3;
  50. };

  51. Triangle::Triangle(int x1=0, int y1=0, int x2=1, int y2=1, int x3=-1, int y3=-1)
  52. {
  53.     point1.set_value(x1, y1);
  54.     point2.set_value(x2, y2);
  55.     point3.set_value(x3, y3);
  56. }

  57. Triangle::Triangle(Point &p1, Point &p2, Point &p3)
  58. {
  59.     point1 = p1;
  60.     point2 = p2;
  61.     point3 = p3;
  62. }

  63. void Triangle::set_value1(int x1, int x2)
  64. {
  65.     point1.set_value(x1, x2);
  66. }

  67. void Triangle::set_value2(int x2, int y2)
  68. {
  69.     point2.set_value(x2, y2);
  70. }

  71. void Triangle::set_value3(Point &p)
  72. {
  73.     point3 = p;
  74. }


  75. void Triangle::pointer1(int *p1, int *p2)
  76. {
  77.     point1.get_value(*p1, *p2);
  78. }

  79. void Triangle::pointer2(int *p1, int *p2)
  80. {
  81.     point2.get_value(*p1, *p2);
  82. }

  83. void Triangle::pointer3(Point &p)
  84. {
  85.     p = point3;
  86. }

  87. void Triangle::display()
  88. {
  89.     cout << "the point of the triangle is: " << endl;
  90. }

  91. int main()
  92. {

  93.     return 0;
  94. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-10-11 19:06:50 | 显示全部楼层
用get_value取
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2012-10-11 22:35:11 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-11-15 12:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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