| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1088 人关注过本帖
标题:在继承与派生中的类型问题
只看楼主 加入收藏
whl20888
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2008-9-3
收藏
 问题点数:0 回复次数:5 
在继承与派生中的类型问题
#include <iostream.h>
#include <math.h>
double pi=3.14;
class shape
{
 public:
      float x,y,z;
      double s;
      shape(float x1,float  y1,float z1)
      {
          x=x1;
          y=y1;
          z=z1;
      }
};
class  circle: public shape
{
public:
    circle(float x2,float y2,float z2):shape(x2,y2,z2)
    {}
    double getarea()
    {
        s=pi*z*z;
        return s;
    }
};
class rectangle : public shape
{
public:
    float p;
    rectangle(float x2,float y2,float z2):shape(x2,y2,z2)
    {  p=(x+y+z)/2;}
    float getarea()
    {   
        s=sqrt(p*(p-x)*(p-y)*(p-z));
        return s;
    }
};
/*class  squre: public rectangle
{   public:
    int i;
    squre(float x3,float y3,float z3):rectangle(x3,y3,z3){i=9;}
   
};*/
void main()
{
    circle  c1(1.12,2.1,3.5);
    rectangle  t1(3,4,5);
    
    
    cout<<t1.getarea()<<endl<<c1.getarea()<<endl;
}
为什么这样可以运行,但当去掉注释符号,老是说类型不对,这是怎么回事呢,高手解释下了!

[[it] 本帖最后由 whl20888 于 2008-12-13 22:36 编辑 [/it]]
搜索更多相关主题的帖子: 继承 派生 类型 
2008-12-13 22:17
PcrazyC
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:5652
专家分:0
注 册:2006-10-20
收藏
得分:0 
你主函数里都没有用squre类,怎么会是这里的问题呢?主函数里怎么写的?

雁无留踪之意,水无取影之心
2008-12-13 23:43
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
验证了下除了,有精度丢失以外没什么问题

学习需要安静。。海盗要重新来过。。
2008-12-14 09:56
hitcolder
Rank: 1
等 级:新手上路
威 望:1
帖 子:124
专家分:0
注 册:2008-10-28
收藏
得分:0 
给加了几个强制类型转换,还用了个define,总算没warning了
#include <iostream.h>
#include <math.h>
#define pi (float)3.14
class shape
{
public:
      float x,y,z;
      float s;
      shape(float x1,float  y1,float z1)
      {
          x=x1;
          y=y1;
          z=z1;
      }
};
class  circle: public shape
{
public:
    circle(float x2,float y2,float z2):shape(x2,y2,z2)
    {}
    float getarea()
    {
        s=pi*z*z;
        return s;
    }
};
class rectangle : public shape
{
public:
    float p;
    rectangle(float x2,float y2,float z2):shape(x2,y2,z2)
    {  p=(x+y+z)/2;}
   float getarea()
    {   
        s=(float)sqrt(p*(p-x)*(p-y)*(p-z));
        return s;
    }
};

class  squre: public rectangle
{   public:
    int i;
    squre(float x3,float y3,float z3):rectangle(x3,y3,z3)
    {i=9;}
   
};

void main()
{
    circle  c1(1.12,2.1,3.5);
    rectangle  t1(3,4,5);
   
   
    cout<<t1.getarea()<<endl<<c1.getarea()<<endl;
}

不要在你的智慧中夹杂傲慢,也不要使你们的谦卑缺乏智慧的成分。
2008-12-15 22:50
hitcolder
Rank: 1
等 级:新手上路
威 望:1
帖 子:124
专家分:0
注 册:2008-10-28
收藏
得分:0 
顺便说句: pi是被默认为const double类型的,你s设置成double类型的,而x,y,z却是float,计算的时候必然发生类型转换,肯定会有警告的,而且sqrt()函数的返回值好像是double型的吧。

不要在你的智慧中夹杂傲慢,也不要使你们的谦卑缺乏智慧的成分。
2008-12-15 22:57
快速回复:在继承与派生中的类型问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021616 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved