CS174: Module 9: 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 by private:
  • 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 as

    MyClass myObject(constructor parameters...);

  • A dynamic object of a class MyClass is defined as the reference

    MyClass* myObject = new MyClass(constructor parameters...);

    We have to then remember to say 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 notation ref->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 in this