C++ cool cheetsheet produced by ChatGPT to refresh your memory

chatGPT is really helpful tool to programmers. these cheetsheet were produced by chatGPT 3.5 to help refresh your memory. if you are advanced programmer or have background in other languages. it can help to look at simple one liners instead of going through long pages of documentations

Keep in mind, chatGPT sometimes produces inaccurate information and sometimes it hallucinates:

  • Declaration of variables: int a = 10;
  • Conditional statements: if (a == 10) { ... } else { ... }
  • Loops: for (int i = 0; i < 10; i++) { ... } or while (condition) { ... }
  • Functions: void myFunction(int arg1, double arg2) { ... }
  • Classes and objects: class MyClass { ... }; MyClass myObject;
  • Inheritance: class ChildClass : public ParentClass { ... }
  • Polymorphism: virtual void myFunction() { ... } and override keyword in child classes
  • Templates: template void myFunction(T arg) { ... }
  • Standard libraries: #include for input/output, #include for dynamic arrays, #include for string manipulation, and many others.
  • Pointers and references: int *ptr = &a; and int &ref = a;
  • Memory management: new and delete for dynamic memory allocation and deallocation
  • Exception handling: try { ... } catch (exceptionType e) { ... }
  • Namespaces: namespace myNamespace { ... }
  • Macros: #define MY_MACRO(value) (value * 2)
  • Preprocessor directives: #include for header files, #ifdef and #ifndef for conditional compilation
  • Operator overloading: operator+, operator-, etc. can be overloaded for custom classes
  • Type casting: static_cast, dynamic_cast, reinterpret_cast, and const_cast
  • Standard Template Library (STL): provides containers like vector, map, set, and algorithms like sort, find, and accumulate
  • File input/output: fstream for reading/writing to files
  • String streams: stringstream for parsing and formatting strings
  • Multi-threading: std::thread for creating threads, std::mutex for thread synchronization
  • Function objects: std::function for storing and invoking callable objects
  • Lambda expressions: [] (int a, int b) { return a + b; } for inline functions or function objects
  • Range-based for loop: for (auto x : myVector) { ... } for iterating over a range of values
  • Smart pointers: std::unique_ptr and std::shared_ptr for automatic memory management
  • Regular expressions: std::regex for pattern matching and search in strings
  • Type traits: std::is_integral, std::is_floating_point, and others for compile-time type analysis
  • Move semantics: std::move for efficient transfer of ownership of objects
  • Variadic templates: template void myFunction(Args... args) { ... } for functions that can take any number of arguments.
  • Type deduction: auto for automatic type deduction of variables and lambda expressions
  • Rvalue references: int &&rref = 42; for binding to temporaries and move semantics
  • User-defined literals: 42_milliseconds or 3.14_pi for custom literals of user-defined types
  • Initialization lists: {1, 2, 3} for initializing objects and containers
  • Uniform initialization: MyClass obj{1, "hello"}; for consistent initialization syntax
  • constexpr functions: constexpr int factorial(int n) { ... } for compile-time evaluation of functions
  • Type aliases: using myInt = int; for creating aliases for types or templates
  • Concepts: template concept MyConcept = ... for expressing requirements on template arguments
  • Modules: import myModule; for modularization of code and faster compilation
  • Co-routines: co_await and co_yield for implementing asynchronous code and generators.
  • esign patterns: C++ supports many design patterns such as the Singleton pattern, the Observer pattern, and the Factory pattern.
  • Multiple inheritance: C++ allows classes to inherit from multiple base classes, providing flexibility in class hierarchy design.
  • C++11 features: C++11 introduced new language features such as lambdas, range-based for loops, and smart pointers to improve expressiveness and safety of C++ code.
  • C++14 features: C++14 added new features such as binary literals, generic lambdas, and extended constexpr to further improve the language.
  • C++17 features: C++17 added new features such as structured bindings, constexpr if, and fold expressions to provide more powerful and expressive templates.
  • C++20 features: C++20 introduced new features such as concepts, coroutines, and modules to further improve the language.
  • Third-party libraries: C++ has a vast ecosystem of third-party libraries such as Boost, Eigen, and OpenCV that provide additional functionality for C++ developers.
  • IDEs: Integrated development environments (IDEs) such as Visual Studio, CLion, and Code::Blocks provide powerful tools for editing, debugging, and testing C++ code.
  • Here are some briefs about common C++ headers, Qt headers, controls, and external libraries:C++ headers:
    • : Input/output library for C++.
    • : STL container for dynamic arrays.
    • : STL container for strings.
    • : Input/output library for files.
    • : STL algorithms for containers.

    Qt headers:

    • : Main application class for Qt Widgets.
    • : Main application class for Qt Quick.
    • : Base class for all Qt Widgets.
    • : Base class for all Qt Quick items.
    • : Base class for all Qt Widgets layouts.
    • : Class for creating QML objects dynamically.

    Common Qt controls:

    • QLabel: Control for displaying text and images.
    • QLineEdit: Control for editing text.
    • QPushButton: Control for triggering actions.
    • QCheckBox: Control for toggling on/off states.
    • QRadioButton: Control for selecting a single option from a set.
    • QComboBox: Control for selecting one option from a drop-down list.
    • QSpinBox: Control for selecting a numeric value from a range.
    • QSlider: Control for selecting a numeric value from a range with a slider.
    • QTableView: Control for displaying data in a table format.

    External libraries:

    • Boost: Collection of libraries for C++, including support for smart pointers, regular expressions, and numerical algorithms.
    • OpenCV: Library for computer vision and image processing.
    • Eigen: Library for linear algebra.
    • Qt Charts: Library for creating charts and graphs in Qt applications.
    • Qt WebEngine: Library for embedding web content into Qt applications.
    • Qt Network: Library for network programming in Qt applications.
    • Qt Multimedia: Library for audio and video playback and recording in Qt applications.

Keep pebble time watchface backlight on for longer than 3 seconds

The pebble time watchface dims pretty fast and it only lasts 3 seconds, unfortunately there is no option right to increase that. in watchface it is possible to change this behavior programatically here is how i did it.


static int seconds; static void set_backlight(){ light_enable_interaction(); if (seconds<=4)return; app_timer_register(3000,set_backlight, NULL); } static void accel_tap_handler(AccelAxisType axis,int32_t direction) { light_enable_interaction(); seconds = 10; app_timer_register(3000,set_backlight, NULL); } static void main_window_load(Window *window) { ... accel_tap_service_subscribe(&accel_tap_handler); ... } static void tick_handler(struct tm *tick_time, TimeUnits units_changed) { if (seconds>0)seconds--; ... } static void init() { ... tick_timer_service_subscribe(SECOND_UNIT, tick_handler); ... }

How to binary serialize an object such as Font and store it in Windows Registry then deserialize it in C#

Smart Notepad App (At least smarter than some unnamed notepads)Say you want to make a notepad like application, and you want to let the user choose the default font for the text and that should be remembered each time the user open the app. The solution is instead of saving each font property, you save the whole class object. But you need to serialize the class using Binary in order to be able to store it in Windows Registry.

Below is the class that create registry keys, and can save and serialize a font object, and deserialize it back again using IFormatter.Serialize, IFormatter.Deserialize and MemoryStream.

Main:

For C# (C-Sharp)

The registry class


using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; using System.Drawing; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; class winreg { private RegistryKey prgReg; private RegistryKey recentReg; public winreg() { RegistryKey reg = Registry.CurrentUser.OpenSubKey(“software”, true); RegistryKey regMilky = reg.CreateSubKey(“MyBrand”); prgReg = regMilky.CreateSubKey(“MySoftware”); } public void save_font(Font fnt) { RegistryKey fntReg = prgReg.CreateSubKey(“Font”); IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(); formatter.Serialize(stream,fnt); stream.Flush(); byte[] buffer = new byte[stream.Length]; stream.Seek(0, SeekOrigin.Begin); stream.Read(buffer, 0, (int)stream.Length); stream.Close(); fntReg.SetValue(“FontInfo”, buffer,RegistryValueKind.Binary); } public bool get_font(ref Font fnt) { RegistryKey fntReg = prgReg.CreateSubKey(“Font”); if (fntReg.GetValue(“FontInfo”) != null) { byte[] buffer = (byte[])fntReg.GetValue(“FontInfo”); IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(buffer); fnt = (Font)formatter.Deserialize(stream); stream.Close(); return true; }else return false; } }

Below is how to use the above class.


winreg WinRegClass = new winreg(); Font fnt = MyTextBox.Font; if (WinRegClass.get_font(ref fnt)!=false) { MyTextBox.Font = fnt; }

This is not a fine work. I’m still practicing C#, so, don’t judge (; Yesterday I wanted to play a DOS game using DosBox, but somehow ended up practicing more C#. You see: Games=useful.