As part of my programming class, I need to go through the tutorials, and make my own program that shows off whats in the tutorial, I'm done with the class tutorial and have it all done except one part. The volume part doesn't return the right value and I don't know why.

#include <cstdlib>
#include <iostream>

using namespace std;

class cube //global class
     {
     private: //private members follow
          unsigned int Length;
          unsigned int Width;
          unsigned int Height;
          unsigned int Volume;
     public:  //public members follow
          cube(unsigned int initialLength, unsigned int initialWidth, unsigned int initialHeight); //constructor called
          ~cube();  //destructor called
          
          void setLength(unsigned int le) {Length = le;} //inline
          unsigned int getLength() const {return Length;} //inline
          
          void setWidth(unsigned int wi) {Width = wi;} //inline
          unsigned int getWidth() const {return Width;} //inline
          
          void setHeight(unsigned int he) {Height = he;} //inline
          unsigned int getHeight() const {return Height;} //inline
          
          void setVolume() {Volume = (Length*Width*Height);} //inline
          unsigned int getVolume() const {return Volume;} //inline          
     
     };
     
     /*     Defined accessor methods follow, they allow manipulation and printing
            of the Length variable     */
     
     cube::cube(unsigned int initialLength, unsigned int initialWidth, unsigned int initialHeight) //cube constructor
          {
          Length = initialLength;
          Width = initialWidth;
          Height = initialHeight;
          Volume = (Length*Width*Height);
          }
          
     cube::~cube() //cube destructor
          {
          }
     
     /*     If you changed the above inline accessor methods to not be inline, you would need the following code
     and would thus need to uncomment it. You would also need to copy it and change it to comply with the Width 
     and Height part of the class.    */
     
     
     //void cube::setLength(unsigned int le) //allows changing of the private length variable
     //     {                      
     //     Length = le;
     //     }
     //int cube::getLength() //accesses and returns the length variables value
     //     {
     //     return Length;
     //     }
     

int main()
     {
     cube box1(12,45,2); //creates box1 of class "cube" and initializes it with "12, 45, and 2".
     
     unsigned int boxes_length = 0;
     unsigned int boxes_width = 0;
     unsigned int boxes_height = 0;
     
     cout << "Current length: " << box1.getLength() << endl
          << "Current width:  " << box1.getWidth() << endl
          << "Current height: " << box1.getHeight() << endl 
          << "Current volume: " << box1.getVolume() << endl << endl
          << "Enter the boxes new length: ";
     cin >> boxes_length;
     cout << "Enter the boxes new width:  ";
     cin >> boxes_width;
     cout << "Enter the boxes new height: ";
     cin >> boxes_height;
     
     box1.setLength(boxes_length); //Uses accessor methods
     box1.setWidth(boxes_width);
     box1.setHeight(boxes_height);
     
     cout << endl
          << "The boxes length: " << box1.getLength() << endl
          << "The boxes width:  " << box1.getWidth() << endl
          << "The boxes height: " << box1.getHeight() << endl
          << "The boxes volume: " << box1.getVolume() << endl
          << endl;
     
     system("PAUSE");
     return 0;
     }

The problem part of the code I beleive to be this as it returns the same value I had before.

void setVolume() {Volume = (Length*Width*Height);} //inline
unsigned int getVolume() const {return Volume;} //inline

Any and all help is much appreciated, thanks

It might help if you actually called setVolume()... ;)

box1.setLength(boxes_length); //Uses accessor methods
     box1.setWidth(boxes_width);
     box1.setHeight(boxes_height);

where are you using setVolume ?

Just glancing at the code, would this modification work?

unsigned int getVolume() {
     setVolume();
     return Volume;
}

Thanks guys, I knew it would be something rediculous which is why i put very simple class in the title. But it was literally my second day with classes at all.

Thanks again guys, its working now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.