小程序问题,大家帮忙。。
#include<iostream>
#include<string>
using namespace std;
class Shape
{
public:
virtual float area()const{return 0.0;}
virtual float volume() const{return 0.0;}
virtual void shapeName() const=0;
};
class Point:public Shape
{
public:
Point(float=0,float=0);
void setPoint(float,float);
float getX()const{return x;}
float getY()const{return y;}
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;
}
int main()
{
Point point(3.2,4.5);
Shape *pt;
pt=&point;
cout<<"x="<<point.getX()<<",y="<<point.getY()<<"/narea="<<pt->area()<<"/nvolume="<<pt->volume()<<"/n/n";
}
不知道怎么会报不能引用抽象类的????
[解决办法]
首先明确一下抽象类的特点:
1.不能定义对象
2.包含有纯虚函数的类为抽象类
3.抽象类不能用作函数返回值类型,函数参数类型
但可以定义对象指针或对象引用
4.抽象类可以且只能用作派生结构的基类.(我上课做的C++笔记)
然后,你的程序:
派生类Point并没有对基类中的纯虚函数进行重新定义,
相当于派生类中也有 纯虚函数,看上面的第二点
所以 Point类也成为了抽象类,不能定义对象.
我又查了了下谭浩强书中有矛盾的两句话:
1,如果在一个类中声明了纯虚函数,而在其派生类中没有对该函数定义,则该虚函数在派生类中仍然为纯虚函数。
2,纯虚函数是在抽象基类中声明的,只是在抽象基类中它才称为纯为纯虚函数,在其派生类中虽然继承了该函数,但除非再次用"=0"把它声明为纯虚函数,否则它就不是也不能称为纯虚函数。
应该说第一句是对的.
小程序有关问题,大家帮忙。
小程序问题,大家帮忙。。#includeiostream#includestringusing namespace stdclass Shape{public:virtu
