C++ Cheat Sheet: Structs, Classes, Pointers, Arrays, and More
Classified in Computers
Written at on English with a size of 6.65 KB.
```c++
//input
int main(int argc, char * argv[]){ // or char ** argv
const char* strs[] = {"Hello", "World!"};
const char str[] = {"Hello"};
const int arr[] = {1,2,3};
std::cout << *strs<< std::endl;
std::cout << str << std::endl;
std::cout << arr << std::endl;
std::cout << *arr << std::endl;
std::cout << *str << std::endl;
std::cout << *(strs + 1)<
}
//output
Hello
Hello
0x77d02fb38050
1
H
World!
void insert(int array[], int size, int item, int num_copies){
for(int * i = array; i < array + size - num_copies; i ++){
*(i + num_copies) = * i;
}
for(int i = 0; i < num_copies; i ++){
array[i] = item;
}
for (int i = 0;i < size; i ++){
std::cout << array[i];
}
}
//istream example
void printWordPairs(istream &is, ostream &os) {
string word1, word2;
char dest[1024];
while(is >> word1 >> word2){
sharedLetters(word1.c_str(), word2.c_str());
os << dest << endl;
}
}
```
```c++
//filestream example
ifstream fin;
fin.open("filename");
fin.open(argv[n]);
```
Structs
- Use an Init function for the constructor.
- !!!You can't just call struct member directly STRUCTNAME -> MEMBER
Classes
- You can not call the instructor of a pure virtual class in main.
- You can not have a pure virtual object
- constructors order from parents to child
- destructors order from child to parents
- if a class has no implementation declared, then it is a pure virtual class, therefore can not call its constructor directly.
Pointers and Arrays
- When a function takes a pointer, its pass by value.
Useful Code
```c++
//it is a sorted array
int removeDuplicates(int arr[], int n){
int unique_count = 1; //at least 1
int *ptr = arr;
int *ptr_next = arr+1;
while (ptr_next < (arr + n)){
if (*ptr < *ptr_next){
++unique_count;
++ptr;
*ptr = *ptr_next;
++ptr_next;
}else{
++ptr_next;
}
}
return unique_count;
}
void flip(int arr[], int n){
// TASK 2 - YOUR CODE HERE
int *ptr_front = arr; //assign the first element to the first pointer
int *ptr_back = arr + n -1; //assign the last element to the back pointer
int swp = 0;
if (n % 2 != 0){
while ((ptr_front != ptr_back)){
swp = *ptr_front;
*ptr_front = *ptr_back;
*ptr_back = swp;
++ptr_front;
--ptr_back;
}
}else{
while (ptr_front < arr + n/2 -1){
swp = *ptr_front;
*ptr_front = *ptr_back;
*ptr_back = swp;
++ptr_front;
--ptr_back;
}
}
}
// REQUIRES: there are at least n elements in arr
// MODIFIES: arr
// EFFECTS: all elements are "shifted" right by one unit
// for example, [0,1,3,3,4]
// would become [4,0,1,3,3]
// NOTE: You must use traversal by pointer.
// You may not use an extra array.
void slideRight(int arr[], int n){
// TASK 2 - YOUR CODE HERE
int *ptr_one = arr + (n-1);
int *ptr_two = arr + (n-2);
int last = *(arr + n-1); //remember the last element of the array
while(arr <= ptr_two){
*ptr_one = *ptr_two;
--ptr_one;
--ptr_two;
}
*arr = last;
}
```
Concepts
- Polymorphism -- Ability to associate many meanings to one function name by means of a special mechanism called virtual functions or late binding.
- Information hiding -- The details of the implementation of the class are hidden from the programmer who uses the class, another term for data abstraction or encapsulation.
- virtual function -- I do not know how this function is implemented, wait until runtime. This is known as late binding or dynamic binding.
- subtype polymorphism , allow a variable of the base type hold an variable of a subtype
- Static type -- The static type of a pointer/reference is the type it is declared with and is known at compile time.
- Dynamic type -- The dynamic type of a pointer/reference is the type of the object it is actually pointing to at run time.
- Static binding -- The compiler determines at compile time which function to call. This is called static binding. (non- virtual)
- dynamic_cast -- dynamic_cast performs an explicit downcast.
- dynamic_binding -- virtual functions
Review
- dynamic - you control the lifetime
- atomic (primitive) - int, double, char
- Compound (heterogeneous) - A compound object made up of member subobjects. The members and their types are defined by a class or struct
- Abstract class - class that contains at least one pure virtual function
- Interface: what some does
- Unit testing -- One piece at a time, test smaller, less complex units
- System testing -- entire system -- many units together
- Abstraction - Separating what code does from how it works.
- C style string is simply an array of characters that uses a null terminator.
- destructors - a derived class constructors calls the base class constructor. A derived class destructor calls the base class destructor.