# C++ #### PROG Exercises: https://github.com/Samuuuh/feup-prog/blob/master/Study/1st%20Part/exercises.pdf Resolution (of some): https://github.com/Samuuuh/feup-prog/tree/master/Study/1st%20Part #### AEDA Exercises: https://github.com/Samuuuh/feup-aeda ## First Steps ### Relation to C To start, C library is available in C++, but without using .h and having `c` at start ```c++ #include <cstdio> ``` ### Main Function Duas maneiras de correr o programa com a função main: - Sem passar argumentos - Passagem de argumentos #### Exemplos: ```c++ int main() { return 0; } ``` ```c++ int main(int argc, char* argv []) { return 0; } ``` ### General Functions Passagem de argumentos nas funções pode ser feita de diferentes maneiras ```c++ void doSomething(int a = 1, int b = 1) { // body } int main() { doSomething() // a = 1, b = 1 doSomething(20) // a = 20, b = 1 doSomething(20, 20) // a = 20, b = 20 return 0; } ``` ```c++ void doSomething(int a, int b) { // body } int main() { doSomething() // error doSomething(20) // error doSomething(20, 20) // a = 20, b = 20 return 0; } ``` Importante! Os argumentos especificados devem estar sempre no fim, nunca no começo. A próxima declaração de função dá erro ```c++ void doSomething(int a = 1, int b) { // body } ``` ### Control Structures - if / else - switch's - while - do while - for - break and continue ### Input Read and write from the standard output/input ```c++ #include <iostream> using namespace std; // Standard Library, oh yeaah int main() { int myInt; cout << "Fav number!\n"; cin >> myInt; cout << "My number is " << myInt << "\n"; return 0; } ``` ### Strings ### Stringstreams ### Pointers Everything like in C. Just use `nullptr` instead of null for `null` pointer A pointer is a variable that holds a memory address. This address is the location of a object. If one variable contains the address of another variable, the first variable is said to point to the second. (double pointers, how fun isn't it?) ```c++ int *ip = nullptr; // be careful! int *ptr1, ptr2; // ptr1 is a pointer, ptr2 isn't! ``` The two special operators of pointers are `*`(pointer) and `&`(reference). Imagine we had the following code: ```c++ int x = 10; // Stored in memory address: 0x0400 (hexadecimal) ``` -> & return the memory address of its operand ```c++ int *xPtr; xPtr = &x; // Now xPtr has the reference of the address of x. // xPtr points to the address: 0x0400 ``` -> * returns the value located at the address that follows ```c++ int y; y = *xPtr; // y receives the value stored on the address pointed by xPtr. // xPtr points to address 0x0400. In that address is stored the value 10. ``` Make sure that your pointer variables point to the correct type of data. #### Pointer assignment ```c++ int x; int *p1, *p2; p1 = &x; // p1 points to the address of X p2 = p1; // Now p2 is equal to p1. Then it points to the address of X. ``` #### Pointer arithmetic Be careful when increasing or decrement addresses in pointer. The only support actions are addition and subtraction ```c++ int *p1, *p2; // int type, size = 4 bytes p1 = 2000; p2 = p1+3; // 2000 + (3 * sizeof(int)) = 2012 ``` #### Pointers and arrays #### Initializing pointers #### Multiple Indirection #### Pointers to functions #### References and Pointers ### References Besides pointers, C++ has *references*. Used as `&` ```c++ using namespace std; string foo = "I am foo"; string bar = "I am bar"; string &fooRef = foo; // Create reference fooRef += ". Hi!"; // Modify foo through reference cout << fooRef; // Prints "I am foo. Hi!" fooRef = bar; // Could be used foo = bar. Since foo changes, fooRef changes :) cout << fooRef; // Prints "I am bar" const string &barRef = bar; // Creates a const reference to bar barRef += ". Hi!"; // Error! :) ``` ### Dynamic Memory Allocation - Todo ### Classes ```c++ // Declaration class Dog { // private by default std::string name; // Without std if namespace used std::string speak; public: Dog(); // Default constructor void setSpeak(const std::string speak); virtual ~Dog(); // Destructor }; // Implementation void Dog::dog() { std::cout << "Created"; } void Dog::setSpeak(const std::String speak) { this->speak = speak; } // Called when Dog goes out of scope void Dog::~Dog() { std::cout << "Delete Dog"; } ``` ### Exceptions ```c++ using namespace std; try { throw exception("A problem!"); } catch (const exception &error) { cout << error.what(); } catch (...) { // Catch all // Do anything, i dont care at all } ``` ### Vector - Todo ### Separate compilation and Abstract Data Types (ATDs) ### Templates ### Friend functions ### Virtual Functions ### Inheritance ### Polymorphism ### Associative Containers Some types of associative containers on C++ are: - set - multiset - map - multimap is that map can only store unique key-value pairs while in multimap, no key value pair is unique. ``` #include <map> map<int, int> myMap; myMap.insert(pair<int, int>(1, 40)); myMap.insert(pair<int, int>(2, 70)); myMap.insert(pair<int, int>(3, 20)); ``` #### Multimap ### Linked Lists