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.
short // at least 16 bits
int // usually 32 bits
long long // at least 64 bits (C++11)
float // 32‑bit single precision
double // 64‑bit double precision
char // at least 8 bits
bool // implementation‑defined size
void // no object representation
nullptr_t // type of nullptr (C++11)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.
int a{0};
double b{3.14};
MyClass obj{5, 3.14}; // requires a matching constructorImplicit 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).
int a = 7.7; // compiles with warning
int b = {1.0}; // error: narrowing conversionPointers 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.
int *p = &a; // pointer to int
const int *cp = &a; // pointer to const int
int * const pc = &a; // const pointer to int
int arr[5] = {1,2,3,4,5};
int *p1 = arr; // array decays to pointerDynamic arrays can be allocated with new[] and must be released with delete[] . For multi‑dimensional dynamic arrays, a double‑pointer approach is shown.
int *arr = new int[10];
int **matrix = new int*[rows];
for (int i=0;i
delete[] matrix[i]; // per‑row
delete[] matrix; // pointer arrayFunctions
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.
int add(int a, int b) { return a + b; }
void swap(int &a, int &b) { int t=a; a=b; b=t; }
void printInfo(const int arr[], int size); // pointer to const arrayFunction Overloading and Templates
Overloading allows multiple functions with the same name but different signatures. Templates provide type‑independent implementations.
int add(int a, int b);
float add(float a, float b);
template<typename T> T add(T a, T b) { return a + b; }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.
class Person {
public:
Person(const std::string& name, int age);
void printInfo() const;
private:
std::string mName;
int mAge;
};
// Move constructor example
class Buffer {
int* data;
public:
Buffer(Buffer&& other) noexcept : data(other.data) { other.data=nullptr; }
Buffer& operator=(Buffer&& other) noexcept { if(this!=&other){ delete[] data; data=other.data; other.data=nullptr; } return *this; }
~Buffer(){ delete[] data; }
};Inheritance examples illustrate polymorphism via virtual methods and the need for a virtual destructor.
class Base { public: virtual void foo() { } virtual ~Base() {} };
class Derived : public Base { public: void foo() override { /* ... */ } };
Base* b = new Derived(); b->foo(); delete b; // safe because Base::~Base is virtualStandard 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.
auto up = std::make_unique
();
std::shared_ptr
sp = std::make_shared
();
std::function
cb = [](int v){ std::cout<Overall, the article serves as a concise reference for modern C++ syntax, type system, memory handling, function design, and object‑oriented features.
DaTaobao Tech
Official account of DaTaobao Technology
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.