A collection of practical C++ examples demonstrating modern language features, Windows GUI programming, and CMake build system usage. All examples are fully documented with Doxygen-compatible comments.
EasyExamples/
├── PolimorficSmart/ # Advanced C++ with polymorphism & smart pointers
├── ModernCPP_Containers/ # STL containers, algorithms, and file I/O
├── ExceptionHandling/ # Custom exceptions and error recovery
├── WindowsHEasiest/ # Minimal Windows GUI example
├── WindowsDialog/ # Dialog boxes with input validation
├── WindowsAndCommctrlH/ # Windows with common controls
├── WindowsDrawing/ # Graphics drawing and mouse interaction
├── Gdiplus_H/ # Windows GUI with GDI+ graphics
├── ResourcesAndGdiplus_H/ # Resources + GDI+ integration
├── ResourcesProgressBar/ # Progress bar control example
├── VsCodeCmakeEasiest/ # Minimal CMake project
├── VsCodeCmakeW2src/ # Multi-file CMake project
└── README.md # This file
Here the examples are ordered from easiest to most advanced:
VsCodeCmakeEasiestVsCodeCmakeW2srcWindowsHEasiestModernCPP_ContainersExceptionHandlingWindowsAndCommctrlHWindowsDialogGdiplus_HResourcesAndGdiplus_HResourcesProgressBarWindowsDrawingPolimorficSmart
Location: PolimorficSmart/
Comprehensive example of modern C++ features through a financial instruments system:
- Polymorphism: Abstract
FinancialInstrumentbase class withStock,Bond, andOptionderived classes - Smart Pointers:
unique_ptrfor exclusive ownership with move semanticsshared_ptrfor shared ownership and reference countingweak_ptrfor non-owning references avoiding circular dependencies
- Design Patterns: Factory pattern for object creation, Observer pattern with weak_ptr
- RAII: Automatic resource management through smart pointers
- Threading (advanced): Thread-safe portfolio operations, market simulation with concurrent workers
- Testing: Simple test framework + Google Test (GTest) integration
Key Files:
financial_instrument.h- Polymorphic class hierarchyportfolio_market.h/cpp- Portfolio and market managementmain.cpp- Demonstrations of all smart pointer typessimple_test.cpp- Basic test frameworkthreaded_main.cpp- Advanced threading examplesthreaded_market.h- Thread-safe concurrent operations
Build & Run:
cd PolimorficSmart
mkdir build && cd build
cmake -S .. -B .
cmake --build .
./Debug/financial_example # Run main example
./Debug/simple_test # Run simple tests
./Debug/threaded_example # Run threading demo
./Debug/financial_tests # Run GTest suite (if GTest installed)Demonstrates:
✓ Inheritance and virtual functions
✓ Smart pointer semantics and ownership models
✓ Factory pattern
✓ Reference counting
✓ Move semantics
✓ Circular reference prevention
✓ Multithreading with smart pointers
✓ Condition variables and producer-consumer pattern
Location: ExceptionHandling/
Comprehensive guide to exception handling with custom exceptions, error recovery, and exception safety guarantees.
Key Features:
- Custom Exception Classes: Inheriting from std::exception
- Try/Catch Blocks: Multiple catch blocks for different exception types
- Exception Propagation: Passing exceptions through function call stacks
- RAII with Exceptions: Exception-safe resource management
- Exception Safety Guarantees: Strong, basic, and no-throw guarantees
- Catch Order: Matching specific exceptions before generic ones
- Transaction Rollback: Undoing operations on failure
- Practical Error Recovery: Real-world error handling patterns
Key Files:
src/main.cpp- Bank account system demonstrating exception handling patternsCMakeLists.txt- C++17 project configuration with exception flags
Demonstrates:
✓ Custom exception class design
✓ Try/catch/throw mechanics
✓ Multiple exception types
✓ Exception propagation and stack unwinding
✓ Exception safety guarantees
✓ RAII with exception cleanup
✓ Transaction safety and rollback
✓ Real-world error scenarios
Build & Run:
cd ExceptionHandling
mkdir build && cd build
cmake -S .. -B .
cmake --build .
./Debug/exception_exampleLocation: ModernCPP_Containers/
Comprehensive guide to STL containers, algorithms, and modern C++ features using practical examples.
Key Features:
- Vector Operations: Sorting, filtering, and aggregation
- Map Containers: Efficient key-value lookups
- Set Operations: Unique value management
- String Manipulation: Trimming, splitting, parsing
- File I/O: Reading and writing text files
- Lambda Expressions: Functional programming with STL
- Range-based For Loops: Clean iteration syntax
- Structured Bindings: C++17 feature demonstration
- Algorithms: std::sort, std::find, std::transform, std::accumulate
Key Files:
main.cpp- Demonstrations of all STL featuresCMakeLists.txt- C++17 project configuration
Demonstrates:
✓ STL container types (vector, map, set)
✓ Algorithms and functional operations
✓ Lambda expressions and captures
✓ File I/O with formatted output
✓ String manipulation and parsing
✓ C++17 structured bindings
✓ Iterator patterns
Build & Run:
cd ModernCPP_Containers
mkdir build && cd build
cmake -S .. -B .
cmake --build .
./Debug/containers_exampleLocation: WindowsHEasiest/
The simplest possible Windows GUI application using native Win32 API.
Key Files:
CMakeLists.txt- Basic Windows project configurationsrc/main.cpp- Simple MessageBox example
Demonstrates:
✓ Windows.h header usage
✓ MessageBox API
✓ WinMain entry point
✓ Basic CMake for Windows
Build & Run:
cd WindowsHEasiest
mkdir build && cd build
cmake -S .. -B .
cmake --build .
./Debug/MyWindowsProject.exeLocation: WindowsDialog/
Demonstrates dialog-like windows with input controls, validation, and event handling.
Key Features:
- Edit Controls: Text input fields
- Button Controls: Click event handling
- Static Text: Display messages
- Input Validation: Age and name validation logic
- Message Boxes: User feedback dialogs
- Window Message Handling: WM_COMMAND, WM_CREATE, WM_DESTROY
- Helper Functions: Extracting and setting control text
Key Files:
src/main.cpp- Dialog window with controlsCMakeLists.txt- Windows subsystem configurationsrc/resource.rc- Resource file template
Demonstrates:
✓ Creating child window controls
✓ Button click event handling
✓ Edit control text retrieval
✓ Input validation patterns
✓ Message boxes for user feedback
✓ Control management
✓ Window procedure message routing
Build & Run:
cd WindowsDialog
mkdir build && cd build
cmake -S .. -B .
cmake --build .
./Debug/dialog_example.exeLocation: WindowsAndCommctrlH/
Demonstrates using Windows Common Controls library for standard UI components.
Key Files:
CMakeLists.txt- Link against comctl32src/main.cpp- InitCommonControlsEx initialization
Demonstrates:
✓ Common Controls initialization
✓ ICC_WIN95_CLASSES flag
✓ Standard Windows controls setup
✓ Proper linking configuration
Build & Run:
cd WindowsAndCommctrlH
mkdir build && cd build
cmake -S .. -B .
cmake --build .
./Debug/MyWindowsProject.exeLocation: WindowsDrawing/
Interactive drawing application with GDI+ graphics, shape manipulation, and animation.
Key Features:
- GDI+ Graphics: Drawing circles, ellipses, and lines
- Mouse Interaction: Click-to-select and drag-to-move shapes
- Animation: Timer-driven shape rotation
- Color Management: Multiple colored shapes
- Double-Buffering: Smooth rendering without flicker
- Shape Selection: Visual feedback with highlighting
- Real-time Rendering: WM_PAINT message handling
Key Files:
src/main.cpp- Drawing window with graphics and input handlingCMakeLists.txt- GDI+ linking configuration
Demonstrates:
✓ GDI+ graphics initialization and cleanup
✓ Drawing shapes (circles, lines)
✓ Color and pen styling
✓ Mouse event handling (down, move, up)
✓ Hit detection for shape selection
✓ Timer-based animation
✓ Window class with member function callback
✓ Double-buffering rendering pattern
Build & Run:
cd WindowsDrawing
mkdir build && cd build
cmake -S .. -B .
cmake --build .
./Debug/drawing_example.exeLocation: Gdiplus_H/
Graphics programming with GDI+ (Graphics Device Interface Plus) library on Windows.
Key Files:
CMakeLists.txt- Link against gdi32 and gdiplussrc/main.cpp- GdiplusStartup/Shutdown lifecycle
Demonstrates:
✓ GDI+ initialization and cleanup
✓ Gdiplus namespace usage
✓ Graphics library linking
✓ Resource management with graphics APIs
Build & Run:
cd Gdiplus_H
mkdir build && cd build
cmake -S .. -B .
cmake --build .
./Debug/MyexeWindowsProject.exeLocation: ResourcesAndGdiplus_H/
Integration of Windows resource files (.rc) with GDI+ graphics library.
Key Files:
CMakeLists.txt- Resource file compilation configurationresource.h- Resource ID definitionsresource.rc- Resource file with icon definitionsrc/main.cpp- Loading and using resourcesicon.ico- Application icon resource
Demonstrates:
✓ Resource file compilation
✓ Resource ID management
✓ Icon loading with LoadIcon
✓ Resource + graphics integration
✓ hInstance usage for resource loading
Build & Run:
cd ResourcesAndGdiplus_H
mkdir build && cd build
cmake -S .. -B .
cmake --build .
./Debug/MyWindowsProject.exeLocation: ResourcesProgressBar/
Demonstrates a progress bar Windows control with timer-driven updates.
Key Files:
CMakeLists.txt- Common controls and resource configurationresource.h- Progress bar and timer IDsresource.rc- Icon resource definitionsrc/main.cpp- Window procedure, progress bar creation, timer handlingicon.ico- Application icon
Demonstrates:
✓ Progress bar control creation
✓ Window message handling (WM_CREATE, WM_TIMER, WM_DESTROY)
✓ SetTimer and KillTimer usage
✓ Control message sending (PBM_SETPOS, PBM_SETRANGE)
✓ Timer-driven UI updates
✓ Resource loading and usage
Build & Run:
cd ResourcesProgressBar
mkdir build && cd build
cmake -S .. -B .
cmake --build .
./Debug/MyWindowsProject.exeLocation: VsCodeCmakeEasiest/
Minimal CMake project with a single source file - the simplest possible C++ project.
Key Files:
CMakeLists.txt- Minimal configurationmain.cpp- Hello world program
Demonstrates:
✓ Basic CMakeLists.txt structure
✓ project(), add_executable()
✓ CMAKE_CXX_STANDARD setting
✓ Minimal project organization
Build & Run:
cd VsCodeCmakeEasiest
mkdir build && cd build
cmake -S .. -B .
cmake --build .
./Debug/MyProject.exeLocation: VsCodeCmakeW2src/
Multi-file CMake project demonstrating modular code organization across multiple source files.
Key Files:
CMakeLists.txt- Multiple source file configurationsrc/main.cpp- Main entry pointsrc/helper.h- Helper function declarationsrc/helper.cpp- Helper function implementation
Demonstrates:
✓ Multi-file project structure
✓ Header/implementation separation
✓ Linking multiple source files
✓ Include directory management
✓ Translation unit organization
Build & Run:
cd VsCodeCmakeW2src
mkdir build && cd build
cmake -S .. -B .
cmake --build .
./Debug/MyProject.exe- C++ Compiler: MSVC 2019+, GCC, or Clang with C++11+ support
- CMake: Version 3.10 or higher
- Windows SDK (for Windows examples)
- Google Test (optional, for GTest integration in PolimorficSmart)
# Build all examples
for /d %i in (*) do (
if exist "%i\CMakeLists.txt" (
cd "%i"
mkdir build 2>nul
cd build
cmake -S .. -B .
cmake --build .
cd ../..
)
)Visual Studio (Windows):
cmake -S . -B build -G "Visual Studio 16 2019"
cmake --build buildMinGW (Windows):
cmake -S . -B build -G "MinGW Makefiles"
cmake --build buildUnix/Linux:
cmake -S . -B build
cmake --build buildAll examples include comprehensive Doxygen documentation:
- File-level docs:
\fileand\brieftags describe purpose and scope - Class documentation: Design patterns, usage notes, and inheritance structure
- Function documentation: Parameters, return values, and side effects
- Code examples: Inline
\codeblocks demonstrate usage patterns
cd [example-directory]
doxygen Doxyfile # If Doxyfile exists
# OR generate docs for all files
doxygen -g Doxyfile
doxygen DoxyfileBeginners:
- Start with
VsCodeCmakeEasiest- understand basic project setup - Move to
VsCodeCmakeW2src- learn modular organization - Try
WindowsHEasiest- first Windows GUI program - Study
ModernCPP_Containers- STL basics and practical usage
Intermediate:
- Learn
ExceptionHandling- error handling and recovery patterns - Explore
WindowsDialog- interactive controls and validation - Study
WindowsAndCommctrlH- Windows common controls - Learn
Gdiplus_H- graphics programming basics - Try
WindowsDrawing- interactive graphics with mouse handling
Advanced:
- Deep dive into
PolimorficSmart/main.cpp- smart pointer patterns - Study
PolimorficSmart/simple_test.cpp- testing approach - Explore
PolimorficSmart/threaded_main.cpp- concurrency patterns - Combine multiple concepts in custom projects
| Concept | Example |
|---|---|
| Polymorphism | PolimorficSmart |
| Smart Pointers | PolimorficSmart |
| Factory Pattern | PolimorficSmart |
| STL Containers | ModernCPP_Containers |
| Algorithms & Lambdas | ModernCPP_Containers |
| File I/O | ModernCPP_Containers |
| String Manipulation | ModernCPP_Containers |
| Exception Handling | ExceptionHandling |
| Custom Exceptions | ExceptionHandling |
| Error Recovery | ExceptionHandling |
| Exception Safety | ExceptionHandling |
| CMake Basics | VsCodeCmakeEasiest |
| Multi-file Projects | VsCodeCmakeW2src |
| Windows GUI | WindowsHEasiest |
| Dialog Controls | WindowsDialog |
| Input Validation | WindowsDialog |
| Common Controls | WindowsAndCommctrlH |
| Graphics (GDI+) | WindowsDrawing, Gdiplus_H |
| Mouse Input | WindowsDrawing |
| Animation | WindowsDrawing |
| Resources | ResourcesAndGdiplus_H |
| Progress Bar | ResourcesProgressBar |
| Threading | PolimorficSmart (threaded_main.cpp) |
| Unit Testing | PolimorficSmart (simple_test.cpp, test_financial_instruments.cpp) |
- Ensure CMake is installed and in PATH:
cmake --version - Try specifying a generator explicitly:
-G "Visual Studio 16 2019" - Delete
build/directory and reconfigure
- Check C++ standard: Projects use C++11 or C++17
- Verify Windows SDK is installed (for Windows examples)
- Check for missing dependencies (e.g., GTest for test_financial_instruments.cpp)
- Ensure all required libraries are linked in CMakeLists.txt
- Windows examples need: user32, gdi32, gdiplus, comctl32
- Install Google Test:
vcpkg install gtestor use package manager - Optional - PolimorficSmart builds without GTest (skips test_financial_instruments.cpp)
- Target Audience: C++ learners, intermediate developers, educational use
- Language: C++11/17
- Platforms: Windows (primary), Unix/Linux compatible (CMake portions)
- Build System: CMake 3.10+
- License: Educational use
Improvements and additional examples are welcome! When contributing:
- Add comprehensive Doxygen documentation
- Update this README with new examples
- Include working CMakeLists.txt
- Test on both MSVC and GCC/Clang
- Follow existing code style