12.1 — Welcome to object-oriented programming

Back in lesson 1.3 -- Introduction to objects and variables, we defined an object in C++ as, “a piece of memory that can be used to store values”. An object with a name is called a variable.

In traditional programming (what we’ve been doing prior to this point), programs are basically lists of instructions to the computer that define data (via objects) and then work with that data (via statements and functions). Data and the functions that work on that data are separate entities that are combined together to produce the desired result. Because of this separation, traditional programming often does not provide a very intuitive representation of reality. It’s up to the programmer to manage and connect the properties (variables) to the behaviors (functions) in an appropriate manner. This leads to code that looks like this:

So what is object-oriented programming? As with many things, it is perhaps understood most easily through use of an analogy. Take a look around you -- everywhere you look are objects: books and buildings and food and even you. Objects have two major components to them: 1) A list of relevant properties (e.g. weight, color, size, solidity, shape, etc…), and 2) Some number of behaviors that they can exhibit (e.g. being opened, making something else hot, etc…). These properties and behaviors are inseparable.

Object-oriented programming (OOP) provides us with the ability to create objects that tie together both properties and behaviors into a self-contained, reusable package. This leads to code that looks more like this:

This not only reads more clearly, it also makes it clearer who the subject is (you) and what behavior is being invoked (driving somewhere). Rather than being focused on writing functions, we’re focused on defining objects that have a well-defined set of behaviors. This is why the paradigm is called “object-oriented”.

This allows programs to be written in a more modular fashion, which makes them easier to write and understand, and also provides a higher degree of code-reusability. These objects also provide a more intuitive way to work with our data by allowing us to define how we interact with the objects, and how they interact with other objects.

Note that OOP doesn’t replace traditional programming methods. Rather, it gives you additional tools in your programming tool belt to manage complexity when needed.

Object-oriented programming also brings several other useful concepts to the table: inheritance, encapsulation, abstraction, and polymorphism (language designers have a philosophy: never use a short word where a long one will do). We will be covering all of these concepts in the upcoming tutorials over the next few chapters. It’s a lot of new material, but once you’ve been properly familiarized with OOP and it clicks, you may never want to go back to pure traditional programming again.

Note that the term “object” is overloaded a bit, and this causes some amount of confusion. In traditional programming, an object is a piece of memory to store values. And that’s it. In object-oriented programming, an “object” implies that it is both an object in the traditional programming sense, and that it combines both properties and behaviors. From this point forward, when we use the term “object”, we’ll be referring to “objects” in the object-oriented sense.

12.2 -- Classes and class members
11.x -- Chapter 11 comprehensive quiz

71 comments to 12.1 — Welcome to object-oriented programming

  • Waldo Lemmer

    > language designers have a philosophy: never use a small word where a big one
    > will do


  • s1ngular1tea

    I like how "Welcome to Object Oriented Programming" section keeps moving further down the page... ;)

    El oh el...

    • Alex

      Yeah, I'm not really happy about that... Current intent is to add a new chapter introducing Object Oriented programming earlier and talking about how to USE objects... then the current chapter on Object Oriented programming will be focused on how to create your own objects rather than using existing ones. But it's still pretty far back in the chapter order.

      The alternative is to start with a bunch of OO stuff first and hand-wave through a bunch of the why/how. That might work for people who are good at memorization, but for those who like to understand what everything means it's pretty awful.

      • Joseph Grace

        I'd like to chime in here and say you shouldn't worry! The way the tutorials are already structured is fantastic. These have been the greatest learning materials for me on my journey: a great balance between comprehensibility and accessibility! Well done you and the other contributors.

  • Prasetya

    I've learned java and now I want to implement the whole java standard library with c++, not all, just api level 1(android). But I can't figure out how to implement this OOP thing:



    I've already asked on internet and most answer is to forward declare a class like:

    But when I try, I keep getting "invalid use of incomplete type 'class B'" error.
    Is it because of my compiler or what? Please help

    • Siska

      Don't know why but you have to use forward declared items form pointer or from reference like:

      class A
          B *b;
              // or B &b;

      class B : public A


      I'm not really an expert, but at least on VS it works like this

      • GibMirDeinArsch

        You're right, and the reasoning for this is that the compiler needs to know how much space to allocated for all variables in your program. So when it sees `B b`, and B is an incomplete type, how will it know how much memory to allocated for it? Whereas if you have a pointer or a reference, then that's a constant size, and the compiler simply allocates that much space, and moves on.

Leave a Comment

Put all code inside code tags: [code]your code here[/code]