| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 475 人关注过本帖
标题:虚函数使用
只看楼主 加入收藏
zjjh8877227
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-8-15
收藏
 问题点数:0 回复次数:4 
虚函数使用
#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)
不知道为什么?
搜索更多相关主题的帖子: 函数 
2008-08-15 18:11
maying_11
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-8-15
收藏
得分:0 
程序代码:
#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;
}
2008-08-15 18:46
zjjh8877227
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-8-15
收藏
得分:0 
可以运行了,不过在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的部分
2008-08-16 07:13
maying_11
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-8-15
收藏
得分:0 
if((a+b>c)&&(a+c>b)&&(a+c>b))  你用构造函数都初使化0,你if里面的表达式有成立的吗?0+0>0?
2008-08-16 09:33
maying_11
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-8-15
收藏
得分:0 
if((a1+b1>c1)&&(a1+c1>b1)&&(a1+c1>b1))
2008-08-16 09:34
快速回复:虚函数使用
数据加载中...
 
   



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

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