Skip to content

Instantly share code, notes, and snippets.

View Adobe-Android's full-sized avatar

David Brown Adobe-Android

View GitHub Profile
@Adobe-Android
Adobe-Android / member_copy_semantics.cpp
Created September 18, 2020 16:36
Demonstrates copy semantics on member variables and C++17 structured binding.
#include <cstdio>
struct Point {
int x, y;
};
Point make_transpose(Point p) {
int tmp = p.x;
p.x = p.y;
p.y = tmp;
@Adobe-Android
Adobe-Android / arrays.cpp
Last active November 17, 2020 00:30
C-style array vs. C++ std::array
#include <iostream>
#include <array>
struct Person {
char name[256];
};
void print_name(Person* person_ptr) {
std::cout << person_ptr->name << '\n';
}
@Adobe-Android
Adobe-Android / arc_hello.m
Last active May 25, 2020 16:03
My Objective-C collection (using GNUstep)
// ARC environment
#import <stdio.h>
int main( int argc, const char *argv[] ) {
@autoreleasepool {
// Code benefitting from a local autorelease pool.
NSLog(@"Hello, World!");
}
}
@Adobe-Android
Adobe-Android / ntp_time.ino
Last active July 10, 2020 14:32
Shows time and date data in the format of day of week, month, day of month, year, hour:minute:second using a Wi-Fi connection and NTP servers
#include <WiFi.h>
#include "time.h"
const char* ssid = "SSID";
const char* password = "password";
const char* ntpServer = "pool.ntp.org";
// Default time from NTP is GMT. Replace with your timezone offset. Expected to be measured in seconds.
// 3600 seconds in 1 hour. CDT is 5 hours behind GMT. 3600 * 5 = 18,000.
@Adobe-Android
Adobe-Android / objects.cpp
Last active July 10, 2020 14:31
User defined objects and how they can be used
#include <iostream>
#include <string>
using string = std::string;
struct Entity {
Entity() { m_Name = "Unknown"; }
Entity(const string& name) { m_Name = name; }
@Adobe-Android
Adobe-Android / inheritance.cpp
Last active September 17, 2020 17:28
A simple demonstration of inheritance
#include <iostream>
struct Superclass {
int x{};
};
struct Subclass : public Superclass {
public:
int y{};
int foo() { return x + y; }
@Adobe-Android
Adobe-Android / member_initializer_list.cpp
Last active September 17, 2020 17:25
A demonstration of the advantages of using member initializer lists
#include <iostream>
#include <string>
using string = std::string;
struct Example {
public:
// Default constructor
Example() { std::cout << "Created Entity!\n"; };
@Adobe-Android
Adobe-Android / calc.cpp
Last active December 14, 2022 17:26
A calculator exercise in C++ using enum classes/scoped enums, structs, and member functions
#include <iostream>
enum class Operation { Add, Subtract, Multiply, Divide };
struct Calculator {
Operation op;
Calculator(Operation operation) { op = operation; }
int calculate(int a, int b) {
switch (op) {
@Adobe-Android
Adobe-Android / cpp_compile.ps1
Last active September 29, 2019 17:44
PowerShell script collection (built with Windows 10 and Linux (pengwin - Debian-based) in mind on PowerShell Core 7.0.0-preview.4
#!/usr/bin/pwsh-preview
# Setting default values for some variables
$configVersion = ""
$preferredCompiler = ""
$cppStandard = ""
$additionalFlags = ""
$currentDir = (Get-Item -Path ".\").FullName
$configPath = "$currentDir/cpp_config.xml"
$PSVersion = "PowerShell " + $PSVersionTable.PSEdition + " version: " + $PSVersionTable.PSVersion
@Adobe-Android
Adobe-Android / int_fast.cpp
Last active September 17, 2020 17:22
A comparison of int_fast and int_least at various guaranteed sizes (will vary based on system architecture and whether the program is 32 or 64-bit)
#include <iostream>
int main() {
// Optimizes for speed over memory use.
// On modern machines, int_fast16_t will often instead be 32-bit, consuming 4 bytes like
// int_fast32_t. This is actually better for performance.
std::cout << sizeof(int_fast16_t) << '\n';
std::cout << sizeof(int_fast32_t) << '\n';
std::cout << sizeof(int_fast64_t) << '\n';