Re-Learning C++ Basic
Abstract: Recently I start using C++ again but having a long time in Python makes me almost forget everything on C++, in which case is common. This programming language is not a big deal, what matters are the logistics behind your programming thoughts. This post is where I collect my old coding snippets when learning C++. Hope these are good to you as well !
Tips
- Primary operators(name(),[],->,.), left to right rule.
- Unary operators(++,—,~,*,&,sizeof) are in same order, follow right to left rule. For instance:
*b++
: after all expressions increase the pointer by one, take b value,(*b)++
: take b value and increase it by one.*++b
: increase the pointer by one and take the value
Important Filing Ideas
Header file:
The definitions of macros, classes, structures, unions, and inline functions, as well as external data declarations, function prototypes, and typedefs. It is acceptable to place all common #include directives in a common header file and include it instead.
For a large project the cpp files should be placed in a folder named src
and the hpp files should be put in a folder called include
. When deeper category is asked, you should orgnized the files in their category name folders, such that:
test/include/handler/HandlerTest.hpp
and test/src/[handler/]HandlerTest.cpp
are exist. When you use #include "handler/HandlerTest.hpp"
this will by default search under the include
folder.
For example, This is called definitions of a class:
And This is called declarations of a class and should NOT be placed in a header file
Don’t mess them up !
Naming Conventions
- Function and function-like macro names should be verbs; all other names should be nouns.
- Object-like macro, const-qualified variable, and typedef names should be in upper case.
- Function, function-like macro, and class/struct/union/enum tag names should be in Pascal case (i.e., PascalCaseName).
- All other names should be in camel case, (i.e., camelCaseName).
Programming Shorthands
Good Return code
Create a typed instance with pointer:
Queue<int> *pQ = new Queue<int>();
Open File Examples C Version
Open File Examples C++ Version
Miscellaneous Topics
Literals & Magic Number
For C Style: #define DIAGONAL '.'
For C++ Style: const char LEADER = '#';
Functions
In multi-file programs all functions not shared with other files should be declared static to limit their scope.
External Variables
Referencing declarations of external variables used by more than one file must be kept in header files that are included by any files needing them. Do not by duplicate the declaration in each file. In multi-file programs, all external variables not shared with other files should be declared static to limit their scope.
Example: Basic Struct Uses
Pass Pointer back from a function and define incoming pointer parameters as constants
To use this, the string compare requires pointer:
Example: Call A Static Function Of A Class
Class Definition with “static” key word, this is in a header file
Class Declaration in cpp file
Call static function without an instance
Example: Formatting Input Into Struct
Partially declare struct member after definition and allocate it later on.
Example: Operator Overloading and "Friend" Keyword
A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.
In Header File:
In CPP File: Fast construction
How to use the code:
Example: Inheritance and Virtual Function
Public Inheritance: When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class.
Protected Inheritance: When deriving from a protected base class, public and protected members of the base class become protectedmembers of the derived class.
Private Inheritance: When deriving from a private base class, public and protected members of the base class become privatemembers of the derived class
In Header File:
In CPP File:
In Main File:
Example: Template and Typename. Exception-Safety
In Header File:
A template function or class can only be declared in header files