CPP as a second language: References

Key Points

Introduction
Where does C++ fit in?
  • C++ is a low level language offering great flexibility.

  • C++ allows the creation of very performant programs.

  • If starting from scratch use a higher level language such as Python and convert performance critical parts to C++.

  • Do not try to use all the features of C++

First C++ program
  • g++ can be used to compiler and link C++ programs.

  • Program execution starts in the main function.

  • std::cout allows you to write strings to the terminal using the << operator.

  • A class is a datatype.

  • An object is a particular instance of that datatype or class.

  • Namespaces group names and help to avoid name clashes.

Memory
  • Every use of new/new [] should be match by a delete/delete [].

  • The amount of dynamic memory allocated can be determined at runtime.

  • Need to be careful about incorrect access and especially modification of memory.

A first class
  • Use the class keyword to create a new class.

  • Class access modifiers control what parts of the program can access the classes members.

  • Class members can be accessed with the . operator.

Class member functions
  • Class member functions have access to object members implicitly.

  • Access modifiers apply to both class member functions and variables.

Function overloading
  • Any function can be overloaded, class member function or stand alone function

  • Overloaded functions are only distinguished by their parameters and not their return types.

  • The this keyword allows you to explicitly reference the object within a member function.

Constructors and destructors
  • Constructors are called when objects are first created.

  • Destructors are called when objects go out of scope.

  • Copy constructors are called when a new object is initialized from an existing object.

Operator overloading
  • You can create your own operators for your classes.

  • When any one of destructor, copy constructor, or assignment operator are defined you likely want to define all three (e.g. rule of three).

Inheritance
  • Inheritance allows reuse of code in related classes.

  • Inheritance can happen in one of three ways public, protected or private and affects how the inherited members are accessed in the derived class.

Virtual functions
  • Polymorphism allows different member functions to be called depending on the object’s base class

  • Polymorphism is achieved using the virtual keyword in function declarations

Abstract classes
  • A pure virtual function is a function that does not have an implementation.

  • An abstract class is a class that has one or more virtual member functions.

  • Abstract classes can not be used to create objects.

Using templates
  • STL containers can hold any datatype.

  • Templates generate type specific implementation code at compile time.

  • Heavy use of templates can increase compile times.

Making templates
  • Create a new template with the template keyword.

  • Template parameters are specified inside <> brackets after the typename keyword.

  • Template parameters can be used in place of types and will be replace with actual types as needed at compile time.

Wrapping up

Glossary

cd
a command to change directories.