免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3993 | 回复: 3
打印 上一主题 下一主题

[C++] 为啥不能class Line:public Point继承呢? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-05-11 21:35 |只看该作者 |倒序浏览
定义一个Shape抽象类,建立一个point类,表示平面中的一个点;建立一个line类,表示平面中的一条线段,内含两个point类的对象;建立triangle类,表示一个三角形,内含三个line类的对象构成一个三角形,确定派生关系,编制相应程序输出相关信息,设计triangle类的成员函数完成三条边是否能构成三角形的检验和三角型面积计算,输出相关信息。

shape类好像没什么意义吧,我定义了一个空类。
class Line  必须单独定义,如果用继承:class Line:public Point,就会提示什么构造函数?
这个题与一般提不同之处就在于:不仅仅是继承,2、3个基类的对象构成派生类。(2个点对象构成一个线,3个线对象构成一个三角形)。
为啥不能class Line:public Point继承呢?


#include<iostream>
#include<string>
#include<math.h>
using namespace std;
class Shape
{public:

};

class Point:public Shape
{public:
Point(float a,float b)
{x=a;y=b;}

void setPofloat(float a,float b)
{x=a;y=b;}

        float x,y;
};

class Line//:public Point
{public:
Line(float a1,float b1,float a2,float b2):
p1(a1,b1),p2(a2,b2){};

double length()
{        return (sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)));        }

void setline(){
        cin>>p1.x>>p1.y>>p2.x>>p2.y;
}

protected:
        Point p1,p2;
};

class Triangle
{
public:
        Triangle(float a1,float b1,float a2,float b2,   float a3,float b3,float a4,float b4   ,float a5,float b5,float a6,float b6):
          l1(a1,b1,a2,b2), l2(a3,b3,a4,b4) ,  l3(a5,b5,a6,b6){};

float delta(){
                d1=l1.length();
                d2=l2.length();
                d3=l3.length();
         if((d1+d2>d3)&&(d2+d3>d1)&&(d3+d1>d2))
        return 1;
        else
                return 0;
          }

void sett(){
        cout<<"输入3条线的6个点的坐标(12个数字)"<<endl;
        l1.setline();
        l2.setline();
        l3.setline();
        cout<<"3条线的长度分别为:"<<endl;
        cout<<l1.length()<<", "<<l2.length()<<", "<<l3.length()<<"."<<endl;

        if
                (  (l1.length()+l2.length()>l3.length()) && (l2.length()+l3.length()>l1.length()) &&(l3.length()+l1.length()>l2.length()) )
        {  cout<<"恭喜!您输入的这几个点可以组成三角形"<<endl;
        double p=(l1.length()+l2.length()+l3.length())/2;
        double s=sqrt(p*(p-l1.length()) * (p-l2.length()) *(p-l3.length())  );
        cout<<"三角形的面积:"<<s<<endl;
        }
        else cout<<"您输入的这几个点不可以构成三角形"<<endl;        }



protected:
        Line l1,l2,l3;
        float d1,d2,d3;
};

float main()
{
        Triangle t1(0,0,1,1, 1,2,3,4 ,   4,5,6,7);
        float whether=t1.delta();
        if(whether) cout<<"(0,0)(1,1),(1,2),(3,4),(4,5),(6,7)这几个点组成的3条线段可以构成三角形"<<endl;
        else cout<<"(0,0)(1,1),(1,2),(3,4),(4,5),(6,7)这几个点不可以构成三角形"<<endl;
        t1.sett();
         
        return 0;
}

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
2 [报告]
发表于 2014-05-11 21:57 |只看该作者
  1. lint(float x1,float y1, float x2, float y2):point(x1,y1),pt(x2,y2)
复制代码
??

论坛徽章:
0
3 [报告]
发表于 2014-05-13 16:45 |只看该作者
回复 2# folklore


    请问大神,这是什么意思呢?

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
4 [报告]
发表于 2014-05-13 17:15 |只看该作者
回复 3# 慢跑20

  1. #include <stdio.h>
  2. #include <cmath>
  3. class Point{
  4. private:
  5. double m_x,m_y;
  6. public:
  7.     Point(double x,double y);
  8.     double x(){return this->m_x;}
  9.     double y(){return this->m_y;}
  10. };
  11. Point::Point(double x,double y){
  12.         this->m_x =x;
  13.         this->m_y =y;
  14. };

  15. class Line:public Point{
  16. private:
  17.         Point endpt;
  18. public:
  19.         Line(double x1,double y1,double x2,double y2):Point(x1,y1),endpt(x2,y2){}
  20.         double length();
  21. };

  22. double Line::length(){
  23.         double xdiff =this->Point::x() -this->endpt.x();
  24.         double ydiff =this->Point::y() -this->endpt.y();
  25.         return sqrt(xdiff *xdiff +ydiff *ydiff);
  26. }

  27. int main(){
  28.         printf("length:%lf",Line(0,0,2,2).length());
  29.         return 0;
  30. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP