diff --git a/sheets/_c++/func b/sheets/_c++/func new file mode 100644 index 0000000..e75b27b --- /dev/null +++ b/sheets/_c++/func @@ -0,0 +1,29 @@ +// ### Functions +// a simple function +void functionName() {} + +// function with parameters of integers +void functionName(int param1, int param2) {} + +// return type declaration +int functionName() { + return 420; +} + +// Return multiple values at once +// (C++ uses pairs [ 2 values ] and tuple [ more than 2 values ]) +// Please refer pairs and tuple respectively for more information +std::tuple functionName() { + return std::make_tuple( double1, double2 ); +} + +pair result = functionName(); +std::cout << result.first << std::endl; +std::cout << result.second << std::endl; + +// Pass by reference (the caller and the callee use the same variable for the parameter) +int functionName(int &referenceName) {} + +// Pass by value (the caller and callee have two independent variables with the same value) +int functionName(int valueName) {} + diff --git a/sheets/_c++/hello b/sheets/_c++/hello new file mode 100644 index 0000000..0c7e255 --- /dev/null +++ b/sheets/_c++/hello @@ -0,0 +1,13 @@ +// To install: (Mac OSX) type g++ in terminal and click enter to download the XCode Developer Tools + (Debian/Ubuntu) apt-get install g++ + (Fedora/CentOS) dnf install gcc-c++ + +// To Compile: g++ my_script.cpp +// To Execute: ./a.out + +// To Compile w/ specific executable name: g++ my_script.cpp -o my.executable +// To Execute: ./my.executable +int main() { + std::cout << "Hello" << endl; + return 0; +} diff --git a/sheets/_c++/loops b/sheets/_c++/loops new file mode 100644 index 0000000..ab7c809 --- /dev/null +++ b/sheets/_c++/loops @@ -0,0 +1,19 @@ +// There are only 'for', 'while', and 'do while' + +// For loop - Outputs "0 1 2 3 4 5 6 7 8 9" +for( int i=0; i<10; i++ ) { + std::cout << i << " "; +} + +// While loop - Outputs "0 1 2 3 4 5 6 7 8 9" +int i = 0; +while( i < 10 ) { + std::cout << i << " "; + i++; +} + +// While loop - Outputs "0 " +int i = 0; +do { + std::cout << i << " "; +}while( i<0 ); diff --git a/sheets/_c++/pointers b/sheets/_c++/pointers new file mode 100644 index 0000000..5cea4a7 --- /dev/null +++ b/sheets/_c++/pointers @@ -0,0 +1,8 @@ +int *int_Ptr; // Declare a pointer variable called iPtr pointing to an int (an int pointer) + // It contains an address. That address holds an int value. +double *double_Ptr; // Declare a pointer of type double. + +int a = 5; // Initializes a to the integer value 5 +int *int_Ptr = &a // Set int_Ptr which is an int pointer to the address of a +std::cout << int_Ptr; // Returns the address of a +std::cout << *int_Ptr; // Returns 5 because it dereference the pointer to retrieve the value of a. diff --git a/sheets/_c++/structs b/sheets/_c++/structs new file mode 100644 index 0000000..bc30e8b --- /dev/null +++ b/sheets/_c++/structs @@ -0,0 +1,23 @@ +// structs are like classes, but everything is by default public + +// Declaration +ObjectName structName { + + // Declaration of variables + int a; + string b; + + // Constructor + structName(new_a, new_b) { + a = new_a; + b = new_b; + } + + // Implement any public functions below +}; + +// Accessing PUBLIC variables +ObjectName v = structName(5, "Hello"); // Creates a struct via the constructor +std::cout << v.a << " " << v.b << std::endl; // Prints to console "5 Hello" + +