Docsity

C/C++ Complete Handwritten Notes: Pointers, OOP, Inheritance, STL & More, Study notes of C programming

Partial preview of the text

Download C/C++ Complete Handwritten Notes: Pointers, OOP, Inheritance, STL & More and more Study notes C programming in PDF only on Docsity!

C/C++ Notes 41. Pointers A pointen is a vantable that stores the memony addness of anothex vaniable. It is denoted by 7 Syntax : [datatype * pointer _ vaniable | Example : int a = 10; tnt * py // pointer declaxation p=&a; // stores addness of a Here, &a is the addwess of vaniable a. ¥*p will give the value stoned at that addness. Pointe with diagram : Pp a Ae) 10 (address) (value) 1000 10 eee 2. ARRAYS Important Opexatos : * & (address of) “* (deneference) * => (used with pointer to structure / object ) Example : #include using namespace std ; int main() { int a= 25; tnt *p = &a; cout << “Value of a:” << a << endl; cout << “Addness of a:” << &a << end| ; cout << “Value at addness pi” << *p << endl; netunn 0 : Hene , pointen P Stores address 1000, which is the address of vaniable a. *p will access the value 10 stoned at that addzess. An array is a collection of elements of the same type stared in contiguous memony locations. Impontant Points : Syntax : [datatype annay name [size]; | Example : ° Tndexing stants from 0. * awt[i] = * (an +i) * Size must be a positive integer int alo] = 1 1,2,3, 4,5} 5 constant. axx[0] alt] anl2] anx[3] anl4] Example : oe | i | 2 3 4 | 5 for (int i= 0; i< 5; i++) Addxess: 1000 4004 1008 1012 1046 (assuming 4 bytes int) CoH cout <¢ anz[i] << : 3. FUNCTIONS A function is a block of code that pexfoums a Specific task. Tt helps in modulanity and reusability of code. Syntax ; netunn_type function — name ( paxameters ) { Ae body of function netunn value; // if netunn type not void i Example : int add(int a, int b) { int sum =a +b; netuun sum ; J 3 ; Types of functions : int main() { . Libraxy functions inte = 15), y= 3; * Usen defined functions int $ = add(x, y)3 * Inline functions cout << “Sum =” << s3 © Recuxsive functions netunn 0; } 4. POINTERS AND ARRAYS RELATION Memory nepnesentation : In C/C++, the name ei an annay ay acts as a pointex to its finst 10 Ma | 2 iy CEA 1000 1004 = 1008-1012 (addnesses) Example : i int ann[4] = {10, 20, 30, 40}; P int =p Sanne) P points to ax[0] ila petAn EN nino el POE LORIE ICCRIILS Now, eanmli] = *(axm+i) ¥(p+i) is same as annx[i] Oris AeA) 4 a * Pointen axithmetic is possible. Example : « Size of ax = total bytes for inte Ont < 4. itt) Size of ann = size of one element cout << ¥(p+i) << “ "5 H! Output: 10 20 30 40 Remembex : Annoy name (ant) is constant pointer. to finst element. 7. PotymMorPHIsM Polymorphism means “many founs ”. It allows one interface to be used for different data types. Types of Polymorphism : * Compile time (Static) - Function Overloading , Operator Overloading © Run time ( Dynamic) — Function Overriding ( using vintual functions) 1) Function Overloading (Compile time) Same function name with different parameters : Example : int add(int a, int b) { return a+b; } int add(int a, int b, int c) { xeturn atb+c; } add(2,3); add(2,3,4); I! calls fist add() /I calls second add() 2) Function Overriding (Run time) When a denived class provides a Specific implementation of a base class function us: ing virtual function : eau class Base { public: virtual void show() { cout << “Base”; } 33 class Dexived : public Base { ublic: void show() override { cout <¢ “Derived”; } 33 Base ¥ ptr; Derived d; pee = bd; pe ->show(); — // Output: Derived 3) Operator Overloading User can redefine the meaning of operators for user-defined types. Example : complex cl, c2, 3; e3 = ef +2; // “+” operator overloaded ee eee ee en eek 8. ConsTRUCToRS AND DEsTRucTORS Constructor : * Special member function with same name as class. * Called automatically when object is created. * No netuan type. Example : es Spb § pul: Sta sell Student (int x) { noll = 1%; } hi s4 (401); 9. Fite Hanpuing (Basic) If constructor H constructor called Used to perform opexations on files. Steps : Open File Read/Write Close File Destnuctor : * Special member function with ~ (tilde) before class name * Called automatically when object is destroyed. © No arguments and no netutn type. Example : class Student { public: ~ Student () { II. destuactor cout << “Object destroyed a 35 // called when object goes out of scope Example : Hinclude usin namespace std ; int main() { ofstneam fout (“data.txt"); // open fon wxiting ‘out << “Hello C++”; fout close(); xetuin 0; M1 close file } 10. Stanparp Temptate Ligrary (STL) STL is a powerfull library in C++ that provides 3) Vectors ( Dynamic Anxay) : generic classes and functions for common data structures and algorithms © Stores elements in contiguous memory. 0 Automatically resizes. 1) Components of STL : * Provides fast access. * Containers —> store data Example : * Algorithms —> i gers pore operagions vector v= {1,2, 3}; * Iterators —> navigate containers v. push_back(4); for(int x :v) cout << x << AO, , 2) Containers : Types : 4) Iterators : * Sequence containers : vecton, list, deque, f dist © act like pointers for containers . orward_lis . ; : * Used to traverse elements. * Associative containers: set, map, multiset * Container adapters: stack, queue, priority _ queue * Types: begin(), end(), +4it, --it 5) Algorithms : 6) Other Important Containers : Common algorithms : Container | Ordered | Duplicates | Access ° sort() — sorts a range * find() — finds an element ese Ne ues oly) A t() ‘ list No Yes O(n) coun Counts occuanences = Yes "i O(log ») * neverse() — reverses a range ans Yes Ri O(log n) Example : vector v = {3, 1, 4}; 7) Advantages of STL: sort(v.begin(), v.end()); * Saves time and code. b a3 ae Leas REY * Highly reusable. * Efficient and well-tested . 8) Example Program : #Hinclude 9) Key Points : #include using namespace std; ee amts. part of C++ standard. int main GP ees v= {5,2, 8}; * Uses templates (generic programming). sort (v. begin(), v.end()); Le for(int xiv) cout << x <¢”; ° Makes code shorter and more efficient. 3 return 0;