C++ - Chapter 4: Function

Functions Functions are self-contained blocks of code designed to perform specific tasks. They promote code reusability, modularity, and abstraction. Functions can participate in polymorphism through: Compile-time polymorphism (Static Polymorphism) Function overloading Operator overloading Function templates Runtime polymorphism Virtual functions 1. Function Overloading Function overloading allows creating multiple functions with the same name but different parameter types or parameter counts. Different return types alone are not enough to overload a function. Function delete using delete keyword. Default-arguments is a default value provided for a function parameter. Parameters with default arguments must always be the rightmost parameters, and they are not used to differentiate functions when resolving overloaded functions. void print_int(int x) {} void print_int_or_default(int x, int y = 2) {} template <typename T> void print_int(T) = delete; void main() { print_int(97); // okay // printInt('a'); // compile error // printInt(true); // compile error print_int_or_default(5); // prints: 5, 2 print_int_or_default(5, 10); // prints: 5, 10 } 2. Operator Overloading Operator overloading allows C++ operators to be customized for user-defined types. ...

May 24, 2026 · 7 min · Phong Nguyen

C++ - Chapter 5: Data Types

Data Types A data type defines the kind of value a variable can store, how much memory it uses, and what operations can be performed on it. 1. Type Conversion Type conversions │ ├── Implicit conversions (compiler performed automatically) │ │ │ ├── Numeric promotions │ │ ├── bool -> int │ │ ├── char -> int │ │ └── float -> double │ │ │ └── Numeric conversions │ ├── Widening conversions │ │ ├── int -> long long │ │ └── int -> double │ │ │ └── Narrowing conversions │ ├── double -> int │ ├── int -> char │ └── long long -> int │ └── Explicit conversions (casts) ├── static_cast<T>(expr) ├── dynamic_cast<T>(expr) ├── const_cast<T>(expr) ├── reinterpret_cast<T>(expr) └── (T)expr ///< C-style cast 1.1 Implicit Implicit type conversion is performed automatically by the compiler when an expression of some type is supplied in a context where some other type is expected. ...

May 24, 2026 · 6 min · Phong Nguyen

C++ - Chapter 6: Pointer and Reference

Pointer and Reference lvalue/rvalue An lvalue is an expression that refers to an identifiable object, function, or bit-field. It has an address that can be taken and typically persists beyond a single expression. An rvalue is an expression whose primary purpose is to provide a value rather than identify an object. e.g. temporary objects and literals An lvalue can generally be used wherever an rvalue is expected because its value can be read. ...

May 24, 2026 · 11 min · Phong Nguyen

Cpp

See plus plus :) . Refer Introduce Fundamentals String 21.8. Printing inherited classes using operator« Refer to the learncpp.com ## 23. I/O ### 23.1. I/O Streams - It is a part of the STL. - I/O is implemented with `streams`. <br> - **stream** is a sequence of bytes that can be accessed sequentially. It may produce or consume amounts data over time. - **input stream**: used to hold input data from a data producer. - **output stream**: used to hold output data for a particular data consumer. - *e.g.* when writing/input data to an device, the device may not be ready to accept that data, so the data will sit in the stream. - [C++] <=> streams <=> [os] <br> - **I/O in C++**: we can use the STL classes to deal with streams - **input stream**: `istream` & extraction operation (`>>`) is used to remove values from the stream - **output stream**: `ostream` & insertio operation (`<<`) is used to put vlaues in the stream.\ - `iostream` can handle both i & o <br> - **Standard streams**: - `cin`: an `istream` object tied to the standard input - `cout`: an `ostream` object tied to the standard output - `cerr`: an `ostream` object tied to the standard error - unbuffered output - `clog`: an `ostream` object tied to the standard error - buffered output ### 23.2. Input with istream - Use `extraction operator (>>)` to read information from an input stream. It skips **whitespace (blanks, tabs, and newlines)**. Use `get(), getLine()` to not discard the whitespace. - `manipulator` is an object that is used to modify a stream when applied with the `extraction (>>)` or `insertion (<<)` operators. <iomanip> ### 23.3. Output with ostream https://www.learncpp.com/cpp-tutorial/output-with-ostream-and-ios/ ### 23.4. Stream classes for string <sstream> - String streams provide a buffer to hold data but are not connected to an I/O channel. - The primary uses of the string stream: - Display data later then or proccess i/o data. - Get data into a string stream - Get data from a string stream - Convertion strings/numbers - Clear a string stream - e.g: ```cpp #include <sstream> #include <string> int main() { std::stringstream os{}; // input os << "0xF"; std::cout << os.str(); os.str("0x1 0x2"); std::cout << os.str(); // output std::string bytesStr = os.str(); std::cout << bytesStr; os.str("0x0 0xF 0xE 0x2"); os >> bytesStr; std::cout <<bytesStr; // conversions int byte_low = 0xFFF; int byte_high = 0x001; os.clear(); os << byte_low << ' ' << byte_high; std::cout << os.str(); } 23.5. Validation https://www.learncpp.com/cpp-tutorial/stream-states-and-input-validation/ ...

February 9, 2025 · 5 min · Phong Nguyen

GTK4

Gtk4 with Cpp. References: Gtk 1. Introduction Gtk4 is a widget toolkit for creating graphical user interface (UI). It works on many UNIX-like platform, Windows and Macos. 2. Setup the environment Note: - This is the guides for Windows machine using Windows Subsystem for Linux (WSL). - [W]: The action will be performed on Windows Machine. - [U]: The action will be performed on WSL (in my case, it named "Ubuntu") on the host Windows Machine. - (Guides: ...): The reference link that I refer the step. - (Not try yet): This is referred from the guides but I was not in that case, so that I could not try. ------------------------------- A. Installing from packages ------------------------------- 1. [L] Install required packages. 1.1. [L] Debian/Ubuntu: sudo apt install libgtk-4-1 libgtk-4-dev gtk-4-examples 2. [L] Verify installation. 2.1. [L] gtk version - Run: pkg-config --modversion gtk4 2.2. [L] examples - Run: gtk4-demo ------------------------------- B. (Optional) Build from Source ------------------------------- 1. [L] Install build tools. 1.1. [L] sudo apt install build-essential meson ninja-build \ libglib2.0-dev libpango1.0-dev libgdk-pixbuf-2.0-dev \ libatk1.0-dev libepoxy-dev libgirepository1.0-dev 2. [L] Clone GTK source. 2.1. [L] git clone https://gitlab.gnome.org/GNOME/gtk.git 2.2. [L] cd gtk 3. [L] Build with Meson + Ninja. 3.1. [L] meson setup builddir 3.2. [L] ninja -C builddir 4. [L] (Optional) Install system-wide. 4.1. [L] sudo ninja -C builddir install 3. HelloWorld Create a new file named hello-world-gtk4.c with following content: #include <gtk/gtk.h> static void print_hello (GtkWidget *widget, gpointer data) { g_print ("Hello World\n"); } static void activate (GtkApplication *app, gpointer user_data) { GtkWidget *window; GtkWidget *button; window = gtk_application_window_new (app); gtk_window_set_title (GTK_WINDOW (window), "Hello"); gtk_window_set_default_size (GTK_WINDOW (window), 200, 200); button = gtk_button_new_with_label ("Hello World"); g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL); gtk_window_set_child (GTK_WINDOW (window), button); gtk_window_present (GTK_WINDOW (window)); } int main (int argc, char **argv) { GtkApplication *app; int status; app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS); g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); status = g_application_run (G_APPLICATION (app), argc, argv); g_object_unref (app); return status; } - Hints: ...

August 21, 2025 · 2 min · Phong Nguyen