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
     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;
     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

9 Years
Discussion Span
Last Post by Matt Tacular
box1.setLength(boxes_length); //Uses accessor methods

where are you using setVolume ?


Just glancing at the code, would this modification work?

unsigned int getVolume() {
     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.

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.