I have a problem with implementing a virtual function of the mother class: so basically my code is:
class Shape
{
public:
virtual ~Shape();
virtual bool Intersect (const Ray& ray, double& t) const =0;
virtual Vector GetNormal(const Vector& at) const =0;
protected:
Color color;
double dc;
};
class Ball: public Shape
{
public:
Ball(const Color& col,const double diff,const double x,const double y,const double z,const double radius):
cx(x),cy(y),cz(z),r(radius)
{
Shape::color=col;
Shape::dc=diff;
assert(radius!=0);
}
virtual bool Intersect (const Ray& ray, double& t)
{
Vector c(cx,cy,cz), s(ray.xs,ray.ys,ray.zs);
Vector v(s-c);
double delta(std::pow(v*ray.dir,2)-v*v+r*r);
if(delta<0) return false;
const double thigh(-v*ray.dir+std::sqrt(delta)), tlow(-v*ray.dir-std::sqrt(delta));
if(thigh<0) return false;
else if (tlow<0){t=thigh; return true;}
else{t=tlow; return true;}
assert(false);
};
virtual Vector GetNormal(const Vector& at)
{
Vector normal(at - Vector(cx,cy,cz));
assert(Norm(normal)==r);
return normal;
};
private:
double cx,cy,cz;
double r;
};
and basically Ball * ball = new Ball (parameters);
I get the following message: "You cannot select an object of type ball, because the implemented functions are clean in the ball."
I do not understand why this does not work, since the implementation is implemented in a subclass ...
source
share