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.
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.
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.
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:
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.