mirror of
https://github.com/chubin/cheat.sheets
synced 2024-11-19 03:25:44 +00:00
Started addition of C++ cheat
This commit is contained in:
parent
2e7f6cefff
commit
0723e91d05
29
sheets/_c++/func
Normal file
29
sheets/_c++/func
Normal file
@ -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<double, double> functionName() {
|
||||
return std::make_tuple( double1, double2 );
|
||||
}
|
||||
|
||||
pair<double,double> 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) {}
|
||||
|
13
sheets/_c++/hello
Normal file
13
sheets/_c++/hello
Normal file
@ -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;
|
||||
}
|
19
sheets/_c++/loops
Normal file
19
sheets/_c++/loops
Normal file
@ -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 );
|
8
sheets/_c++/pointers
Normal file
8
sheets/_c++/pointers
Normal file
@ -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.
|
23
sheets/_c++/structs
Normal file
23
sheets/_c++/structs
Normal file
@ -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"
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user