0

I wanted to write a simple program that uses classes but I've been running into errors. Anyone have a clue as to what is wrong with my code because I can't seem to figure it out.

    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;

    class cone{
          private:
                double radius;
                double height;

          public:
          cone();
          void setvolume();//
          double conevol();
    };

    //implementation
    void cone(){
              double radius;
              double height;
     }

    void setvolume(double r,double h){
                      double radius=r;
                      double height=h;
    }

    double conevol(double r,double h){
           double vol=(1/3.0)*(3.14159265)*r*r*h;
           return vol;
    }      

    int main(){
        cone A;
        cout<<"The current volume of the cone is: "<<A.convol<<endl;
        A.setvolume(3.4,5.63);
        cout<<"The volume of the cone is now"<<A.conevol()<<endl;    
        system("PAUSE");
        return 0;
    }             

Edited by Pyler: question missing

3
Contributors
2
Replies
7
Views
5 Years
Discussion Span
Last Post by Rashakil Fol
0

You need the class name before the method in the implementation. IE,

class cone
    {
          private:
                double radius;
                double height;
          public:
          cone();
          void setvolume(double r, double h);
          double conevol();
    };
    //implementation
    void cone::cone() : radius(0.0), height(0.0)
    {
    }
    void cone::setvolume(double r,double h)
    {
          radius=r;
          height=h;
    }
    double cone::conevol()
    {
           double vol=(1/3.0)*(3.14159265)*radius*radius*height;
           return vol;
    }

Note that I fixed a few other issues as well... :-)

0

Note that I fixed a few other issues as well... :-)

Oh really? Now Valgrind can't catch uninitialized value errors when somebody forgets to initialize the object with setvolume.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.