I'll try to clarify a bit about the role of classes and member functions. If you work with completely abstract names like hello
, foo
, etc, it can be difficult to connect your learning to anything practical.
Class names tend to be nouns. For example class Dog { ... };
. Because they represent types of objects.
Function names tend to be verbs. For example void Bark() { ... }
. Because functions do things.
So take this example:
class Dog // define a "Dog" type of object
{
public:
void Bark() // give dogs the ability to bark.
{
std::cout << "Woof!" << std::endl;
}
};
You might be tempted to think you can call Bark()
"directly", for example Dog::Bark();
. But this is nonsensical because "dog" cannot bark. *A* dog barks. Dog
is just a type of object, it's not an object itself. You must instantiate a dog object. Like this:
Dog fido;
fido.Bark();