CS 70

Key Points

Recap Video

There was a fair amount to digest in this lesson. As a review, and because sometimes it's helpful to see things presented a different way, here's a video that goes over what we've covered in this lesson. If you feel like you've already mastered the material, you can skip it.

  • Pig speaking

    MORE videos! I'm excited!!

  • Cat speaking

    I'm pretty sure I've understood, but sometimes it's comforting to watch a video that tells me things I think I know.

Key Points

  • Classes are conceptually similar to what you've seen before, but have syntactic/terminological differences.
  • Instead of “instance variables” and “methods” we say “member variables” or “data members” and “member functions” (“members” to refer to them all together).
  • Declarations/definitions
    • The class definition goes in the header file (.hpp). It declares all of the class members.
      • In CS 70 convention we name all member variables with a trailing underscore (e.g., age_).
    • The member-function definitions go in the source file (.cpp). They specify the instructions for each function.
    • When we define a member function we have to specify which class it belongs to using the scope-resolution operator (::):
      • For example, void Cow::moo(int numMoos);.
  • A member function can be declared const.
    • A const member function promises not to change the values of member variables.
    • If you have a const object, you can only call const member functions on it.
  • We looked at two different kinds of constructors:
    1. Parameterized constructors take parameters and must be invoked explicitly (e.g., Cow bessie{3, 12}).
    2. Default constructors take no parameters and are implicitly invoked for default initialization (e.g., Sheep fluffy;).
  • You can disable the use of a default constructor using the delete keyword (e.g., Cow() = delete;).
  • In a constructor, the member-initialization list specifies how to initialize each member variable.
    • For example, Cow(int numSpots, int age) : numSpots_{numSpots}, age_{age}.
    • In CS 70, you must use a member-initialization list whenever possible. (The only exception is initializing a primitive array when you want a copy of another array—for that case, you need to loop over the elements of the array you're copying to populate your new array.)
  • A class definition ends in a semicolon (;).
    • A class definition ends in a semicolon.
    • A class definition ends in a semicolon.
  • LHS Cow speaking

    Did we mention that a class definition ends in a semicolon?

(When logged in, completion status appears here.)