Skip to content

Instantly share code, notes, and snippets.

@iPAS
Created July 10, 2022 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iPAS/9bc1baa3760712899422540e1c9a996a to your computer and use it in GitHub Desktop.
Save iPAS/9bc1baa3760712899422540e1c9a996a to your computer and use it in GitHub Desktop.
Some code of my learning on C++
#include <iostream>
using namespace std;
// Implicit type casting
float test_function(void) {
uint16_t a = 30;
float b = 10.4;
return a + b;
}
// Function overloading
int sum(int a, int b) {
return a + b;
}
float sum(float a, float b) {
return a + b;
}
char sum(char a, char b) {
return a + b - '0';
}
// Default argument
float thrust_budget(uint8_t rotor_number = 4,
float max_thrust_per_rotor = 15.3, // kg
float rotor_weight = 1.2, // kg
float drone_weight = 13, // kg
float payload_weight = 12 // kg
) {
return (rotor_number * max_thrust_per_rotor) - (
rotor_number * rotor_weight) - drone_weight - payload_weight;
}
// Check float equality
#define EPSILON (1.0E-8)
bool is_equal(float a, float b) {
//return (a == b)? true : false;
float diff = a - b;
return (diff < EPSILON) && (diff > -EPSILON);
}
// Pass by reference
void pass_by_value(int arg) {
arg++;
}
void pass_by_reference(int &arg) {
arg++;
}
void pass_by_pointer(int *arg) {
(*arg)++;
}
class Takeme {
private:
int i;
public:
Takeme () {
this->i = 0;
};
Takeme & operator ++ () {
this->i++;
return *this;
}
operator int () const {
return this->i;
}
};
// Main
int main() {
cout << test_function() << endl;
cout << sum(1, 2) << endl;
cout << sum(1.1f, 2.2f) << endl;
cout << sum('1', '2') << endl;
cout << thrust_budget() << endl;
cout << thrust_budget(6) << endl;
cout << thrust_budget(8) << endl;
cout << thrust_budget(12) << endl;
cout << (char *[]){"false", "true"}[ is_equal(1.2f, 1.2f) ] << endl;
cout << (char *[]){"false", "true"}[ is_equal(1.2f, 1.1f) ] << endl;
int normal_v = 0;
pass_by_value(normal_v);
cout << normal_v << endl;
pass_by_reference(normal_v);
cout << normal_v << endl;
pass_by_pointer(&normal_v);
cout << normal_v << endl;
Takeme takeme;
cout << ++takeme << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment