C and C++

NOTE: This page is still being constructed; please check back frequently for updates.

This page contains basic beginner and more advanced C and C++ snippets. The purpose of this page is two fold; a reference for those who are learning (and like to learn via pure code), as well as, a location for mpettersson to keep snippets and C/C++ related links. This pages subsections are:

 

C/C++ 101

The following are the general and technical aspects of the C and C++ programming languages:

C C++
Uses: Application, System, Low-Level Application, System
Website: gcc.gnu.org isocpp.org
Get Started: gcc.gnu.org Get Started!
Download: gcc.gnu.org/install gcc.gnu.org
Documents: ISO/IEC 9899:2018 isocpp.org
Creator: Dennis Ritchie Bjarne Stroustrup
First Released: 1972 1985
Implementation: Compiled Compiled
Type Safety: Weak Weak
Type System: Explicit Explicit with optional Implicit (in/after C++11)
Type Checking: Static Static
Imperative: Yes Yes
Aspect Oriented: No No
Object Oriented: No Yes
Functional: No Yes
Procedural: Yes Yes
Generic: Yes Yes
Reflective: No Yes
Event Driven: No Yes
Standardized: Yes (ISO C18) Yes (ISOCPP)
Failsafe I/O: No Some (STL iostreams do, but C APIs like stdio do not)
Garbage Collected: No (can be added through Boehm GC library) No (can be added through Boehm GC library)

 

C/C++ Concepts and Definitions

Pointer
– A pointer VARIABLE can store a memory address (Unlike normal variable which stores a value, such as an int).
– A VARIABLE that stores the address of another VARIABLE.
– A void pointer is a type of pointer that can point to somewhere without a specific type.
– A NULL pointer is a value that any pointer can take to represent that it is pointing to “nowhere”

& (Address of Operator)
– The address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&).
– For example: “foo = &myvar;” This would assign the address (not value) of myvar to foo;

* (Dereference Operator) (indirection operator)
– Operates on a pointer, and returns the value stored in the address kept in the pointer variable.
– Can be read as “value pointed to by”

References (&)
– When a variable is declared as reference, it becomes an alternative name for an existing variable.
– A variable can be declared as reference by putting ‘&’ in the declaration.
– A reference must be initialized when declared.
– Once a reference is created, it cannot be it cannot be reassigned (pointers can be reassigned).
– References cannot be NULL (pointers are often made NULL).
– Example: “int x = 10; int& ref = x;” now you can use x or ref interchangeably, without use of the dereference op.
– For common uses see: https://www.geeksforgeeks.org/references-in-c/

namespace
– Namespaces allow for a named scope (of types, functions, variables, etc.).
– Namespace is a feature added in C++ and not present in C.
– Multiple namespace blocks with the same name are allowed.
– A namespace definition begins with the keyword “namespace” followed by the namespace name and a {}
– Namespace declarations appear only at global scope.
– Namespace declarations can be nested within another namespace.
– Namespace declarations don’t have access specifiers. (Public or private)
– No need to give semicolon after the closing brace of definition of namespace.
– Example: “namespace namespace_name {int x, y;}” now you can access elsewhere with namespace_name::x
– Example: “#include <iostream>; using namespace std;” will let us do “cout << …” as opposed to “std::cout << …”
– NOTE: It’s not recommended to use “using namespace std;”.

Types
– Primitive Data Types:
– Are built-in or predefined data types and can be used directly to declare variables.
– Include: int, char, bool, float, double, void, wchar_t
– Derived Data Types:
– Are derived from the primitive or built-in datatypes.
– Namely: Function, Array, Pointer, Reference
– Abstract or User-Defined Data Types:
– These data types are defined by user.
– User-defined datatypes include: Class, Structure, Union, Enumeration, Typedef defined DataType
– Data type modifiers available in C++ are: signed, unsigned, short, long

volatile
– The volatile keyword informs the compiler that the value of variable it is applied to can change from the outside, without any update done by the code. This may be done by the OS, the hardware, or another thread.
– The compiler will load the value from memory each time.
– Volatile variables are not optimized.
– Volatile var are useful when multi-threaded programs have global var and any thread can modify shared vars.

Constructor
– A member function of a class which initializes an object of the class.
– A constructor is different from normal functions in following ways:
– Constructors has same name as the class itself
– Constructors do NOT have a return type
– A constructor is automatically called when an object is created.
– If no constructor is specified, the C++ compiler generates a default constructor (no args w/ empty body).

Destructor (~)
– A member function which destructs or deletes an object.
– A destructor function is called automatically when the object goes out of scope:
(1) the function ends
(2) the program ends
(3) a block containing local variables ends
(4) a delete operator is called
– Destructors have same name as the class preceded by a tilde (~)
– Destructors don’t take any argument and don’t return anything
– There can only one destructor in a class with classname preceded by ~, no parameters and no return type.
– When do we need to write a user-defined destructor?
– If we do not write our own destructor in class, compiler creates a default destructor for us.
– The default destructor works fine unless we have dynamically allocated memory or pointer in class.
– When a class contains a pointer to memory allocated in class, we should write a destructor to release memory
before the class instance is destroyed. This must be done to avoid memory leak.
– Destructors can be virtual (and should be virtual in base class with virtual function(s))

Structures (struct)
– Are user defined data types which are used to store group of items of non-similar data types.
– Example: “struct Point { int x, y; };” … “struct Point p1” use p1.x to access x (use pointer->x if pointer).

typedef
– Is used to assign a new name to any existing data-type.
– Example: “typedef unsigned int uint;” … “uint i = 5;”
– NOTE: typedef is used a lot with templates.

Declarations
– A declaration introduces a name into a scope.
– Example: “extern int y;” declares y, but does not define it (y is defined elsewhere)

Prototype
– A declaration for a function.
– Example: “double someFunction( double, int );”

Definition
– A definition fully specifies an entity.
– Definitions are where the actual creation of the entity in memory takes place.
– All definitions are also declarations, but not all declarations are definitions.
– Example: “int x;” declares and defines x; it is a definition because it creates the variable allocating memory

Implementations
– An implementation is another name for a definition of a function.
– That is, the implementation is the actual code of the function itself.

Preprocessor Directives
– Tell the compiler to preprocess (do something to) the source code before compiling.
– All preprocessor directives begin with a ‘#’ (hash) symbol (i.e., #include, #define, #ifndef)
– The (‘#’) symbol at the beginning of a statement indicates that it is a pre-processor directive.
– Preprocessor directives can be anywhere in a program.
– There are 4 main types of preprocessor directives:
– Macros (with and without arguments) – start with #define
– File Inclusion – start with #include
– Conditional Compilation – start with #ifdef macro_name and ends with #endif
– Other directives: #undef to undefine an existing macro and #pragma to turn on/off features

:: (scope resolution operator) is used to:
1) To access a global variable when there is a local variable with same name: I.E., ::var
2) To define a function (for a class) outside the class.
3) To access a class’s static variables from outside the class.
4) In case of multiple Inheritance (to access a classes variables).
5) For namespace I.E., std::cout
6) Refer to a class inside another class: I.E., outsideClass::insideClass::someVar = 7

Templates
– A way of reusing code to apply the same class to different data types (similar to Generics in Java).

Header Files
– The files that tell the compiler how to call some functionality (without knowing how the functionality actually works) are called header files.
– They contain the function prototypes.
– They also contain Data types and constants used with the libraries.
– We use #include to use these header files in programs. These files end with .h extension.

Library
– Library is the place where the actual functionality is implemented i.e. they contain function body.
– Libraries have mainly two categories: Static and Dynamic

Static Library
– Contains object code linked with an end user application and then they become the part of the executable.
– These libraries are specifically used at compile time which means the library should be present in correct location when user wants to compile his/her C or C++ program.
– In windows they end with .lib extension and with .a for MacOS.

Shared Library or Dynamic Library
– These libraries are only required at run-time i.e, user can compile his/her code without using these libraries.
– In short these libraries are linked against at compile time to resolve undefined references and then its distributed to the application so that application can load it at run time.
– For example, when we open our game folders we can find many .dll(dynamic link libraries) files. As these
libraries can be shared by multiple programs, they are also called as shared libraries.
– These files end with .dll or .lib extensions. In windows they end with .dll extension.

Virtual (base) classes
– Are used in virtual inheritance in a way of preventing multiple “instances” of a given class appearing in an
inheritance hierarchy when using multiple inheritances.

Virtual function
– A member function which is declared within a base class and is re-defined(Overriden) by a derived class.
– When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function.
– Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for function call.
– They are mainly used to achieve Runtime polymorphism
– Functions are declared with a virtual keyword in base class.
– The resolving of function call is done at Run-time.
– Virtual functions cannot be static and also cannot be a friend function of another class.
– Should be accessed using pointer or reference of base class type to achieve run time polymorphism.
– The prototype of virtual functions should be same in base as well as derived class.
– They are always defined in base class and overridden in derived class.
– It is not mandatory for derived class to override (or re-define the virtual function),
in that case base class version of function is used.
– A class may have virtual destructor but it cannot have a virtual constructor.

Pure Virtual Function (abstract function)
– A virtual function that doesn’t have an implementation (only method signature is declared).
– A pure virtual function is declared by assigning 0 in declaration.
– Example: virtual void show() = 0;

Abstract Class
– A class that contains at least one Pure Virtual Function

Operator Overloading
– The ability to provide the operators with a special meaning for a custom data type.
– The overloading function is always “operator” keyword followed by symbol of operator (i.e., “+”) and operator
functions are called when the corresponding operator is used.
– Almost all operators can be overloaded, the EXCEPTIONS are: . (dot) :: ?: sizeof

Default (Method) Values
– Methods/Functions can specify default values (like python)
– All default values must be after (right of) all non-default parameters.
– Example: “int func(int a, int b = 3){ … }”

? : (Ternary Operator) (same as Java)

 

C Keywords

auto break case char const continue
default do double else enum extern
float for goto if int long
register return short signed sizeof static
struct switch typedef union unsigned void
volatile while

 

C++14 Keywords

alignas alignof asm auto bool break
case catch char char16_t char32_t class
const constexpr const_cast continue decltype default
delete do double dynamic_cast else enum
explicit export extern false float for
friend goto if inline int long
mutable namespace new noexcept nullptr operator
private protected public register reinterpret_cast return
short signed sizeof static static_assert static_cast
struct switch template this thread_local throw
true try typedef typeid typename union
unsigned using virtual void volatile wchar_t
while

 

C/C++ Syntax

NOTE: This page is still being developed…

If you’re using a Mac or other Unix based machine, you’re probably able to execute C and C++ code. For C/C++ on Windows you have several C compiler and C++ compiler options… In case you were wondering, yes, Eclipse and NetBeans have support for C/C++.

The following is basic C and C++ syntax.

 

Pointers

The following is a short set of pointer notes:

 

Miscellaneous C/C++ Code

This section will contain a mix of interesting, helpful, and otherwise slightly more advanced snippets.

 

C and C++ Resources

The following are helpful tutorials, tools, code and miscellaneous Java resources:
10 Major Differences Between C and C++

 

As always, please contact us with comments, concerns, questions, etc. about C/C++!