怎样算是在派生类中实现基类中的纯虚函数?
#include<iostream>usingnamespacestd;staticconstdoublePI=3.14;classShape{public:Shape(...
#include<iostream>
using namespace std;
static const double PI = 3.14;
class Shape
{
public:
Shape(double temp)
{
cout<<"enter the redius:"<<endl;
cin>>temp;
};
virtual void area()=0;
virtual void perimeter()=0;
virtual void volume()=0;
};
class Circle:public Shape
{
public:
double redius;
Circle(double redius):Shape(redius){}
void aera()
{
cout<<"the area is "<<PI*redius*redius<<endl;
}
void perimeter()
{
cout<<"the perimeter is "<<2*PI*redius<<endl;
}
void volume()
{
cout << "Volume is not defined for circle." << endl;
}
};
我在派生类Circle里重新定义了area(),perimeter(),volume()三个函数,但在创建一个Circle对象时,如Circle cir,编译器会报错说不允许使用抽象类Circle对象什么的。我现在搞不懂怎样才算把纯虚函数实现。 展开
using namespace std;
static const double PI = 3.14;
class Shape
{
public:
Shape(double temp)
{
cout<<"enter the redius:"<<endl;
cin>>temp;
};
virtual void area()=0;
virtual void perimeter()=0;
virtual void volume()=0;
};
class Circle:public Shape
{
public:
double redius;
Circle(double redius):Shape(redius){}
void aera()
{
cout<<"the area is "<<PI*redius*redius<<endl;
}
void perimeter()
{
cout<<"the perimeter is "<<2*PI*redius<<endl;
}
void volume()
{
cout << "Volume is not defined for circle." << endl;
}
};
我在派生类Circle里重新定义了area(),perimeter(),volume()三个函数,但在创建一个Circle对象时,如Circle cir,编译器会报错说不允许使用抽象类Circle对象什么的。我现在搞不懂怎样才算把纯虚函数实现。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询