Tech

Classes: A Deeper Look, Part 2–STRUCTURED PROGRAMMING Course Notes

Classes: A Deeper Look, Part 2

‘const’ (Constant) Objects and ‘const’ Member Functions

  • The keyword ‘const‘ can be used to specify that an object is not modifiable and that any attempt to modify the object should result in a compilation error.
  • C++ compilers disallow non-const member function calls on const objects.
  • An attempt by a const member function to modify an object of its class is a compilation error.
  • A member function (behavior) is specified as const both in its prototype and in its definition.
  • A const object must be initialized.
  • Constructors & destructors cannot be declared const.
  • const data member & reference data members must be initialized using member initializers.

Composition: Objects as Members of Classes

  • A class can have objects of other classes as members–this concept is called composition.
  • Member objects are constructed in the order in which they’re declared in the class definition, & before their enclosing class objects are constructed.
  • If a member initializer is not provided for a member object, the member object’s default constructor will be called implicitly.

‘friend’ Functions and ‘friend’ Classes

  • A ‘friend‘ function of a class is defined outside that class’s scope, yet has the right to access all of the class’s members.
  • Stand-alone functions or entire classes may be declared to be friends.
  • A friend declaration can appear anywhere in the class.
  • The friendship relation is neither symmetric nor transitive.

Using the ‘this’ Pointer

  • Every object has access to its own address through the ‘this‘ pointer.
  • An object’s ‘this‘ pointer is not part of the object itself–i.e., the size of the memory occupied by the ‘this‘ pointer is not reflected in the result of a ‘sizeof‘ operation on the object.
  • The ‘this‘ pointer is passed as an implicit argument to each non-‘static’ member function.
  • Objects use the ‘this‘ pointer implicitly or explicitly to reference their data members (attributes) & member functions (behaviors).
  • The ‘this‘ pointer enables cascaded member-function calls in which multiple functions are invoked in the same statement.

‘static’ Class Members

  • A ‘static‘ data member represents “class-wide” information (i.e., a property of the class shared by all instances, not a property of a specific object of the class).
  • static‘ data members have ‘class‘ scope & can be declared ‘public‘, ‘private‘, or ‘protected‘.
  • A class’s ‘static‘ members exist even when no objects of that class exist.
  • To access a ‘public static‘ class member when no objects of the class exist, simply prefix the class name & the binary scope resolution operator ( :: ) to the name of the data member.
  • A member function should be declared ‘static‘ if it does not access ‘non-static‘ data members or non-‘static‘ member functions of the class. Unlike non-‘static‘ member functions, a ‘static‘ member function does not have a ‘this‘ pointer, because ‘static‘ data members & ‘static‘ member functions exist independently of any objects of a class.

Data Abstraction & Information Hiding

  • Abstract data types are ways of representing real-world & conceptual notions to some satisfactory level of precision within a computer system.
  • An abstract data type captures two notions: a data representation & the operations that can be performed on that data.