虚函数代码中的错误

时间:2016-02-21 18:54:23

标签: c++

#include<iostream>
#include<cmath>
using namespace std;
const double PI=3.14;
class shape
{
public:
    virtual void print()=0;
    virtual float area()=0;
};
class twodimensional:public shape
{
public:
    virtual float perimeter()=0;
};
class threedimensional:public shape
{
public:
    virtual float volume()=0;
};
class circle:public twodimensional
{
private:
    float raduis;
public:
    circle(float r):raduis(r)
    {}
    void print()
    {
        cout<<"shape is a circle\n";
        cout<<"circle raduis="<<raduis<<endl;
    }
    float perimeter()
    { return 2*PI*raduis;}
    float area()
    { return PI*raduis*raduis;}
};
class cone:public threedimensional
{
private:
    float raduis,height;
public:
    cone(float r,float h)
    {
        r=raduis;h=height;
    }
    void print()
    {
        cout<<"Shaps is a cone"<<endl;
        cout<<"cone's raduis="<<raduis<<",height="<<height<<endl;
    }
    float area
    {
        float s=sqrt(raduis*raduis+height*height);
        return PI*raduis*(raduis+s);
    }
    float volume
    {
        return PI*raduis*raduis*height/3;
    }
};
int main()
{
    circle x(12.5f);
    x.print();
    cout<<"the perimeter of the circle="<<x.perimeter()<<endl;
    cout<<"the area of the circle="<<x.area<<endl;
    cout<<endl;
    cone y(4.3f,8.2f);
    y.print();
    cout<<"The volume of the circle ="<<y.volume()<<endl;
    cout<<"The area of the circle ="<<y.area()<<endl;
    system("pause");
    return 0;
}

我在尝试调试关于多态性和虚函数的代码时遇到了很多错误,所以如果有人可以帮助我!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!我在尝试调试这个关于多态性和虚函数的代码时遇到了很多错误,所以如果有人可以帮助我!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!

1 个答案:

答案 0 :(得分:0)

void resetViews() { recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext())); RecyclerView.LayoutManager lm = recyclerView.getLayoutManager(); if (lm != null && lm instanceof LinearLayoutManager) { currPosition = ((LinearLayoutManager)lm).findFirstVisibleItemPosition(); Log.i("saz","curr pos : " + currPosition); } int count = lm.getChildCount(); if (currPosition != RecyclerView.NO_POSITION && currPosition < count){ lm.scrollToPosition(currPosition); } } 类中的方法定义不正确。您遗漏了Cone()定义中的area。请修改如下;

volume

您还需要在float area() { float s=sqrt(raduis*raduis+height*height); return PI*raduis*(raduis+s); } float volume() { return PI*raduis*raduis*height/3; } 方法中更改方法调用。

main()

此处您还没有在cout<<"the area of the circle="<<x.area<<endl; 中使用()。修改如下。

x.area

希望这有帮助。