zhuang@linux:~/notes/cpp/cpp_basics/$ cat
CPP Basics
Overview of CPP Basics.
The function header describes the interface between a function and the function that calls it.
The list-initialization protects against narrowing.
Raw string use R"(...)".
Bit Fields in Structures
struct torgle_register
{
unsigned int SN : 4; // 4 bits for SN value
unsigned int : 4; // 4 bits unused
bool goodIn : 1; // valid input
bool goodTorgle: 1; // successful torgling
};Prefixing Versus Postfixing
The user defines a prefix function that works by incrementing a value and then returning it. But the postfix version works by first stashing a copy of the value, incrementing the value, and then returning the stashed copy.Thus, for classes, the prefix version is a bit more efficient than the postfix version. In short, for built-in types, it most likely makes no difference which form you use. For user-defined types having user-defined increment and decrement operators, the prefix form is more efficient.
The comma operator is a sequence point. C++ states that the value of a comma expression is the value of the second part of the expression.
cin.get(ch) Versus cin.get()
| Property | cin.get(ch) | ch = cin.get() |
|---|---|---|
| Method for conveying input character | Assign to argument ch | Use function return value to assign to ch |
| Function return value for character input | An istream object; converts to true after bool conversion | Character code represented as an int value |
| Function return value at EOF | An istream object; converts to false after bool conversion | EOF |
A reference is rather like a const pointer; you have to initialize it when you create it, and when a reference pledges its allegiance to a particular variable, it sticks to its pledge.
Rvalue reference, that can refer to an rvalue. It’s declared using &&:
double && rref = std::sqrt(36.00);
double j = 15.0;
double && jref = 2.0* j + 18.5;
std::cout << rref << '\n';
std::cout << jref << '\n';Storage Duration, Scope, and Linkage
| Storage Description | Duration | Scope | Linkage | How Declared |
|---|---|---|---|---|
| Automatic | Automatic | Block | None | In a block |
| Register | Automatic | Block | None | In a block with the keyword register |
| Static with no linkage | Static | Block | None | In a block with the keyword static |
| Static with external linkage | Static | File | External | Outside all functions |
| Static with internal linkage | Static | File | Internal | Outside all functions with the keyword static |
The following are the most important OOP features:
- Abstraction
- Encapsulation and data hiding
- Polymorphism
- Inheritance
- Reusability of code
You can use only member functions to overload the following operators:
| Operator | Description |
|---|---|
= | Assignment operator |
() | Function call operator |
[] | Subscripting operator |
-> | Class member access by pointer operator |
Conversion Function
To convert to type typeName, you use a conversion function in this form:
operator typeName();Note the following points:
- The conversion function must be a class method.
- The conversion function must not specify a return type.
- The conversion function must have no arguments.
Special Member Functions
C++ automatically provides the following member functions:
- A default constructor if you define no constructors
- A default destructor if you don’t define one
- A copy constructor if you don’t define one
- An assignment operator if you don’t define one
- An address operator if you don’t define one
- move constructor
- move assignment operator
Virtual Function Mechanism

Using virtual functions has the following modest costs in memory and execution speed:
- Each object has its size increased by the amount needed to hold an address.
- For each class, the compiler creates a table (an array) of addresses of virtual functions.
- For each function call, there’s an extra step of going to a table to look up an address.
Member Function Properties
| Function | Inherited | Member or Friend | Generated by Default | Can Be Virtual | Can Have a Return Type |
|---|---|---|---|---|---|
| Constructor | No | Member | Yes | No | No |
| Destructor | No | Member | Yes | Yes | No |
operator= | No | Member | Yes | Yes | Yes |
operator& | Yes | Either | Yes | Yes | Yes |
| Conversion function | Yes | Member | No | Yes | No |
operator() | Yes | Member | No | Yes | Yes |
operator[] | Yes | Member | No | Yes | Yes |
operator-> | Yes | Member | No | Yes | Yes |
operator op= | Yes | Either | No | Yes | Yes |
operator new | Yes | Static member | No | No | void* |
operator delete | Yes | Static member | No | No | void |
| Other operators | Yes | Either | No | Yes | Yes |
| Other members | Yes | Member | No | Yes | Yes |
| Friends | No | Friend | No | No | Yes |
Here, operator op= represents compound-assignment operators such as operator+=, operator-=, operator*=, and operator/=.
zhuang@linux:~/notes/cpp/cpp_basics/$ comments