Object Oriented Programming

Back to Computer-Science-Concepts

Object based programming forces the programmer to think about designing clean API for clients and allows hiding implementation. Adds inheritance which is the creation of subclasses who share an API with the superclass.

OO Terminology

Member Variable field:

Member Method:


A hierarchy of classes that share functionality and API.

Abstract Base Class

Cannot be instantiated, contains many virtual methods, so that children (inheritance) have consistent API for polymorphism. An ABC constructor is a recipe to be invoked by children's constructors.

A Polymorphic container:

vector<Humanoid*> humanoidList;
// pointers of a parent class can point to an instance
Humanoid *p = new Elf ("Elrond"); 
Dwarf *d = new Dwarf("Gimli"); // static pointer, pointing to its type
Monster *m = NULL; // dynamic pointer, currently pointing to nullptr

Protected describes private elements, available to children. ABCs should have constructors in protected, ABCs need ctors for children to be instantiated


A benefit of inheritance, the ability to treat all subclasses in similar ways (a unified API), e.g. all Shapes can use .area(). This is an example of subtype polymorphism (see link).

A rule of thumb: polymorphic methods should have different behaviours, not just different return values. An area function will compute area differently. A get type funciton is a bad polymorphic function, since it can be dealt with using a variable.

Some C++ Object Oriented Programming

Syntax is well explained using code:

Balloon b;
Balloon *pb = new Balloon;
class Balloon : public ParentClass{     // class declaration, with parent, in .h file
        Balloon(); // constructor
        Balloon(string colour); // constructor with parameter
        virtual ~Balloon();     // allows for redefinition in derived classes
        getColour() const ; // constant method, cannot change object
        static balloon_counter; // class variable
        string colour;
// Method definitions, in .cc file
Balloon::Balloon () {
    this->colour = “clear”;
Balloon::Balloon (string colour){
    this->colour = colour;
Balloon::Balloon(string colour) : ParentClass(), colour(colour) {}     // shorthand syntax, stops double instantiation of object parameters, calls parent constructor

Balloon::~Balloon (){}     // destructor

Static dispatch is used for non virtual methods, which is faster and hard-coded at compile time.

Dynamic dispatch looks up at run time the implementation of a virtual method.

Copy Constrcutor

Takes a const ref to existing object, creates a new copy of it as a new object:

Classname::Classname( const Classsname & c);

There is an implicit default copy constructor predefined for every class, it performs a memberwise copy construction, for every subpart of Classname.

Rule of 3: if your class needs to override the default implementation of any of the destructorm copy constructor or = operator, then it should be explicitly defined for all three.


Handles freeing memory, and is implicitly called when an objects scope is exited or delete is called on object.

Sharing Objects

  1. Objects to be shared should be declared on the heap using a ptr.
  2. Need clear "owner", who is responsible for deletion in dtor.
  3. "owner" needs protocol for transferring ownership.

Template Design Pattern

Design a high-level recipee that is the same for all children, with different details depending on child. Typically, public parent method and private sub-pieces in children.