├── CPP001_First_Program.cpp ├── CPP002_Variables.cpp ├── CPP003_Variables_Types.cpp ├── CPP004_Input.cpp ├── CPP005_Arithmetic_Assignment_Increment_Decrement_Operators.cpp ├── CPP006_Relational_Operators.cpp ├── CPP007_Logical_Operators.cpp ├── CPP008_Calculator_Exercise.cpp ├── CPP009_Arrays.cpp ├── CPP010_Multidimensional_Arrays.cpp ├── CPP011_For.cpp ├── CPP012_While_DoWhile.cpp ├── CPP013_Counting_Digits_Exercise.cpp ├── CPP014_Nested_Loops.cpp ├── CPP015_Break_Continue.cpp ├── CPP016_Variables_Scope.cpp ├── CPP017_Functions.cpp ├── CPP018_Overloading_Functions.cpp ├── CPP019_CalculateArea_Exercise.cpp ├── CPP020_Enumeration.cpp ├── CPP021_ReferenceVariables.cpp ├── CPP022_ReferenceVariables_Functions.cpp ├── CPP023_Pointers.cpp ├── CPP024_Pointers_in_Arrays.cpp ├── CPP025_Dynamic_Memory_Allocation.cpp ├── CPP026_String_Char_Pointers.cpp ├── CPP027_Functions_and_Pointers.cpp ├── CPP028_Lottery_Exercise.cpp ├── CPP029_Type_Casting.cpp ├── CPP030_Structure.cpp ├── CPP031_Classes.cpp ├── CPP032_StaticVariables_StaticFunctions.cpp ├── CPP033_Constant_Variables_and_Methods.cpp ├── CPP034_Friend_Function.cpp ├── CPP035_Friend_Classes.cpp ├── CPP036_Copy_Constructor.cpp ├── CPP037_Operator_Overloading.cpp ├── CPP038_Virtual_Function.cpp ├── CPP039_AbstractClass_PureVirtualFunction.cpp ├── CPP040_Exception_Handling.cpp ├── CPP041_Namespaces.cpp └── README.md /CPP001_First_Program.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | main() 5 | { 6 | cout<<"Hello World !"; 7 | } 8 | -------------------------------------------------------------------------------- /CPP002_Variables.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | main() 6 | { 7 | //Declaring an integer type variable A, allocates 4 bytes of memory. 8 | int A=4; 9 | 10 | cout< 2 | 3 | using namespace std; 4 | 5 | main() 6 | { 7 | int a=10,b=35; // 4 bytes 8 | cout<<"Value of a : "< 2 | 3 | using namespace std; 4 | 5 | main() 6 | { 7 | // cin - console input 8 | int a; 9 | cin >> a; 10 | cout << "Variable name a = " << a << endl; 11 | 12 | string name,surname; 13 | 14 | cout << "Enter your name : "; 15 | cin >> name; 16 | 17 | cout << "Enter your surname : "; 18 | cin >> surname; 19 | 20 | cout << "Welcome "< 2 | 3 | using namespace std; 4 | 5 | main() 6 | { 7 | int a=10,b=5; 8 | 9 | cout << a + b < Remainder 15 | 16 | cout << ++a < 7 | 8 | using namespace std; 9 | 10 | main() 11 | { 12 | cout<<"Relational Operators !"<b)<=b)< 7 | 8 | using namespace std; 9 | 10 | main() 11 | { 12 | // AND - && 13 | // True only when both the expressions are true 14 | 15 | cout << (7>5 && 5!=10) <5 && 5!=10) <5 || 5!=10) <5 || 5!=5) <5 && 5!=10) < 7 | using namespace std; 8 | 9 | main() 10 | { 11 | double var1, var2; 12 | 13 | beginning: 14 | 15 | cout << "Enter first number: " << endl; 16 | cin >> var1; 17 | 18 | cout << "Enter second number: " << endl; 19 | cin >> var2; 20 | 21 | cout << "What do you want to do with that numbers?" << endl; 22 | cout << "+ - add" << endl; 23 | cout << "- - substract" << endl; 24 | cout << "* - multiply" << endl; 25 | cout << "/ - divide" << endl; 26 | 27 | char decision; 28 | 29 | cin >> decision; 30 | 31 | 32 | 33 | switch(decision) 34 | { 35 | case '+': 36 | cout << var1 << " + " << var2 << " = " << (var1 + var2) << endl; 37 | break; 38 | case '-': 39 | cout << var1 << " - " << var2 << " = " << (var1 - var2) << endl; 40 | break; 41 | case '*': 42 | cout << var1 << " * " << var2 << " = " << (var1 * var2) << endl; 43 | break; 44 | case '/': 45 | if (var2) //var2 != 0 46 | cout << var1 << " / " << var2 << " = " << (var1 / var2) << endl; 47 | else 48 | cout << "You can't divide by 0" << endl; 49 | break; 50 | default: 51 | cout << "You typed wrong character"; 52 | 53 | } 54 | 55 | char decision2; 56 | 57 | cout << "Do you want to continue that program? (Y/N)" << endl; 58 | cin >> decision2; 59 | 60 | if (decision2 == 'y' || decision2 == 'Y') 61 | goto beginning; 62 | } 63 | -------------------------------------------------------------------------------- /CPP009_Arrays.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Author: Tridib Samanta 3 | * Created: 17.11.2019 4 | **/ 5 | 6 | #include 7 | using namespace std; 8 | 9 | main() 10 | { 11 | int array[4]; //Type_Name[Size_of_Elements] 12 | 13 | //Initialization 14 | array[0]=10; 15 | array[1]=20; 16 | array[2]=30; 17 | array[3]=40; 18 | 19 | //Printing the array elements and address of each element 20 | cout<<"array[0] = "< 7 | using namespace std; 8 | 9 | main() 10 | { 11 | int biArray[3][4]={0}; //Initialize all array elements to zero 12 | 13 | //Initialize individual array elements to respective values 14 | biArray[0][0]=11; 15 | biArray[0][1]=12; 16 | biArray[0][2]=13; 17 | biArray[0][3]=14; 18 | 19 | biArray[1][0]=21; 20 | biArray[1][1]=22; 21 | biArray[1][2]=23; 22 | biArray[1][3]=24; 23 | 24 | biArray[2][0]=31; 25 | biArray[2][1]=32; 26 | biArray[2][2]=33; 27 | biArray[2][3]=34; 28 | 29 | //Print all array elements 30 | cout< 7 | using namespace std; 8 | 9 | main() { 10 | /* 11 | for(initialization;condition;inc/dec) 12 | instructions_to_repeat; 13 | */ 14 | 15 | for(int i=0;i<3;i++) 16 | cout<<"Hello"< 7 | using namespace std; 8 | 9 | main() { 10 | int i=0; 11 | while(i<10) { 12 | cout<<"Hello"< 7 | using namespace std; 8 | 9 | main() { 10 | int num,digits_count=1; 11 | cin>>num; 12 | int temp=num; 13 | //Divide the number by 10, until we get quotient as 0 14 | while(temp/=10) 15 | //Increase the digit count by 1 16 | digits_count++; 17 | 18 | cout<<"The number "< 7 | using namespace std; 8 | 9 | main() { 10 | for(int i=1;i<=10;i++) { 11 | cout<<"Multiplication table of "< 7 | using namespace std; 8 | 9 | void welcome(); // Declaration of function 10 | bool isNumber(string tmp); 11 | //bool isNumber(string); Mentioning variable name during declaration not mandatory 12 | void enterString(); 13 | 14 | //Inline Function 15 | double add(double a,double b) {return a+b;} 16 | 17 | void changeValueTO10(int x) { x=10; } 18 | //main() is a function 19 | int main() { 20 | 21 | welcome(); 22 | 23 | char ch; 24 | cout<<"Do you want to end the program ? (Y/N)"<>ch; 26 | if(ch=='Y'||ch=='y') 27 | return 0; 28 | 29 | cout<= 48 && tmp[i] <= 57)) 53 | return false; //Once false is returned it wont execute the loop anymore 54 | } 55 | 56 | return true; 57 | } 58 | 59 | void enterString() { 60 | string tmp; 61 | cout<<"Enter the number : "<>tmp; 63 | 64 | if(isNumber(tmp)) 65 | cout<<"Number entered properly"< 7 | using namespace std; 8 | 9 | int power(int,int); 10 | double power(double,int); 11 | //While overloading functions the function name will be the same. 12 | //Function return type and function parameters will be different. 13 | 14 | int main() { 15 | 16 | cout<1) { 24 | b = b*tmp; 25 | e--; 26 | } 27 | return b; 28 | } 29 | 30 | double power(double b,int e) { 31 | int tmp=b; 32 | while(e>1) { 33 | b = b*tmp; 34 | e--; 35 | } 36 | return b; 37 | } 38 | -------------------------------------------------------------------------------- /CPP019_CalculateArea_Exercise.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Author: Tridib Samanta 3 | * Created: 15.01.2020 4 | **/ 5 | 6 | #include 7 | #define PI 3.14 8 | using namespace std; 9 | 10 | void initMenu(); 11 | void menuDecision(int); 12 | double areaCircle(double); 13 | double areaSquare(double); 14 | double areaRectangle(double,double); 15 | double areaTriangle(double,double); 16 | 17 | 18 | int main() { 19 | 20 | int choice; 21 | char cont; 22 | 23 | do { 24 | //system("cls"); 25 | initMenu(); 26 | cin>>choice; 27 | 28 | menuDecision(choice); 29 | 30 | cout<<"Do you want to continue ? (Y/N)"<>cont; 32 | } while(cont=='Y'||cont=='y'); 33 | 34 | return 0; 35 | } 36 | 37 | void initMenu() { 38 | cout<<"Select from the options :"<>r; 51 | areaCircle(r); 52 | break; 53 | case 2: 54 | cout<<"Enter a side of the square : "<>a; 56 | areaSquare(a); 57 | break; 58 | case 3: 59 | cout<<"Enter the width and height of the rectangle : "<>a>>b; 61 | areaRectangle(a,b); 62 | break; 63 | case 4: 64 | cout<<"Enter the base and height of the triangle : "<>a>>h; 66 | areaTriangle(a,h); 67 | break; 68 | default: 69 | cout<<"Wrong choice !"< 7 | using namespace std; 8 | 9 | enum dayOfWeek {Monday = 1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; 10 | 11 | string getDay(dayOfWeek); 12 | 13 | int main() { 14 | 15 | //dayOfWeek d = Wednesday; 16 | //cout<>i; 22 | cout< 7 | using namespace std; 8 | 9 | /* 10 | ~ Reference Variables are the alias to another variable. 11 | ~ Must be initialized when created. 12 | ~ They cant change the connections/associations. 13 | ~ Must be of the same type as associated variables. 14 | */ 15 | 16 | int main() { 17 | 18 | string name="Peter"; 19 | cout<<"Value of variable is : "< 7 | using namespace std; 8 | 9 | void swap(int &, int &); 10 | 11 | int main() { 12 | 13 | int a=10,b=20; 14 | swap(a,b); 15 | 16 | cout<<"a : "< 7 | using namespace std; 8 | 9 | int main() { 10 | 11 | int var = 5; 12 | cout<<&var< 7 | using namespace std; 8 | 9 | int main() { 10 | 11 | int a[3]={10,20,30}; 12 | short int zm; 13 | int * const p = &a[0]; 14 | 15 | cout< 7 | using namespace std; 8 | 9 | int main() { 10 | 11 | int amount; 12 | cout<<"How many numbers would you like to store in the array ? "<>amount; 14 | 15 | int *p = new (nothrow) int(amount); 16 | 17 | if(p != NULL) { 18 | for(int i=0;i>p[i]; 21 | } 22 | 23 | cout< 7 | using namespace std; 8 | 9 | int main() { 10 | 11 | string text = "Hello"; // H e l l o \0 12 | 13 | char ch = text[0]; 14 | 15 | char characters[]="Hello World"; 16 | 17 | cout< 7 | using namespace std; 8 | 9 | void multiplyBy(int *, int); 10 | void multiplyArrayBy(int *, int, int); 11 | int main() { 12 | 13 | int a = 10; 14 | 15 | multiplyBy(&a,5); 16 | 17 | cout<>total_people; 18 | cout<<"Enter the total number of prizes : "<>total_winners; 20 | lottery(total_people,total_winners); 21 | return 0; 22 | } 23 | 24 | void lottery(int total_balls, int balls_to_draw) 25 | { 26 | if(total_balls < balls_to_draw) { 27 | cout<<"Draw not possible"; 28 | return; 29 | } 30 | 31 | cout<<"The winners are : "< 7 | using namespace std; 8 | 9 | int main() { 10 | 11 | double val=5.5; 12 | 13 | cout<(val)<(ch)< 7 | using namespace std; 8 | 9 | struct personalData { 10 | string name; 11 | string address; 12 | string contactNo; 13 | short age; 14 | }; 15 | 16 | void modify(personalData *); 17 | 18 | int main() { 19 | 20 | personalData person[5]; 21 | 22 | person[0].name = "Tridib Samanta"; 23 | person[0].address = "Kolkata"; 24 | person[0].contactNo = "+91 9000000000"; 25 | person[0].age = 21; 26 | 27 | person[1].name = "Nikola Tesla"; 28 | 29 | cout<name<name<name<name="Peter"; 56 | } 57 | -------------------------------------------------------------------------------- /CPP031_Classes.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Author: Tridib Samanta 3 | * Created: 02.02.2020 4 | **/ 5 | 6 | #include 7 | using namespace std; 8 | 9 | class MyDetails { 10 | 11 | private: 12 | short age; 13 | int *p; 14 | public: 15 | 16 | MyDetails() //Constructor 17 | { 18 | static int i=0; 19 | cout<<"This constructor has been just invoked "<<++i<<" time"< 7 | using namespace std; 8 | 9 | class StaticFunction 10 | { 11 | public: 12 | static int my_var; 13 | static void display() 14 | { 15 | cout<<"Static member function"< 7 | using namespace std; 8 | 9 | class Demo 10 | { 11 | public: 12 | void showMessage() 13 | { 14 | cout<<"Hello World inside showMessage()"< 7 | using namespace std; 8 | class Box 9 | { 10 | private: 11 | int length; 12 | public: 13 | Box(): length(0) { } 14 | friend int printLength(Box); 15 | }; 16 | int printLength(Box b) 17 | { 18 | b.length += 10; 19 | return b.length; 20 | } 21 | int main() 22 | { 23 | Box b; 24 | cout<<"Length of box: "<< printLength(b)< 10 | using namespace std; 11 | 12 | class XYZ { 13 | 14 | private: 15 | string message ="Hello Tridib"; 16 | int pin = 700150; 17 | public: 18 | /* This statement would make class ABC 19 | * a friend class of XYZ, this means that 20 | * ABC can access the private and protected 21 | * members of XYZ class. 22 | */ 23 | friend class ABC; 24 | }; 25 | 26 | class ABC { 27 | public: 28 | void disp(XYZ obj){ 29 | cout< 13 | using namespace std; 14 | class Samplecopyconstructor 15 | { 16 | private: 17 | int x, y; //data members 18 | 19 | public: 20 | Samplecopyconstructor(int x1, int y1) 21 | { 22 | x = x1; 23 | y = y1; 24 | } 25 | 26 | /* Copy constructor */ 27 | Samplecopyconstructor (const Samplecopyconstructor &sam) 28 | { 29 | x = sam.x; 30 | y = sam.y; 31 | } 32 | 33 | void display() 34 | { 35 | cout< 16 | using namespace std; 17 | 18 | class MinusOverload { 19 | private: 20 | int a; 21 | int b; 22 | 23 | public: 24 | void Distance() 25 | { 26 | a = 0; 27 | b = 0; 28 | } 29 | 30 | MinusOverload(int f, int i) 31 | { 32 | int c; 33 | a = f; 34 | b = i; 35 | c = a - b; 36 | cout << "\nC:" << c< 7 | using namespace std; 8 | 9 | class A 10 | { 11 | public: 12 | virtual void display() 13 | { 14 | cout << "Base class is invoked"<display(); //Late Binding occurs 31 | } 32 | -------------------------------------------------------------------------------- /CPP039_AbstractClass_PureVirtualFunction.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Author: Tridib Samanta 3 | * Created: 09.02.2020 4 | **/ 5 | 6 | /* 7 | One important thing to note is that, you should override the pure virtual function of the base class in the derived class. 8 | If you fail the override it, the derived class will become an abstract class as well. 9 | */ 10 | 11 | #include 12 | using namespace std; 13 | // Abstract class 14 | class Shape 15 | { 16 | protected: 17 | float l; 18 | public: 19 | void getData() 20 | { 21 | cin >> l; 22 | } 23 | 24 | // virtual Function 25 | virtual float calculateArea() = 0; 26 | }; 27 | class Square : public Shape 28 | { 29 | public: 30 | float calculateArea() 31 | { return l*l; } 32 | }; 33 | class Circle : public Shape 34 | { 35 | public: 36 | float calculateArea() 37 | { return 3.14*l*l; } 38 | }; 39 | int main() 40 | { 41 | Square s; 42 | Circle c; 43 | cout << "Enter length to calculate the area of a square: "; 44 | s.getData(); 45 | cout<<"Area of square: " << s.calculateArea(); 46 | cout<<"\nEnter radius to calculate the area of a circle: "; 47 | c.getData(); 48 | cout << "Area of circle: " << c.calculateArea(); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /CPP040_Exception_Handling.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Author: Tridib Samanta 3 | * Created: 03.03.2020 4 | **/ 5 | 6 | #include 7 | using namespace std; 8 | 9 | class DivisionByZeroException 10 | { 11 | public: 12 | void getErrorMessage() 13 | { 14 | cout<<"Error : Do not divide by zero !"< 7 | using namespace std; 8 | 9 | namespace A 10 | { 11 | int var = 20; 12 | } 13 | 14 | namespace B 15 | { 16 | int var = 40; 17 | } 18 | 19 | int main() { 20 | int var = 50; 21 | //Accessing var from namespace std 22 | cout<