Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Structures and Classes giving me problems. This program simply calculates volume of a CBox class.

Structures and Classes giving me problems. This program simply calculates volume of a CBox class.

Scheduled Pinned Locked Moved C / C++ / MFC
performance
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rbwest86
    wrote on last edited by
    #1

    Hello all, I am working on my homework and I am having issues. When I added a third box to the equation, the total volume in bytes went from 24bytes down to 8bytes. This is not right, when adding another box to my program it should increase in size. But all I had to do was add a third box and calculate the Length, Width, and Height of the third added box. Then calculate the total volume for all three boxes. Sounds very simple but after adding my third box and changing the code a little bit, I am having issues. I do not understand why it is not adding the two boxVolume1 + boxVolume3 together and then displaying it. The source is listed below. Thank you all to can contribute to pointing out why this program is not working correctly.

    #include <iomanip>
    #include <iostream>
    using std::cout;
    using std::endl;
    class CBox // Class definition at global scope
    {
    public:
    double m_Length; // Length of a box in inches
    double m_Width; // Width of a box in inches
    double m_Height; // Height of a box in inches
    };
    int main()
    {
    CBox box1; // Declare box1 of type CBox
    CBox box2; // Declare box2 of type CBox
    CBox box3; // Declare box3 of type CBox
    double boxVolume1 = 0.0; // Stores the volume of a box1
    double boxVolume3 = 0.0; // Stores the volume of a box3
    double volumetotal = 0.0;
    box1.m_Height = 18.0; // Define the values
    box1.m_Length = 78.0; // of the members of
    box1.m_Width = 24.0; // the object box1
    box2.m_Height = box1.m_Height - 10; // Define box2
    box2.m_Length = box1.m_Length/2.0; // members in
    box2.m_Width = 0.25*box1.m_Length; // terms of box1
    box3.m_Height = 108.0; // Define the values
    box3.m_Length = 32.0; // of the members of
    box3.m_Width = 18.0; // the object box3
    // Calculate volume of box1 and box3
    boxVolume1 = box1.m_Height*box1.m_Length*box1.m_Width;
    boxVolume3 = box3.m_Height*box3.m_Length*box3.m_Width;
    cout << endl
    << "Volume of box1 = " << boxVolume1;
    cout << endl
    << "Volume of box3 = " << boxVolume3;
    cout << endl
    << "box2 has sides which total "
    << box2.m_Height+ box2.m_Length+ box2.m_Width
    << " inches.";
    // Calculate total volume of all boxes
    volumetotal = boxVolume1+boxVolume3;
    cout << volumetotal;
    cout << endl // Display the size of a box in memory
    << "A CBox object occupies "
    << sizeof volumetotal << " bytes.";
    cout <<endl;
    return 0;
    }

    C 1 Reply Last reply
    0
    • R rbwest86

      Hello all, I am working on my homework and I am having issues. When I added a third box to the equation, the total volume in bytes went from 24bytes down to 8bytes. This is not right, when adding another box to my program it should increase in size. But all I had to do was add a third box and calculate the Length, Width, and Height of the third added box. Then calculate the total volume for all three boxes. Sounds very simple but after adding my third box and changing the code a little bit, I am having issues. I do not understand why it is not adding the two boxVolume1 + boxVolume3 together and then displaying it. The source is listed below. Thank you all to can contribute to pointing out why this program is not working correctly.

      #include <iomanip>
      #include <iostream>
      using std::cout;
      using std::endl;
      class CBox // Class definition at global scope
      {
      public:
      double m_Length; // Length of a box in inches
      double m_Width; // Width of a box in inches
      double m_Height; // Height of a box in inches
      };
      int main()
      {
      CBox box1; // Declare box1 of type CBox
      CBox box2; // Declare box2 of type CBox
      CBox box3; // Declare box3 of type CBox
      double boxVolume1 = 0.0; // Stores the volume of a box1
      double boxVolume3 = 0.0; // Stores the volume of a box3
      double volumetotal = 0.0;
      box1.m_Height = 18.0; // Define the values
      box1.m_Length = 78.0; // of the members of
      box1.m_Width = 24.0; // the object box1
      box2.m_Height = box1.m_Height - 10; // Define box2
      box2.m_Length = box1.m_Length/2.0; // members in
      box2.m_Width = 0.25*box1.m_Length; // terms of box1
      box3.m_Height = 108.0; // Define the values
      box3.m_Length = 32.0; // of the members of
      box3.m_Width = 18.0; // the object box3
      // Calculate volume of box1 and box3
      boxVolume1 = box1.m_Height*box1.m_Length*box1.m_Width;
      boxVolume3 = box3.m_Height*box3.m_Length*box3.m_Width;
      cout << endl
      << "Volume of box1 = " << boxVolume1;
      cout << endl
      << "Volume of box3 = " << boxVolume3;
      cout << endl
      << "box2 has sides which total "
      << box2.m_Height+ box2.m_Length+ box2.m_Width
      << " inches.";
      // Calculate total volume of all boxes
      volumetotal = boxVolume1+boxVolume3;
      cout << volumetotal;
      cout << endl // Display the size of a box in memory
      << "A CBox object occupies "
      << sizeof volumetotal << " bytes.";
      cout <<endl;
      return 0;
      }

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      rbwest86 wrote:

      sizeof volumetotal

      sizeof volumetotal is always 8 whatever value it contains (in the range allowed for doubles, of course, see [^]). Adding values to volumetotal cannot change its size in memory (it changes just the bit pattern in memory). :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      L 1 Reply Last reply
      0
      • C CPallini

        rbwest86 wrote:

        sizeof volumetotal

        sizeof volumetotal is always 8 whatever value it contains (in the range allowed for doubles, of course, see [^]). Adding values to volumetotal cannot change its size in memory (it changes just the bit pattern in memory). :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        CPallini wrote:

        is always 8 a constant, often 8

        FTFY The good old Data General machines had 36-bit words holding five 7-bit bytes plus a flag bit, so they would store a double in slightly more than 10 bytes. :laugh:

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
        All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


        C 1 Reply Last reply
        0
        • L Luc Pattyn

          CPallini wrote:

          is always 8 a constant, often 8

          FTFY The good old Data General machines had 36-bit words holding five 7-bit bytes plus a flag bit, so they would store a double in slightly more than 10 bytes. :laugh:

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
          All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          Klingon developers aren't allowed to talk with someone who thinks there are 7 bits bytes. ;P

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          L 1 Reply Last reply
          0
          • C CPallini

            Klingon developers aren't allowed to talk with someone who thinks there are 7 bits bytes. ;P

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            CPallini wrote:

            Klingon developers aren't allowed to talk with someone who thinks there are 7 bits bytes.

            If that is true, you've just proven yourself not to be a Klingon developer. :doh:

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
            All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


            R 1 Reply Last reply
            0
            • L Luc Pattyn

              CPallini wrote:

              Klingon developers aren't allowed to talk with someone who thinks there are 7 bits bytes.

              If that is true, you've just proven yourself not to be a Klingon developer. :doh:

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
              All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


              R Offline
              R Offline
              rbwest86
              wrote on last edited by
              #6

              thank you everyone. I figured out what the problem was. Thank you all again.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups