CS174: Classes And Inheritance: Intro To C++ Classes
Developed by Professor Tralie and Professor Mongan.
Please watch the video below, and click the next button once you have finished (NOTE: This video ends abruptly because I decided to split it up)
Notes
- Classes in C++ are declared with the
class
keyword, just as in Java, but they need a semicolon after the closing bracket. - Public and private variables/methods are all declared together in blocks. The public block is denoted by
public:
, and the private block is denoted byprivate:
- Constructors are defined just as they are in Java, except they don't need
void
in front of them - A static object of a class
MyClass
is defined asMyClass myObject(constructor parameters...);
- A dynamic object of a class
MyClass
is defined as the reference
We have to then remember to sayMyClass* myObject = new MyClass(constructor parameters...);
delete myObject
to de-allocate the object once we're finished with it, since dynamic objects (like dynamic arrays) are not de-allocated automatically. - A shorthand for accessing instance variables and methods in an object reference
ref
is the arrow notationref->field
- The
this
keyword within an object is a reference/pointer to the object, so we need to use the arrow notation if we want to access variables inthis