void class bmiCalculator
This is incorrect. You seem to be mixing up the concept of functions and classes. You seem like you need a deeper explanation of what actually is going on here. Let me try to elaborate.
A
function is a block of code that can be "called". Functions perform a certain task by executing code when they're called. As such... you can think of a function as a
verb... in that it "does something".
In my previous example.... there were two functions: main() and myFunction(). As mentioned, your program starts executing code inside of main. When main calls another function (like myFunction), the program will do the following:
- stop executing code in main
- jump to myFunction
- execute code inside of myFunction
- when myFunction exits, return back to main
- continue executing from where it left off in main
Once the program reaches the end of main, the program will close.
Understanding functions is CRITICAL to programming in languages like C++. Make sure you completely understand everything I said above. If you don't,
ask questions. You won't be able to understand classes or other more complex concepts until you understand the above.
Assuming you understand functions:
A
class is something else entirely. Classes are not blocks of executable code like functions are. You do not "call" classes. The code you put inside of a class body is not executable.
Instead, classes represent a "thing".
If functions are verbs because they do something.... then classes are
nouns because they
are a thing.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
class Foo // a simple class called 'Foo'
{
};
int main()
{
int var; // this create a variable named 'var'. var is an integer becaue
// it is of type 'int'
// We can use var in our code by assigning values to it, printing
// it, etc. It represents a tangible thing.
Foo obj; // Here, we're creating another variable named 'obj'. Just like
// var, we can use it and manipulate it. It's a "thing".
// The difference is... instead of being an integer... it's a "Foo"
}
|
See the difference between classes and functions?
Now... moving on....
A
member function is a function that is part of a class. Just like any other function... member functions are verbs in that they do something. But since they're part of a class, it's implied that they do something with that class.
So... to recap:
- Functions are verbs
- Classes are nouns
- Member functions are verbs which use a noun
Coming back to the code in your original post:
1 2 3 4 5 6 7
|
class bmiCalculator {
public:
int bmiEquation ()
{
//...
}
};
|
Here,
bmiCalculator
is a class. It's a thing.
bmiEquation
is a function. It's a verb... it does something: It gets input from the user and calculates an prints the bmi.
But bmiEquation is also inside of bmiCalculator... which makes it a
member function. That is... it uses a bmiCalculator object to do its work.
(This is not very clear from your code example though... because the way you have this class written... bmiEquation could be in just a normal non-member function. The actual bmiCalculator class doesn't really do anything and serves no purpose. If you need a better example, let me know and I'll try to make one... but this post is already very long)
Now let's take a look at main and see what you're doing there:
1 2 3 4 5
|
int main()
{
bmiCalculator bmiObject;
bmiObject.bmiEquation();
}
|
First thing you do is you are creating an object... 'bmiObject'. This is our 'thing'. We now want to use that thing to actually do the calculation... so you use it to call the member function bmiEquation:
bmiObject.bmiEquation();
<- This uses our bmiObject variable and calls the member function bmiEquation. Remember that member functions need an object on which to perform their action. So we have to tell it which object we want to use. That's why we need bmiObject here.
Once this function is called... as with any function... the program will stop running code in main, it will jump to the code inside bmiEquation.
So.... does that help clarify things? Hopefully this wall of text doesn't just confuse you further.