These notes are sourced from this C++ guide.


Class Structure

Header File Example (.h)

Classes can declare methods and instance variables. The three options for scoping variables are public, private, and protected. Virtual methods are functions that are declared within a base class and overridden by a derived (child) class.

Classes can declare methods and instance variables. The three options for scoping variables are public, private, and protected. Virtual methods are functions that are declared within a base class and overridden by a derived (child) class.

Implementation File Example (.cpp)

![In an implementation file, we can declare implementations of methods using the "::" operator. For example, int Example::getNumber() {} will allow us to declare an implementation for that method.

Within method implementations, the scope that we're dealing with includes the private methods of the particular class. Inside of Example:: methods, we can access the number private instance variable.](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6585bc5a-57d6-4976-a527-fd29bd80d28a/Untitled.png)

In an implementation file, we can declare implementations of methods using the "::" operator. For example, int Example::getNumber() {} will allow us to declare an implementation for that method.

Within method implementations, the scope that we're dealing with includes the private methods of the particular class. Inside of Example:: methods, we can access the number private instance variable.


Constructors

Constructors may be implemented in header files, or they may be implemented in an associated implementation file. Constructors are automatically called when an object is instantiated.

In our constructor, we can initialize local variables!

In our constructor, we can initialize local variables!


Destructor

The destructor is called when the stack frame collapses or a free operation is performed.

The tilde indicates that this is a destructor [otherwise, this would look like a constructor].

The tilde indicates that this is a destructor [otherwise, this would look like a constructor].


Memory Management

Malloc/Free

In C++, we can malloc and free just like we do in C.