Fundamentals of Modern C++: Types, Pointers, Functions, and Classes
The article offers a concise, modern C++ (C++11+) tutorial covering fundamental built‑in types, variable declaration with auto and uniform initialization, pointer and array handling, function definitions, overloading, templates, class design—including constructors, move semantics, inheritance, virtual functions—and essential standard‑library utilities such as smart pointers and std::function.
This article provides a comprehensive introduction to modern C++ (C++11 and later) aimed at readers who have some prior exposure to C++ or want to quickly grasp the language fundamentals.
Key Characteristics of C++
C++ is a general‑purpose language known for its performance, low‑level memory control, and support for multiple programming paradigms (object‑oriented, generic, procedural, functional). It is widely used in system software, game engines, and performance‑critical applications.
Basic Types
The article lists the fundamental built‑in types, including integer families (short, int, long, long long), floating‑point types (float, double, long double), character types (char, char16_t, char32_t, wchar_t), boolean, and special types (void, nullptr_t). A table summarises each type’s typical size and remarks.
<span>short // at least 16 bits</span>
<span>int // usually 32 bits</span>
<span>long long // at least 64 bits (C++11)</span>
<span>float // 32‑bit single precision</span>
<span>double // 64‑bit double precision</span>
<span>char // at least 8 bits</span>
<span>bool // implementation‑defined size</span>
<span>void // no object representation</span>
<span>nullptr_t // type of nullptr (C++11)</span>Variable Declaration and Initialization
Variables must be declared with a type. C++11 introduced auto for type deduction and uniform initialization using braces {}, which prevents narrowing conversions.
<span>int a{0};</span>
<span>double b{3.14};</span>
<span>MyClass obj{5, 3.14}; // requires a matching constructor</span>Implicit Conversions
The compiler performs safe implicit conversions (e.g., integer promotion) and warns on potentially unsafe narrowing (e.g., assigning a floating‑point literal to an int).
<span>int a = 7.7; // compiles with warning</span>
<span>int b = {1.0}; // error: narrowing conversion</span>Pointers and Arrays
Pointers store memory addresses and enable direct memory manipulation. The article explains pointer declaration, const‑pointer vs. pointer‑to‑const, pointer arithmetic, and the relationship between arrays and pointers.
<span>int *p = &a; // pointer to int</span>
<span>const int *cp = &a; // pointer to const int</span>
<span>int * const pc = &a; // const pointer to int</span>
<span>int arr[5] = {1,2,3,4,5};</span>
<span>int *p1 = arr; // array decays to pointer</span>Dynamic arrays can be allocated with new[] and must be released with delete[]. For multi‑dimensional dynamic arrays, a double‑pointer approach is shown.
<span>int *arr = new int[10];</span>
<span>int **matrix = new int*[rows];</span>
<span>for (int i=0;i<rows;++i) matrix[i] = new int[cols];</span>
<span>delete[] matrix[i]; // per‑row</span>
<span>delete[] matrix; // pointer array</span>Functions
Function syntax includes return type, name, parameter list, and body. The article covers value, pointer, reference, and rvalue parameter passing, as well as const‑correctness.
<span>int add(int a, int b) { return a + b; }</span>
<span>void swap(int &a, int &b) { int t=a; a=b; b=t; }</span>
<span>void printInfo(const int arr[], int size); // pointer to const array</span>Function Overloading and Templates
Overloading allows multiple functions with the same name but different signatures. Templates provide type‑independent implementations.
<span>int add(int a, int b);</span>
<span>float add(float a, float b);</span>
<span>template<typename T> T add(T a, T b) { return a + b; }</span>Return‑type deduction with auto and trailing return types ( auto f() -> decltype(expr)) are demonstrated.
Classes
Classes encapsulate data and behaviour. The article shows class definition, access specifiers (public, private, protected), constructors (including member‑initializer lists), destructors, copy/move semantics, operator overloading, inheritance (public, protected, private), virtual functions, abstract classes, and friend declarations.
<span>class Person {</span>
<span>public:</span>
<span> Person(const std::string& name, int age);</span>
<span> void printInfo() const;</span>
<span>private:</span>
<span> std::string mName;</span>
<span> int mAge;</span>
<span>};</span>
<span>// Move constructor example</span>
<span>class Buffer {</span>
<span> int* data;</span>
<span>public:</span>
<span> Buffer(Buffer&& other) noexcept : data(other.data) { other.data=nullptr; }</span>
<span> Buffer& operator=(Buffer&& other) noexcept { if(this!=&other){ delete[] data; data=other.data; other.data=nullptr; } return *this; }</span>
<span> ~Buffer(){ delete[] data; }</span>
<span>};</span>Inheritance examples illustrate polymorphism via virtual methods and the need for a virtual destructor.
<span>class Base { public: virtual void foo() { } virtual ~Base() {} };</span>
<span>class Derived : public Base { public: void foo() override { /* ... */ } };</span>
<span>Base* b = new Derived(); b->foo(); delete b; // safe because Base::~Base is virtual</span>Standard Library Utilities
Smart pointers ( std::unique_ptr, std::shared_ptr, std::weak_ptr) and std::function are introduced for automatic resource management and generic callbacks.
<span>auto up = std::make_unique<MyClass>();</span>
<span>std::shared_ptr<MyClass> sp = std::make_shared<MyClass>();</span>
<span>std::function<void(int)> cb = [](int v){ std::cout<<v; };</span>Overall, the article serves as a concise reference for modern C++ syntax, type system, memory handling, function design, and object‑oriented features.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
