ATM Logo

C++ Tutorial - Learn C++

C++ Logo

This page was written to help students to understand some of the advanced concepts of object orientated programming in C++. It does not start with the details of getting started in C++, or the basics of controlling program flow. Rather, it starts off with the advanced concepts of classes and objects, inheritance, polymorphism, arrays and pointers.

The advanced concepts are all demonstrated using working C++ programs. The programs have been heavily commented to give students a step by step explanation for every line of code and every new concept. The code has been formatted using an easy to follow colour scheme specially designed for C++ code listings.

Importantly, all of the programs have been released as free software under the GNU GPL.

GNU GPL logo

The Free C++ Advanced Concepts Guide

The Free C++ Advanced Concepts Guide - cover photo

A free C++ advanced concepts guide with free software programs:

The Free C++ Advanced Concepts Guide

Free C++ Software

C++ Classes and Objects:

This program demonstrates classes, objects, constructors, destructors and member functions in C++. Free software published under the GNU GPL:

  • Classes - A class is a data type defined by the programmer, in this case Cuboid. The class has members (dimensions a,b and c) and member functions to return useful information about the object. The class describes the structure of all objects of that class.
  • Objects - An object is an instance of a class. When an object is created (constructed or instantiated), memory is allocated to it.
  • Constructor - A constructor is a member function used to initialise an object when it is created. The constructor is public and returns no value.
  • Destructor - A member function used to remove the object from memory when it is no longer needed.
  • Member functions - class functions that return useful information about objects of the class, in this case volume, surface area etc.


C++ Classes and Objects (view code in browser)
C++ Classes and Objects (download .cpp file)

C++ Class Inheritance, Polymorphism and Friend Demo:

This program demonstrates class inheritance, polymorphism and friend functions in C++. Free software published under the GNU GPL:

  • Inheritance - The subclasses Plastic, Metal and Ceramic are all materials and inherit some common properties from their parent (base) class Material
  • Polymorphism - From the Greek meaning "many forms" this is an important principle of Object Orientated Programming. The derived classes can exhibit different forms of behaviour depending upon their properties. In this case, the properties member function is overridden in the derived classes. Also, the friend function twoPoundsOff(Material *poA) is able to operate on objects of the base class or derived classes, another example of Polymorphism
  • Friend function - The class member cost is protected in the Material base class, the function twoPoundsOff(Material *poA)is a friend, it is authorised to change the class member
  • Abstraction - This is the concept of simplifying real world things and representing their essential characteristics in software. For example, the plastic toy is represented as a plastic object with the properties of plastic, a density and a cost.
  • Classification - The materials plastic, metal and ceramic are grouped together as members of the class material


C++ Class Inheritance, Polymorphism and Friend Demo (view code in browser)
C++ Class Inheritance, Polymorphism and Friend Demo (download .cpp file)

C++ Arrays and Pointers Demo:

This program demonstrates passing arrays to functions by reference, passing pointers to arrays to functions and changing the contents of a memory address using a pointer. Free software published under the GNU GPL:

C++ Arrays and Pointers Demo (view code in browser)
C++ Arrays and Pointers Demo (download .cpp file)

A Brief History of Object Orientated Programming

Bjarne Stroustrup, the creator of C++
Bjarne Stroustrup, the creator of C++

C++ (Stroustrup, AT&T Bell Labs, New Jersey, 1979) extended the C programming language by adding Simula 67-like features to it, creating a powerful object orientated programming language; it was originally named C with classes.

Fortran (Backus, IBM, New York, 1957), short for mathematical FORmula TRANslation, was the first widely used high-level computer language to use a compiler to produce machine code, making programming easier. Fortran was most popular during the 1950s and 1960s. It included the modern concept of data abstraction (information hiding). The floating point data type had it’s internal mechanism hidden from the programmer.

In 1967, Simula 67 (Nygaard & Dahl, Norwegian Computing Center, Oslo, 1967) was the world’s first object orientated programming (OOP) language. It used classes; data structures with data and functions packaged together.

Smalltalk (Kay, Ingalls & Goldberg, Xerox PARC, California, 1972) was a programming language designed so that objects could be communicated with by passing messages. The Smalltalk team were the first to introduce the term object orientated programming (OOP). Smalltalk was strongly influenced by Lisp (McCarthy, MIT AI Labs, Cambridge, Massachusetts, 1958), short for LISt Processor, which used lists of objects/ atoms.

The C programming language (Ritchie, AT&T Bell Labs, New Jersey, 1972) was written to re-write a portable version of the Unix operating system (originally written in assembly language) in C, a high-level computer language which uses a compiler to produce machine code. C++ (Stroustrup, AT&T Bell Labs, New Jersey, 1979) extended the C programming language by adding Simula 67-like features to it, creating a powerful object orientated programming language; it was originally named C with classes. C++ was first standardised by the ISO (International Organization for Standardization ) in 1998.


Compiling and Running C++ Programs

C++ programs can be written using a simple text editor:

All of the programs in this guide were written using the gedit text editor in Linux. This text editor has the advantage of recognising C++ code and formatting it in colour (syntax highlighting).

C++ files are saved with the file extension .cpp For example: ClassesAndObjects.cpp

A compiler converts the C++ program into executable machine code. The GNU C++ compiler is freely available from the Free Software Foundation:

https://gcc.gnu.org/

The programs in this guide were compiled using the Code::Blocks IDE (integrated development environment). Code::Blocks is a free cross-platform IDE which runs on Linux, Mac and Windows.

http://www.codeblocks.org/

MinGW (Minimalist GNU for Windows) is a free programming environment for Windows.

http://www.mingw.org/

The Nanyang Technological University in Singapore have produced an excellent C++ introduction here:

https://www.ntu.edu.sg/home/ehchua/programming/cpp/cp0_Introduction.html