mirror of
https://github.com/chubin/cheat.sheets
synced 2024-11-05 12:00:16 +00:00
Added more c++ fun
This commit is contained in:
parent
0723e91d05
commit
79b845a668
6
sheets/_c++/arrays
Normal file
6
sheets/_c++/arrays
Normal file
@ -0,0 +1,6 @@
|
||||
// In C++11 you can do the following, but most people use vector instead of arrays
|
||||
a.size(); // Returns the size of array a
|
||||
|
||||
int a[10]; // Declare an int array with length 10.
|
||||
a[0] = 420; // Set the value 420 to index 0 of array a
|
||||
int b = a[0] // Set variable b to 420 by accessing index 0 of array a.
|
44
sheets/_c++/class
Normal file
44
sheets/_c++/class
Normal file
@ -0,0 +1,44 @@
|
||||
// classes are like structs, but everything is by default private.
|
||||
|
||||
// Declaration
|
||||
ObjectName className {
|
||||
// Sets everything below it to be private.
|
||||
private:
|
||||
// Declaration of variables
|
||||
int a;
|
||||
string b;
|
||||
|
||||
// Implement any private / helper functions below
|
||||
|
||||
// Sets everything below it to be public.
|
||||
public:
|
||||
// Constructor
|
||||
structName(new_a, new_b) {
|
||||
a = new_a;
|
||||
b = new_b;
|
||||
}
|
||||
|
||||
// accessors or getters functions
|
||||
int getA() {
|
||||
return a;
|
||||
}
|
||||
|
||||
string getB() {
|
||||
return b;
|
||||
}
|
||||
|
||||
// mutators or setter functions
|
||||
void setA(int new_a) {
|
||||
a = new_a;
|
||||
}
|
||||
|
||||
void setB(int new_b) {
|
||||
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"
|
20
sheets/_c++/if
Normal file
20
sheets/_c++/if
Normal file
@ -0,0 +1,20 @@
|
||||
int main() {
|
||||
// Basic
|
||||
if(x > 0) {
|
||||
return x;
|
||||
}
|
||||
else {
|
||||
return -x;
|
||||
}
|
||||
|
||||
// if else
|
||||
if(x > 0) {
|
||||
return x;
|
||||
}
|
||||
else if(x == 0) {
|
||||
return 420;
|
||||
}
|
||||
else {
|
||||
return -x;
|
||||
}
|
||||
}
|
@ -19,5 +19,3 @@ ObjectName structName {
|
||||
// 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"
|
||||
|
||||
|
21
sheets/_c++/types
Normal file
21
sheets/_c++/types
Normal file
@ -0,0 +1,21 @@
|
||||
// ## Built-in Types
|
||||
bool
|
||||
|
||||
char
|
||||
|
||||
string
|
||||
|
||||
// Assume signed, prepend unsigned to make it unsigned
|
||||
short
|
||||
|
||||
int
|
||||
|
||||
long
|
||||
|
||||
// C++ 11
|
||||
long long
|
||||
|
||||
// Floating Points
|
||||
float
|
||||
double
|
||||
long double
|
10
sheets/_c++/vector
Normal file
10
sheets/_c++/vector
Normal file
@ -0,0 +1,10 @@
|
||||
// Vectors are sequence containers representing arrays that can change in size.
|
||||
|
||||
// Library to include
|
||||
#include <vector>
|
||||
|
||||
vector<int> a; // Declare an empty vector, a.
|
||||
a.push_back(1); // Appends/Adds an element whose value is 1 to vector a.
|
||||
std::cout << a.at(0) << std::endl; // Accessing index 0 of vector a.
|
||||
a.size(); // Returns the size of the vector
|
||||
a.at(0) = 420; // Changing the value at index 0 to 420.
|
15
sheets/c++
Normal file
15
sheets/c++
Normal file
@ -0,0 +1,15 @@
|
||||
# C++
|
||||
#
|
||||
# C++ is an object-oriented programming language which provides facilities for low-level memory manipulation.
|
||||
# It is widely used by big tech companies, such as, Amazon, Facebook, Google, and SpaceX
|
||||
#
|
||||
# To Compile: g++ my_script.cpp
|
||||
# To Execute: ./a.out
|
||||
#
|
||||
# See also
|
||||
# C++ language cheat sheet at /c++/
|
||||
# list of pages: /c++/:list
|
||||
# learn C++: /c++/:learn
|
||||
# C++ one-liners: /c++/:1line
|
||||
# C++ weirdness: /c++/weirdness
|
||||
# search in pages: /c++/~keyword
|
Loading…
Reference in New Issue
Block a user