标题:C++关于无参的纯虚函数的调用怎么弄?
只看楼主
遗LOVE忘
Rank: 2
等 级:论坛游民
帖 子:9
专家分:11
注 册:2012-6-15
结帖率:50%
已结贴  问题点数:20 回复次数:2 
C++关于无参的纯虚函数的调用怎么弄?
#include<iostream>
using namespace std;
class Shape
{public:
    virtual float area()const{return 0.0;}
    virtual float volume()const{return 0.0;}
    virtual float shapeName() const =0;
};
class Point:public Shape
{public:

    Point(float x=0,float y=0);
    void setPoint(float,float);
    float getX()const {return x;}
    float getY() const{return y;}
    virtual void shapeName()const{cout<<"Point:";}
    friend ostream & operator<<(ostream &,const Point &);
protected:
    float x,y;
};

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

void Point::setPoint(float a,float b)
{x=a;y=b;}

ostream & operator<<(ostream &output,const Point &p)
{output<<"["<<p.x<<","<<p.y<<"]";
 return output;
}

class Circle:public Point
{public:
     Circle(float x=0,float y=0 ,float r=0);
     void setRadius(float r);
     float getRadius() const;
     virtual float area() const;
     virtual void shapeName() const{cout<<"Circle:";}
     friend ostream &operator<<(ostream &,const  Circle &);
protected:
    float radius;
};

Circle::Circle(float x,float y,float r):Point(x,y),radius(r){}

void Circle::setRadius(float r){r=radius;}

float Circle::getRadius()const {return radius;}

float Circle::area()const {return 3.14159*radius*radius;}

ostream &operator<<(ostream &output,const Circle &c)
{output<<"["<<c.x<<","<<c.y<<"],r="<<c.radius;
return output;
}


class Cylinder:public Circle
{public:
     Cylinder(float x=0,float y=0,float r=0,float h=0);
     void setHeight(float);
     virtual float area()const;
     virtual float volume() const;
     virtual void shapeName() const{cout<<"Cylinder:";}
     friend ostream&operator<<(ostream&,const Cylinder&);
protected:
    float height;
};

Cylinder::Cylinder(float a,float b,float r,float h):Circle(a,b,r),height(h){}

void Cylinder::setHeight(float h){height=h;}

float Cylinder::area()const
{return 2*Circle::area()+2*3.14159*radius*height;}

float Cylinder::volume()const
{return Circle::area()*height;}

ostream &operator<<(ostream &output,const Cylinder &cy)
{output<<"["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height;
 return output;
}

int main()
{Point point(3.2,4.5);
Circle circle(2.4,1.2,5.6);
Cylinder cylinder(3.5,6.4,5.2,10.5);
point.shapeName();
cout<<point<<endl;

circle.shapeName();
cout<<circle<<endl;

cylinder.shapeName();
cout<<cylinder<<endl<<endl;

Shape *pt;

pt=&point;
pt->shapeName();
cout<<"x="<<point.getX()<<",y="<<point.getY()<<"\narea="<<pt->area()<<"/nvolume="<<pt->volume()<<"\n\n";

pt=&circle;
pt->shapeName();
cout<<"x="<<circle.getX()<<",y="<<circle.getY()<<"\narea="<<pt->area()<<"\nvolume="<<pt->volume()<<"\n\n";

pt=&cylinder;
pt->shapeName();
cout<<"x="<<cylinder.getX()<<",y="<<cylinder.getY()<<"\narea="<<pt->area()<<"\nvolume="<<pt->volume()<<"\n\n";
return 0;
}
这个程序有点长,麻烦大家看看那个shapeName的纯虚函数怎么调用错了???拜谢!
搜索更多相关主题的帖子: void include public friend 
2012-06-18 23:26
一只小蚂蚁
Rank: 2
等 级:论坛游民
威 望:2
帖 子:19
专家分:92
注 册:2012-6-19
得分:20 
抽象类返回类型改成void就行了
至于friend错误 这是因为你函数定义和声明在一个文件里导致的
2012-06-19 00:39
遗LOVE忘
Rank: 2
等 级:论坛游民
帖 子:9
专家分:11
注 册:2012-6-15
得分:0 
回复 2楼 一只小蚂蚁
恩恩,感谢,打那个Shape类的时候还没注意呢,感谢
2012-06-19 10:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-371826-1-1.html




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

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