class method using integers

i really hope you can help. I am a beginner, and when i say beginner I mean beginner. I have been to over 20 sites and tried to read the text book but could not figure this out. so please help be as simple as possible. I am trying to figure out to create a method that will do a simple claculation of 3 integers and then print the result out. Please keep it simple.. This is what I tried but no luck.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 #include <iostream>

using namespace std;

class Box
{
   public:
      double length;   // Length of a box
      double breadth;  // Breadth of a box
      double height;   // Height of a box
      double v;
     
      void volume(double)
      {  
        v = height * length * breadth;
       }
};

int main( )
{
   Box myBox1;        // Declare Box1 of type Box
   Box myBox2;        // Declare Box2 of type Box
       // Store the volume of a box here
 
   // box 1 specification
   myBox1.height = 5.0; 
   myBox1.length = 6.0; 
   myBox1.breadth = 7.0;
                  

   // box 2 specification
   myBox2.height = 10.0;
   myBox2.length = 12.0;
   myBox2.breadth = 13.0;
   // volume of box 1
  
   cout << "Volume of Box1 : " << myBox1.volume() <<endl;
    << mybox1.volume(v)<<  endl;
 

   // volume of box 2
   //volume = myBox2.height * myBox2.length * myBox2.breadth;
  // cout << "Volume of Box2 : " << volume <<endl
  system ("pause");
   return 0;
Hi dariusd7,

Try changing line 13 to this:
void volume()

There is no reason to send an argument to this function, and you haven't on line 37. Line 38 isn't needed.

37
38
39
40
41
42
43
44
45
46
   cout << "Volume of Box1 : " << myBox1.volume() <<endl;
    << mybox1.volume(v)<<  endl;
 

   // volume of box 2
   cout << "Volume of Box2 : " << myBox2.volume() <<endl;
   //volume = myBox2.height * myBox2.length * myBox2.breadth;
  // cout << "Volume of Box2 : " << volume <<endl
  system ("pause");  // this isn't portable, but OK for now as a beginner
   return 0;


Once you have this working, change your member variables to be private:, and provide some constructors. Don't fall into the bad practice of providing a get / set function for each member variable. You probably won't need any of them

Also investigate the use of constructors with initialiser lists - this is a way to initialise all your member variables, and is better than doing by assignment in a constructor.

http://www.cplusplus.com/doc/tutorial/classes/


Also, start getting into the habit of putting the definition of class functions into their own .cpp file, with the declaration of the class in it's own header file. Name these files the same as the class name.

Good Luck!!
Well, first off, the Box::volume() function expects to take a double as an argument, but the argument never gets used. The body of the function is essentially just an assignment operation performed on 'v', which is a member of the Box class.
So instead of calling myBox1.volume(), simply access myBox1.v.
Also, line 38 is incomplete, and the usage of .volume(v) makes even less sense.
You also never print the volume of the second box. Here is my attempt at cleaning things up a bit:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

class Box {
public :
	Box() {length=width=height=0;}
	double length, width, height;
	double volume() {//this member function computes the volume of the box, and returns the volume.
		return length*width*height;
	}
};

int main(int argc, char* argv[]) {
	Box box1, box2;

	//Might be a good idea to write a Box constructor.
	box1.length = 6.0;
	box1.height = 5.0;
	box1.width = 7.0;

	box2.length = 12.0;
	box2.height = 10.0;
	box2.width = 13.0;

	std::cout << "Volume of box1:\t" << box1.volume() << std::endl;
	std::cout << "Volume of box2:\t" << box2.volume() << std::endl;
	std::cin.get();
	return 0;
}
Thanks...xism I didnt bother with box2 because i couoldnt get box 1 to work.. I suually write my code that way.. i write code test, then add more then test..etc
first question why di you create int argc, and char* argv[]
also why did you have to put std::cout instead of just cout?
Thanks theideaman.. that is a lot to swollow.. well basically this program was just a response to an assignment where we were espected to give an example of a class a method and use it in a program.. well everyone else wrote example using strings and chars, no one used integers so that why i attempted this.. thank you i will watch video.. I need a really good clean simple lesson to build on..
first question why di you create int argc, and char* argv[]

Typically, the program entry point (in this case, main()) expects at least two arguments, namely the number of command line arguments and the actual arguments.
I could have also written int main() as you did, as the compiler will fill in the blanks. At this point you do not have to worry about these things, as you said that you are a beginner.

also why did you have to put std::cout instead of just cout?

Unlike in your program, I didn't explicitly tell my program to search for identifiers without name qualifiers in the standard (std) namespace. On line 3 of your program, you've implemented the 'using' statement in global scope to achieve this. However, using a 'using namespace' in global scope is considered to be amateur and bad practice.
Topic archived. No new replies allowed.