Skip to content

ACtE0303 C++ language constructs with objects and classes

Namespace, overloading, inline, default argument, references

Construct Core idea
Namespace avoids global name collisions
Function overloading same function name with different parameter lists
Inline function suggests substitution at call site; does not guarantee it
Default argument caller may omit trailing arguments with defaults
Reference alias to an existing object

Overloading rule cue:

  • parameter list must differ in number, types, or order;
  • return type alone cannot overload a function.

Reference facts:

  • a reference must be bound when created;
  • ordinary lvalue references cannot be reseated to refer to another object;
  • pass-by-reference can avoid copies and allow modification.

Return by reference uses a reference return type such as T& f(). The result aliases an existing object, so the referred object must outlive the call. Never return a reference to an automatic local object because it is destroyed when the function returns.

Class and object basics

Class = user-defined type combining data and functions.

class Box {
    int width;
public:
    Box(int w) : width(w) {}
    int get() const { return width; }
};

Object = instance of a class.

Access specifiers:

Specifier Access
public accessible through the interface
private accessible only within class and friends
protected accessible within class, friends, and derived classes

Default access trap:

  • class members are private by default;
  • struct members are public by default in C++.

Member access and member functions

Form Use
obj.member object member access
ptr->member pointer member access
Class::member scope-qualified static member or out-of-class definition

Const member function cue:

  • a member function ending with const promises not to modify the observable state of the object through that object reference, except for mutable members or external side effects.

Constructors and destructor

Constructor facts:

  • constructor name equals class name;
  • no return type;
  • runs when object is created;
  • can be overloaded.

Common constructor categories:

Category Recognition
Default constructor can be called with no arguments
Parameterized constructor takes arguments
Copy constructor constructs from another object of same type
Move constructor constructs from expiring object resource state

Destructor facts:

  • name is ~ClassName();
  • no return type and cannot take parameters;
  • runs on object destruction to release owned resources and perform cleanup.

Important accuracy condition:

  • dynamically allocated objects do not by themselves make a user-written destructor mandatory;
  • a class needs a custom destructor when it owns resources requiring explicit release or has non-default cleanup semantics;
  • if a class only contains members that clean up themselves, the implicitly generated destructor may be correct.

Dynamic memory allocation for objects and arrays

Form Meaning
new T(args) allocate and construct one object
delete p destroy and deallocate one object
new T[n] allocate array of objects
delete[] p destroy and deallocate array

Trap: pairing new[] with delete or new with delete[] is wrong.

this, static, const, and friend

this pointer facts:

  • inside a non-static member function, this points to the current object;
  • it is an implicit parameter;
  • unavailable in static member functions because they are not tied to an object instance.

Static members:

Member type Property
Static data member shared by all objects of the class
Static member function callable without an object; can access only static members directly

Constant objects:

  • can call only const member functions unless a non-const access is otherwise permitted through other means;
  • their data members cannot be modified through that object interface.

Friend facts:

  • friend function or friend class is granted access to private/protected members;
  • friendship is explicit, not inherited, not transitive, and not automatically mutual.

C++-class-construct revision box

  • Return type alone cannot distinguish overloads.
  • Reference is an alias, not a reseatable pointer-like object in normal use.
  • class defaults to private access.
  • Constructor initializes; destructor cleans up owned resources when needed.
  • new[] pairs with delete[].
  • this exists only in non-static member functions.