找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 729|回复: 3

虚函数使用

[复制链接]
发表于 2009-11-4 01:38:04 | 显示全部楼层 |阅读模式 IP:江苏扬州
#include<iostream>
#include<cmath>
using namespace std;
#define pi 3.14159265
class Shape
{public:
     virtual void ShapeName() const=0;
     virtual void PrintArea() const=0;
};
class Circle:public Shape
{private:float radium;
public:Circle(){radium=0;}
       Circle(float r){radium=r;}
    virtual void ShapeName()
         const {cout<<"Circle:"<<endl;}
     virtual void PrintArea()
     const {cout<<"Area->"<<pi*radium*radium<<endl;}
};
class Rectangle:public Shape
{private:float length;
         float width;
public:Rectangle(){length=0; width=0;}
       Rectangle(float l,float w){length=l;width=w;}
       virtual void ShapeName() const {cout<<"Rectangle:"<<endl;}
       virtual void PrintArea() const {cout<<"Area->"<<length*width;}
};
class Triangle:public Shape
{private:float a;
         float b;
         float c;
public:Triangle(){a=0;b=0;c=0;}
       Triangle(float a1,float b1,float c1)
       {if((a+b>c)&&(a+c>b)&&(a+c>b))
       {a=a1;b=b1;c=c1;}
       else{a=0;b=0;c=0;}}
       virtual void ShapeName() const {cout<<"Triangle:"<<endl;}
       virtual void PrintArea() const {float s=a+b+c;
       cout<<"Area->"<<sqrt(s*(s-a)*(s-b)*(s-c))<<endl;}
};
int main()
{Rectangle rec(4.0,5.0);
Circle cir(3.0);
Triangle tri(6.0,7.0,8.0);

Shape *ptr;
ptr=&Circle;
ptr->ShapeName();
ptr->PrintArea();

ptr=&Rectangle;
ptr->ShapeName();
ptr->PrintArea();

ptr=&Triangle;
ptr->ShapeName();
ptr->PrintArea();

system("pause");
return 0;
}
01.cpp
F:\我的文档\桌面\virturl function\001.cpp(47) : error C2275: 'Circle' : illegal use of this type as an expression
        F:\我的文档\桌面\virturl function\001.cpp(11) : see declaration of 'Circle'
F:\我的文档\桌面\virturl function\001.cpp(51) : error C2275: 'Rectangle' : illegal use of this type as an expression
        F:\我的文档\桌面\virturl function\001.cpp(20) : see declaration of 'Rectangle'
F:\我的文档\桌面\virturl function\001.cpp(55) : error C2275: 'Triangle' : illegal use of this type as an expression
        F:\我的文档\桌面\virturl function\001.cpp(28) : see declaration of 'Triangle'
Error executing cl.exe.

001.obj - 3 error(s), 0 warning(s)
不知道为什么?
发表于 2009-11-4 01:38:05 | 显示全部楼层 IP:江苏扬州
程序代码:
#include<iostream>
#include<cmath>
using namespace std;
#define pi 3.14159265
class Shape
{
public:
     virtual void ShapeName() const=0;
     virtual void PrintArea() const=0;
};
class Circle:public Shape
{
      private:
              float radium;
      public:
              Circle(){radium=0;}
              Circle(float r){radium=r;}
              virtual void ShapeName() const {cout<<"Circle:"<<endl;}
              virtual void PrintArea() const {cout<<"Area->"<<pi*radium*radium<<endl;}
};
class Rectangle:public Shape
{
      private:
              float length;
              float width;
      public:
              Rectangle(){length=0; width=0;}
              Rectangle(float l,float w){length=l;width=w;}
              virtual void ShapeName() const {cout<<"Rectangle:"<<endl;}
              virtual void PrintArea() const {cout<<"Area->"<<length*width;}
};
class Triangle:public Shape
{
      private:
              float a;
              float b;
              float c;
      public:
              Triangle(){a=0;b=0;c=0;}
              Triangle(float a1,float b1,float c1)
              {if((a+b>c)&&(a+c>b)&&(a+c>b))
              {a=a1;b=b1;c=c1;}
              else{a=0;b=0;c=0;}}
              virtual void ShapeName() const {cout<<"Triangle:"<<endl;}
              virtual void PrintArea() const {float s=a+b+c; cout<<"Area->"<<sqrt(s*(s-a)*(s-b)*(s-c))<<endl;}
};

int main()
{
    Rectangle rec(4.0,5.0);
    Circle cir(3.0);
    Triangle tri(6.0,7.0,8.0);

    Shape* ptr;
    ptr=&cir;
    ptr->ShapeName();
    ptr->PrintArea();

    ptr=&rec;
    ptr->ShapeName();
    ptr->PrintArea();

    ptr=&tri;
    ptr->ShapeName();
    ptr->PrintArea();

    system("pause");
    return 0;
}
回复

使用道具 举报

发表于 2009-11-4 01:38:06 | 显示全部楼层 IP:江苏扬州
可以运行了,不过在class Triangle:public Shape
{private:float a;
         float b;
         float c;
public:Triangle(){a=0;b=0;c=0;}
       Triangle(float a1,float b1,float c1)
       {if((a+b>c)&&(a+c>b)&&(a+c>b))
       {a=a1;b=b1;c=c1;
       }
       else
       {a=1;b=1;c=1;
       }       }好像有问题,不管三角形的三边合不合法都输出else的部分
回复

使用道具 举报

发表于 2009-11-4 01:38:08 | 显示全部楼层 IP:江苏扬州
if((a+b>c)&&(a+c>b)&&(a+c>b))  你用构造函数都初使化0,你if里面的表达式有成立的吗?0+0>0?
回复

使用道具 举报

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

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-10-1 03:30 , Processed in 0.270415 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

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