-
draw function HELP!!!
I am writing this program to draw a sphere, I am having problems with the draw function which is giving me an error(bold). Can anyone help me to write the draw function for this program.
also, I want to have the user input the values for x,y, z, and radius. I now that I have to put a cout asking for the values and cin to get the values in main or maybe I can use get. Can anyone give me an idea how to work this out.
here is the code
Code:
#include <iostream>
#include <math.h>
#include <iomanip>
#include <stdio.h>
#define PI 3.1415927
using namespace std;
class point
{
public:
point(const float xx, const float yy, const float zz) { x=xx; y=yy; z=zz; }
point(const point &p) { x = p.getX(); y = p.getY(); z = p.getZ(); }
point() { x = y = z = 0.0; }
float getX() const { return x; }
float getY() const { return y; }
float getZ() const { return z; }
private:
float x,y,z;
};
class shape3d
{
public:
shape3d(const point &pp) { p = pp; }
shape3d(const float x, const float y, const float z) { p = point(x,y,z); }
point getPoint() const { return p; }
virtual float getVolume() const = 0;
//virtual void draw( shape3d *todraw) const { todraw->draw(); }
private:
point p;
};
class sphere: public shape3d
{
public:
sphere(const point &p, const float r):shape3d(p) { radius = r; }
float getRadius() const { return radius; }
float getVolume() const { return 3.0*(PI*radius*radius*radius)/4.0; }
//error: draw function doesn't take 0 parameters
virtual void draw( sphere *todraw) const { todraw->draw(); }
private:
float radius;
};
main()
{
point p = point (0,0,0);
sphere s = sphere (p,50);
//error: draw function doesn't take 0 parameters
s.draw();
return 0;
}
-
I don't think you need "sphere * todraw" as an argument to the draw() function; take it out of the definition.
Sorry, I'm being incoherent. I can't speak English at the moment, apparently; only C. Make the following changes:
Code:
virtual void draw( shape3d *todraw) const { todraw->draw(); };
should be
virtual void draw() const;
or
virtual void draw() const = 0; // (if you don't plan to ever implement a draw() routine for shape3d)
Similarly,
Code:
virtual void draw( sphere *todraw) const { todraw->draw(); }
should be
virtual void draw() const {
// actual drawing code here
};
You do not need to provide a member function of a class with a pointer to the class, assuming the member is working on that particular instance of the class and not some other, and you need to implement an actual draw() routine instead of
Code:
void draw() {
draw();
};
which will crash/loop forever.