Object-Oriented Programming in C++

C++ was originally called “c with classes” meaning that it was everything that the c language was plus the ability to create classes and objects in the language.  I’m sure that you can see the value in creating a higher level construct over and above a simple variable.  For example, if we were modeling the data for a group of workers at a company, we would want to keep track of a number of different things about each employee: name, address, employee id number, yearly salary, and department perhaps.  We could make use of the concept of parallel arrays meaning that we have 2 or more arrays whose elements are related via their position (or subscript) in the array.  So, name[9], empID[9], salary[9], etc would all refer to data about the same employee because each is being accessed with the same subscript.  However, after a while, this becomes cumbersome and isn’t very extensible as we add new variables to track. 

C has a construct known as a struct.  You can think of a struct as a collection of variables.  So, you might see something like this:

struct employee
    char name[50];
    int idNumber;
    float salary;
    char deptCode[2];

However, what you may notice is that there are no methods associated with the struct (because that wasn’t a thing in C).  So, they aren’t, strictly speaking, objects in the OOP sense.

OK – enough about C.  C++ is what we’re talking about.  C++ has language features for that allow for robust object oriented programming including:

  • encapsulation
  • inheritance
  • polymorphism

An important thing to remember about a C++ class:  it is just a new type that is defined by the programmer.  Once the class is declared, it can be used like any other type that is included in the language such as int, float, or char.

OK – Time for you to read.  Overland Chapter 7 – Sections 1 – 7.  This is important info.  Don’t just peruse.  Read, take notes, etc. (You can ignore anything that talks about the “union” topic!)

Some Videos (sorry, more than usual though):  Header Files in C++, OOP Concepts, User Defined Types (kind of long), Rule of Three, Operator Overloading in C++, Overloaded Insertion Operator ExampleRule of 3 Implementation  For these videos, you can ignore the fact that he is using Eclipse as an IDE instead of QtCreator.  The C++ stuff is the same.

Some Big Differences between Java and C++ with OOP

Here are some big things that you’ll see different between the two languages:

  • C++ allows the programmer to separate the interface of the class from the implementation of the member functions in a class.  There are a few different reasons that you want to do this that we’ll hopefully talk about.
  • Related to the last point, the interface and implementation can actually be put in two separate files.  It is convention to have a .h and a .cpp file for each class you create.
    • .h -> the class interface.  This shows the data members and member functions.  Think about looking at a remote control for a TV.  The class interface is kind of like looking at the buttons on the control.
    • .cpp -> implementation for the class member functions.  This would be like taking the remote control apart and looking at the internal circuitry.  To use the control, you don’t really need to know about that stuff.  You just need to be assured by the implementor of the remote that if you press the “volume up” button, the volume will be raised.
  • C++ allows the programmer to overload a large number of operators with each class.  (Covered in section 7.6)  This is not at all possible in Java.  Ultimately, operators are just “syntactic sugar”.  Every operator performs a function really, so all you’re doing with operator overloading is saying what underlying functionality should be executed when a particular operator is encountered in a certain context.
  • C++ classes have destructors.  This isn’t a thing in Java either.  A destructor is a method that is automatically called whenever an object is being destroyed (it goes out of lifetime, for example). The most common use case for us will be to clean up dynamically allocated memory.

Speak Your Mind



This site uses Akismet to reduce spam. Learn how your comment data is processed.