├── .gitignore ├── 2_Basics ├── 2_1_HelloWorld.cpp ├── 2_2_DefineVariables.cpp ├── 2_3_Pointers_Reference.cpp ├── 2_4_Arrays_Strings │ ├── 2_4_1_Arrays.cpp │ └── 2_4_2_String.cpp ├── 2_5_Conditions.cpp ├── 2_6_Switch_Case.cpp ├── 2_7_While_Loop.cpp ├── 2_8_For_Loop.cpp └── 2_9_Range_For.cpp ├── 3_Functions ├── 3_1_Define_Function │ ├── 3_1_1_Define_Function.cpp │ ├── 3_1_2_Forward_Declaration.cpp │ ├── 3_1_3_Function_Header.cpp │ └── 3_1_3_Function_Header.h ├── 3_2_Passing_Values │ ├── 3_2_1_Pass_by_Value.cpp │ ├── 3_2_2_Pass_by_Reference.cpp │ └── 3_2_3_Pass_by_Reference_Const.cpp ├── 3_3_Auto_Static_Variables │ ├── 3_3_1_Auto_Variable.cpp │ └── 3_3_2_Static_Variable.cpp ├── 3_4_Return_Function.cpp ├── 3_5_Function_Pointer.cpp ├── 3_6_Overloading_Fucntion_Names.cpp ├── 3_7_Oveloading_Operators.cpp ├── 3_8_Variable_Arguments.cpp └── 3_9_Recursive_Function.cpp ├── 4_Preprocessors ├── 4_1_Macro_Constants.cpp ├── 4_2_Include_File │ ├── main.cpp │ └── preprocessor.h ├── 4_3_Preprocessor_Conditions │ ├── main.cpp │ └── preprocessor.h ├── 4_4_Macro_Expansion.cpp ├── 4_5_Macro_Problems.cpp ├── 4_6_Include_Guard │ ├── in1.h │ ├── in2.h │ └── main.cpp └── 4_6_Line_Continuation.cpp ├── 5_Classes_Objects ├── 5_10_Conversion_Operator.cpp ├── 5_11_New_and_Delete.cpp ├── 5_1_Define_Classes │ ├── 5_1_1_Define_Classes.cpp │ └── 5_1_2_Define_Classes.cpp ├── 5_2_Data_Members.cpp ├── 5_3_Fucntion_Members.cpp ├── 5_4_Constructors_Dstructors.cpp ├── 5_5_Implicit_Explicit.cpp ├── 5_6_Namespaces.cpp ├── 5_7_Using_this.cpp ├── 5_8_Operator_Overload_Member_Function.cpp └── 5_9_Operator_Overload_Non_Member_Function.cpp ├── 6_File_IO ├── 6_1_Reading_File.cpp ├── 6_2_Writing_File.cpp └── file.txt ├── 7_Data_Structures └── 7_1_Structs.cpp ├── LICENSE ├── README.md └── Screenshots ├── vs-new-project.png ├── vs-project-property.png ├── xcode-create-project-1.png ├── xcode-create-project.png └── xcode-workspace.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | 19 | # Compiled Static libraries 20 | *.lai 21 | *.la 22 | *.a 23 | *.lib 24 | 25 | # Executables 26 | *.exe 27 | *.out 28 | *.app 29 | 30 | # Xcode 31 | ## Build generated 32 | build/ 33 | DerivedData/ 34 | 35 | ## Various settings 36 | *.pbxuser 37 | !default.pbxuser 38 | *.mode1v3 39 | !default.mode1v3 40 | *.mode2v3 41 | !default.mode2v3 42 | *.perspectivev3 43 | !default.perspectivev3 44 | xcuserdata/ 45 | 46 | ## Other 47 | *.moved-aside 48 | *.xccheckout 49 | *.xcscmblueprint 50 | 51 | 52 | *.xcworkspace 53 | *.xcodeproj 54 | 55 | CPP-Notes/CPP-Notes/* 56 | 57 | CPP-Notes/CPP-Notes/main.cpp 58 | 59 | .idea/ 60 | /main.cpp 61 | /main.h 62 | /CMakeLists.txt 63 | 64 | cmake-build-debug/ 65 | 66 | *.txt 67 | -------------------------------------------------------------------------------- /2_Basics/2_1_HelloWorld.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // 2_1_HelloWorld.cpp 3 | // CPP 4 | // 5 | // Created by akshay raj gollahalli on 28/01/16. 6 | // Copyright © 2016 akshay raj gollahalli. All rights reserved. 7 | // 8 | 9 | // This is a comment 10 | 11 | /* 12 | This is a block code. 13 | */ 14 | #include //<- Libraries 15 | #include 16 | 17 | using namespace std; //<- scope to identifiers 18 | 19 | int main(int argc, char ** argv) { 20 | puts("hello"); // this statement outputs hello with a new line 21 | printf("hello\n"); // this is similar to puts but doesn't end with new line 22 | cout << "hello\n"; // more complex way to output without new line 23 | return 0; // 0 means success 24 | } 25 | -------------------------------------------------------------------------------- /2_Basics/2_2_DefineVariables.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // 2_2_DefineVariables.cpp 3 | // CPP 4 | // 5 | // Created by akshay raj gollahalli on 2/02/16. 6 | // Copyright © 2016 akshay raj gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | 11 | 12 | int main(int argc, char ** argv) { 13 | int someNumber = 10; 14 | /* 15 | this can also be written as 16 | int someNumber; 17 | someNumber = 10; 18 | */ 19 | const int constantNumber = 10; 20 | // constantNumber = 11; // this cannot be changed once initalized. 21 | printf("some number is %d\n", someNumber); 22 | printf("constant number is %d\n", constantNumber); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /2_Basics/2_3_Pointers_Reference.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // 2_3_Pointers_Reference.cpp 3 | // CPP 4 | // 5 | // Created by akshay raj gollahalli on 2/02/16. 6 | // Copyright © 2016 akshay raj gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | 11 | int main(int argc, char ** argv) { 12 | int a = 10; 13 | int b = 20; 14 | int *pointer = &a; // a pointer referencing to the address of a 15 | int &reference = b; // a reference of b is saved in `reference` 16 | 17 | 18 | printf("value of a is %d\n", a); 19 | printf("the address &a is %p\n", &a);// printing the address of a 20 | printf("value of *pointer is %d\n\n", *pointer); // pointer to the reference of a 21 | 22 | *pointer = 30; // when the pointer value is changed even the int value is changed 23 | 24 | printf("value of a is %d\n", a); 25 | printf("the address &a is %p\n", &a);// printing the address of a 26 | printf("value of *pointer is %d\n\n", *pointer); // pointer to the reference of a 27 | 28 | printf("value of b is %d\n", b); 29 | printf("reference of b is %d\n", reference); // reference of b 30 | printf("%p\n", &b); // Address of b 31 | printf("%p\n", &reference); // Address of b in reference 32 | 33 | return 0; 34 | 35 | } 36 | -------------------------------------------------------------------------------- /2_Basics/2_4_Arrays_Strings/2_4_1_Arrays.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // 2_4_String_Arrays.cpp 3 | // CPP 4 | // 5 | // Created by akshay raj gollahalli on 2/02/16. 6 | // Copyright © 2016 akshay raj gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | 11 | int main(int argc, char ** argv) { 12 | 13 | int a[5]; // Array initalize with 5 memory locations 14 | 15 | a[0] = 1; // Array always starts with address 0 16 | printf("%d\n", a[0]); 17 | 18 | *a = 2; // a[0] can be written as *a, that means its at address 0 19 | printf("%d\n", *a); // its 2 20 | 21 | int *b = a; // We don't have to put &. Still at address 0 22 | printf("%d\n", *b); // its 2 23 | 24 | ++b; // increment address to 1 25 | *b = 3; 26 | printf("%d\n", *b); 27 | 28 | *(++b) = 4; // incerement address to 2 and assign 4 29 | printf("%d\n\n", *b); 30 | 31 | for (int i=0; a[i]; i++) { 32 | printf("a[%d] = %d\n",i,a[i]); 33 | } 34 | 35 | puts(""); 36 | 37 | // in C11 you can assign values directly to the array 38 | int arr[5] = {1,2,3,4,5}; 39 | printf("value at arr[1] is: %d\n", arr[1]); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /2_Basics/2_4_Arrays_Strings/2_4_2_String.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // 2_4_2_String.cpp 3 | // CPP 4 | // 5 | // Created by akshay raj gollahalli on 2/02/16. 6 | // Copyright © 2016 akshay raj gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | 11 | int main (int argc, char ** argv){ 12 | 13 | char a[6] = {'a', 'b', 'c', 'd', 'e', 0}; 14 | 15 | printf("The character at a[1] is = %c\n", a[1]); // print a character 16 | printf("The string is %s\n\n", a); 17 | 18 | // Loop using address number 19 | for (int i = 0; a[i]; i++) { 20 | printf("Character at a[%d] is: %c\n",i,a[i]); 21 | } 22 | 23 | puts(""); 24 | 25 | char b[] = "akshay"; 26 | printf("The character at b[0] is = %c\n", b[0]); 27 | printf("My name is %s\n",b); 28 | 29 | // Loop using pointers 30 | for (char *ptr = b; *ptr; ptr++) { 31 | printf("its %c\n", *ptr); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /2_Basics/2_5_Conditions.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // 2_5_Conditions.cpp 3 | // CPP 4 | // 5 | // Created by akshay raj gollahalli on 2/02/16. 6 | // Copyright © 2016 akshay raj gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | 11 | int main(int argc, char ** argv){ 12 | 13 | int a = 10; 14 | int b = 20; 15 | 16 | // traditional conditional methods 17 | if (a > b) { // checks if a is greater than b 18 | puts(" a > b"); // if the condition is true then this is returned 19 | } else if (a < b){ // if the first condition does not satisfy then it goes here 20 | puts("a < b"); // if the condition is true this is returned 21 | } else{ // if non of the above condition satisfies then it comes here 22 | puts("a == b"); // and this is returned 23 | } 24 | 25 | // Ternary conditon 26 | printf("the correct answer is %d\n", a > b ? a : b); // if a is greater than b, print a else print b 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /2_Basics/2_6_Switch_Case.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // 2_6_Switch_Case.cpp 3 | // CPP 4 | // 5 | // Created by akshay raj gollahalli on 2/02/16. 6 | // Copyright © 2016 akshay raj gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | 11 | int main(int argc, char ** argv) { 12 | 13 | int a[] = {1,2,3,4}; 14 | int b = 0; 15 | 16 | while (b<5) { 17 | printf("Index number %d is %d\n", b, a[b]); 18 | b++; 19 | } 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /2_Basics/2_7_While_Loop.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // CPP-Notes 4 | // 5 | // Created by Akshay Raj Gollahalli on 23/05/16. 6 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | 11 | int main(int argc, char ** argv) { 12 | 13 | int a[]={1,2,3,4}; 14 | int b = 0; 15 | 16 | while (b<4) { 17 | printf("index of array a is %d and its content is %d\n", b, a[b]); 18 | ++b; 19 | } 20 | 21 | int c[]={1,2,3,4}; 22 | int d = 0; 23 | 24 | // Another way of doing While loop is Do.. While.. 25 | do { 26 | printf("index of array c is %d and its content is %d\n", d, c[d]); 27 | ++d; 28 | }while (d<4); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /2_Basics/2_8_For_Loop.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // CPP-Notes 4 | // 5 | // Created by Akshay Raj Gollahalli on 23/05/16. 6 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | 11 | int main(int argc, char ** argv) { 12 | 13 | int a[]={1,2,3,4}; 14 | 15 | for (int b=0; b<4; ++b) { 16 | printf("Index of array a is %d and its content is %d\n", b, a[b]); 17 | } 18 | 19 | // The for loop is just like while loop but with extra functionality 20 | // The for loop can be written as 21 | // 22 | // int b = 0; 23 | // while (b<4) { 24 | // printf("Index of array a is %d and its content is %d\n", b, a[b]); 25 | // ++b; 26 | // } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /2_Basics/2_9_Range_For.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // CPP-Notes 4 | // 5 | // Created by Akshay Raj Gollahalli on 23/05/16. 6 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | int main(int argc, char ** argv) { 14 | 15 | int a[]={1,2,3,4}; 16 | 17 | for (int b=0; b<4; ++b) { 18 | // (Initalise, condition, incrment) 19 | printf("Index of array a is %d and its content is %d\n", b, a[b]); 20 | } 21 | 22 | // For Integer 23 | 24 | int c[]={1,2,3,4}; 25 | 26 | for (int d : c) { 27 | // (Initalise, condition, incrment) 28 | printf("Index of array c is %d and its content is %d\n", d-1, d); 29 | } 30 | 31 | // For char 32 | 33 | char e[]="akshay"; 34 | 35 | for (char f : e) { 36 | // (Initalise, condition, incrment) 37 | // to remove the blank line you can put 38 | // if (f == 0) break; 39 | printf("contents %c\n", f); 40 | // this will print with a blank line as a string always ends with 0 41 | } 42 | 43 | // Using STL strings 44 | string g = "Akshay"; 45 | for (char h : g) { 46 | printf("Contents %c\n", h); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /3_Functions/3_1_Define_Function/3_1_1_Define_Function.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // CPP-Notes 4 | // 5 | // Created by Akshay Raj Gollahalli on 23/05/16. 6 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | // A void function does not return any value, hence void. 14 | void function_name(){ 15 | puts("function_name() is called"); 16 | } 17 | 18 | // C++ first calls main() function 19 | int main(int argc, char ** argv) { 20 | 21 | puts("main function"); // First this is printed out, 22 | function_name(); // Then this is printed out. 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /3_Functions/3_1_Define_Function/3_1_2_Forward_Declaration.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // CPP-Notes 4 | // 5 | // Created by Akshay Raj Gollahalli on 23/05/16. 6 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | void function_name(); 14 | 15 | // C++ first calls main() function 16 | int main(int argc, char ** argv) { 17 | 18 | puts("main function"); // First this is printed out, 19 | function_name(); // Then this is printed out. 20 | return 0; 21 | } 22 | 23 | void function_name(){ 24 | puts("function_name() is called"); 25 | } 26 | -------------------------------------------------------------------------------- /3_Functions/3_1_Define_Function/3_1_3_Function_Header.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // CPP-Notes 4 | // 5 | // Created by Akshay Raj Gollahalli on 23/05/16. 6 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | #include "3_1_3_Function_Header.h" 12 | using namespace std; 13 | 14 | // C++ first calls main() function 15 | int main(int argc, char ** argv) { 16 | 17 | puts("main function"); // First this is printed out, 18 | function_name(); // Then this is printed out. 19 | return 0; 20 | } 21 | 22 | void function_name(){ 23 | puts("function_name() is called"); 24 | } 25 | -------------------------------------------------------------------------------- /3_Functions/3_1_Define_Function/3_1_3_Function_Header.h: -------------------------------------------------------------------------------- 1 | // 2 | // main.h 3 | // CPP-Notes 4 | // 5 | // Created by Akshay Raj Gollahalli on 23/05/16. 6 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 7 | // 8 | 9 | #ifndef 3_1_3_Function_Header_h 10 | #define 3_1_3_Function_Header_h 11 | 12 | void function_name(); 13 | 14 | #endif /* main_h */ 15 | -------------------------------------------------------------------------------- /3_Functions/3_2_Passing_Values/3_2_1_Pass_by_Value.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // CPP-Notes 4 | // 5 | // Created by Akshay Raj Gollahalli on 23/05/16. 6 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | void sum(int a, int b){ 14 | printf("%d\n", a+b); 15 | } 16 | 17 | int main(int argc, char ** argv) { 18 | int a = 10; 19 | int b = 20; 20 | sum(a,b); 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /3_Functions/3_2_Passing_Values/3_2_2_Pass_by_Reference.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // CPP-Notes 4 | // 5 | // Created by Akshay Raj Gollahalli on 23/05/16. 6 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | void sum(int *a, int *b){ 14 | printf("%d\n", *a+*b); // *a and *b pointing to the address given to them. 15 | } 16 | 17 | void mul(int &a, int &b){ 18 | printf("%d\n", a*b); 19 | } 20 | 21 | int main(int argc, char ** argv) { 22 | int a = 10; 23 | int b = 20; 24 | sum(&a,&b); // address of a and b 25 | mul(a,b); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /3_Functions/3_2_Passing_Values/3_2_3_Pass_by_Reference_Const.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // CPP-Notes 4 | // 5 | // Created by Akshay Raj Gollahalli on 23/05/16. 6 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | void sum(const int &a, const int &b){ 14 | printf("%d\n", a+b); 15 | } 16 | 17 | int main(int argc, char ** argv) { 18 | int a = 10; 19 | int b = 20; 20 | sum(a,b); 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /3_Functions/3_3_Auto_Static_Variables/3_3_1_Auto_Variable.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // CPP-Notes 4 | // 5 | // Created by Akshay Raj Gollahalli on 23/05/16. 6 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | void hello(){ 14 | int a = 20; 15 | printf("%d\n", a); 16 | a = 30; 17 | } 18 | 19 | int main(int argc, char ** argv) { 20 | hello(); 21 | hello(); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /3_Functions/3_3_Auto_Static_Variables/3_3_2_Static_Variable.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // CPP-Notes 4 | // 5 | // Created by Akshay Raj Gollahalli on 23/05/16. 6 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | void hello(){ 14 | static int a = 20; 15 | printf("%d\n", a); 16 | a = 30; 17 | } 18 | 19 | int main(int argc, char ** argv) { 20 | hello(); 21 | hello(); // The value of a changes when this function is called 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /3_Functions/3_4_Return_Function.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // CPP-Notes 4 | // 5 | // Created by Akshay Raj Gollahalli on 23/05/16. 6 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | int sum(int a, int b){ 14 | return a+b; 15 | } 16 | 17 | const string & name(){ // & calling the reference of this function, as its big 18 | static string name = "akshay"; 19 | return name; 20 | } 21 | 22 | int main(int argc, char ** argv) { 23 | printf("%d\n", sum(10,20)); 24 | printf("%s\n", name().c_str()); 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /3_Functions/3_5_Function_Pointer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // CPP-Notes 4 | // 5 | // Created by Akshay Raj Gollahalli on 23/05/16. 6 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | void call_me(){ 13 | puts("hello"); 14 | } 15 | 16 | int main(int argc, char ** argv) { 17 | void (*function_pointer)()=call_me; // you can put &call_me, thats optional 18 | function_pointer(); // you can put (*function_poinyer)(), thats optional 19 | 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /3_Functions/3_6_Overloading_Fucntion_Names.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // CPP-Notes 4 | // 5 | // Created by Akshay Raj Gollahalli on 23/05/16. 6 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | void name(){ 14 | puts("my name is akshay"); 15 | } 16 | 17 | string name(string a){ 18 | return a; 19 | } 20 | 21 | int main(int argc, char ** argv) { 22 | name(); 23 | printf("my name is %s\n", name("Raj").c_str()); 24 | 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /3_Functions/3_7_Oveloading_Operators.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // CPP-Notes 4 | // 5 | // Created by Akshay Raj Gollahalli on 23/05/16. 6 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | class A{ 13 | int a; 14 | public: 15 | A(const int &a) : a(a){} 16 | const int & value() const {return a;} 17 | }; 18 | 19 | // The + operator acts as a multiplication operator 20 | int operator + (const A & lhs, const A & rhs){ 21 | puts("operator + for class A"); 22 | return lhs.value() * rhs.value(); 23 | } 24 | 25 | int main(int argc, char ** argv) { 26 | 27 | A a(7); 28 | A b(42); 29 | printf("the value is %d\n", a+b); 30 | 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /3_Functions/3_8_Variable_Arguments.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // CPP-Notes 4 | // 5 | // Created by Akshay Raj Gollahalli on 23/05/16. 6 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | double avg (const int count, ...){ 14 | va_list ap; // It is a macro and is used as a parameter 15 | int i; 16 | double total = 0.0; 17 | 18 | va_start(ap, count); // Initialize a variable argument list 19 | 20 | for (i=1; i 10 | using namespace std; 11 | 12 | 13 | // Factorial using for loop 14 | int factorial(int n){ 15 | int fact = 0; 16 | for (int i = 0; i <= n; i++){ 17 | if (i == 0) 18 | fact = 1; 19 | else 20 | fact = fact * i; 21 | } 22 | return fact; 23 | } 24 | 25 | 26 | // Factorial using recursive function 27 | int recursive_factorial(int n){ 28 | if (n < 2) { 29 | return 1; 30 | } 31 | else{ 32 | return recursive_factorial(n-1) * n; 33 | } 34 | } 35 | 36 | int main(int argc, char ** argv) { 37 | printf("%d\n", recursive_factorial(4)); 38 | printf("%d\n", factorial(4)); 39 | return 0; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /4_Preprocessors/4_1_Macro_Constants.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #include 9 | using namespace std; 10 | 11 | #define NUMBER 12 // Preprocessor macro 12 | #define STRING "Hello!" // Preprocessor macro 13 | 14 | int main(int argc, char ** argv) { 15 | 16 | const int SOME_NUMBER = 21; // C++ constant 17 | printf("SOME_NUMBER = %d and it's address is 0x%p\n", SOME_NUMBER, (void *)&SOME_NUMBER); 18 | 19 | printf("%d\n", NUMBER); 20 | // printf("%d\n", 12); // before the code goes to the compile the macro is replace by literal value. 21 | 22 | printf("%s\n", STRING); 23 | // printf("%s\n", "Hello!"); 24 | 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /4_Preprocessors/4_2_Include_File/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #include 9 | using namespace std; 10 | 11 | #include "preprocessor.h" 12 | 13 | int main(int argc, char ** argv) { 14 | 15 | 16 | printf("SOME_NUMBER = %d and it's address is %p\n", SOME_NUMBER, (void *)&SOME_NUMBER); 17 | 18 | printf("%d\n", NUMBER); 19 | // printf("%d\n", 12); // before the code goes to the compile the macro is replace by literal value. 20 | 21 | printf("%s\n", STRING); 22 | // printf("%s\n", "Hello!"); 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /4_Preprocessors/4_2_Include_File/preprocessor.h: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #ifndef main_h 9 | #define main_h 10 | 11 | #define NUMBER 12 // Preprocessor macro 12 | #define STRING "Hello!" // Preprocessor macro 13 | 14 | const int SOME_NUMBER = 21; // C++ constant 15 | 16 | #endif /* main_h */ 17 | -------------------------------------------------------------------------------- /4_Preprocessors/4_3_Preprocessor_Conditions/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #include 9 | using namespace std; 10 | 11 | #define AGE // Comment this to get 12 12 | 13 | #include "preprocessor.h" 14 | 15 | int main(int argc, char ** argv) { 16 | 17 | printf("Age is %d\n", NUMBER); 18 | 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /4_Preprocessors/4_3_Preprocessor_Conditions/preprocessor.h: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #ifndef main_h 9 | #define main_h 10 | 11 | #ifdef AGE // check to see if `#define AGE` is available in main.cpp 12 | //#if defined(AGE) // Alternate to `#ifdef AGE` 13 | 14 | #define NUMBER 21 // If AGE is defined let the NUMBER be 21 15 | 16 | #else // if not defined 17 | 18 | #define NUMBER 12 // If AGE is not defined let the number be 19 | 20 | #endif // End the if condition 21 | 22 | 23 | #endif /* main_h */ 24 | -------------------------------------------------------------------------------- /4_Preprocessors/4_4_Macro_Expansion.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #include 9 | using namespace std; 10 | 11 | #define ADD(a,b) (a + b) 12 | 13 | 14 | int main(int argc, char ** argv) { 15 | 16 | int aa = 10; 17 | int bb = 20; 18 | 19 | printf("Age is %d\n", ADD(aa, bb)); 20 | // printf("Age is %d\n", (a + b)); // The parameter macro is expanded like this. 21 | 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /4_Preprocessors/4_5_Macro_Problems.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #include 9 | using namespace std; 10 | 11 | #define MAX(a,b) (a > b ? a : b) 12 | 13 | int incerement(){ 14 | static int a = 20; 15 | a+=5; 16 | printf("incerement() = %d\n",a); 17 | return a; 18 | } 19 | 20 | 21 | int main(int argc, char ** argv) { 22 | 23 | int b = 20; 24 | 25 | printf("b = %d, incerement = %d and max = %d\n",b, incerement(), MAX(b, incerement())); 26 | // If you think the actual answer should be - 27 | // b = 20, incerement = 25 and max = 25 28 | // But thats not the case. 29 | 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /4_Preprocessors/4_6_Include_Guard/in1.h: -------------------------------------------------------------------------------- 1 | // 2 | // in1.h 3 | // CPP-Notes 4 | // 5 | // Created by Akshay Raj Gollahalli on 30/05/16. 6 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 7 | // 8 | 9 | #include "in2.h" // We are including in2.h here 10 | 11 | int b = 20; 12 | -------------------------------------------------------------------------------- /4_Preprocessors/4_6_Include_Guard/in2.h: -------------------------------------------------------------------------------- 1 | // 2 | // in2.h 3 | // CPP-Notes 4 | // 5 | // Created by Akshay Raj Gollahalli on 30/05/16. 6 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 7 | // 8 | 9 | #ifndef in2_h // Check if in2.h is not defined, if so 10 | #define in2_h // define it. 11 | 12 | int a = 10; 13 | 14 | #endif /* in2_h */ 15 | -------------------------------------------------------------------------------- /4_Preprocessors/4_6_Include_Guard/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #include 9 | #include "in1.h" 10 | #include "in2.h" 11 | using namespace std; 12 | 13 | 14 | int main(int argc, char ** argv) { 15 | 16 | printf("a = %d, b = %d\n",a,b); 17 | 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /4_Preprocessors/4_6_Line_Continuation.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #include 9 | using namespace std; 10 | 11 | #define LOOPER(i) do \ 12 | { \ 13 | printf("%d\n",i++); \ 14 | } while (i < 3); 15 | 16 | 17 | int main(int argc, char ** argv) { 18 | 19 | int a = 0; 20 | 21 | LOOPER(a); 22 | 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /5_Classes_Objects/5_10_Conversion_Operator.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | class Rational { 13 | int _n = 0; 14 | int _d = 1; 15 | public: 16 | Rational ( int numerator = 0, int denominator = 1 ) : _n(numerator), _d(denominator) {}; 17 | Rational ( const Rational & rhs ) : _n(rhs._n), _d(rhs._d) {}; // copy constructor 18 | ~Rational (); 19 | inline int numerator() const { return _n; }; 20 | inline int denominator() const { return _d; }; 21 | 22 | // Operator overload 23 | Rational & operator = ( const Rational & ); 24 | // Rational operator + ( const Rational & ) const; 25 | Rational operator - ( const Rational & ) const; 26 | Rational operator * ( const Rational & ) const; 27 | Rational operator / ( const Rational & ) const; 28 | operator std::string () const; // += 29 | }; 30 | 31 | Rational & Rational::operator = ( const Rational & rhs ) { 32 | if( this != &rhs ) { 33 | _n = rhs.numerator(); 34 | _d = rhs.denominator(); 35 | } 36 | return *this; 37 | } 38 | 39 | // Non member function 40 | Rational operator + ( const Rational & lhs, const Rational & rhs ) { 41 | return Rational((lhs.numerator() * rhs.denominator()) + (lhs.denominator() * rhs.numerator()), lhs.denominator() * rhs.denominator()); 42 | } 43 | 44 | // operator overload as a member function 45 | Rational Rational::operator - ( const Rational & rhs ) const { 46 | return Rational((_n * rhs._d) - (_d * rhs._n), _d * rhs._d); 47 | } 48 | 49 | Rational Rational::operator * ( const Rational & rhs ) const { 50 | return Rational(_n * rhs._n, _d * rhs._d); 51 | } 52 | 53 | Rational Rational::operator / ( const Rational & rhs ) const { 54 | return Rational(_n * rhs._d, _d * rhs._n); 55 | } 56 | 57 | // += 58 | Rational::operator std::string () const{ 59 | const static int maxstring = 64; 60 | char s[maxstring]; 61 | snprintf(s, maxstring, "%d/%d", _n, _d); 62 | return std::string(s); 63 | } 64 | 65 | Rational::~Rational() { 66 | _n = 0; _d = 1; 67 | } 68 | 69 | // useful for std::cout 70 | std::ostream & operator << (std::ostream & o, const Rational & r) { 71 | return o << r.numerator() << '/' << r.denominator(); 72 | } 73 | 74 | int main( int argc, char ** argv ) { 75 | 76 | Rational a = 7; // 7/1 77 | cout << "a is: " << a << endl; 78 | Rational b(5, 3); // 5/3 79 | cout << "b is: " << b << endl; 80 | Rational c = b; // copy constructor 81 | cout << "c is: " << c << endl; 82 | Rational d; // default constructor 83 | cout << "d is: " << d << endl; 84 | d = c; // assignment operator 85 | cout << "d is: " << d << endl; 86 | Rational & e = d; // reference 87 | d = e; // assignment to self! 88 | cout << "e is: " << e << endl; 89 | 90 | cout << a << " + " << b << " = " << a + b << endl; 91 | cout << a << " - " << b << " = " << a - b << endl; 92 | cout << a << " * " << b << " = " << a * b << endl; 93 | cout << a << " / " << b << " = " << a / b << endl; 94 | 95 | cout << 14 << " + " << b << " = " << 14 + b << endl; 96 | 97 | // Concatenate a string 98 | string s = "this is a string of: "; 99 | s += b; 100 | printf("%s\n",s.c_str()); 101 | // cout << s << endl; // insted of printf this can be used. 102 | return 0; 103 | } 104 | -------------------------------------------------------------------------------- /5_Classes_Objects/5_11_New_and_Delete.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | class Some_class { 14 | int _a = 0; 15 | int _b = 0; 16 | int _c = 0; 17 | public: 18 | Some_class(int i = 0); 19 | ~Some_class(); 20 | int get_a() {return _a;} 21 | int get_b() {return _b;} 22 | int get_c() {return _c;} 23 | }; 24 | 25 | Some_class::Some_class(int i): _a(i), _b(i + 1), _c(i + 2){ 26 | static int bb = 1; 27 | printf("A constructor is called %d\n", bb); 28 | ++bb; 29 | } 30 | 31 | // Destructor 32 | Some_class::~Some_class(){ 33 | static int aa = 1; 34 | printf("dtr called %d \n", aa); 35 | ++aa; 36 | } 37 | 38 | int main( int argc, char ** argv ) { 39 | 40 | Some_class *var1 = new (nothrow) Some_class; // nothrow will not throw any exception if found. 41 | Some_class *var2 = new (nothrow) Some_class[3]; // constructor will be called 3 times due to array size 42 | Some_class *var3 = new (nothrow) Some_class(10); 43 | 44 | // If an nullpts occurs 45 | if (var1 == nullptr || var2 == nullptr || var3 == nullptr) { 46 | puts("new Some_class failed"); 47 | return 1; 48 | } 49 | 50 | // Printing 51 | printf("a = %d, %d, %d\n", var1->get_a(), var1->get_b(), var1->get_c()); 52 | printf("b = %d, %d, %d\n", var2->get_a(), var2->get_b(), var2->get_c()); 53 | printf("b = %d, %d, %d\n", var3->get_a(), var3->get_b(), var3->get_c()); 54 | 55 | // Destructor 56 | delete var1; // Destructor is called 57 | delete[] var2; // Destructor for array is called, 3 times 58 | delete var3; 59 | 60 | puts("Some_class deleted"); // destructor is called before the end end of the file. 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /5_Classes_Objects/5_1_Define_Classes/5_1_1_Define_Classes.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #include 9 | 10 | class Some_class{ 11 | // private: // The Data member is a private memebr of this class 12 | int i; // I cannot access this variable out side the class 13 | public: 14 | int a; // Can be accessed outside the class 15 | void setValue(const int value){ 16 | i = value; 17 | } 18 | int getValue() const{ 19 | return i; 20 | } 21 | }; 22 | 23 | int main(int argc, char ** argv){ 24 | int i = 10; 25 | int a = 20; // This int a is different from obj1.a 26 | Some_class obj1; // Instantiate Some_class to obj1 27 | 28 | obj1.setValue(i); 29 | obj1.a = a; // for public var you dont need a getter and setter implemented 30 | printf("The number is %d\n", obj1.getValue()); // getting the value using getter 31 | printf("The public int a = %d\n", obj1.a); // getting the value using public var 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /5_Classes_Objects/5_1_Define_Classes/5_1_2_Define_Classes.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #include 9 | 10 | class Some_class{ 11 | // private: // The Data member is a private memebr of this class 12 | int i; // I cannot access this variable out side the class 13 | public: 14 | int a; // Can be accessed outside the class 15 | void setValue(const int value); // declare function 16 | int getValue() const; 17 | }; 18 | 19 | // define declared function 20 | void Some_class::setValue(const int value){ 21 | i = value; 22 | } 23 | 24 | int Some_class::getValue() const{ 25 | return i; 26 | } 27 | 28 | int main(int argc, char ** argv){ 29 | int i = 10; 30 | int a = 20; 31 | Some_class obj1; 32 | 33 | obj1.setValue(i); 34 | obj1.a = a; 35 | printf("The number is %d\n", obj1.getValue()); 36 | printf("The public int a = %d\n", obj1.a); 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /5_Classes_Objects/5_2_Data_Members.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #include 9 | 10 | // structure defaults to public data member 11 | struct Some_structure { 12 | //public: // It is same a writing public: 13 | int sa; 14 | int sb; 15 | int sc; 16 | int sd[3]; // You would have to define the array size 17 | }; 18 | 19 | int main(int argc, char ** argv){ 20 | 21 | Some_structure a = { 1, 2, 3, {4, 5, 6}}; 22 | Some_structure *pointer = &a; // To use the structure as a pointer 23 | 24 | printf("sa = %d, sb = %d, sc = %d\n", a.sa, a.sb, a.sc); // to call a structure 25 | printf("sa = %d, sb = %d, sc = %d\n", pointer->sa, pointer->sb, pointer->sc); // -> is pointer selection operator 26 | 27 | // using loop to print out an array 28 | for (int b = 0; b<3; ++b) { 29 | printf("sd[%d] = %d\n", b, a.sd[b]); 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /5_Classes_Objects/5_3_Fucntion_Members.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #include 9 | 10 | class Some_class{ 11 | int i; 12 | public: 13 | void setValue(const int value); 14 | int getValue(); // you can have same function with different signatures 15 | int getValue() const; 16 | }; 17 | 18 | void Some_class::setValue(const int value){ 19 | i = value; 20 | } 21 | 22 | int Some_class::getValue(){ 23 | return i; 24 | } 25 | 26 | int Some_class::getValue() const{ 27 | return i; 28 | } 29 | 30 | int main(int argc, char ** argv){ 31 | int i = 10; 32 | Some_class obj1; 33 | Some_class const obj2 = obj1; 34 | 35 | obj1.setValue(i); 36 | // obj2.setValue(i); // you cannot set a value as it is c++ constant 37 | printf("The number is %d\n", obj1.getValue()); 38 | printf("The number is %d\n", obj2.getValue()); 39 | return 0; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /5_Classes_Objects/5_4_Constructors_Dstructors.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | const string unk = "unknown"; 13 | const string clone_prefix = "clone_"; 14 | 15 | class Animal{ 16 | string _type = ""; // Default private stringd 17 | string _name = ""; 18 | string _sound = ""; 19 | public: 20 | Animal(); // default constructor 21 | Animal(const string & type, const string & name, const string & sound); 22 | 23 | Animal(const Animal &); // copy constructor 24 | Animal & operator = (const Animal &); // copy operator 25 | ~Animal(); // Default destructor 26 | void print() const; // a function called print 27 | 28 | }; 29 | 30 | Animal::Animal() : _type(unk), _name(unk), _sound(unk){ // : initiliser list - this is a default constructor 31 | puts("default constructor"); 32 | } 33 | 34 | // Constructor thats takes in arguments 35 | Animal::Animal(const string & type, const string & name, const string & sound) : _type(type), _name(name), _sound(sound){ 36 | printf("type = %s, name = %s and sound = %s\n", type.c_str(), name.c_str(), sound.c_str()); 37 | } 38 | 39 | Animal::Animal(const Animal & a){ 40 | puts("copy constructor"); 41 | _name = clone_prefix + a._name; 42 | _type = a._type; 43 | _sound = a._sound; 44 | } 45 | 46 | Animal::~Animal(){ // Default destructor 47 | printf("Destructing: %s the %s\n", _name.c_str(), _type.c_str()); 48 | } 49 | 50 | void Animal::print() const{ // Print method 51 | printf("%s the %s says %s\n", _name.c_str(), _type.c_str(), _sound.c_str()); 52 | } 53 | 54 | Animal & Animal::operator=(const Animal & o){ // Operator overload 55 | puts("assignment operator"); 56 | if (this != &o) { 57 | _name = clone_prefix + o._name; 58 | _type = o._type; 59 | _sound = o._sound; 60 | } 61 | return *this; 62 | } 63 | 64 | 65 | int main(int argc, char ** argv){ 66 | Animal a; // Calling a default constructor 67 | a.print(); // Prints it by calling the print function 68 | 69 | const Animal b("da", "di", "du"); 70 | b.print(); 71 | 72 | const Animal c = b; // Copying a constructor with arguments to a default constructor via `=` copy operator 73 | c.print(); 74 | 75 | a = c; // cloning a cloned constructor 76 | a.print(); 77 | return 0; 78 | } // After the program ends the default destructor is called. 79 | 80 | -------------------------------------------------------------------------------- /5_Classes_Objects/5_5_Implicit_Explicit.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #include 9 | using namespace std; 10 | 11 | const size_t _maxsize = 1024; // public constant 12 | 13 | class Some_class { 14 | size_t _size = 0; 15 | public: 16 | explicit Some_class(const int); // constructor: size from int 17 | explicit Some_class(const char *); // constructor: size from c-string 18 | size_t size() const; 19 | }; 20 | 21 | Some_class::Some_class(const int n) { 22 | puts("constructor: size from int"); 23 | _size = (n <= _maxsize) ? n : 0; 24 | } 25 | 26 | Some_class::Some_class(const char * s) { 27 | puts("constructor: size from c-string"); 28 | for(size_t i = 0; i < _maxsize; i++) { 29 | if(s[i] == 0) { 30 | _size = i; 31 | return; 32 | } 33 | } 34 | _size = 0; 35 | } 36 | 37 | size_t Some_class::size() const { 38 | return _size; 39 | } 40 | 41 | using namespace std; 42 | 43 | void func(const Some_class & s) { 44 | printf("size is %ld\n", s.size()); 45 | } 46 | 47 | int main( int argc, char ** argv ) { 48 | Some_class s("Whats up?"); 49 | printf("size is %ld\n", s.size()); 50 | func(Some_class(30)); 51 | return 0; 52 | } -------------------------------------------------------------------------------- /5_Classes_Objects/5_6_Namespaces.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #include 9 | #include 10 | 11 | namespace bwstring { 12 | const std::string prefix = "(bwstring)"; // creating a standard lib string name prefix 13 | 14 | class cust_string{ // class called string 15 | std::string _s = ""; // creating a standard lib string name _s 16 | cust_string(); // private constructor 17 | public: 18 | cust_string(const std::string & g) : _s(prefix + g) {}; 19 | const char * c_str() {return _s.c_str();} 20 | // operator const std::string & () {return _s;}; // if () is used then insted of = you can use () - ### 21 | }; 22 | }; 23 | 24 | std::string s1 = "this is a string"; // creating a standard lib string name s1 25 | //std::string s2("this is a string"); // If ### is uncommented you can use this 26 | 27 | int main(int argc, char ** argv){ 28 | bwstring::cust_string some_string = s1; // cust_string of namespace bwstring is assigned with std lib string s1 29 | // bwstring::cust_string some_string(s2); // If ### is uncommented you can use this 30 | printf("New cust_string is = %s\n", some_string.c_str()); 31 | printf("STD string is = %s\n", s1.c_str()); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /5_Classes_Objects/5_7_Using_this.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #include 9 | 10 | 11 | class Some_class { 12 | int _s = 0; 13 | 14 | public: 15 | void setter(int i) {_s = i;} 16 | int getter(); 17 | }; 18 | 19 | int Some_class::getter() { 20 | printf("%p\n", this); // pointing to this object 21 | return _s; 22 | } 23 | 24 | int main(int argc, char ** argv){ 25 | 26 | int aa = 10; 27 | 28 | Some_class a; 29 | a.setter(aa); 30 | 31 | printf("%d\n", a.getter()); 32 | printf("%p\n", &a); // pointing to `a` object 33 | // The address of `a` should be equal to Some_class 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /5_Classes_Objects/5_8_Operator_Overload_Member_Function.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | class Rational { 13 | int _n = 0; 14 | int _d = 1; 15 | public: 16 | Rational ( int numerator = 0, int denominator = 1 ) : _n(numerator), _d(denominator) {}; 17 | Rational ( const Rational & rhs ) : _n(rhs._n), _d(rhs._d) {}; // copy constructor 18 | ~Rational (); 19 | inline int numerator() const { return _n; }; 20 | inline int denominator() const { return _d; }; 21 | 22 | // Operator overload 23 | Rational & operator = ( const Rational & ); 24 | Rational operator + ( const Rational & ) const; 25 | Rational operator - ( const Rational & ) const; 26 | Rational operator * ( const Rational & ) const; 27 | Rational operator / ( const Rational & ) const; 28 | }; 29 | 30 | Rational & Rational::operator = ( const Rational & rhs ) { 31 | if( this != &rhs ) { 32 | _n = rhs.numerator(); 33 | _d = rhs.denominator(); 34 | } 35 | return *this; 36 | } 37 | 38 | // operator overload as a member function 39 | Rational Rational::operator + ( const Rational & rhs ) const { 40 | return Rational((_n * rhs._d) + (_d * rhs._n), _d * rhs._d); 41 | } 42 | 43 | Rational Rational::operator - ( const Rational & rhs ) const { 44 | return Rational((_n * rhs._d) - (_d * rhs._n), _d * rhs._d); 45 | } 46 | 47 | Rational Rational::operator * ( const Rational & rhs ) const { 48 | return Rational(_n * rhs._n, _d * rhs._d); 49 | } 50 | 51 | Rational Rational::operator / ( const Rational & rhs ) const { 52 | return Rational(_n * rhs._d, _d * rhs._n); 53 | } 54 | 55 | Rational::~Rational() { 56 | _n = 0; _d = 1; 57 | } 58 | 59 | // useful for std::cout 60 | std::ostream & operator << (std::ostream & o, const Rational & r) { 61 | return o << r.numerator() << '/' << r.denominator(); 62 | } 63 | 64 | int main( int argc, char ** argv ) { 65 | 66 | Rational a = 7; // 7/1 67 | cout << "a is: " << a << endl; 68 | Rational b(5, 3); // 5/3 69 | cout << "b is: " << b << endl; 70 | Rational c = b; // copy constructor 71 | cout << "c is: " << c << endl; 72 | Rational d; // default constructor 73 | cout << "d is: " << d << endl; 74 | d = c; // assignment operator 75 | cout << "d is: " << d << endl; 76 | Rational & e = d; // reference 77 | d = e; // assignment to self! 78 | cout << "e is: " << e << endl; 79 | 80 | cout << a << " + " << b << " = " << a + b << "\n"<< endl; 81 | cout << a << " - " << b << " = " << a - b << endl; 82 | cout << a << " * " << b << " = " << a * b << endl; 83 | cout << a << " / " << b << " = " << a / b << endl; 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /5_Classes_Objects/5_9_Operator_Overload_Non_Member_Function.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // CPP-Notes 3 | // 4 | // Created by Akshay Raj Gollahalli on 23/05/16. 5 | // Copyright © 2016 Akshay Raj Gollahalli. All rights reserved. 6 | // 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | class Rational { 13 | int _n = 0; 14 | int _d = 1; 15 | public: 16 | Rational ( int numerator = 0, int denominator = 1 ) : _n(numerator), _d(denominator) {}; 17 | Rational ( const Rational & rhs ) : _n(rhs._n), _d(rhs._d) {}; // copy constructor 18 | ~Rational (); 19 | inline int numerator() const { return _n; }; 20 | inline int denominator() const { return _d; }; 21 | 22 | // Operator overload 23 | Rational & operator = ( const Rational & ); 24 | // Rational operator + ( const Rational & ) const; 25 | Rational operator - ( const Rational & ) const; 26 | Rational operator * ( const Rational & ) const; 27 | Rational operator / ( const Rational & ) const; 28 | }; 29 | 30 | Rational & Rational::operator = ( const Rational & rhs ) { 31 | if( this != &rhs ) { 32 | _n = rhs.numerator(); 33 | _d = rhs.denominator(); 34 | } 35 | return *this; 36 | } 37 | 38 | // Non member function 39 | Rational operator + ( const Rational & lhs, const Rational & rhs ) { 40 | return Rational((lhs.numerator() * rhs.denominator()) + (lhs.denominator() * rhs.numerator()), lhs.denominator() * rhs.denominator()); 41 | } 42 | 43 | // operator overload as a member function 44 | Rational Rational::operator - ( const Rational & rhs ) const { 45 | return Rational((_n * rhs._d) - (_d * rhs._n), _d * rhs._d); 46 | } 47 | 48 | Rational Rational::operator * ( const Rational & rhs ) const { 49 | return Rational(_n * rhs._n, _d * rhs._d); 50 | } 51 | 52 | Rational Rational::operator / ( const Rational & rhs ) const { 53 | return Rational(_n * rhs._d, _d * rhs._n); 54 | } 55 | 56 | Rational::~Rational() { 57 | _n = 0; _d = 1; 58 | } 59 | 60 | // useful for std::cout 61 | std::ostream & operator << (std::ostream & o, const Rational & r) { 62 | return o << r.numerator() << '/' << r.denominator(); 63 | } 64 | 65 | int main( int argc, char ** argv ) { 66 | 67 | Rational a = 7; // 7/1 68 | cout << "a is: " << a << endl; 69 | Rational b(5, 3); // 5/3 70 | cout << "b is: " << b << endl; 71 | Rational c = b; // copy constructor 72 | cout << "c is: " << c << endl; 73 | Rational d; // default constructor 74 | cout << "d is: " << d << endl; 75 | d = c; // assignment operator 76 | cout << "d is: " << d << endl; 77 | Rational & e = d; // reference 78 | d = e; // assignment to self! 79 | cout << "e is: " << e << endl; 80 | 81 | cout << a << " + " << b << " = " << a + b << endl; 82 | cout << a << " - " << b << " = " << a - b << endl; 83 | cout << a << " * " << b << " = " << a * b << endl; 84 | cout << a << " / " << b << " = " << a / b << endl; 85 | 86 | cout << 14 << " + " << b << " = " << 14 + b << endl; 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /6_File_IO/6_1_Reading_File.cpp: -------------------------------------------------------------------------------- 1 | // File IO 2 | 3 | #include 4 | #include // Used to read and write files 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | 10 | ifstream file; // ifstream - input file stream 11 | file.open("file.txt"); // Opening the file 12 | 13 | string data; 14 | 15 | if (!file.fail()) { // Checks if the file exists 16 | cout << "The contents of the file are:"; 17 | while (file.good()) { // Checks if state of stream is good 18 | file >> data; // Write the contents of file to string "data" 19 | cout << " " << data; // Print it to the console 20 | } 21 | } else { 22 | cout << "File not found." << endl; 23 | } 24 | file.close(); // Close the file once read. 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /6_File_IO/6_2_Writing_File.cpp: -------------------------------------------------------------------------------- 1 | // File IO 2 | 3 | #include 4 | #include // Used to read and write files 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | 10 | ofstream file; // ofstream - output file stream 11 | file.open("textFile.txt", 12 | ios::app); // Opening the file and append it (if exists) 13 | 14 | string data = "This is a string"; 15 | 16 | if (!file.fail()) { // Checks if the file exists 17 | file << data << endl; // Write the contents of data to file 18 | } else { 19 | cout << "File not found." << endl; 20 | } 21 | file.close(); // Close the file once read. 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /6_File_IO/file.txt: -------------------------------------------------------------------------------- 1 | This is a test file.This is a string 2 | -------------------------------------------------------------------------------- /7_Data_Structures/7_1_Structs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Creating a s struct 6 | struct students { 7 | string name; 8 | float gpa; 9 | }; 10 | 11 | int main() { 12 | 13 | students student; // Initialise students structure 14 | 15 | student.name = "Akshay"; // Access the name and add string to it 16 | student.gpa = 5.0; // Access the gpa and add float to it. 17 | 18 | cout << student.name << " has " << student.gpa << endl; // Print out its values. 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Akshay Raj Gollahalli 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C++ Notes 2 | C++ Notes 3 | 4 | **Table of Contents** 5 | 6 | - [1 Requirements](#1-requirements) 7 | - [1.1 Mac](#11-mac) 8 | - [1.2 Windows](#12-windows) 9 | - [CMake](#cmake) 10 | - [2 Basics](#2-basics) 11 | - [2.1 Identifiers](#21-identifiers) 12 | - [2.2 Defining Variables](#22-defining-variables) 13 | - [2.3 Pointer and Reference](#23-pointer-and-reference) 14 | - [2.4 Arrays and Strings](#24-arrays-and-strings) 15 | - [2.5 Conditions](#25-conditions) 16 | - [2.6 Switch Case](#26-switch-case) 17 | - [2.7 While Loop](#27-while-loop) 18 | - [2.8 For Loop](#28-for-loop) 19 | - [2.9 Range base `For` loop](#29-range-base-for-loop) 20 | - [2.10 Using stdout](#210-using-stdout) 21 | - [3 Functions](#3-functions) 22 | - [3.1 Defining a function](#31-defining-a-function) 23 | - [3.2 Passing values to a function](#32-passing-values-to-a-function) 24 | - [3.3 Automatic variables vs. Static variables](#33-automatic-variables-vs-static-variables) 25 | - [3.4 Return a function call](#34-return-a-function-call) 26 | - [3.5 Function pointer](#35-function-pointer) 27 | - [3.6 Overloading function names](#36-overloading-function-names) 28 | - [3.7 Overloading operators with function](#37-overloading-operators-with-function) 29 | - [3.8 Variable number of arguments](#38-variable-number-of-arguments) 30 | - [3.9 Recursive function](#39-recursive-function) 31 | - [4 Preprocessors](#4-preprocessors) 32 | - [4.1 Macro constants](#41-macro-constants) 33 | - [4.2 Including a file](#42-including-a-file) 34 | - [4.3 Conditions in preprocessor](#43-conditions-in-preprocessor) 35 | - [4.4 Macro expansion](#44-macro-expansion) 36 | - [4.5 Problems with Macro's](#45-problems-with-macros) 37 | - [4.6 Line continuation](#46-line-continuation) 38 | - [4.6 Include guard](#46-include-guard) 39 | - [5 Classes and Objects in C++](#5-classes-and-objects-in-c) 40 | - [5.1 Defining Classes and Objects](#51-defining-classes-and-objects) 41 | - [5.2 Data members](#52-data-members) 42 | - [5.3 Function members](#53-function-members) 43 | - [5.4 Constructors and Destructors](#54-constructors-and-destructors) 44 | - [5.5 Implicit and Explicit conversion](#55-implicit-and-explicit-conversion) 45 | - [5.6 Namespaces](#56-namespaces) 46 | - [5.7 Using `this`](#57-using-this) 47 | - [5.8 Operator overload: Member function](#58-operator-overload-member-function) 48 | - [5.9 Operator overload: Non-member function](#59-operator-overload-non-member-function) 49 | - [5.10 Conversion operator](#510-conversion-operator) 50 | - [5.11 Using new and delete](#511-using-new-and-delete) 51 | - [5.6 File IO](#56-file-io) 52 | - [5.6.1 Reading Files](#561-reading-files) 53 | - [5.6.2 Writing File](#562-writing-file) 54 | - [6. Data Structures](#6-data-structures) 55 | - [6.1 Structs](#61-structs) 56 | 57 | ## 1 Requirements 58 | 59 | This tutorial is based on C++11. Make sure you are using the latest IDE's - XCode (Mac) or Visual Studio (Windows). You can use other IDE's but make sure it supports the latest C++ version. 60 | 61 | IDE's that I think are good: 62 | 63 | 1. [Visual Studio 2019 Community](https://www.visualstudio.com/) - Cross-platform 64 | 2. [XCode](https://developer.apple.com/xcode/) - Mac 65 | 3. [CLion](https://www.jetbrains.com/clion) - Cross-platform 66 | 4. [NetBeans](https://netbeans.org/downloads/index.html) - Cross-platform 67 | 5. [Eclipse](https://eclipse.org/downloads/) - Cross-platform 68 | 69 | ### 1.1 Mac 70 | 71 | **Adding files to Xcode** 72 | 73 | 1. Download `XCode` from the App Store. 74 | 2. Open Xcode application and click on `File > New > Workspace...`. 75 | 3. Name the `Workspace` as `CPP-Notes`. 76 | 4. Click on the `+` sign and then click on `Add Files to "CPP-Notes"...` 77 | ![Add files to workspace](https://raw.githubusercontent.com/akshaybabloo/CPP-Notes/master/Screenshots/xcode-workspace.png) 78 | 79 | **Creating Project** 80 | 81 | 1. Click on the `+` sign and then click on `New Project...` 82 | ![Creating a Project to Workspace](https://raw.githubusercontent.com/akshaybabloo/CPP-Notes/master/Screenshots/xcode-create-project.png) 83 | 2. Under `OSX > Application` select `Command Line Tool` and click next. In that, type in the name of the product and make sure you select the language as `C++`. 84 | ![Selecting command line tools](https://raw.githubusercontent.com/akshaybabloo/CPP-Notes/master/Screenshots/xcode-create-project-1.png) 85 | 3. Save it where every you want. 86 | 87 | ### 1.2 Windows 88 | 89 | 1. Go to [https://www.visualstudio.com/en-us/visual-studio-homepage-vs.aspx](https://www.visualstudio.com/en-us/visual-studio-homepage-vs.aspx) and download `Visual Studio Community` version. Make sure `Visual C++` package is selected and continue the installation. 90 | 2. Once installed, Click on `File > New > Project` or use the shortcut Ctrl + Shift + N. 91 | 92 | ![Visual Studio New Project](https://raw.githubusercontent.com/akshaybabloo/CPP-Notes/master/Screenshots/vs-new-project.png) 93 | 94 | 3. We are adding the preprocessing variable because of the way Microsoft has written its c++ compiler. For that to happen, we are creating a C++ file. Under `Solution Explorer` right click on `Source Files > Add > New Item...` name it as `test.cpp` and type int he following: 95 | 96 | ```cpp 97 | #include 98 | 99 | using namespace std; 100 | 101 | int main(int argc, char ** argv) { 102 | puts("hello"); 103 | return 0; 104 | } 105 | ``` 106 | 107 | From the menubar click on `Build > Build Solution` 108 | 4. Under Solution Explorer, right click on `CPP > Properties` 109 | ![Visual Studio Project Property](https://raw.githubusercontent.com/akshaybabloo/CPP-Notes/master/Screenshots/vs-project-property.png) 110 | As shown in the image above click on ``. In the text box type in the following: 111 | 112 | ``` 113 | _CRT_SECURE_NO_WARNINGS 114 | _HAS_ITERATOR_DEBUGGING=0 115 | ``` 116 | 117 | Click on `OK` 118 | 119 | **Running the CPP files** 120 | 121 | 1. Open your command prompt, by doing WINDOWS + R and type in `cmd`. 122 | 2. To execute the built `test.cpp`, You would have to go to the project folder `CPP > Build > CPP.exe`. Drag and drop `CPP.exe` on the command prompt the press `Enter`, this will output `hello`. 123 | 124 | ### CMake 125 | 126 | CMake is a build system for C++. See their website - [CMake](https://cmake.org) - for more information. 127 | 128 | ## 2 Basics 129 | 130 | C++ inherits most of its code style from C language, but both are very different from each other. Let's consider an example: 131 | 132 | See [2_1_HelloWorld.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_1_HelloWorld.cpp) 133 | 134 | ```cpp 135 | // This is a comment 136 | 137 | /* 138 | This is a block code. 139 | */ 140 | #include //<- Libraries 141 | #include 142 | 143 | using namespace std; //<- scope to identifiers 144 | 145 | int main(int argc, char ** argv) { 146 | puts("hello"); // this statement outputs hello with a new line 147 | printf("hello\n"); // this is similar to puts but doesn't end with new line 148 | cout << "hello\n"; // more complex way to output without new line 149 | return 0; // 0 means success 150 | } 151 | ``` 152 | 153 | A C++ program can also be written like this (though I wouldn't recommend it): 154 | 155 | ```cpp 156 | #include 157 | using namespace std; 158 | 159 | int 160 | main 161 | ( 162 | int 163 | argc, 164 | char 165 | ** 166 | argv) { 167 | puts(" 168 | hello") 169 | ; 170 | return 0; 171 | } 172 | ``` 173 | 174 | **Things to remember** 175 | 176 | 1. A statement should always end with `;`. 177 | 2. `#Include` should always be in single line without any space followed by `<>` or `""`. 178 | 179 | ### 2.1 Identifiers 180 | 181 | C++ follows the following standards 182 | 183 | * [ISO Latin Alphabets](https://en.wikipedia.org/wiki/ISO_basic_Latin_alphabet) 184 | 185 | | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | 186 | |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---| 187 | | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z | 188 | 189 | * [Western arabic numbers](https://en.wikipedia.org/wiki/Arabic_numerals) 190 | 191 | | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 192 | |---|---|---|---|---|---|---|---|---|---| 193 | 194 | * [ASCII Underscore](http://www.theasciicode.com.ar/ascii-printable-characters/underscore-understrike-underbar-low-line-ascii-code-95.html) 195 | 196 | | _ | 197 | |---| 198 | 199 | * These identifiers cannot conflict with C++ 86 keywords (which includes 11 tokens) 200 | 201 | | | | | 202 | |-------------------------|------------------------|-----------------------------| 203 | | alignas (since C++11) | explicit | signed | 204 | | alignof (since C++11) | export(1) | sizeof | 205 | | and | extern | static | 206 | | and_eq | FALSE | static_assert (since C++11) | 207 | | asm | float | static_cast | 208 | | auto(1) | for | struct | 209 | | bitand | friend | switch | 210 | | bitor | goto | template | 211 | | bool | if | this | 212 | | break | inline | thread_local (since C++11) | 213 | | case | int | throw | 214 | | catch | long | TRUE | 215 | | char | mutable | try | 216 | | char16_t (since C++11) | namespace | typedef | 217 | | char32_t (since C++11) | new | typeid | 218 | | class | noexcept (since C++11) | typename | 219 | | compl | not | union | 220 | | concept (concepts TS) | not_eq | unsigned | 221 | | const | nullptr (since C++11) | using(1) | 222 | | constexpr (since C++11) | operator | virtual | 223 | | const_cast | or | void | 224 | | continue | or_eq | volatile | 225 | | decltype (since C++11) | private | wchar_t | 226 | | default(1) | protected | while | 227 | | delete(1) | public | xor | 228 | | do | register | xor_eq | 229 | | double | reinterpret_cast | | 230 | | dynamic_cast | requires (concepts TS) | | 231 | | else | return | | 232 | | enum | short | | 233 | 234 | * Identifiers are case sensitive. 235 | 236 | ### 2.2 Defining Variables 237 | 238 | See [2_2_DefineVariables.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_2_DefineVariables.cpp) 239 | 240 | Identifiers (or variables) can be initialized by using the following syntax: 241 | 242 | ``` 243 | DataType VariableName = "String" or number; 244 | ``` 245 | 246 | You can also define a read-only variable or a constant in C++ by using the keyword `const`. 247 | 248 | ### 2.3 Pointer and Reference 249 | 250 | see [2_3_Pointers_Reference.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_3_Pointers_Reference.cpp) 251 | 252 | A variable can be called in two ways; `call by value` and `call by reference`. 253 | 254 | A pointer and reference are a type of data type, which is commonly used in C/C++ programming. It is a very powerful and confusing concept. It will take some time to understand. 255 | 256 | * A pointer can take reference of another variable or a real value 257 | 258 | Lets understand how identifiers work. When we say `int a = 10;`, an integer variable `a` has a value of `10` in the memory. When we say `int b = a;`, an integer variable `b` has a copy of variable `a` 259 | 260 | ```cpp 261 | int a = 10; 262 | int b = a; // b = 10 263 | ``` 264 | 265 | **Pointers** 266 | 267 | So, When I say `int *c = &a`, it means that pointer `c` points to the reference of `a`. 268 | 269 | ```cpp 270 | int a = 10; 271 | int *b = &a; 272 | printf("%d\n", *b); // this will print the reference value of a, which is 10 273 | ``` 274 | 275 | **Reference** 276 | 277 | References are the address of value in the memory. The pointer points to this address while calling. 278 | 279 | * A reference can only call a variable which is already initialized. 280 | 281 | So, when I say `int &d = b` the address if `b` is stored in `d`. 282 | 283 | ```cpp 284 | int b = 20; 285 | int &d = b; 286 | printf("%d\n", d); 287 | ``` 288 | 289 | ### 2.4 Arrays and Strings 290 | 291 | See [2_4_1_Arrays.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_4_Arrays_Strings/2_4_1_Arrays.cpp) and [2_4_2_String.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_4_Arrays_Strings/2_4_2_String.cpp) 292 | 293 | There are two types of Arrays and String in C++, one using C style Arrays & String and the second one using Standard Library (STL), which will be covered later. 294 | 295 | **Arrays** 296 | 297 | The syntax of a C-based array is 298 | 299 | ```cpp 300 | int a[5] = {1,2,3,4,5}; // array[SizeOfArray] = {'contents'}; 301 | printf("%d\n", a[0]); // 1 302 | ``` 303 | **Strings** 304 | 305 | A string is an array of characters terminated string or also called as null terminated string. A string is always terminated with 0. 306 | 307 | ```cpp 308 | char a[6] = {'a', 'b', 'c', 'd', 'e', 0}; 309 | printf("%s\n", a); // abcde 310 | ``` 311 | 312 | ### 2.5 Conditions 313 | 314 | See [2_5_Conditions.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_5_Conditions.cpp) 315 | 316 | There are two ways to use conditional operators. 317 | 318 | 1. Traditional conditional operator. 319 | 2. Ternary conditional operator. 320 | 321 | **Traditional conditional operator** 322 | 323 | `if..else..` are the common type of conditional statements. 324 | 325 | ```cpp 326 | int a = 10; 327 | int b = 20; 328 | 329 | if (a > b) { 330 | puts("a>b"); 331 | } else { 332 | puts("b>a"); 333 | } 334 | ``` 335 | 336 | **Ternary conditional operator** 337 | 338 | Its a one liner conditional operator 339 | 340 | ```cpp 341 | int a = 10; 342 | int b = 20; 343 | 344 | printf("%d\n", a > b ? a : b); // if a is greater than b, print a else print b 345 | ``` 346 | 347 | ### 2.6 Switch Case 348 | 349 | See [2_6_Switch_Case.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_6_Switch_Case.cpp) 350 | 351 | It is a conditional statement, which requires an expression which should satisfy a condition. If a condition is not satisfied, then it jumps to `default`. An expression should always be a constant of integer or a character. Syntax looks something like this 352 | 353 | ```cpp 354 | switch (/* expression */) { 355 | case /* value */: 356 | /* statement */ 357 | } 358 | ``` 359 | 360 | ### 2.7 While Loop 361 | 362 | See [2_7_While_Loop.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_7_While_Loop.cpp) 363 | 364 | There are two types of `While` loop in C++ 365 | 366 | 1. `While` loop 367 | 368 | ```cpp 369 | while (/* condition */) { 370 | /* code */ 371 | } 372 | ``` 373 | 374 | 2. `do.. While..` loop 375 | 376 | ```cpp 377 | do { 378 | /* code */ 379 | } while(/* condition */); 380 | ``` 381 | 382 | ### 2.8 For Loop 383 | 384 | See [2_8_For_Loop.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_8_For_Loop.cpp) 385 | 386 | For loop is like `while` loop but with some extra features 387 | 388 | ```cpp 389 | for (size_t i = 0; i < count; i++) { 390 | /* code */ 391 | } 392 | ``` 393 | 394 | ### 2.9 Range base `For` loop 395 | 396 | See [2_9_Range_For.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_9_Range_For.cpp) 397 | 398 | Starting from C++11, we can use range based `For` loop 399 | 400 | ```cpp 401 | for (type var1 : var2) { 402 | /* code */ 403 | } 404 | ``` 405 | 406 | ### 2.10 Using stdout 407 | 408 | C++ also has a way to used object oriented way of printing out contents to the terminal/command prompt. So far we have used `printf` and `puts`. 409 | 410 | ```cpp 411 | std::cout << "Hello World!" << std::endl; 412 | ``` 413 | 414 | The above code shows a bitwise stream of string to `cout`. The `<<` shows left shift of the content. 415 | 416 | Creating a compiled version of `cout` uses a lot of resources when compared to `puts` or `printf`, this is because to compile `cout` the whole standard library - `STL` - is copied. 417 | 418 | ## 3 Functions 419 | 420 | A function can be defined as a block of code that is separate from the existing code; that is all the variables used in a function would only belong to that particular function. For example (pseudo code): 421 | 422 | ```cpp 423 | int a = 10; 424 | int b = 20; 425 | 426 | c = sum(a, b); 427 | 428 | int sum (int a, int b){ 429 | return a + b; 430 | } 431 | 432 | printf("%d\n", c); 433 | ``` 434 | 435 | From the above the variables `a` and `b` in function `sum()` are different from the initialized variable `a` and `b`. 436 | 437 | This particular type of function is call `call by value` function. Another type of function is called as the `call by reference` or sometimes called as the `call by address`. For example (pseudo code): 438 | 439 | ```cpp 440 | int a = 10; 441 | int b = 20; 442 | 443 | c = sum(&a, &b); 444 | 445 | int sum (int *a, int *b){ 446 | return *a + *b; 447 | } 448 | 449 | printf("%d\n", c); 450 | ``` 451 | 452 | ### 3.1 Defining a function 453 | 454 | See [3_1_1_Define_Function](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_1_Define_Function/3_1_1_Define_Function.cpp) 455 | 456 | In C++, a function should be declared first before calling it. That is: 457 | 458 | ```cpp 459 | void name(/* arguments */) { 460 | /* code */ 461 | } 462 | 463 | int main(int argc, char const *argv[]) { 464 | name() 465 | return 0; 466 | } 467 | ``` 468 | 469 | C++ will not compile if the function being called is written after the main function. 470 | 471 | See [3_1_2_Forward_Declaration](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_1_Define_Function/3_1_2_Forward_Declaration.cpp) 472 | 473 | To overcome this problem, we have something called `Forward Declaration`. For example: 474 | 475 | ```cpp 476 | void name(/* arguments */); 477 | 478 | int main(int argc, char const *argv[]) { 479 | name() 480 | return 0; 481 | } 482 | 483 | void name(/* arguments */) { 484 | /* code */ 485 | } 486 | ``` 487 | 488 | `void name(/* arguments */);` is know as `Forward Declaration` or prototype of `name()` 489 | 490 | See [3_1_3_Function_Header.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_1_Define_Function/3_1_3_Function_Header.cpp) and [3_1_3_Function_Header.h](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_1_Define_Function/3_1_3_Function_Header.cpp) 491 | 492 | The common way to do `Forward Declaration` is to put the prototype in a header file. For example: 493 | 494 | `3_1_3_Function_Header.cpp` 495 | 496 | ```cpp 497 | #include "3_1_3_Function_Header.h" 498 | 499 | int main(int argc, char const *argv[]) { 500 | name() 501 | return 0; 502 | } 503 | 504 | void name(/* arguments */) { 505 | /* code */ 506 | } 507 | ``` 508 | 509 | `3_1_3_Function_Header.h` 510 | 511 | ```cpp 512 | #ifndef 3_1_3_Function_Header_h 513 | #define 3_1_3_Function_Header_h 514 | 515 | void name(/* arguments */); 516 | 517 | #endif 518 | ``` 519 | 520 | ### 3.2 Passing values to a function 521 | 522 | There are two two ways to pass values to a function 523 | 524 | 1. Pass by Value 525 | 2. Pass by Reference 526 | 527 | **Pass by value:** 528 | 529 | See [3_2_1_Pass_by_Value.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_2_Passing_Values/3_2_1_Pass_by_Value.cpp) 530 | 531 | When you pass a value to a function, a copy of that value is stored in the argument. 532 | 533 | ```cpp 534 | void sum(int c, int d) { 535 | printf("%d\n", c + d); 536 | } 537 | 538 | int main(int argc, char const *argv[]) { 539 | int a = 10; 540 | int b = 20; 541 | 542 | sum(a,b); 543 | return 0; 544 | } 545 | ``` 546 | 547 | **Pass by Reference:** 548 | 549 | See [3_2_2_Pass_by_Reference.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_2_Passing_Values/3_2_2_Pass_by_Reference.cpp) 550 | 551 | We will talk more about pointers in the coming chapters. In C/C++ (but not limited to theses languages), when you create a variable a memory is allocated to that variable, this memory space has an address (location of it), so the reference here means we are sending the address of the variable rather than the variable itself. 552 | 553 | For example, let us consider `int a = 10;`, which means an integer variable `a` has a value of `10` if you convert this in a diagrammatically you will get the following: 554 | 555 | ``` 556 | int a = 10; 557 | ---------- 558 | | a | 10 | --> 123456 559 | ---------- 560 | ``` 561 | 562 | The number `123456` is the address/location of integer `a` in the memory. When passing the value by reference you send this address, that means you do not create extra space for data; you just use what you have. 563 | 564 | ```cpp 565 | void sum(int *a, int *b){ 566 | printf("%d\n", *a+*b); // *a and *b pointing to the address given to them. 567 | } 568 | 569 | int main(int argc, char ** argv) { 570 | int a = 10; 571 | int b = 20; 572 | sum(&a,&b); // address of a and b 573 | return 0; 574 | } 575 | ``` 576 | 577 | See [3_2_3_Pass_by_Reference_Const.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_2_Passing_Values/3_2_3_Pass_by_Reference_Const.cpp) 578 | 579 | There is one problem with pointers in C/C++, that is if you change the contents of the address in `sum()` function you will change the value of the variable. For example If we add a new integer `a=30` or `*a=30` variable to `sum()` 580 | 581 | ```cpp 582 | void sum(int &a, int &b){ 583 | a = 30; 584 | printf("%d\n", a+b); 585 | } 586 | 587 | // or 588 | 589 | void sum(int *a, int *b){ 590 | *a = 30; 591 | printf("%d\n", *a+*b); 592 | } 593 | 594 | ``` 595 | 596 | The value of `a` is completely changed, for this not to happen we will have to use a keyword called `const`. 597 | 598 | ```cpp 599 | void sum(const int &a, const int &b){ 600 | a = 30; 601 | printf("%d\n", a+b); 602 | } 603 | 604 | // or 605 | 606 | void sum(const int *a, const int *b){ 607 | *a = 30; 608 | printf("%d\n", *a+*b); 609 | } 610 | 611 | ``` 612 | 613 | ### 3.3 Automatic variables vs. Static variables 614 | 615 | **Automatic variable** 616 | 617 | See [3_3_1_Auto_Variable.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_3_Auto_Static_Variables/3_3_1_Auto_Variable.cpp) 618 | 619 | By default, C++ uses automatic variables in every function. Whenever the function is called the variable local to it is initialized on a stack. For example 620 | 621 | ```cpp 622 | void name() { 623 | int a = 10; 624 | printf("%d\n", a); 625 | a = 30; 626 | } 627 | 628 | int main(int argc, char const *argv[]) { 629 | name(); 630 | name();// this will always print the same thing 631 | return 0; 632 | } 633 | ``` 634 | 635 | **Static variable** 636 | 637 | See [3_3_2_Static_Variable.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_3_Auto_Static_Variables/3_3_2_Static_Variable.cpp) 638 | 639 | Unlike automatic variables Static variables do not get created on every function call, they just use whatever was previously defined. Don't forget to use `const` if you don't want to change the value. 640 | 641 | ### 3.4 Return a function call 642 | 643 | See [3_4_Return_Function.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_4_Return_Function.cpp) 644 | 645 | To return a function, we would have to type in the return type and use the keyword `return` at the end of the function. For example: 646 | 647 | ```cpp 648 | int number(){ 649 | return 10; 650 | } 651 | 652 | int main(int argc, char const *argv[]) { 653 | printf("%d\n", number()); 654 | return 0; 655 | } 656 | ``` 657 | 658 | ### 3.5 Function pointer 659 | 660 | See [3_5_Function_Pointer.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_5_Function_Pointer.cpp) 661 | 662 | You can call a function by pointing to it, the same way you point to a variable. The only difference is that the data type of the function should match with the data type of the function pointer. For example 663 | 664 | ```cpp 665 | void name( { 666 | puts("hello"); 667 | } 668 | 669 | int main(int argc, char const *argv[]) { 670 | void (*function_pointer)() = name; 671 | function_pointer(); 672 | return 0; 673 | } 674 | ``` 675 | 676 | ### 3.6 Overloading function names 677 | 678 | See [3_6_Overloading_Fucntion_Names.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_6_Overloading_Fucntion_Names.cpp) 679 | 680 | In C++ you can have multiple functions with the same name, but the signature (data type) should be same all over the function. 681 | 682 | ### 3.7 Overloading operators with function 683 | 684 | See [3_7_Oveloading_Operators.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_7_Oveloading_Operators.cpp) 685 | 686 | In C++ you can change the definition of the following 38 operators: 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 |
+-*/%^&|~!=<>+=-=
*=/=%=^=&=|=<<>>>>=<<===!=<=>=&&
||++--,->*->( )[ ]
741 | 742 | That means an addition operator can be turned into multiplication operator. 743 | 744 | ### 3.8 Variable number of arguments 745 | 746 | See [3_8_Variable_Arguments.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_8_Variable_Arguments.cpp) 747 | 748 | In C++ you can have multiple arguments given to a function, this can be achieved by adding `...` in the function arguments space. 749 | 750 | There are four macros that needs to be called when using a variable arguments: 751 | 752 | * *va_list*: `va_list fa` is used as a parameter. 753 | * *va_start*: `va_start(ap, parameter)` initialize a variable argument list. 754 | * *va_arg*: `va_arg(ap, type)` gets the next available argument of a data type. 755 | * *va_end*: `va_end(ap)` Ends using variable argument list 756 | 757 | ### 3.9 Recursive function 758 | 759 | See [3_9_Recursive_Function.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_9_Recursive_Function.cpp) 760 | 761 | In C++ you can call a function itself. For example: 762 | 763 | ```cpp 764 | int main(int argc, char const *argv[]) { 765 | main(); 766 | return 0; 767 | } 768 | ``` 769 | 770 | These types of functions are called recursive functions. These functions as an alternate to For loops. 771 | 772 | ## 4 Preprocessors 773 | 774 | The preprocessors are used to process the code before sending it to the compiler. The most common way is the file inclusion using `#include <>`. You can also use macro preprocessors by using `#define NUMBER 1`, these acts like a string substitution. 775 | 776 | When you open a `.h` the contents of the file you often see looks something like this: 777 | 778 | ```cpp 779 | #ifndef main_h 780 | #define main_h 781 | 782 | void function_name(); 783 | 784 | #endif /* main_h */ 785 | ``` 786 | 787 | They are called as an "include guard" which checks for inclusion. 788 | 789 | Another type of preprocessor is used by using `#pragma` that are used (or targeted) for specific compilers and architectures. 790 | 791 | ### 4.1 Macro constants 792 | 793 | See [4_1_Macro_Constants.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/4_Preprocessors/4_1_Macro_Constants.cpp) 794 | 795 | You can define a macro constant by using `#define macro`. For example: 796 | 797 | ```cpp 798 | #define NUMBER 1 799 | 800 | int main(int argc, char const *argv[]) { 801 | printf("%d\n", NUMBER); 802 | return 0; 803 | } 804 | ``` 805 | 806 | When the above code is compiled the NUMBER is replaced by a literal value before the code reaches to the compiler. At this point you cannot get its address or use pointers. 807 | 808 | ### 4.2 Including a file 809 | 810 | See [4_2_Include_File](https://github.com/akshaybabloo/CPP-Notes/tree/master/4_Preprocessors/4_2_Include_File/) 811 | 812 | To include a file in a C++ file you would have to use `#include "file_name.h"`. This will place all the contents in the `cpp` before the code is sent to the compiler. 813 | 814 | ### 4.3 Conditions in preprocessor 815 | 816 | See [4_3_Preprocessor_Conditions](https://github.com/akshaybabloo/CPP-Notes/tree/master/4_Preprocessors/4_3_Preprocessor_Conditions) 817 | 818 | Preprocessor consists of different types of conditional compilation 819 | 820 | 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 834 | 835 | 836 | 837 | 838 | 839 | 840 | 841 | 842 | 843 | 844 | 845 |
#ifOpening `if` condition
#else`else` condition
#elif`else if` condition
#ifdef`if defined` condition
#ifndef`if not defined` condition
#endif`end if` condition
846 | 847 | Also, there are two alternatives for `#ifdef` and `#ifndef`, they are: 848 | 849 | ```cpp 850 | #if defined(macro) 851 | #if !defined(macro) 852 | ``` 853 | 854 | ### 4.4 Macro expansion 855 | 856 | See [4_4_Macro_Expansion.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/4_Preprocessors/4_4_Macro_Expansion.cpp) 857 | 858 | 859 | Macro's can also take parameters and replace them when called. For example: 860 | 861 | ```cpp 862 | #define ADD(a,b) (a+b) 863 | 864 | int main(int argc, char const *argv[]) { 865 | printf("%d\n", ADD(10,20)); 866 | return 0; 867 | } 868 | ``` 869 | 870 | ### 4.5 Problems with Macro's 871 | 872 | See [4_5_Macro_Problems.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/4_Preprocessors/4_5_Macro_Problems.cpp) 873 | 874 | You should always be careful when using parameterised macros. See [4_5_Macro_Problems.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/4_Preprocessors/4_5_Macro_Problems.cpp) for more details. 875 | 876 | ### 4.6 Line continuation 877 | 878 | See [4_6_Line_Continuation.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/4_Preprocessors/4_6_Line_Continuation.cpp) 879 | 880 | If you want to use complex macros you can use `line continuation` by add `\` at the end of the each line. For example: 881 | 882 | ```cpp 883 | #define LOOPER(i) do \ 884 | { \ 885 | printf("%d\n",i++); \ 886 | } while (i < 3); 887 | ``` 888 | 889 | ### 4.6 Include guard 890 | 891 | See [4_6_Include_Guard](https://github.com/akshaybabloo/CPP-Notes/tree/master/4_Preprocessors/4_6_Include_Guard) 892 | 893 | There might be a situation where you might have to define a header file in another header file and when called in a C++ file you might include both header files. When you compile this you will have a `Build fail`, to over come this we have to include something called as `Include guard`. It looks something like this 894 | 895 | ```cpp 896 | #ifndef _HEADERNAME_H 897 | #define _HEADERNAME_H 898 | ... 899 | #endif 900 | ``` 901 | 902 | ## 5 Classes and Objects in C++ 903 | 904 | C++ is a an Object Oriented Program, that's what makes it different from C programming language. A class is define by using `class` keyword followed by class name. For example: 905 | 906 | ```cpp 907 | class name_t { 908 | int i; // Data members 909 | public: // Function members 910 | name_t (arguments); // Constructor 911 | ~name_t (); // Destructor 912 | 913 | }; 914 | ``` 915 | 916 | Few points to remember: 917 | 918 | * A class can have multiple constructor and only one destructor. 919 | * A class when called and naming it is called an instance of that class. Example `name_t name;`, `name` is an instance of class `name_t`. 920 | * Using classes you can allocate memory properly. 921 | 922 | More information can be found [here](http://www.cplusplus.com/doc/tutorial/classes/). 923 | 924 | ### 5.1 Defining Classes and Objects 925 | 926 | There are different ways to define a class. For example 927 | 928 | See [5_1_1_Define_Classes.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_1_Define_Classes/5_1_1_Define_Classes.cpp) 929 | 930 | ```cpp 931 | class name_t { 932 | int i; 933 | public: 934 | void some_name (arguments){ /* do something */}; 935 | }; 936 | 937 | int main(int argc, char const *argv[]) { 938 | name_t obj1; 939 | return 0; 940 | } 941 | ``` 942 | 943 | Another way is to use `private` keyword, you can then use this to define `private` variables and function after `public`. For example: 944 | 945 | ```cpp 946 | class name_t { 947 | public: 948 | void some_name (arguments){/* do something */}; 949 | private: 950 | int i; 951 | }; 952 | 953 | int main(int argc, char const *argv[]) { 954 | name_t obj1; 955 | return 0; 956 | } 957 | ``` 958 | 959 | The public functions can be used outside, just declare it in the class file and define it outside the `class`. For example: 960 | 961 | See [5_1_2_Define_Classes.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_1_Define_Classes/5_1_2_Define_Classes.cpp) 962 | 963 | ```cpp 964 | class name_t { 965 | public: 966 | void some_name (arguments); 967 | private: 968 | int i; 969 | }; 970 | 971 | void name_t::some_name (arguments){/* do something */}; 972 | 973 | int main(int argc, char const *argv[]) { 974 | name_t obj1; 975 | return 0; 976 | } 977 | ``` 978 | 979 | The code can be divided into 3 stages: 980 | 981 | * *Interface*: Usually kept in the header file. 982 | 983 | ```cpp 984 | class name_t { 985 | private: 986 | /* data */ 987 | public: 988 | void some_name (arguments); 989 | }; 990 | ``` 991 | * *Implementation*: Usually kept in an implementation file 992 | 993 | ```cpp 994 | void name_t::some_name (arguments){/* do something */}; 995 | ``` 996 | * *Usage*: Usually kept in `cpp` file 997 | 998 | ```cpp 999 | int main(int argc, char const *argv[]) { 1000 | name_t obj1; 1001 | return 0; 1002 | } 1003 | ``` 1004 | 1005 | ### 5.2 Data members 1006 | 1007 | See [5_2_Data_Members.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_2_Data_Members.cpp) 1008 | 1009 | In C and C++ we can find keyword called `struct`, when used in C++ it is an object. The different between a `struct` and `class` is that, `struct` by default has `public` data members, where as `class` has `private` data members, everything else is the same. For example: 1010 | 1011 | ```cpp 1012 | struct name_t { 1013 | /* data */ 1014 | }; 1015 | 1016 | ``` 1017 | 1018 | is same as 1019 | 1020 | ```cpp 1021 | struct name_t { 1022 | public: 1023 | /* data */ 1024 | }; 1025 | ``` 1026 | 1027 | ### 5.3 Function members 1028 | 1029 | See [5_3_Fucntion_Members.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_3_Fucntion_Members.cpp) 1030 | 1031 | You can define a same function with different signatures in C++. 1032 | 1033 | ### 5.4 Constructors and Destructors 1034 | 1035 | See [5_4_Constructors_Dstructors.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_4_Constructors_Dstructors.cpp) 1036 | 1037 | A constructor can be used to send in arguments while initializing a class. Destructors are used to clear the memory after the program ends, in C++ destructor are always called at the end of the program by default. 1038 | 1039 | ### 5.5 Implicit and Explicit conversion 1040 | 1041 | See [5_5_Implicit_Explicit.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_5_Implicit_Explicit.cpp) 1042 | 1043 | By Default C++ does implicit conversion. To make an explicit conversion we need to use `explicit` keyword for a constructor. 1044 | 1045 | For example: 1046 | 1047 | ```cpp 1048 | class name_t { 1049 | 1050 | public: 1051 | explicit name_t (arguments); 1052 | virtual ~name_t (); 1053 | 1054 | }; 1055 | ``` 1056 | 1057 | ### 5.6 Namespaces 1058 | 1059 | See [5_6_Namespaces.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_6_Namespaces.cpp) 1060 | 1061 | Namespace in C++ acts like a scope to a group of classes, functions etc... A Namespace can be created by using `namespace` keyword. For example: 1062 | 1063 | ```cpp 1064 | namespace name { 1065 | 1066 | }; 1067 | ``` 1068 | 1069 | ### 5.7 Using `this` 1070 | 1071 | See [5_7_Using_this.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_7_Using_this.cpp) 1072 | 1073 | An object in C++ can access its own pointer, to do so, `this` keyword is used. You can print out the address of a pointer by using 1074 | 1075 | ```cpp 1076 | printf("%p\n", this); 1077 | ``` 1078 | 1079 | ### 5.8 Operator overload: Member function 1080 | 1081 | See [5_8_Operator_Overload_Member_Function.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_8_Operator_Overload_Member_Function.cpp) 1082 | 1083 | Any function that belongs to a class is called a member function. Operator overload can be a part of member function. 1084 | 1085 | ### 5.9 Operator overload: Non-member function 1086 | 1087 | See [5_9_Operator_Overload_Non_Member_Function.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_9_Operator_Overload_Non_Member_Function.cpp) 1088 | 1089 | Any function that does not belong to a class is called a non-member function. 1090 | 1091 | ### 5.10 Conversion operator 1092 | 1093 | See [5_10_Conversion_Operator.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_10_Conversion_Operator.cpp) 1094 | 1095 | You can use `+=` to concatenate a string with a rational number that belongs to a member function. 1096 | 1097 | ### 5.11 Using new and delete 1098 | 1099 | See [5_11_New_and_Delete.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_11_New_and_Delete.cpp) 1100 | 1101 | C++ allows you to allocate and delete memory for different data types using two keywords, `new` - To allocate memory and `delete` - To deallocate memory. For example: 1102 | 1103 | ```cpp 1104 | class name_t { 1105 | private: 1106 | /* data */ 1107 | public: 1108 | name_t (arguments); 1109 | virtual ~name_t (); 1110 | 1111 | }; 1112 | 1113 | int main(int argc, char const *argv[]) { 1114 | name_t *a = new name_t(); // to allocate memory 1115 | delete a; // to deallocate memory 1116 | return 0; 1117 | } 1118 | ``` 1119 | 1120 | ## 5.6 File IO 1121 | 1122 | In this section we will look at how to read and write files using `fstream`.; 1123 | 1124 | ### 5.6.1 Reading Files 1125 | 1126 | [6_1_Reading_File.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/6_File_IO/6_1_Reading_File.cpp) 1127 | 1128 | Reading a file in C++ can be done using `ifstream`, this data type has many functions associated to it but we want `open`, `good` and `close`. `open` opens a file from the memory, `good` checks if the state of stream is good or not and `close` closes the file after reading from it. 1129 | 1130 | ### 5.6.2 Writing File 1131 | 1132 | [6_2_Writing_File.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/6_File_IO/6_2_Writing_File.cpp) 1133 | 1134 | Writing file can be done using `ofstring`, like `ifstring`, this data type provides the same functions - `open` and `close`. If a file already exists with that name, its over written, this can be changed using `ios::app` option that appends any string given to it. 1135 | 1136 | 1137 | ## 6. Data Structures 1138 | 1139 | _A data structure is a group of data elements grouped together under one name._ A structure can have multiple data types grouped together to form a way of representation. 1140 | 1141 | ### 6.1 Structs 1142 | 1143 | [7_1_Structs.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/7_Data_Structures/7_1_Structs.cpp) 1144 | 1145 | It has a group of data types that can be called by name. It can be represented as: 1146 | 1147 | ```cpp 1148 | struct STRUCT_NAME { 1149 | DATA_TYPE NAME; 1150 | }; 1151 | ``` 1152 | 1153 | You can access them as: 1154 | 1155 | ```cpp 1156 | STRUCT_NAME some_name; 1157 | some_name.NAME 1158 | ``` 1159 | -------------------------------------------------------------------------------- /Screenshots/vs-new-project.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akshaybabloo/CPP-Notes/412afd5089cbf1e81f9fdebead71303fa7a18f31/Screenshots/vs-new-project.png -------------------------------------------------------------------------------- /Screenshots/vs-project-property.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akshaybabloo/CPP-Notes/412afd5089cbf1e81f9fdebead71303fa7a18f31/Screenshots/vs-project-property.png -------------------------------------------------------------------------------- /Screenshots/xcode-create-project-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akshaybabloo/CPP-Notes/412afd5089cbf1e81f9fdebead71303fa7a18f31/Screenshots/xcode-create-project-1.png -------------------------------------------------------------------------------- /Screenshots/xcode-create-project.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akshaybabloo/CPP-Notes/412afd5089cbf1e81f9fdebead71303fa7a18f31/Screenshots/xcode-create-project.png -------------------------------------------------------------------------------- /Screenshots/xcode-workspace.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akshaybabloo/CPP-Notes/412afd5089cbf1e81f9fdebead71303fa7a18f31/Screenshots/xcode-workspace.png --------------------------------------------------------------------------------