What are classes and methods in C++?

C++ supports the idea of objects. These are basically structs with functions attached.

This is the typical syntax for declaring such an object.

class APPfoo{ public: APPfoo() { // This is a constructor - called when the object is created } ~APPfoo() { // This is destructor - the funny tilde ~ character goes in front // of the name of the class - it's called at the object is destroyed } void run(){ // This is a "method". This is function that acts on the object // that gets access to the member variables of the class m_AMemberVariable = m_AMemberVariable + 1; } private: int m_AMemberVariable; // This is only accessible to methods of the class };

This is how we might use it:

{ APPfoo AFoo; // AFoo is created and the APPfoo is called. AFoo.run(); } // AFoo goes out of scope so ~APPfoo is called.

On the whole with the teams I run I tend to discourage object orientated programming. Binding methods to classes in my opinion goes against the principle of separation of concerns.

Notice how we could have more than one ‘instance’ of a APPfoo object? Each APPfoo object gets a unique address - we can access this address through what we call the “this” pointer. i.e.:

void APPfoo::run(){ printf("We can use the this point to access members like %i", this->m_AMemberVariable); }

See tracing methods.