├── .vscode ├── settings.json └── tasks.json ├── 01 C++ Overview ├── Helloword.cpp ├── Helloword.exe ├── README.md └── output │ └── Helloword.exe ├── 02 C++ Comments ├── Comments.cpp ├── Comments.exe └── README.md ├── 03 C++ Variable ├── Constants_Literals.cpp ├── README.md ├── Variable.cpp ├── VariableExample.cpp └── VariableExample.exe ├── 04 C++ Data Types ├── Datatype.cpp ├── Example1 ├── Example1.cpp └── README.md ├── 05 C++ Basic Input ├── README.md ├── UserInput.cpp └── UserInput.exe ├── 06 C++ Storage Classes ├── README.md ├── SC.cpp └── StorageClasses.cpp ├── 07 C++ Operators ├── ArithmeticOperators ├── ArithmeticOperators.cpp ├── AssignmentOperators ├── AssignmentOperators.cpp ├── LogicalOperators ├── LogicalOperators.cpp ├── LogicalOperators.exe ├── Operators ├── Operators.cpp ├── README.md ├── RelationalOperators └── RelationalOperators.cpp ├── 08 C++ Loop ├── For Loop │ ├── calculate_sum.cpp │ ├── forloop1 │ ├── forloop1.cpp │ ├── multiplication_table.cpp │ ├── print_numbers.cpp │ ├── simple_pattern.cpp │ ├── whileloop │ └── whileloop.cpp ├── README.md └── While Loop │ ├── counting_numbers.cpp │ ├── factorial_calculation.cpp │ ├── password_validation.cpp │ └── sum_of_numbers.cpp ├── 09 C++ Decision Making ├── README.md ├── if_else │ ├── FindSmallest3Number.cpp │ ├── SmallestNumber.cpp │ ├── SmallestNumber.exe │ ├── even_odd_check.cpp │ ├── leap_year_check.cpp │ ├── positive_negative_zero.cpp │ └── positive_negative_zero_ternary.cpp └── switch │ ├── menu_selection.cpp │ ├── GradingSystem.cpp │ ├── GradingSystem.exe │ ├── day_of_week │ ├── day_of_week.cpp │ ├── season_of_year.cpp │ └── simple_calculator.cpp ├── 10 C++ Functions ├── Example01 ├── Example01.cpp ├── Function.cpp ├── Function.exe ├── README.md ├── addition_function.cpp ├── factorial_function.cpp ├── maximum_function.cpp └── power_function.cpp ├── 11 C++ Numbers ├── MathLibraryFunctions.cpp ├── MathLibraryFunctions.exe ├── README.md ├── arithmetic_operations.cpp ├── input_output_numbers.cpp ├── random_number_generation.cpp └── type_conversion.cpp ├── 12 C++ Arrays ├── Multi Array │ ├── 2d_array_initialization.cpp │ ├── 3d_array.cpp │ ├── MultiArray.cpp │ ├── MultiArray.exe │ ├── dynamic_2d_array.cpp │ └── matrix_addition.cpp ├── README.md └── Single Array │ ├── Array_Single.cpp │ ├── Array_Single.exe │ ├── CharArray │ ├── CharArray.cpp │ ├── IntArray │ ├── IntArray.cpp │ ├── SingleArray.cpp │ ├── SingleArray.exe │ ├── array_initialization.cpp │ ├── array_modification.cpp │ ├── array_operations.cpp │ ├── array_search.cpp │ ├── example1 │ ├── example1.cpp │ ├── userInputArray │ └── userInputArray.cpp ├── 13 C++ Strings ├── README.md ├── StringExample1 ├── StringExample1.cpp ├── string_comparison ├── string_comparison.cpp ├── string_concatenation ├── string_concatenation.cpp └── string_length_access.cpp ├── 14 C++ Pointers ├── Array_pointer01.cpp ├── README.md ├── array_and_pointer.cpp ├── basic_pointer_example.cpp ├── dynamic_memory_allocation.cpp └── function_with_pointers.cpp ├── 15 C++ Date & Time ├── Example ├── Example.cpp └── README.md ├── 16 C++ Data Structures └── README.md ├── 17 C++ Object Oriented ├── Example │ ├── Employee │ └── Employee.cpp └── README.md ├── 18 C++ Classes & Objects ├── Class_object │ ├── Example01.cpp │ └── Example01.exe ├── Example │ ├── Client.cpp │ ├── Client.exe │ ├── CreditCard │ └── CreditCard.cpp ├── README.md └── constructor ├── 19 C++ Inheritance ├── Example_01.cpp ├── Example_01.exe ├── HierarchicalInheritance.cpp ├── HierarchicalInheritance │ ├── Car │ └── Car.cpp ├── Hybrid inheritance │ └── books.cpp ├── HybridInheritance.cpp ├── MultilevelInheritance.cpp ├── MultilevelInheritance │ ├── Student │ └── Student.cpp ├── MultipleInheritance │ ├── Food │ └── Food.cpp ├── README.md ├── SingleInheritance │ ├── Client │ └── Client.cpp └── Single_inheritance.cpp ├── 20 C++ Overloading ├── ConstructorOverloading.cpp └── README.md ├── 21 C++ Polymorphism ├── Example1.cpp ├── README.md └── example2.cpp ├── 22 C++ Abstraction ├── AbstractClasses.cpp ├── AbstractClasses1.cpp └── README.md ├── 23 C++ Encapsulation ├── CreditCard.cpp ├── CreditCard.exe ├── Employee.cpp ├── Employee.exe ├── Encapsulation.cpp ├── EncapsulationExample.cpp ├── EncapsulationExample.exe └── README.md ├── 24 C++ Interfaces ├── Example2.cpp ├── InterfaceExample.cpp └── README.md ├── 25 C++ Files and Streams ├── Create_Write_To_File.cpp ├── README.md └── Read_To_File.cpp ├── Example ├── Array │ ├── MultiArray │ ├── MultiArray.cpp │ ├── SingleArray │ ├── SingleArray.cpp │ ├── userInputArray │ └── userInputArray.cpp ├── Basic │ ├── helloword │ ├── helloword.cpp │ ├── user_input │ ├── user_input.cpp │ ├── variable │ └── variable.cpp ├── Class_object │ ├── CreditCard.cpp │ ├── Employee │ └── Employee.cpp ├── Datatype ├── Datatype.cpp ├── Encapsulation │ ├── Customers │ └── Customers.cpp ├── Inheritance │ ├── Customers │ ├── Customers.cpp │ ├── MultilevelInheritance │ ├── MultilevelInheritance.cpp │ ├── SingleInheritance.cpp │ └── SingleInheritance.exe ├── Lecture01 │ ├── Datalimit.md │ ├── UserInput │ ├── UserInput.cpp │ ├── basic │ ├── basic.cpp │ ├── basic.dSYM │ │ └── Contents │ │ │ ├── Info.plist │ │ │ └── Resources │ │ │ └── DWARF │ │ │ └── basic │ ├── variable │ └── variable.cpp ├── Methods │ ├── With_Arg_WithOut_Return │ ├── With_Arg_WithOut_Return.cpp │ ├── With_Arg_With_Return.cpp │ ├── Without_Arg_With_Return │ ├── Without_Arg_With_Return.cpp │ ├── Without_Arg_Without_Return │ └── Without_Arg_Without_Return.cpp ├── OPPS │ ├── Clients │ └── Clients.cpp ├── Operator ├── Operator.cpp ├── Variables ├── Variables.cpp ├── basic.cpp ├── if_else ├── if_else.cpp ├── if_else_ │ ├── if_else │ └── if_else.cpp ├── output │ ├── Loop │ ├── Loop.cpp │ ├── SwitchCase │ ├── SwitchCase.cpp │ ├── output │ │ ├── SwitchCase │ │ └── SwitchCase.dSYM │ │ │ └── Contents │ │ │ ├── Info.plist │ │ │ └── Resources │ │ │ └── DWARF │ │ │ └── SwitchCase │ ├── userInput │ └── userInput.dSYM │ │ └── Contents │ │ ├── Info.plist │ │ └── Resources │ │ └── DWARF │ │ └── userInput └── userInput.cpp ├── Questions ├── Question.md ├── Question5 ├── Question5.cpp ├── Question_4 ├── Question_4.cpp ├── Question_5 ├── Question_5.cpp ├── Question_Array01 ├── Question_Array01.cpp ├── Question_Array01.dSYM │ └── Contents │ │ ├── Info.plist │ │ └── Resources │ │ └── DWARF │ │ └── Question_Array01 ├── Question_a_1 ├── Question_a_1.cpp ├── Question_a_11 ├── Question_a_11.cpp ├── Question_a_2 ├── Question_a_2.cpp ├── Question_a_3 ├── Question_a_3.cpp └── Question_a_3.dSYM │ └── Contents │ ├── Info.plist │ └── Resources │ └── DWARF │ └── Question_a_3 └── README.md /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "cmake.sourceDirectory": "/workspaces/Learn-CPP/Operator Overloading", 3 | "files.associations": { 4 | "fstream": "cpp", 5 | "iostream": "cpp", 6 | "array": "cpp", 7 | "atomic": "cpp", 8 | "bit": "cpp", 9 | "*.tcc": "cpp", 10 | "cctype": "cpp", 11 | "chrono": "cpp", 12 | "clocale": "cpp", 13 | "cmath": "cpp", 14 | "cstdarg": "cpp", 15 | "cstddef": "cpp", 16 | "cstdint": "cpp", 17 | "cstdio": "cpp", 18 | "cstdlib": "cpp", 19 | "ctime": "cpp", 20 | "cwchar": "cpp", 21 | "cwctype": "cpp", 22 | "deque": "cpp", 23 | "unordered_map": "cpp", 24 | "vector": "cpp", 25 | "exception": "cpp", 26 | "algorithm": "cpp", 27 | "functional": "cpp", 28 | "iterator": "cpp", 29 | "memory": "cpp", 30 | "memory_resource": "cpp", 31 | "numeric": "cpp", 32 | "optional": "cpp", 33 | "random": "cpp", 34 | "ratio": "cpp", 35 | "string": "cpp", 36 | "string_view": "cpp", 37 | "system_error": "cpp", 38 | "tuple": "cpp", 39 | "type_traits": "cpp", 40 | "utility": "cpp", 41 | "initializer_list": "cpp", 42 | "iosfwd": "cpp", 43 | "istream": "cpp", 44 | "limits": "cpp", 45 | "new": "cpp", 46 | "ostream": "cpp", 47 | "sstream": "cpp", 48 | "stdexcept": "cpp", 49 | "streambuf": "cpp", 50 | "typeinfo": "cpp", 51 | "cstring": "cpp", 52 | "cinttypes": "cpp" 53 | } 54 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: g++ build active file", 6 | "command": "/usr/bin/g++", 7 | "args": [ 8 | "-fdiagnostics-color=always", 9 | "-g", 10 | "${file}", 11 | "-o", 12 | "${fileDirname}/${fileBasenameNoExtension}" 13 | ], 14 | "options": { 15 | "cwd": "${fileDirname}" 16 | }, 17 | "problemMatcher": [ 18 | "$gcc" 19 | ], 20 | "group": { 21 | "kind": "build", 22 | "isDefault": true 23 | }, 24 | "detail": "Task generated by Debugger." 25 | } 26 | ], 27 | "version": "2.0.0" 28 | } -------------------------------------------------------------------------------- /01 C++ Overview/Helloword.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // print hello word - codes with pankaj 6 | 7 | int main() 8 | { 9 | cout << "Welcome to codes with pankaj" << endl; 10 | // print number 11 | cout << 123 << endl; 12 | return 0; 13 | } -------------------------------------------------------------------------------- /01 C++ Overview/Helloword.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/01 C++ Overview/Helloword.exe -------------------------------------------------------------------------------- /01 C++ Overview/README.md: -------------------------------------------------------------------------------- 1 | # C++ Overview - Codes with Pankaj 2 | 3 | ## Introduction 4 | 5 | Welcome to "Codes with Pankaj," your go-to resource for mastering programming concepts. In this tutorial, we'll delve into the fundamentals of C++, a powerful and versatile programming language. 6 | 7 | ### What is C++? 8 | 9 | C++ is a general-purpose programming language developed by Bjarne Stroustrup at Bell Labs in 1979. It is an extension of the C programming language with additional features like classes and objects, making it an object-oriented programming (OOP) language. C++ is widely used for developing system/software infrastructure, game development, embedded systems, and more. 10 | 11 | ### Why Learn C++? 12 | 13 | 1. **Versatility:** C++ is a versatile language used in various domains, from system programming to game development. 14 | 2. **Performance:** It allows low-level manipulation, making it efficient and suitable for performance-critical applications. 15 | 3. **Object-Oriented:** C++ supports object-oriented programming, making code organization and reuse more straightforward. 16 | 4. **Community and Resources:** With a vast community and numerous online resources, learning and problem-solving become more accessible. 17 | 18 | ## Setting Up Your Development Environment 19 | 20 | Before diving into coding, let's set up your development environment. Follow these steps: 21 | 22 | 1. **Install a C++ Compiler:** Choose a C++ compiler suitable for your platform. Popular choices include GCC for Linux, MinGW for Windows, and Xcode for macOS. 23 | 24 | 2. **Choose an Integrated Development Environment (IDE):** Select an IDE that supports C++. Options like Visual Studio, Code::Blocks, and Eclipse are popular among C++ developers. 25 | 26 | 3. **Create Your First C++ Program:** Write a simple "Hello, World!" program to ensure your environment is set up correctly. 27 | 28 | ```cpp 29 | #include 30 | 31 | int main() { 32 | std::cout << "Hello, World!" << std::endl; 33 | return 0; 34 | } 35 | ``` 36 | 37 | ## Basic C++ Concepts 38 | 39 | ### 1. Variables and Data Types 40 | 41 | In C++, you declare variables to store data. Each variable has a data type, such as `int` for integers, `double` for floating-point numbers, and `char` for characters. 42 | 43 | ```cpp 44 | int age = 25; 45 | double pi = 3.14; 46 | char grade = 'A'; 47 | ``` 48 | 49 | ### 2. Control Flow 50 | 51 | C++ supports traditional control flow statements like `if`, `else`, `for`, and `while`. These help in decision-making and looping. 52 | 53 | ```cpp 54 | if (age >= 18) { 55 | std::cout << "You are eligible to vote." << std::endl; 56 | } else { 57 | std::cout << "You are not eligible to vote." << std::endl; 58 | } 59 | ``` 60 | 61 | ### 3. Functions 62 | 63 | Functions allow you to encapsulate a set of instructions for reuse. Here's a simple function to add two numbers: 64 | 65 | ```cpp 66 | int add(int a, int b) { 67 | return a + b; 68 | } 69 | ``` 70 | 71 | ## Conclusion 72 | 73 | This is just the beginning of your C++ journey. In the upcoming tutorials, we'll explore more advanced concepts, including classes, inheritance, and polymorphism. 74 | 75 | Stay tuned to "Codes with Pankaj" for an in-depth exploration of C++ and other programming languages. Happy coding! 76 | 77 | --- 78 | -------------------------------------------------------------------------------- /01 C++ Overview/output/Helloword.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/01 C++ Overview/output/Helloword.exe -------------------------------------------------------------------------------- /02 C++ Comments/Comments.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // This is a single-line comment 4 | 5 | /* 6 | This is a multi-line comment. 7 | You can write multiple lines here. 8 | 9 | Visit my tutorial website for more C++ tutorials: www.codeswithpankaj.com 10 | */ 11 | 12 | int main() { 13 | // Display a message on the console 14 | cout << "Hello, CodesWithPankaj!\n"; // You can also add comments at the end of a line 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /02 C++ Comments/Comments.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/02 C++ Comments/Comments.exe -------------------------------------------------------------------------------- /02 C++ Comments/README.md: -------------------------------------------------------------------------------- 1 | ### Topic: C++ Comments 2 | 3 | #### Introduction: 4 | Comments in C++ are essential for enhancing code readability and providing insights into the code's functionality. They are ignored by the compiler and serve as notes for developers. This tutorial covers the basics of C++ comments along with examples. 5 | 6 | #### Single-line Comments: 7 | Single-line comments start with `//` and extend to the end of the line. They are useful for adding brief explanations or annotations. 8 | 9 | ```cpp 10 | // This is a single-line comment 11 | int main() { 12 | // Code goes here 13 | return 0; 14 | } 15 | ``` 16 | 17 | #### Multi-line Comments: 18 | Multi-line comments begin with `/*` and end with `*/`. They are suitable for longer explanations or commenting out multiple lines of code. 19 | 20 | ```cpp 21 | /* 22 | This is a multi-line comment 23 | It can span multiple lines 24 | */ 25 | int main() { 26 | /* 27 | Code goes here 28 | */ 29 | return 0; 30 | } 31 | ``` 32 | 33 | #### Example: 34 | 35 | ```cpp 36 | #include 37 | 38 | // Function to add two numbers 39 | int add(int a, int b) { 40 | // This is an inline comment 41 | return a + b; 42 | } 43 | 44 | int main() { 45 | /* 46 | This program demonstrates the use of comments in C++ 47 | */ 48 | 49 | // Input numbers 50 | int num1 = 5; // First number 51 | int num2 = 7; // Second number 52 | 53 | // Calculate the sum 54 | int result = add(num1, num2); 55 | 56 | // Output the result 57 | std::cout << "The sum is: " << result << std::endl; 58 | 59 | // Visit my website for more tutorials: https://www.codeswithpankaj.com/ 60 | 61 | return 0; 62 | } 63 | ``` 64 | 65 | #### Additional Resources: 66 | - Learn more about C++ comments: [Codes with Pankaj](https://www.codeswithpankaj.com/c++-comments) 67 | 68 | Feel free to explore more tutorials on [Codes with Pankaj](https://www.codeswithpankaj.com/) for comprehensive programming guidance. -------------------------------------------------------------------------------- /03 C++ Variable/Constants_Literals.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Include cmath for mathematical constants 3 | 4 | int main() { 5 | // Integer constant 6 | const int MY_CONSTANT_INT = 42; 7 | 8 | // Floating-point constants 9 | const double PI = 3.141592653589793; 10 | const float EULER_NUMBER = 2.71828f; 11 | 12 | // Character constant 13 | const char MY_CONSTANT_CHAR = 'A'; 14 | 15 | // Boolean constants 16 | const bool MY_TRUE_CONSTANT = true; 17 | const bool MY_FALSE_CONSTANT = false; 18 | 19 | // Mathematical constants from cmath 20 | const double SQRT_TWO = std::sqrt(2.0); 21 | const double EULER_CONSTANT = std::exp(1.0); 22 | 23 | // String literal 24 | const char* WEBSITE_NAME = "www.codeswithpankaj.com"; 25 | 26 | // Displaying constant values 27 | std::cout << "Integer Constant: " << MY_CONSTANT_INT << std::endl; 28 | std::cout << "Pi: " << PI << std::endl; 29 | std::cout << "Euler's Number: " << EULER_NUMBER << std::endl; 30 | std::cout << "Character Constant: " << MY_CONSTANT_CHAR << std::endl; 31 | std::cout << "True Constant: " << std::boolalpha << MY_TRUE_CONSTANT << std::endl; 32 | std::cout << "False Constant: " << std::boolalpha << MY_FALSE_CONSTANT << std::endl; 33 | std::cout << "Square Root of 2: " << SQRT_TWO << std::endl; 34 | std::cout << "Euler's Constant: " << EULER_CONSTANT << std::endl; 35 | std::cout << "Website Name: " << WEBSITE_NAME << std::endl; 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /03 C++ Variable/README.md: -------------------------------------------------------------------------------- 1 | # Variable Types 2 | 3 | **Codes with Pankaj - C++ Variable Types with Examples** 4 | 5 | Introduction: 6 | Welcome to "Codes with Pankaj," your coding companion for unraveling the intricacies of C++. In this tutorial, we'll delve into the world of C++ variable types, exploring the different kinds of variables and providing practical examples to solidify your understanding. Whether you're a novice or a seasoned programmer, understanding variable types is fundamental for writing expressive and dynamic C++ code. 7 | 8 | Let's embark on this coding journey together with Pankaj! 9 | 10 | **1. Local Variables:** 11 | Local variables are declared within a specific block of code and are only accessible within that block. Explore the concept through practical examples. 12 | 13 | ```cpp 14 | #include 15 | 16 | int main() { 17 | // Local variable example 18 | int localVariable = 42; 19 | 20 | std::cout << "Local Variable: " << localVariable << std::endl; 21 | 22 | return 0; 23 | } 24 | ``` 25 | 26 | **2. Global Variables:** 27 | Global variables are declared outside of any function and can be accessed throughout the entire program. Learn about their scope and usage. 28 | 29 | ```cpp 30 | #include 31 | 32 | // Global variable example 33 | int globalVariable = 100; 34 | 35 | int main() { 36 | std::cout << "Global Variable: " << globalVariable << std::endl; 37 | 38 | return 0; 39 | } 40 | ``` 41 | 42 | **3. Static Variables:** 43 | Static variables maintain their values between function calls. Explore the concept with examples demonstrating their persistence. 44 | 45 | ```cpp 46 | #include 47 | 48 | void staticVariableExample() { 49 | // Static variable example 50 | static int staticVar = 0; 51 | staticVar++; 52 | 53 | std::cout << "Static Variable: " << staticVar << std::endl; 54 | } 55 | 56 | int main() { 57 | staticVariableExample(); 58 | staticVariableExample(); 59 | staticVariableExample(); 60 | 61 | return 0; 62 | } 63 | ``` 64 | 65 | **4. Const Variables:** 66 | Constants are variables whose values cannot be changed once assigned. Learn about const variables and their importance in creating robust and maintainable code. 67 | 68 | ```cpp 69 | #include 70 | 71 | int main() { 72 | // Const variable example 73 | const double pi = 3.14159; 74 | 75 | std::cout << "Value of Pi: " << pi << std::endl; 76 | 77 | return 0; 78 | } 79 | ``` 80 | 81 | **5. Volatile Variables:** 82 | The `volatile` keyword indicates that a variable's value might be changed by external factors. Understand the significance of volatile variables through examples. 83 | 84 | ```cpp 85 | #include 86 | 87 | int main() { 88 | // Volatile variable example 89 | volatile int sensorValue = 0; 90 | 91 | std::cout << "Sensor Value: " << sensorValue << std::endl; 92 | 93 | return 0; 94 | } 95 | ``` 96 | 97 | **Conclusion:** 98 | Congratulations! You've explored various variable types in C++ through practical examples. Understanding these concepts will empower you to write more flexible and robust code. Stay tuned for more exciting coding adventures with Pankaj! -------------------------------------------------------------------------------- /03 C++ Variable/Variable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | // Integer variables 6 | int numberOfTutorials = 50; 7 | int tutorialViews = 1000000; 8 | 9 | // Floating-point variables 10 | double averageRating = 4.5; 11 | 12 | // Character variable 13 | char websiteInitial = 'C'; 14 | 15 | // Boolean variable 16 | bool isCplusplusAwesome = true; 17 | 18 | // String variable from the C++ Standard Library 19 | std::string websiteName = "CodesWithPankaj"; 20 | std::string websiteURL = "www.codeswithpankaj.com"; 21 | 22 | // Displaying variable values 23 | std::cout << "Number of Tutorials: " << numberOfTutorials << std::endl; 24 | std::cout << "Tutorial Views: " << tutorialViews << std::endl; 25 | std::cout << "Average Rating: " << averageRating << std::endl; 26 | std::cout << "Website Initial: " << websiteInitial << std::endl; 27 | std::cout << "Is C++ Awesome? " << std::boolalpha << isCplusplusAwesome << std::endl; 28 | std::cout << "Website Name: " << websiteName << std::endl; 29 | std::cout << "Website URL: " << websiteURL << std::endl; 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /03 C++ Variable/VariableExample.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | string name = "Nishant"; 9 | int age = 25; 10 | float height = 5.8; 11 | char gender = 'M'; 12 | bool isStudent = true; 13 | 14 | cout<<" My Name is : "< 2 | #include 3 | 4 | int main() { 5 | // Integer data type 6 | int myInt = 42; 7 | 8 | // Floating-point data types 9 | double myDouble = 3.14; 10 | float myFloat = 2.718f; // 'f' suffix denotes a float 11 | 12 | // Character data type 13 | char myChar = 'A'; 14 | 15 | // Boolean data type 16 | bool myBool = true; 17 | 18 | // Enumeration data type 19 | enum Color { RED, GREEN, BLUE }; 20 | Color myColor = RED; 21 | 22 | // Pointer data type 23 | int* myIntPtr = nullptr; // Pointer to an integer 24 | 25 | // Array data type 26 | int myArray[5] = {1, 2, 3, 4, 5}; 27 | 28 | // String data type from the C++ Standard Library 29 | std::string myString = "Hello, CodesWithPankaj! Visit us at www.codeswithpankaj.com"; 30 | 31 | // Displaying values 32 | std::cout << "Integer: " << myInt << std::endl; 33 | std::cout << "Double: " << myDouble << std::endl; 34 | std::cout << "Float: " << myFloat << std::endl; 35 | std::cout << "Char: " << myChar << std::endl; 36 | std::cout << "Bool: " << std::boolalpha << myBool << std::endl; // boolalpha for displaying true/false 37 | std::cout << "Color: " << myColor << std::endl; // Prints the enumeration value 38 | std::cout << "Pointer: " << myIntPtr << std::endl; // Prints the pointer address 39 | std::cout << "Array: " << myArray[2] << std::endl; // Accessing array element 40 | std::cout << "String: " << myString << std::endl; 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /04 C++ Data Types/Example1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/04 C++ Data Types/Example1 -------------------------------------------------------------------------------- /04 C++ Data Types/Example1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | // Integer data type 7 | int myint = 20; 8 | 9 | cout<<"Int = "<` library. We’ll cover the basics, provide an example, and include a practice question to reinforce the concept. By the end, you’ll understand how to read different types of input from users in C++. 4 | 5 | --- 6 | 7 | ## 1. Introduction to User Input in C++ 8 | 9 | In C++, the standard way to take user input is by using the `cin` object, which is part of the `` library. The `cin` object reads input from the standard input stream (usually the keyboard) and stores it in variables. 10 | 11 | To use `cin`, you need to: 12 | - Include the `` header. 13 | - Use the `std` namespace (either by adding `using namespace std;` or by prefixing with `std::`). 14 | - Declare a variable to store the input. 15 | 16 | The general syntax for `cin` is: 17 | 18 | ```cpp 19 | cin >> variable; 20 | ``` 21 | 22 | You can read multiple inputs in a single line by chaining the `>>` operator: 23 | 24 | ```cpp 25 | cin >> variable1 >> variable2; 26 | ``` 27 | 28 | --- 29 | 30 | ## 2. Steps to Take User Input 31 | 32 | 1. **Include the necessary header**: 33 | Add `#include ` at the top of your program. 34 | 35 | 2. **Use the `std` namespace**: 36 | Either include `using namespace std;` or use `std::` before `cin` and `cout`. 37 | 38 | 3. **Declare a variable**: 39 | Define a variable of the appropriate data type (e.g., `int`, `float`, `string`) to store the input. 40 | 41 | 4. **Prompt the user**: 42 | Use `cout` to display a message asking for input. 43 | 44 | 5. **Read the input**: 45 | Use `cin` to capture the user’s input and store it in the variable. 46 | 47 | --- 48 | 49 | ## 3. Example Program 50 | 51 | Below is a C++ program that demonstrates how to take user input for different data types (integer, float, and string) and display them. 52 | 53 | ```cpp 54 | #include 55 | #include // Required for string data type 56 | using namespace std; 57 | 58 | int main() { 59 | // Declare variables to store input 60 | int age; 61 | float height; 62 | string name; 63 | 64 | // Prompt user for input 65 | cout << "Enter your name: "; 66 | getline(cin, name); // Use getline for strings with spaces 67 | 68 | cout << "Enter your age: "; 69 | cin >> age; 70 | 71 | cout << "Enter your height (in meters): "; 72 | cin >> height; 73 | 74 | // Display the input 75 | cout << "\n--- User Details ---" << endl; 76 | cout << "Name: " << name << endl; 77 | cout << "Age: " << age << " years" << endl; 78 | cout << "Height: " << height << " meters" << endl; 79 | 80 | return 0; 81 | } 82 | ``` 83 | 84 | ### Explanation of the Example 85 | - **Headers**: We include `` for `cin` and `cout`, and `` for the `string` data type. 86 | - **Namespace**: `using namespace std;` simplifies the use of `cin`, `cout`, and `string`. 87 | - **Input**: 88 | - `getline(cin, name)` is used for the string input to capture the full name, including spaces. 89 | - `cin >> age` and `cin >> height` read integer and float inputs, respectively. 90 | - **Output**: The program displays the entered details using `cout`. 91 | 92 | ### Sample Output 93 | ``` 94 | Enter your name: Pankaj Sharma 95 | Enter your age: 25 96 | Enter your height (in meters): 1.75 97 | 98 | --- User Details --- 99 | Name: Pankaj Sharma 100 | Age: 25 years 101 | Height: 1.75 meters 102 | ``` 103 | 104 | --- 105 | 106 | ## 4. Handling Different Input Types 107 | 108 | Here’s how to handle common data types with `cin`: 109 | 110 | - **Integer (`int`)**: `cin >> variable;` (e.g., `int num; cin >> num;`) 111 | - **Float/Double (`float`, `double`)**: `cin >> variable;` (e.g., `float price; cin >> price;`) 112 | - **String (`string`)**: 113 | - For single words: `cin >> variable;` (e.g., `string word; cin >> word;`) 114 | - For full lines (including spaces): `getline(cin, variable);` 115 | - **Character (`char`)**: `cin >> variable;` (e.g., `char initial; cin >> initial;`) 116 | 117 | **Note**: When using `cin` followed by `getline`, you may need to clear the input buffer using `cin.ignore()` to avoid skipping the `getline` input. Example: 118 | 119 | ```cpp 120 | int num*. 121 | cin >> num; 122 | cin.ignore(); // Clear the newline character from the input buffer 123 | string name; 124 | getline(cin, name); 125 | ``` 126 | 127 | --- 128 | 129 | ## 5. Practice Question (codeswithpankaj.com) 130 | 131 | **Question**: Write a C++ program to take user input for the following: 132 | - A student’s name (full name, including spaces). 133 | - Their roll number (integer). 134 | - Their percentage marks (float). 135 | The program should calculate and display whether the student has passed (marks >= 40) or failed, along with their details. 136 | 137 | **Instructions**: 138 | - Use `getline` for the name. 139 | - Display the result in the format: 140 | ``` 141 | Student Name: [name] 142 | Roll Number: [roll] 143 | Percentage: [marks]% 144 | Result: [Pass/Fail] 145 | ``` 146 | - Share your solution at **codeswithpankaj.com** or in the comments section! 147 | 148 | **Sample Solution** (Try writing your own before checking!): 149 | 150 | ```cpp 151 | #include 152 | #include 153 | using namespace std; 154 | 155 | int main() { 156 | string name; 157 | int roll; 158 | float marks; 159 | 160 | cout << "Enter student name: "; 161 | getline(cin, name); 162 | cout << "Enter roll number: "; 163 | cin >> roll; 164 | cout << "Enter percentage marks: "; 165 | cin >> marks; 166 | 167 | cout << "\nStudent Name: " << name << endl; 168 | cout << "Roll Number: " << roll << endl; 169 | cout << "Percentage: " << marks << "%" << endl; 170 | cout << "Result: " << (marks >= 40 ? "Pass" : "Fail") << endl; 171 | 172 | return 0; 173 | } 174 | ``` 175 | 176 | --- 177 | 178 | ## 6. Conclusion 179 | 180 | Taking user input in C++ is straightforward with `cin` and `getline`. By understanding how to handle different data types and clear the input buffer when needed, you can create interactive programs. Practice with the question above to solidify your skills! 181 | 182 | For more C++ tutorials and coding challenges, visit **codeswithpankaj.com**! 183 | 184 | --- -------------------------------------------------------------------------------- /05 C++ Basic Input/UserInput.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | string name; 9 | int age; 10 | float height; 11 | cout << "Enter your name: "; 12 | cin >> name; 13 | 14 | cout << "Enter your age: "; 15 | cin >> age; 16 | cout << "Enter your height: "; 17 | cin >> height; 18 | cout << "Hello, " << name << " you are " << age << " years old and " << height << " feet tall." << endl; 19 | 20 | } -------------------------------------------------------------------------------- /05 C++ Basic Input/UserInput.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/05 C++ Basic Input/UserInput.exe -------------------------------------------------------------------------------- /06 C++ Storage Classes/SC.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function declaration 4 | void displayMessage(); 5 | 6 | int main() { 7 | // Automatic storage class 8 | int localVar = 5; 9 | 10 | // Static storage class 11 | static int staticVar = 10; 12 | 13 | // External linkage variable 14 | extern int globalVar; 15 | 16 | // Displaying values 17 | std::cout << "Local Variable: " << localVar << std::endl; 18 | std::cout << "Static Variable: " << staticVar << std::endl; 19 | std::cout << "Global Variable: " << globalVar << std::endl; 20 | 21 | // Calling a function 22 | displayMessage(); 23 | 24 | return 0; 25 | } 26 | 27 | // Static variable definition 28 | static int staticVar = 20; 29 | 30 | // Global variable definition 31 | int globalVar = 30; 32 | 33 | // Definition of the function 34 | void displayMessage() { 35 | // Accessing the global variable 36 | std::cout << "Global Variable inside the function: " << globalVar << std::endl; 37 | 38 | 39 | std::cout << "Check out tutorials on C++ at www.codeswithpankaj.com" << std::endl; 40 | } 41 | -------------------------------------------------------------------------------- /06 C++ Storage Classes/StorageClasses.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Global variable with external linkage 4 | extern int globalVar; 5 | 6 | // Function declaration 7 | void displayMessage(); 8 | 9 | int main() { 10 | // Automatic storage class (default for local variables) 11 | auto int autoVar = 10; 12 | 13 | // Register storage class (requests compiler to store the variable in a register) 14 | register int registerVar = 20; 15 | 16 | // Static storage class (preserves the value between function calls) 17 | static int staticVar = 30; 18 | 19 | // External storage class (variable is declared in another file) 20 | extern int externVar; 21 | 22 | // Displaying values 23 | std::cout << "Auto Variable: " << autoVar << std::endl; 24 | std::cout << "Register Variable: " << registerVar << std::endl; 25 | std::cout << "Static Variable: " << staticVar << std::endl; 26 | std::cout << "Extern Variable from another file: " << externVar << std::endl; 27 | 28 | // Calling a function 29 | displayMessage(); 30 | 31 | return 0; 32 | } 33 | 34 | // Definition of the global variable 35 | int globalVar = 50; 36 | 37 | // Definition of the function 38 | void displayMessage() { 39 | // Accessing the global variable 40 | std::cout << "Global Variable inside the function: " << globalVar << std::endl; 41 | 42 | 43 | std::cout << "Visit " << "www.codeswithpankaj.com" << " for C++ tutorials." << std::endl; 44 | } 45 | -------------------------------------------------------------------------------- /07 C++ Operators/ArithmeticOperators: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/07 C++ Operators/ArithmeticOperators -------------------------------------------------------------------------------- /07 C++ Operators/ArithmeticOperators.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int a = 10, b = 5; 6 | 7 | // Addition 8 | int sum = a + b; 9 | cout << "Sum: " << sum << endl; 10 | 11 | // Subtraction 12 | int difference = a - b; 13 | cout << "Difference: " << difference << endl; 14 | 15 | // Multiplication 16 | int product = a * b; 17 | cout << "Product: " << product << endl; 18 | 19 | // Division 20 | int quotient = a / b; 21 | cout << "Quotient: " << quotient << endl; 22 | 23 | // Modulus (remainder) 24 | int remainder = a % b; 25 | cout << "Remainder: " << remainder << endl; 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /07 C++ Operators/AssignmentOperators: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/07 C++ Operators/AssignmentOperators -------------------------------------------------------------------------------- /07 C++ Operators/AssignmentOperators.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int a = 10, b = 5; 6 | 7 | // Simple assignment 8 | int c = a; 9 | cout << "c: " << c << endl; 10 | 11 | // Addition assignment 12 | // a = a + b 13 | a += b; 14 | cout << "a: " << a << endl; 15 | 16 | // Subtraction assignment 17 | // a = a - b 18 | a -= b; 19 | cout << "a: " << a << endl; 20 | 21 | // Multiplication assignment 22 | // a = a * b 23 | a *= b; 24 | cout << "a: " << a << endl; 25 | 26 | // Division assignment 27 | // a = a / b 28 | a /= b; 29 | cout << "a: " << a << endl; 30 | 31 | // Modulus assignment 32 | // a = a % b 33 | a %= b; 34 | cout << "a: " << a << endl; 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /07 C++ Operators/LogicalOperators: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/07 C++ Operators/LogicalOperators -------------------------------------------------------------------------------- /07 C++ Operators/LogicalOperators.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | bool x = true, y = false; 6 | 7 | // Logical AND both are ture then return true 8 | cout << (x && y) << endl; 9 | 10 | // Logical OR one ture 11 | cout << (x || y) << endl; 12 | 13 | // Logical NOT 14 | cout << !x << endl; 15 | 16 | cout<< (90 != 90) < 4 | 5 | int main() { 6 | // Arithmetic Operators: 7 | int a = 5, b = 3; 8 | int sum = a + b; // sum is now 8 9 | 10 | int difference = a - b; // difference is now 2 11 | 12 | int product = a * b; // product is now 15 13 | 14 | int quotient = a / b; // quotient is now 1 (integer division) 15 | 16 | int remainder = a % b; // remainder is now 2 17 | 18 | // Relational Operators: 19 | bool isEqual = (a == b); // isEqual is false 20 | 21 | bool isNotEqual = (a != b); // isNotEqual is true 22 | 23 | bool isGreaterThan = (a > b); // isGreaterThan is true 24 | 25 | bool isLessThan = (a < b); // isLessThan is false 26 | 27 | bool isGreaterOrEqual = (a >= b); // isGreaterOrEqual is true 28 | 29 | bool isLessOrEqual = (a <= b); // isLessOrEqual is false 30 | 31 | // Logical Operators: 32 | bool condition1 = true, condition2 = false; 33 | bool resultAND = (condition1 && condition2); // resultAND is false 34 | 35 | bool resultOR = (condition1 || condition2); // resultOR is true 36 | 37 | bool resultNOT = !condition1; // resultNOT is false 38 | 39 | // Displaying results: 40 | std::cout << "Sum: " << sum << std::endl; 41 | std::cout << "Difference: " << difference << std::endl; 42 | std::cout << "Product: " << product << std::endl; 43 | std::cout << "Quotient: " << quotient << std::endl; 44 | std::cout << "Remainder: " << remainder << std::endl; 45 | 46 | std::cout << "Is Equal? " << std::boolalpha << isEqual << std::endl; 47 | std::cout << "Is Not Equal? " << isNotEqual << std::endl; 48 | std::cout << "Is Greater Than? " << isGreaterThan << std::endl; 49 | std::cout << "Is Less Than? " << isLessThan << std::endl; 50 | std::cout << "Is Greater Or Equal? " << isGreaterOrEqual << std::endl; 51 | std::cout << "Is Less Or Equal? " << isLessOrEqual << std::endl; 52 | 53 | std::cout << "Result AND: " << resultAND << std::endl; 54 | std::cout << "Result OR: " << resultOR << std::endl; 55 | std::cout << "Result NOT: " << resultNOT << std::endl; 56 | 57 | return 0; 58 | } -------------------------------------------------------------------------------- /07 C++ Operators/RelationalOperators: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/07 C++ Operators/RelationalOperators -------------------------------------------------------------------------------- /07 C++ Operators/RelationalOperators.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int a = 10, b = 5; 6 | 7 | // Equal to 8 | cout << (a == b) << endl; 9 | 10 | // Not equal to 11 | cout << (a != b) << endl; 12 | 13 | // Greater than 14 | cout << (a > b) << endl; 15 | 16 | // Less than 17 | cout << (a < b) << endl; 18 | 19 | // Greater than or equal to 20 | cout << (a >= b) << endl; 21 | 22 | // Less than or equal to 23 | cout << (a <= b) << endl; 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /08 C++ Loop/For Loop/calculate_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int sum = 0; 5 | 6 | for (int i = 1; i <= 100; ++i) { 7 | sum += i; 8 | } 9 | 10 | std::cout << "Sum of numbers from 1 to 100: " << sum << "\n"; 11 | std::cout << "Visit my website: www.codeswithpankaj.com\n"; 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /08 C++ Loop/For Loop/forloop1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/08 C++ Loop/For Loop/forloop1 -------------------------------------------------------------------------------- /08 C++ Loop/For Loop/forloop1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | 8 | int a; 9 | 10 | for(a = 1; a <= 10; a++){ 11 | 12 | cout< 2 | 3 | int main() { 4 | int multiplier = 5; 5 | 6 | std::cout << "Multiplication Table of " << multiplier << ":\n"; 7 | 8 | for (int i = 1; i <= 10; ++i) { 9 | std::cout << multiplier << " * " << i << " = " << multiplier * i << "\n"; 10 | } 11 | 12 | std::cout << "Visit my website: www.codeswithpankaj.com\n"; 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /08 C++ Loop/For Loop/print_numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | for (int i = 1; i <= 10; ++i) { 5 | std::cout << i << " "; 6 | } 7 | 8 | std::cout << "\nVisit my website: www.codeswithpankaj.com\n"; 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /08 C++ Loop/For Loop/simple_pattern.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int rows = 5; 5 | 6 | std::cout << "Simple Pattern:\n"; 7 | 8 | for (int i = 1; i <= rows; ++i) { 9 | for (int j = 1; j <= i; ++j) { 10 | std::cout << "* "; 11 | } 12 | std::cout << "\n"; 13 | } 14 | 15 | std::cout << "Visit my website: www.codeswithpankaj.com\n"; 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /08 C++ Loop/For Loop/whileloop: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/08 C++ Loop/For Loop/whileloop -------------------------------------------------------------------------------- /08 C++ Loop/For Loop/whileloop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | 9 | int a = 1; 10 | 11 | while (a <= 10) 12 | { 13 | 14 | cout< 2 | 3 | int main() { 4 | int count = 1; 5 | 6 | while (count <= 10) { 7 | std::cout << count << " "; 8 | count++; 9 | } 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /08 C++ Loop/While Loop/factorial_calculation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int number; 5 | int factorial = 1; 6 | 7 | std::cout << "Enter a number: "; 8 | std::cin >> number; 9 | 10 | int i = 1; 11 | while (i <= number) { 12 | factorial *= i; 13 | i++; 14 | } 15 | 16 | std::cout << "Factorial of " << number << ": " << factorial << std::endl; 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /08 C++ Loop/While Loop/password_validation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | std::string password; 6 | 7 | std::cout << "Enter the password (at least 8 characters): "; 8 | 9 | while (true) { 10 | std::cin >> password; 11 | 12 | if (password.length() >= 8) { 13 | std::cout << "Password accepted." << std::endl; 14 | break; 15 | } else { 16 | std::cout << "Password is too short. Please enter at least 8 characters." << std::endl; 17 | } 18 | } 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /08 C++ Loop/While Loop/sum_of_numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int sum = 0; 5 | int number; 6 | 7 | std::cout << "Enter numbers (enter 0 to stop): "; 8 | 9 | while (true) { 10 | std::cin >> number; 11 | 12 | if (number == 0) { 13 | break; 14 | } 15 | 16 | sum += number; 17 | } 18 | 19 | std::cout << "Sum: " << sum << std::endl; 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /09 C++ Decision Making/README.md: -------------------------------------------------------------------------------- 1 | # Codes with Pankaj - C++ Decision Making Depth Tutorial 2 | 3 | Introduction: 4 | Welcome to another installment of "Codes with Pankaj." In this tutorial, we will explore the fascinating world of decision-making in C++. Decision-making structures allow your programs to make choices based on certain conditions. Whether you are a novice or seeking to enhance your programming skills, this tutorial will cover the fundamentals of decision-making in C++, complete with examples to solidify your understanding. 5 | 6 | ### Table of Contents: 7 | 8 | 1. **Introduction to Decision Making:** 9 | - Explanation of the importance of decision-making in programming. 10 | - Overview of decision-making constructs: `if`, `else if`, `else`. 11 | 12 | 2. **If Statement:** 13 | - Syntax and structure of the `if` statement. 14 | - Example: Checking if a number is positive or negative. 15 | 16 | ```cpp 17 | #include 18 | 19 | int main() { 20 | int number; 21 | 22 | std::cout << "If Statement Example:\n"; 23 | std::cout << "Enter a number: "; 24 | std::cin >> number; 25 | 26 | if (number > 0) { 27 | std::cout << "The number is positive.\n"; 28 | } 29 | 30 | return 0; 31 | } 32 | ``` 33 | 34 | 3. **If-Else Statement:** 35 | - Syntax and structure of the `if-else` statement. 36 | - Example: Determining if a number is even or odd. 37 | 38 | ```cpp 39 | #include 40 | 41 | int main() { 42 | int number; 43 | 44 | std::cout << "If-Else Statement Example:\n"; 45 | std::cout << "Enter a number: "; 46 | std::cin >> number; 47 | 48 | if (number % 2 == 0) { 49 | std::cout << "The number is even.\n"; 50 | } else { 51 | std::cout << "The number is odd.\n"; 52 | } 53 | 54 | return 0; 55 | } 56 | ``` 57 | 58 | 4. **Else-If Statement:** 59 | - Syntax and structure of the `else if` statement. 60 | - Example: Classifying exam scores into grades. 61 | 62 | ```cpp 63 | #include 64 | 65 | int main() { 66 | int score; 67 | 68 | std::cout << "Else-If Statement Example:\n"; 69 | std::cout << "Enter the exam score: "; 70 | std::cin >> score; 71 | 72 | if (score >= 90) { 73 | std::cout << "Grade: A\n"; 74 | } else if (score >= 80) { 75 | std::cout << "Grade: B\n"; 76 | } else if (score >= 70) { 77 | std::cout << "Grade: C\n"; 78 | } else { 79 | std::cout << "Grade: F\n"; 80 | } 81 | 82 | return 0; 83 | } 84 | ``` 85 | 86 | 5. **Switch Statement:** 87 | - Syntax and structure of the `switch` statement. 88 | - Example: Displaying the day of the week. 89 | 90 | ```cpp 91 | #include 92 | 93 | int main() { 94 | int day; 95 | 96 | std::cout << "Switch Statement Example:\n"; 97 | std::cout << "Enter a day (1-7): "; 98 | std::cin >> day; 99 | 100 | switch (day) { 101 | case 1: 102 | std::cout << "Monday\n"; 103 | break; 104 | case 2: 105 | std::cout << "Tuesday\n"; 106 | break; 107 | case 3: 108 | std::cout << "Wednesday\n"; 109 | break; 110 | case 4: 111 | std::cout << "Thursday\n"; 112 | break; 113 | case 5: 114 | std::cout << "Friday\n"; 115 | break; 116 | case 6: 117 | std::cout << "Saturday\n"; 118 | break; 119 | case 7: 120 | std::cout << "Sunday\n"; 121 | break; 122 | default: 123 | std::cout << "Invalid day\n"; 124 | } 125 | 126 | return 0; 127 | } 128 | ``` 129 | 130 | 6. **Nested Decision Making:** 131 | - Explanation of nested decision-making structures. 132 | - Example: Combining `if` and `switch` statements. 133 | 134 | ```cpp 135 | #include 136 | 137 | int main() { 138 | int number; 139 | 140 | std::cout << "Nested Decision Making Example:\n"; 141 | std::cout << "Enter a number: "; 142 | std::cin >> number; 143 | 144 | if (number > 0) { 145 | std::cout << "The number is positive. "; 146 | switch (number % 2) { 147 | case 0: 148 | std::cout << "It is even.\n"; 149 | break; 150 | case 1: 151 | std::cout << "It is odd.\n"; 152 | break; 153 | } 154 | } else { 155 | std::cout << "The number is non-positive.\n"; 156 | } 157 | 158 | return 0; 159 | } 160 | ``` 161 | 162 | 7. **Conclusion:** 163 | - Recap of decision-making structures in C++. 164 | - Encouragement to practice and experiment with different scenarios. 165 | 166 | Congratulations! You've completed the "Codes with Pankaj" C++ Decision Making Depth Tutorial. Happy coding! -------------------------------------------------------------------------------- /09 C++ Decision Making/if_else/FindSmallest3Number.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/09 C++ Decision Making/if_else/FindSmallest3Number.cpp -------------------------------------------------------------------------------- /09 C++ Decision Making/if_else/SmallestNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | 8 | int number1,number2; 9 | number1 = 100; 10 | number2 = 20; 11 | 12 | // find the smallest number 13 | 14 | if(number1 < number2){ 15 | cout << "The smallest number is " << number1 << endl; 16 | }else{ 17 | cout << "The smallest number is " << number2 << endl; 18 | 19 | } 20 | 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /09 C++ Decision Making/if_else/SmallestNumber.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/09 C++ Decision Making/if_else/SmallestNumber.exe -------------------------------------------------------------------------------- /09 C++ Decision Making/if_else/even_odd_check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | int number; 5 | 6 | cout << "Enter a number: "; 7 | cin >> number; 8 | 9 | if (number % 2 == 0) { 10 | cout << number << " is even." << endl; 11 | } else { 12 | cout << number << " is odd." << endl; 13 | } 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /09 C++ Decision Making/if_else/leap_year_check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int year; 5 | 6 | std::cout << "Enter a year: "; 7 | std::cin >> year; 8 | 9 | if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { 10 | std::cout << year << " is a leap year." << std::endl; 11 | } else { 12 | std::cout << year << " is not a leap year." << std::endl; 13 | } 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /09 C++ Decision Making/if_else/positive_negative_zero.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int number; 5 | 6 | std::cout << "Enter a number: "; 7 | std::cin >> number; 8 | 9 | if (number > 0) { 10 | std::cout << number << " is positive." << std::endl; 11 | } else if (number < 0) { 12 | std::cout << number << " is negative." << std::endl; 13 | } else { 14 | std::cout << number << " is zero." << std::endl; 15 | } 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /09 C++ Decision Making/if_else/positive_negative_zero_ternary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int number; 5 | 6 | std::cout << "Enter a number: "; 7 | std::cin >> number; 8 | 9 | std::cout << (number > 0 ? "Positive" : (number < 0 ? "Negative" : "Zero")) << std::endl; 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /09 C++ Decision Making/switch/ menu_selection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | char choice; 5 | 6 | std::cout << "Select an option (A, B, C): "; 7 | std::cin >> choice; 8 | 9 | switch (choice) { 10 | case 'A': 11 | std::cout << "Option A selected." << std::endl; 12 | break; 13 | case 'B': 14 | std::cout << "Option B selected." << std::endl; 15 | break; 16 | case 'C': 17 | std::cout << "Option C selected." << std::endl; 18 | break; 19 | default: 20 | std::cout << "Invalid choice." << std::endl; 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /09 C++ Decision Making/switch/GradingSystem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | 5 | // Percentage Eq U Classification 6 | // 90 to 100 4.5 O Out standing 7 | // 80 to 89 4.0 A+ Distinction 8 | // 60 to 79 3.5 A First Class 9 | // 50 to 59 3.0 B+ Second Class 10 | // 40 to 49 2.5 B Pass Class 11 | // < 39 2.0 C Fail 12 | 13 | char Grade; 14 | cout<<"Enter Grade - "<>Grade; 16 | switch(Grade){ 17 | case 'o': 18 | cout<<"Out Standing"< 2 | using namespace std; 3 | int main() { 4 | int day; 5 | 6 | cout << "Enter a day number (1-7): "; 7 | cin >> day; 8 | 9 | switch (day) { 10 | case 1: 11 | cout << "Sunday" << endl; 12 | break; 13 | case 2: 14 | cout << "Monday" << endl; 15 | break; 16 | case 3: 17 | cout << "Tuesday" << endl; 18 | break; 19 | case 4: 20 | cout << "Wednesday" << endl; 21 | break; 22 | case 5: 23 | cout << "Thursday" << endl; 24 | break; 25 | case 6: 26 | cout << "Friday" << endl; 27 | break; 28 | case 7: 29 | cout << "Saturday" << endl; 30 | break; 31 | default: 32 | cout << "Invalid day number." << endl; 33 | } 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /09 C++ Decision Making/switch/season_of_year.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int month; 5 | 6 | std::cout << "Enter a month number (1-12): "; 7 | std::cin >> month; 8 | 9 | switch (month) { 10 | case 12: 11 | case 1: 12 | case 2: 13 | std::cout << "Winter" << std::endl; 14 | break; 15 | case 3: 16 | case 4: 17 | case 5: 18 | std::cout << "Spring" << std::endl; 19 | break; 20 | case 6: 21 | case 7: 22 | case 8: 23 | std::cout << "Summer" << std::endl; 24 | break; 25 | case 9: 26 | case 10: 27 | case 11: 28 | std::cout << "Fall" << std::endl; 29 | break; 30 | default: 31 | std::cout << "Invalid month number." << std::endl; 32 | } 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /09 C++ Decision Making/switch/simple_calculator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | char operation; 5 | double operand1, operand2; 6 | 7 | std::cout << "Enter an operation (+, -, *, /): "; 8 | std::cin >> operation; 9 | 10 | std::cout << "Enter two operands: "; 11 | std::cin >> operand1 >> operand2; 12 | 13 | switch (operation) { 14 | case '+': 15 | std::cout << operand1 << " + " << operand2 << " = " << operand1 + operand2 << std::endl; 16 | break; 17 | case '-': 18 | std::cout << operand1 << " - " << operand2 << " = " << operand1 - operand2 << std::endl; 19 | break; 20 | case '*': 21 | std::cout << operand1 << " * " << operand2 << " = " << operand1 * operand2 << std::endl; 22 | break; 23 | case '/': 24 | if (operand2 != 0) { 25 | std::cout << operand1 << " / " << operand2 << " = " << operand1 / operand2 << std::endl; 26 | } else { 27 | std::cout << "Error: Division by zero." << std::endl; 28 | } 29 | break; 30 | default: 31 | std::cout << "Invalid operation." << std::endl; 32 | } 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /10 C++ Functions/Example01: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/10 C++ Functions/Example01 -------------------------------------------------------------------------------- /10 C++ Functions/Example01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // create a function 6 | // basic function 7 | void info(){ 8 | 9 | cout<<"codeswithpankaj.com"< 2 | 3 | using namespace std; 4 | 5 | 6 | class student{ 7 | 8 | public : void info(){ 9 | cout<<"welcome to codeswithpankaj.com"< 18 | 19 | // Function declaration 20 | int add(int a, int b); 21 | 22 | int main() { 23 | std::cout << "Function Declaration and Definition Example:\n"; 24 | 25 | // Function invocation 26 | int result = add(5, 3); 27 | 28 | std::cout << "The sum is: " << result << std::endl; 29 | 30 | return 0; 31 | } 32 | 33 | // Function definition 34 | int add(int a, int b) { 35 | return a + b; 36 | } 37 | ``` 38 | 39 | 3. **Function Parameters and Return Types:** 40 | - Explanation of function parameters and return types. 41 | - Example: Function with multiple parameters and a return type. 42 | 43 | ```cpp 44 | #include 45 | 46 | // Function declaration 47 | double calculateAverage(double num1, double num2); 48 | 49 | int main() { 50 | std::cout << "Function Parameters and Return Types Example:\n"; 51 | 52 | // Function invocation 53 | double avg = calculateAverage(4.5, 7.2); 54 | 55 | std::cout << "The average is: " << avg << std::endl; 56 | 57 | return 0; 58 | } 59 | 60 | // Function definition 61 | double calculateAverage(double num1, double num2) { 62 | return (num1 + num2) / 2.0; 63 | } 64 | ``` 65 | 66 | 4. **Function Overloading:** 67 | - Explanation of function overloading. 68 | - Example: Overloading a function to handle different data types. 69 | 70 | ```cpp 71 | #include 72 | 73 | // Function declarations 74 | int add(int a, int b); 75 | double add(double a, double b); 76 | 77 | int main() { 78 | std::cout << "Function Overloading Example:\n"; 79 | 80 | // Function invocations 81 | int sumInt = add(5, 3); 82 | double sumDouble = add(4.5, 7.2); 83 | 84 | std::cout << "Sum of integers: " << sumInt << std::endl; 85 | std::cout << "Sum of doubles: " << sumDouble << std::endl; 86 | 87 | return 0; 88 | } 89 | 90 | // Function definitions 91 | int add(int a, int b) { 92 | return a + b; 93 | } 94 | 95 | double add(double a, double b) { 96 | return a + b; 97 | } 98 | ``` 99 | 100 | 5. **Recursion:** 101 | - Explanation of recursive functions. 102 | - Example: Computing the factorial of a number using recursion. 103 | 104 | ```cpp 105 | #include 106 | 107 | // Function declaration 108 | int factorial(int n); 109 | 110 | int main() { 111 | std::cout << "Recursion Example:\n"; 112 | 113 | // Function invocation 114 | int result = factorial(5); 115 | 116 | std::cout << "Factorial: " << result << std::endl; 117 | 118 | return 0; 119 | } 120 | 121 | // Function definition 122 | int factorial(int n) { 123 | if (n <= 1) { 124 | return 1; 125 | } else { 126 | return n * factorial(n - 1); 127 | } 128 | } 129 | ``` 130 | 131 | 6. **Default Arguments:** 132 | - Explanation of default function arguments. 133 | - Example: Function with default values for parameters. 134 | 135 | ```cpp 136 | #include 137 | 138 | // Function declaration 139 | void greet(std::string name, std::string greeting = "Hello"); 140 | 141 | int main() { 142 | std::cout << "Default Arguments Example:\n"; 143 | 144 | // Function invocations 145 | greet("Alice"); 146 | greet("Bob", "Hi"); 147 | 148 | return 0; 149 | } 150 | 151 | // Function definition 152 | void greet(std::string name, std::string greeting) { 153 | std::cout << greeting << ", " << name << "!\n"; 154 | } 155 | ``` 156 | 157 | 7. **Conclusion:** 158 | - Recap of key concepts related to functions. 159 | - Encouragement to practice and experiment with function concepts. 160 | 161 | Congratulations! You've completed the "Codes with Pankaj" C++ Functions Depth Tutorial. Happy coding! -------------------------------------------------------------------------------- /10 C++ Functions/addition_function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function to add two numbers 4 | int add(int a, int b) { 5 | return a + b; 6 | } 7 | 8 | int main() { 9 | int num1, num2; 10 | 11 | std::cout << "Enter two numbers: "; 12 | std::cin >> num1 >> num2; 13 | 14 | // Call the add function 15 | int result = add(num1, num2); 16 | 17 | std::cout << "Sum: " << result << std::endl; 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /10 C++ Functions/factorial_function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function to calculate the factorial of a number 4 | int factorial(int n) { 5 | if (n == 0 || n == 1) { 6 | return 1; 7 | } else { 8 | return n * factorial(n - 1); 9 | } 10 | } 11 | 12 | int main() { 13 | int num; 14 | 15 | std::cout << "Enter a number: "; 16 | std::cin >> num; 17 | 18 | // Call the factorial function 19 | int result = factorial(num); 20 | 21 | std::cout << "Factorial: " << result << std::endl; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /10 C++ Functions/maximum_function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function to find the maximum of two numbers 4 | int max(int a, int b) { 5 | return (a > b) ? a : b; 6 | } 7 | 8 | int main() { 9 | int num1, num2; 10 | 11 | std::cout << "Enter two numbers: "; 12 | std::cin >> num1 >> num2; 13 | 14 | // Call the max function 15 | int result = max(num1, num2); 16 | 17 | std::cout << "Maximum: " << result << std::endl; 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /10 C++ Functions/power_function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function to calculate the power of a number 4 | double power(double base, int exponent) { 5 | double result = 1.0; 6 | 7 | for (int i = 0; i < exponent; ++i) { 8 | result *= base; 9 | } 10 | 11 | return result; 12 | } 13 | 14 | int main() { 15 | double base; 16 | int exponent; 17 | 18 | std::cout << "Enter the base: "; 19 | std::cin >> base; 20 | 21 | std::cout << "Enter the exponent: "; 22 | std::cin >> exponent; 23 | 24 | // Call the power function 25 | double result = power(base, exponent); 26 | 27 | std::cout << "Result: " << result << std::endl; 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /11 C++ Numbers/MathLibraryFunctions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // sqrt(25) 6 | // pow(2, 3) 7 | // abs(-7) 8 | 9 | using namespace std; 10 | 11 | int main(){ 12 | 13 | 14 | // C++ acos() 15 | // Returns Inverse cosine a Number 16 | 17 | cout<<"Returns Inverse cosine a Number "< for memcpy() 93 | memcpy(&dest, &src, sizeof src); 94 | cout << "nan(\"1\") = " << src << " (" << hex << dest << ")\n"; 95 | 96 | // C++ pow() 97 | // Computes Power a Number 98 | cout<<"Computes Power a Number "< 15 | using namespace std; 16 | 17 | int main() { 18 | int integerNumber = 42; 19 | float decimalNumber = 3.14; 20 | double preciseDecimal = 2.71828; 21 | 22 | cout << "Integer: " << integerNumber << endl; 23 | cout << "Float: " << decimalNumber << endl; 24 | cout << "Double: " << preciseDecimal << endl; 25 | 26 | return 0; 27 | } 28 | ``` 29 | 30 | ## 2. Basic Arithmetic Operations 31 | 32 | Performing arithmetic operations is fundamental to programming. C++ supports common arithmetic operators such as addition, subtraction, multiplication, division, and modulus. 33 | 34 | **Example:** 35 | 36 | ```cpp 37 | #include 38 | using namespace std; 39 | 40 | int main() { 41 | int a = 10, b = 5; 42 | 43 | // Addition 44 | int sum = a + b; 45 | cout << "Sum: " << sum << endl; 46 | 47 | // Subtraction 48 | int difference = a - b; 49 | cout << "Difference: " << difference << endl; 50 | 51 | // Multiplication 52 | int product = a * b; 53 | cout << "Product: " << product << endl; 54 | 55 | // Division 56 | int quotient = a / b; 57 | cout << "Quotient: " << quotient << endl; 58 | 59 | // Modulus (remainder) 60 | int remainder = a % b; 61 | cout << "Remainder: " << remainder << endl; 62 | 63 | return 0; 64 | } 65 | ``` 66 | 67 | ## 3. Math Library Functions 68 | 69 | C++ provides a Math library (``) that includes a variety of mathematical functions. Functions like `sqrt()`, `pow()`, and `abs()` are invaluable for advanced numeric operations. 70 | 71 | **Example:** 72 | 73 | ```cpp 74 | #include 75 | #include 76 | using namespace std; 77 | 78 | int main() { 79 | // Square root 80 | cout << "Square root of 25: " << sqrt(25) << endl; 81 | 82 | // Power 83 | cout << "2 to the power of 3: " << pow(2, 3) << endl; 84 | 85 | // Absolute value 86 | cout << "Absolute value of -7: " << abs(-7) << endl; 87 | 88 | return 0; 89 | } 90 | ``` 91 | 92 | ## Conclusion 93 | 94 | Empower yourself with the knowledge of handling numbers in C++. Visit [CodesWithPankaj.com](https://codeswithpankaj.com) to access this comprehensive tutorial, complete with examples and practical insights. From choosing the right data type to utilizing advanced math functions, this tutorial will guide you in mastering the art of working with numbers in C++. Happy coding! -------------------------------------------------------------------------------- /11 C++ Numbers/arithmetic_operations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Arithmetic operations 5 | int num1 = 10; 6 | int num2 = 5; 7 | 8 | std::cout << "Addition: " << num1 + num2 << std::endl; 9 | std::cout << "Subtraction: " << num1 - num2 << std::endl; 10 | std::cout << "Multiplication: " << num1 * num2 << std::endl; 11 | std::cout << "Division: " << num1 / num2 << std::endl; 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /11 C++ Numbers/input_output_numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Input and output of numbers 5 | int userInput; 6 | 7 | std::cout << "Enter a number: "; 8 | std::cin >> userInput; 9 | 10 | std::cout << "You entered: " << userInput << std::endl; 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /11 C++ Numbers/random_number_generation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // For rand() function 3 | #include // For time() function 4 | 5 | int main() { 6 | // Seed the random number generator 7 | std::srand(std::time(0)); 8 | 9 | // Generate a random number between 1 and 10 10 | int randomNum = std::rand() % 10 + 1; 11 | 12 | std::cout << "Random Number: " << randomNum << std::endl; 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /11 C++ Numbers/type_conversion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Conversion between data types 5 | double doubleNum = 3.14; 6 | int intNum = static_cast(doubleNum); 7 | 8 | std::cout << "Double Number: " << doubleNum << std::endl; 9 | std::cout << "Converted to Int: " << intNum << std::endl; 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /12 C++ Arrays/Multi Array/2d_array_initialization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 2D array initialization and printing 5 | int matrix[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; 6 | 7 | // Display the elements of the 2D array 8 | std::cout << "Elements in the 2D array:\n"; 9 | for (int i = 0; i < 3; ++i) { 10 | for (int j = 0; j < 4; ++j) { 11 | std::cout << matrix[i][j] << " "; 12 | } 13 | std::cout << std::endl; 14 | } 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /12 C++ Arrays/Multi Array/3d_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 3D array 5 | const int x = 2, y = 3, z = 4; 6 | int threeDArray[x][y][z]; 7 | 8 | // Initialize and display elements of the 3D array 9 | int value = 1; 10 | for (int i = 0; i < x; ++i) { 11 | for (int j = 0; j < y; ++j) { 12 | for (int k = 0; k < z; ++k) { 13 | threeDArray[i][j][k] = value++; 14 | std::cout << threeDArray[i][j][k] << " "; 15 | } 16 | std::cout << std::endl; 17 | } 18 | std::cout << std::endl; 19 | } 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /12 C++ Arrays/Multi Array/MultiArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int main() 5 | { 6 | 7 | // create a 2d array 8 | 9 | char data[3][3]; 10 | 11 | data[0][0] = 'a'; 12 | data[0][1] = 'b'; 13 | data[0][2] = 'k'; 14 | data[1][0] = 'c'; 15 | data[1][1] = 'd'; 16 | data[1][2] = 'l'; 17 | data[2][0] = 'e'; 18 | data[2][1] = 'f'; 19 | data[2][2] = 'm'; 20 | // print the 2d array 21 | for (int i = 0; i < 3; i++) 22 | { 23 | for (int j = 0; j < 3; j++) 24 | { 25 | cout << data[i][j] << " "; 26 | } 27 | cout << endl; 28 | } 29 | 30 | return 0; 31 | 32 | } -------------------------------------------------------------------------------- /12 C++ Arrays/Multi Array/MultiArray.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/12 C++ Arrays/Multi Array/MultiArray.exe -------------------------------------------------------------------------------- /12 C++ Arrays/Multi Array/dynamic_2d_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Dynamic 2D array 5 | int rows, cols; 6 | 7 | std::cout << "Enter the number of rows: "; 8 | std::cin >> rows; 9 | 10 | std::cout << "Enter the number of columns: "; 11 | std::cin >> cols; 12 | 13 | // Dynamically allocate memory for the 2D array 14 | int** dynamicArray = new int*[rows]; 15 | for (int i = 0; i < rows; ++i) { 16 | dynamicArray[i] = new int[cols]; 17 | } 18 | 19 | // Initialize and display elements of the dynamic 2D array 20 | int value = 1; 21 | for (int i = 0; i < rows; ++i) { 22 | for (int j = 0; j < cols; ++j) { 23 | dynamicArray[i][j] = value++; 24 | std::cout << dynamicArray[i][j] << " "; 25 | } 26 | std::cout << std::endl; 27 | } 28 | 29 | // Deallocate memory 30 | for (int i = 0; i < rows; ++i) { 31 | delete[] dynamicArray[i]; 32 | } 33 | delete[] dynamicArray; 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /12 C++ Arrays/Multi Array/matrix_addition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Matrix addition 5 | const int rows = 2; 6 | const int cols = 2; 7 | 8 | int matrix1[rows][cols] = {{1, 2}, {3, 4}}; 9 | int matrix2[rows][cols] = {{5, 6}, {7, 8}}; 10 | int result[rows][cols]; 11 | 12 | // Perform matrix addition 13 | for (int i = 0; i < rows; ++i) { 14 | for (int j = 0; j < cols; ++j) { 15 | result[i][j] = matrix1[i][j] + matrix2[i][j]; 16 | } 17 | } 18 | 19 | // Display the result matrix 20 | std::cout << "Result of Matrix Addition:\n"; 21 | for (int i = 0; i < rows; ++i) { 22 | for (int j = 0; j < cols; ++j) { 23 | std::cout << result[i][j] << " "; 24 | } 25 | std::cout << std::endl; 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /12 C++ Arrays/README.md: -------------------------------------------------------------------------------- 1 | 2 | # Mastering C++ Arrays: A Comprehensive Guide 3 | 4 | Welcome to Codes with Pankaj, your go-to resource for learning programming! In this tutorial, we'll delve into one of the fundamental data structures in C++ - Arrays. Whether you're a beginner or looking to deepen your understanding, this guide will take you through the ins and outs of C++ arrays. 5 | 6 | ## Table of Contents 7 | 8 | - [Mastering C++ Arrays: A Comprehensive Guide](#mastering-c-arrays-a-comprehensive-guide) 9 | - [Table of Contents](#table-of-contents) 10 | - [1. Introduction to C++ Arrays ](#1-introduction-to-c-arrays-) 11 | - [2. Declaration and Initialization ](#2-declaration-and-initialization-) 12 | - [3. Accessing Elements ](#3-accessing-elements-) 13 | - [4. Array Size ](#4-array-size-) 14 | - [5. Multidimensional Arrays ](#5-multidimensional-arrays-) 15 | - [6. Array Manipulation ](#6-array-manipulation-) 16 | - [7. Strings as Arrays ](#7-strings-as-arrays-) 17 | - [8. Pointers and Arrays ](#8-pointers-and-arrays-) 18 | - [9. Dynamic Arrays ](#9-dynamic-arrays-) 19 | - [10. STL Arrays ](#10-stl-arrays-) 20 | - [11. Putting It All Together - Example ](#11-putting-it-all-together---example-) 21 | 22 | ## 1. Introduction to C++ Arrays 23 | 24 | Arrays are a crucial aspect of C++ programming, providing a way to store and manipulate collections of data. An array is a contiguous block of memory that holds elements of the same data type. Let's explore the key concepts in detail. 25 | 26 | ## 2. Declaration and Initialization 27 | 28 | In C++, you declare an array by specifying its data type and size. Initialization can be done at the time of declaration or separately. 29 | 30 | ```cpp 31 | int numbers[5]; // Declaration, uninitialized 32 | 33 | int values[] = {1, 2, 3, 4, 5}; // Declaration with automatic size determination 34 | ``` 35 | 36 | ## 3. Accessing Elements 37 | 38 | Accessing elements in an array is done using indices, starting from 0. For instance: 39 | 40 | ```cpp 41 | int values[] = {10, 20, 30, 40, 50}; 42 | int element = values[2]; // Accessing the third element (30) 43 | ``` 44 | 45 | ## 4. Array Size 46 | 47 | The size of an array can be determined using the `sizeof` operator. This is useful when dealing with loops or other operations. 48 | 49 | ```cpp 50 | int numbers[5]; 51 | int size = sizeof(numbers) / sizeof(numbers[0]); // Calculating array size 52 | ``` 53 | 54 | ## 5. Multidimensional Arrays 55 | 56 | Multidimensional arrays are used to store data in multiple dimensions, often representing matrices or tables. 57 | 58 | ```cpp 59 | int matrix[3][3] = { 60 | {1, 2, 3}, 61 | {4, 5, 6}, 62 | {7, 8, 9} 63 | }; 64 | ``` 65 | 66 | ## 6. Array Manipulation 67 | 68 | Arrays can be manipulated using loops and standard library functions. Here's an example of sorting: 69 | 70 | ```cpp 71 | #include 72 | int values[] = {5, 2, 8, 1, 3}; 73 | std::sort(values, values + 5); // Sorting array elements 74 | ``` 75 | 76 | ## 7. Strings as Arrays 77 | 78 | In C++, strings are often represented as arrays of characters. For example: 79 | 80 | ```cpp 81 | char greeting[] = "Hello, World!"; 82 | ``` 83 | 84 | ## 8. Pointers and Arrays 85 | 86 | Arrays and pointers are closely related. The array name can be treated as a pointer to the first element of the array. 87 | 88 | ```cpp 89 | int values[] = {10, 20, 30}; 90 | int* ptr = values; // ptr points to the first element of values 91 | ``` 92 | 93 | ## 9. Dynamic Arrays 94 | 95 | Dynamic arrays are created at runtime using pointers and the `new` operator. They offer flexibility in memory allocation. 96 | 97 | ```cpp 98 | int* dynamicArray = new int[10]; // Creating a dynamic array 99 | delete[] dynamicArray; // Deallocating memory when done 100 | ``` 101 | 102 | ## 10. STL Arrays 103 | 104 | The Standard Template Library (STL) provides a convenient `std::array` container with various utility functions. 105 | 106 | ```cpp 107 | #include 108 | std::array stdArray = {1, 2, 3, 4, 5}; 109 | ``` 110 | 111 | ## 11. Putting It All Together - Example 112 | 113 | Now, let's see a comprehensive example that covers declaration, initialization, manipulation, and usage of a simple integer array. 114 | 115 | ```cpp 116 | // ... (code from the previous example) 117 | ``` 118 | 119 | --- 120 | 121 | That concludes our comprehensive guide to C++ arrays. We've covered the basics and delved into more advanced topics, providing you with a solid foundation for working with arrays in C++. Stay tuned for more tutorials and happy coding! 122 | 123 | Remember, practice is key to mastering any programming concept. Head over to Codes with Pankaj's interactive coding platform to try out examples and solidify your understanding. 124 | 125 | Happy coding! 126 | 127 | **Pankaj, Codes with Pankaj** 128 | *Visit us at [codeswithpankaj.com](https://www.codeswithpankaj.com)* -------------------------------------------------------------------------------- /12 C++ Arrays/Single Array/Array_Single.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class ArrayExample{ 6 | 7 | int data[7] = {11,22,33,44,55,66,77}; 8 | 9 | public: 10 | void printData() { 11 | for(int i = 0 ; i < 7; i++){ 12 | cout << "Data" << data[i] << endl; 13 | } 14 | } 15 | 16 | }; 17 | 18 | 19 | int main(){ 20 | 21 | ArrayExample arr; 22 | arr.printData(); 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /12 C++ Arrays/Single Array/Array_Single.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/12 C++ Arrays/Single Array/Array_Single.exe -------------------------------------------------------------------------------- /12 C++ Arrays/Single Array/CharArray: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/12 C++ Arrays/Single Array/CharArray -------------------------------------------------------------------------------- /12 C++ Arrays/Single Array/CharArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | char data[] = {'a','b','q','w','r'}; 8 | 9 | //cout<<"data 2 "< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int data[] = {100,200,300,400,500,600}; 8 | 9 | //cout<<"data 2 "< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // create a single array 8 | 9 | int numbers[4]; 10 | 11 | numbers[0] = 21; 12 | numbers[1] = 22; 13 | numbers[2] = 23; 14 | numbers[3] = 24; 15 | cout << "numbers[0] = " << numbers[0] << endl; 16 | 17 | // acces all elements 18 | 19 | for(int a = 0 ;a < 4 ; a++){ 20 | 21 | cout << "numbers[" << a << "] = " << numbers[a] << endl; 22 | 23 | } 24 | 25 | 26 | 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /12 C++ Arrays/Single Array/SingleArray.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/12 C++ Arrays/Single Array/SingleArray.exe -------------------------------------------------------------------------------- /12 C++ Arrays/Single Array/array_initialization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Array initialization and printing 5 | int myArray[] = {1, 2, 3, 4, 5}; 6 | 7 | // Display the elements of the array 8 | std::cout << "Elements in the array:\n"; 9 | for (int i = 0; i < sizeof(myArray) / sizeof(myArray[0]); ++i) { 10 | std::cout << myArray[i] << " "; 11 | } 12 | std::cout << std::endl; 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /12 C++ Arrays/Single Array/array_modification.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Array modification 5 | int myArray[] = {5, 10, 15, 20, 25}; 6 | 7 | // Display the original elements 8 | std::cout << "Original elements in the array:\n"; 9 | for (int i = 0; i < sizeof(myArray) / sizeof(myArray[0]); ++i) { 10 | std::cout << myArray[i] << " "; 11 | } 12 | std::cout << std::endl; 13 | 14 | // Modify elements (multiply by 2) 15 | for (int i = 0; i < sizeof(myArray) / sizeof(myArray[0]); ++i) { 16 | myArray[i] *= 2; 17 | } 18 | 19 | // Display the modified elements 20 | std::cout << "Modified elements in the array:\n"; 21 | for (int i = 0; i < sizeof(myArray) / sizeof(myArray[0]); ++i) { 22 | std::cout << myArray[i] << " "; 23 | } 24 | std::cout << std::endl; 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /12 C++ Arrays/Single Array/array_operations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Single array operations 5 | const int arraySize = 5; 6 | int myArray[arraySize]; 7 | 8 | // Input values into the array 9 | std::cout << "Enter " << arraySize << " integers:\n"; 10 | for (int i = 0; i < arraySize; ++i) { 11 | std::cout << "Element " << i + 1 << ": "; 12 | std::cin >> myArray[i]; 13 | } 14 | 15 | // Display the elements of the array 16 | std::cout << "Elements in the array:\n"; 17 | for (int i = 0; i < arraySize; ++i) { 18 | std::cout << myArray[i] << " "; 19 | } 20 | std::cout << std::endl; 21 | 22 | // Find the sum of the elements 23 | int sum = 0; 24 | for (int i = 0; i < arraySize; ++i) { 25 | sum += myArray[i]; 26 | } 27 | std::cout << "Sum of elements: " << sum << std::endl; 28 | 29 | // Find the maximum element 30 | int maxElement = myArray[0]; 31 | for (int i = 1; i < arraySize; ++i) { 32 | if (myArray[i] > maxElement) { 33 | maxElement = myArray[i]; 34 | } 35 | } 36 | std::cout << "Maximum element: " << maxElement << std::endl; 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /12 C++ Arrays/Single Array/array_search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Array search 5 | int myArray[] = {10, 20, 30, 40, 50}; 6 | int searchValue; 7 | 8 | std::cout << "Enter a value to search for: "; 9 | std::cin >> searchValue; 10 | 11 | bool found = false; 12 | for (int i = 0; i < sizeof(myArray) / sizeof(myArray[0]); ++i) { 13 | if (myArray[i] == searchValue) { 14 | found = true; 15 | break; 16 | } 17 | } 18 | 19 | if (found) { 20 | std::cout << "Value found in the array.\n"; 21 | } else { 22 | std::cout << "Value not found in the array.\n"; 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /12 C++ Arrays/Single Array/example1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/12 C++ Arrays/Single Array/example1 -------------------------------------------------------------------------------- /12 C++ Arrays/Single Array/example1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | string data[] = {"c++","python","django","html","c"}; 8 | 9 | //cout<<"data 2 "< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | cout<<"Enter Array Size ---- "<>size; 10 | string food[size]; 11 | 12 | 13 | for(int a = 0 ; a < size ; a++){ 14 | cout<<"Enter food into Array = " <>food[a]; 16 | } 17 | 18 | cout<<"-------output-------"< 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | // String comparison 8 | string str1 = "apple"; 9 | string str2 = "orange"; 10 | 11 | if (str1 == str2) { 12 | cout << "Strings are equal." < 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | // String concatenation 8 | string firstName = "John"; 9 | string lastName = "Doe"; 10 | string fullName = firstName + " " + lastName; 11 | 12 | cout << "Full Name: " << fullName << endl; 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /13 C++ Strings/string_length_access.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | // String length and access 6 | std::string myString = "Hello, C++!"; 7 | 8 | // Display the length of the string 9 | std::cout << "Length of the string: " << myString.length() << std::endl; 10 | 11 | // Access individual characters 12 | std::cout << "First character: " << myString[0] << std::endl; 13 | std::cout << "Last character: " << myString[myString.length() - 1] << std::endl; 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /14 C++ Pointers/Array_pointer01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Array and pointer relationship 5 | int numbers[] = {1, 2, 3, 4, 5}; 6 | int* pointer = numbers; 7 | 8 | for (int i = 0; i < 5; ++i) { 9 | std::cout << "Element " << i << ": " << &numbers[i]<< std::endl; 10 | 11 | } 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /14 C++ Pointers/README.md: -------------------------------------------------------------------------------- 1 | # C++ Pointers: 2 | 3 | Welcome to this extensive tutorial on C++ pointers, a crucial aspect of C++ programming that unlocks advanced memory manipulation and dynamic allocation. Whether you're new to C++ or looking to deepen your understanding, this tutorial will guide you through the fundamentals and intricacies of working with pointers. As you navigate through various topics, you'll find practical examples that illustrate the power and versatility of C++ pointers. 4 | 5 | ## Table of Contents 6 | - [C++ Pointers:](#c-pointers) 7 | - [Table of Contents](#table-of-contents) 8 | - [1. Introduction to C++ Pointers ](#1-introduction-to-c-pointers-) 9 | - [2. Declaring and Initializing Pointers ](#2-declaring-and-initializing-pointers-) 10 | - [3. Pointer Arithmetic ](#3-pointer-arithmetic-) 11 | - [4. Pointers and Arrays ](#4-pointers-and-arrays-) 12 | - [5. Pointers and Functions ](#5-pointers-and-functions-) 13 | - [6. Dynamic Memory Allocation ](#6-dynamic-memory-allocation-) 14 | - [7. Pointers to Pointers ](#7-pointers-to-pointers-) 15 | - [8. Null Pointers ](#8-null-pointers-) 16 | - [9. Const Pointers and Pointers to Constants ](#9-const-pointers-and-pointers-to-constants-) 17 | - [10. Practical Example ](#10-practical-example-) 18 | - [11. Conclusion ](#11-conclusion-) 19 | 20 | ## 1. Introduction to C++ Pointers 21 | 22 | In C++, a pointer is a variable that holds the memory address of another variable. Understanding pointers is essential for tasks like dynamic memory allocation and efficient manipulation of data structures. 23 | 24 | ## 2. Declaring and Initializing Pointers 25 | 26 | Declaring and initializing pointers involves specifying the data type they point to and assigning them the address of a variable. 27 | 28 | ```cpp 29 | #include 30 | 31 | int main() { 32 | int number = 42; 33 | int *ptr; // Declaration 34 | ptr = &number; // Initialization 35 | 36 | // ... (rest of the code) 37 | } 38 | ``` 39 | 40 | ## 3. Pointer Arithmetic 41 | 42 | Pointer arithmetic allows you to navigate through memory by incrementing or decrementing the pointer. 43 | 44 | ```cpp 45 | #include 46 | 47 | int main() { 48 | int numbers[] = {1, 2, 3, 4, 5}; 49 | int *ptr = numbers; // Points to the first element 50 | 51 | // Using pointer arithmetic 52 | std::cout << "Value at ptr: " << *ptr << std::endl; // Output: 1 53 | ptr++; 54 | std::cout << "Value after incrementing ptr: " << *ptr << std::endl; // Output: 2 55 | 56 | // ... (rest of the code) 57 | } 58 | ``` 59 | 60 | ## 4. Pointers and Arrays 61 | 62 | Pointers and arrays are closely related in C++. An array name can be treated as a pointer to the first element of the array. 63 | 64 | ```cpp 65 | #include 66 | 67 | int main() { 68 | int numbers[] = {1, 2, 3, 4, 5}; 69 | int *ptr = numbers; // Points to the first element 70 | 71 | // Accessing array elements using pointers 72 | for (int i = 0; i < 5; ++i) { 73 | std::cout << "Value at ptr: " << *ptr << std::endl; 74 | ptr++; 75 | } 76 | 77 | // ... (rest of the code) 78 | } 79 | ``` 80 | 81 | ## 5. Pointers and Functions 82 | 83 | Pointers can be used to pass variables by reference to functions, enabling the modification of the original values. 84 | 85 | ```cpp 86 | #include 87 | 88 | void increment(int *value) { 89 | (*value)++; 90 | } 91 | 92 | int main() { 93 | int number = 42; 94 | increment(&number); 95 | std::cout << "Incremented value: " << number << std::endl; 96 | 97 | // ... (rest of the code) 98 | } 99 | ``` 100 | 101 | ## 6. Dynamic Memory Allocation 102 | 103 | Dynamic memory allocation allows you to allocate memory at runtime using operators `new` and `delete`. 104 | 105 | ```cpp 106 | #include 107 | 108 | int main() { 109 | int *dynamicNumber = new int; // Allocation 110 | *dynamicNumber = 100; 111 | 112 | // ... (use dynamicNumber) 113 | 114 | delete dynamicNumber; // Deallocation 115 | 116 | // ... (rest of the code) 117 | } 118 | ``` 119 | 120 | ## 7. Pointers to Pointers 121 | 122 | Pointers to pointers (double pointers) are used for managing arrays of pointers or dynamic two-dimensional arrays. 123 | 124 | ```cpp 125 | #include 126 | 127 | int main() { 128 | int number = 42; 129 | int *ptr1 = &number; 130 | int **ptr2 = &ptr1; // Pointer to pointer 131 | 132 | std::cout << "Value at ptr2: " << **ptr2 << std::endl; // Output: 42 133 | 134 | // ... (rest of the code) 135 | } 136 | ``` 137 | 138 | ## 8. Null Pointers 139 | 140 | Null pointers do not point to any memory location. They are often used to indicate that a pointer is not currently pointing to a valid address. 141 | 142 | ```cpp 143 | #include 144 | 145 | int main() { 146 | int *nullPointer = nullptr; 147 | 148 | if (nullPointer == nullptr) { 149 | std::cout << "Null Pointer" << std::endl; 150 | } 151 | 152 | // ... (rest of the code) 153 | } 154 | ``` 155 | 156 | ## 9. Const Pointers and Pointers to Constants 157 | 158 | `const` qualifiers can be applied to pointers or the values they point to, providing additional control over data modification. 159 | 160 | ```cpp 161 | #include 162 | 163 | int main() { 164 | int number = 42; 165 | 166 | // Const pointer to a non-const value 167 | const int *ptr1 = &number; 168 | 169 | // Pointer to a const value 170 | int const *ptr2 = &number; 171 | 172 | // ... (rest of the code) 173 | } 174 | ``` 175 | 176 | ## 10. Practical Example 177 | 178 | Let's apply our knowledge to a practical example: a program that dynamically allocates an array of integers and calculates their sum. 179 | 180 | ```cpp 181 | #include 182 | 183 | int main() { 184 | int size; 185 | std::cout << "Enter the size of the array: "; 186 | std::cin >> size; 187 | 188 | int *dynamicArray = new int[size]; // Dynamic memory allocation 189 | 190 | // Input values 191 | for (int i = 0; i < size; ++i) { 192 | std::cout << "Enter value " << i + 1 << ": "; 193 | std::cin >> dynamicArray[i]; 194 | } 195 | 196 | // Calculate sum 197 | int sum = 0; 198 | for (int i = 0; i < size; ++i) { 199 | sum += dynamicArray[i]; 200 | } 201 | 202 | std::cout << "Sum of the array elements: " << sum << std::endl; 203 | 204 | delete[] dynamicArray; // Dynamic memory deallocation 205 | 206 | // ... (rest of the code) 207 | } 208 | ``` 209 | 210 | ## 11. Conclusion 211 | 212 | Congratulations! You've now mastered the essential concepts of C++ pointers. These versatile entities enable dynamic memory manipulation, efficient data structures, and more. Continue exploring their applications and nuances to become a proficient C++ developer. 213 | 214 | For 215 | 216 | more programming tutorials and resources, visit [codeswithpankaj.com](https://codeswithpankaj.com). Happy coding! -------------------------------------------------------------------------------- /14 C++ Pointers/array_and_pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Array and pointer relationship 5 | int numbers[] = {1, 2, 3, 4, 5}; 6 | int* pointer = numbers; 7 | 8 | for (int i = 0; i < 5; ++i) { 9 | std::cout << "Element " << i << ": " << *pointer << std::endl; 10 | pointer++; 11 | } 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /14 C++ Pointers/basic_pointer_example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Basic pointer example 5 | int number = 42; 6 | int* pointer = &number; 7 | 8 | std::cout << "Value of number: " << number << std::endl; 9 | std::cout << "Address of number: " << &number << std::endl; 10 | std::cout << "Value of pointer: " << *pointer << std::endl; 11 | std::cout << "Address stored in pointer: " << pointer << std::endl; 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /14 C++ Pointers/dynamic_memory_allocation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Dynamic memory allocation with pointers 5 | int* dynamicNumber = new int; // Allocate memory for an integer 6 | 7 | *dynamicNumber = 123; // Assign a value to the dynamically allocated memory 8 | 9 | std::cout << "Value stored in dynamicNumber: " << *dynamicNumber << std::endl; 10 | 11 | // Deallocate the dynamically allocated memory 12 | delete dynamicNumber; 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /14 C++ Pointers/function_with_pointers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function that swaps the values of two variables using pointers 4 | void swap(int* a, int* b) { 5 | int temp = *a; 6 | *a = *b; 7 | *b = temp; 8 | } 9 | 10 | int main() { 11 | // Example of using a function with pointers 12 | int x = 5, y = 10; 13 | 14 | std::cout << "Before swap - x: " << x << ", y: " << y << std::endl; 15 | 16 | // Pass the addresses of x and y to the swap function 17 | swap(&x, &y); 18 | 19 | std::cout << "After swap - x: " << x << ", y: " << y << std::endl; 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /15 C++ Date & Time/Example: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/15 C++ Date & Time/Example -------------------------------------------------------------------------------- /15 C++ Date & Time/Example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | // Get the current time point 9 | auto currentTimePoint = chrono::system_clock::now(); 10 | 11 | // Convert the time point to a time_t object 12 | time_t currentTime = chrono::system_clock::to_time_t(currentTimePoint); 13 | 14 | // Convert the time_t object to a string 15 | char buffer[80]; 16 | strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localtime(¤tTime)); 17 | 18 | // Display the current date and time 19 | cout << "Current Date and Time: " << buffer << endl; 20 | 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /15 C++ Date & Time/README.md: -------------------------------------------------------------------------------- 1 | # C++ Date and Time: Unlocking Temporal Operations 2 | 3 | Welcome to this detailed tutorial on handling date and time in C++, a crucial aspect of many applications. Manipulating and working with dates and times can be complex, but with C++'s `` library and other facilities, you can perform a wide range of operations efficiently. In this tutorial, we'll explore various aspects of dealing with date and time in C++, providing comprehensive examples for each topic. 4 | 5 | ## Table of Contents 6 | - [C++ Date and Time: Unlocking Temporal Operations](#c-date-and-time-unlocking-temporal-operations) 7 | - [Table of Contents](#table-of-contents) 8 | - [1. Introduction to C++ Date and Time ](#1-introduction-to-c-date-and-time-) 9 | - [2. Current Date and Time ](#2-current-date-and-time-) 10 | - [3. Working with std::chrono ](#3-working-with-stdchrono-) 11 | - [4. Formatting and Parsing ](#4-formatting-and-parsing-) 12 | - [5. Duration and Time Points ](#5-duration-and-time-points-) 13 | - [6. Manipulating Dates and Times ](#6-manipulating-dates-and-times-) 14 | - [7. Time Zones ](#7-time-zones-) 15 | - [8. Practical](#8-practical) 16 | - [9. Conclusion ](#9-conclusion-) 17 | 18 | ## 1. Introduction to C++ Date and Time 19 | 20 | C++ provides facilities for handling date and time through the `` library and other functionalities. Managing temporal data is crucial for applications ranging from scheduling to logging. 21 | 22 | ## 2. Current Date and Time 23 | 24 | Obtaining the current date and time is a fundamental operation. C++ provides `std::chrono::system_clock` for this purpose. 25 | 26 | ```cpp 27 | #include 28 | #include 29 | 30 | int main() { 31 | auto now = std::chrono::system_clock::now(); 32 | std::time_t currentTime = std::chrono::system_clock::to_time_t(now); 33 | std::cout << "Current time: " << std::ctime(¤tTime) << std::endl; 34 | 35 | // ... (rest of the code) 36 | } 37 | ``` 38 | 39 | ## 3. Working with std::chrono 40 | 41 | The `` library introduces a flexible and type-safe way to represent durations and time points. 42 | 43 | ```cpp 44 | #include 45 | #include 46 | 47 | int main() { 48 | // Representing a duration 49 | std::chrono::duration> fiveSeconds(5); 50 | 51 | // Representing a time point 52 | std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now(); 53 | 54 | // ... (rest of the code) 55 | } 56 | ``` 57 | 58 | ## 4. Formatting and Parsing 59 | 60 | Formatting and parsing involve converting between strings and `std::chrono::time_point` objects. 61 | 62 | ```cpp 63 | #include 64 | #include 65 | #include 66 | #include 67 | 68 | int main() { 69 | std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); 70 | std::time_t currentTime = std::chrono::system_clock::to_time_t(now); 71 | 72 | // Formatting 73 | std::cout << "Formatted time: " << std::put_time(std::localtime(¤tTime), "%Y-%m-%d %H:%M:%S") << std::endl; 74 | 75 | // Parsing 76 | std::istringstream input("2023-01-15 14:30:00"); 77 | std::tm parsedTime = {}; 78 | input >> std::get_time(&parsedTime, "%Y-%m-%d %H:%M:%S"); 79 | std::chrono::system_clock::time_point parsedPoint = std::chrono::system_clock::from_time_t(std::mktime(&parsedTime)); 80 | 81 | // ... (rest of the code) 82 | } 83 | ``` 84 | 85 | ## 5. Duration and Time Points 86 | 87 | Durations represent the difference between two time points, while time points represent points in time. 88 | 89 | ```cpp 90 | #include 91 | #include 92 | 93 | int main() { 94 | // Duration 95 | std::chrono::duration seconds(5); 96 | std::chrono::duration milliseconds(2500); 97 | 98 | // Time Points 99 | auto start = std::chrono::steady_clock::now(); 100 | // ... (some operation) 101 | auto end = std::chrono::steady_clock::now(); 102 | std::chrono::duration elapsed = end - start; 103 | 104 | // ... (rest of the code) 105 | } 106 | ``` 107 | 108 | ## 6. Manipulating Dates and Times 109 | 110 | The `` library provides functionalities for adding and subtracting durations from time points. 111 | 112 | ```cpp 113 | #include 114 | #include 115 | 116 | int main() { 117 | std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); 118 | 119 | // Adding 3 days 120 | std::chrono::duration> threeDays(3); 121 | auto future = now + threeDays; 122 | 123 | // ... (rest of the code) 124 | } 125 | ``` 126 | 127 | ## 7. Time Zones 128 | 129 | Handling time zones is crucial for applications dealing with internationalization. While C++ does not provide built-in support for time zones, third-party libraries like Boost.DateTime can be used. 130 | 131 | ```cpp 132 | // Example using Boost.DateTime for time zone support 133 | #include 134 | #include 135 | 136 | int main() { 137 | boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); 138 | std::cout << "Current local time: " << now << std::endl; 139 | 140 | // ... (rest of the code) 141 | } 142 | ``` 143 | 144 | ## 8. Practical 145 | 146 | Example 147 | 148 | Let's apply our knowledge to a practical example: a program that calculates the difference between two dates. 149 | 150 | ```cpp 151 | #include 152 | #include 153 | 154 | int main() { 155 | std::tm startDate = {0, 0, 0, 15, 0, 2022 - 1900}; // January 15, 2022 156 | std::tm endDate = {0, 0, 0, 1, 0, 2023 - 1900}; // January 1, 2023 157 | 158 | std::chrono::system_clock::time_point start = std::chrono::system_clock::from_time_t(std::mktime(&startDate)); 159 | std::chrono::system_clock::time_point end = std::chrono::system_clock::from_time_t(std::mktime(&endDate)); 160 | 161 | std::chrono::duration difference = end - start; 162 | 163 | std::cout << "Difference in days: " << difference.count() / (24 * 3600) << std::endl; 164 | 165 | // ... (rest of the code) 166 | } 167 | ``` 168 | 169 | ## 9. Conclusion 170 | 171 | Congratulations! You've now explored the essential aspects of handling date and time in C++. The `` library provides a robust foundation for temporal operations, and third-party libraries can extend functionality for more advanced use cases. 172 | 173 | For more programming tutorials and resources, visit [codeswithpankaj.com](https://codeswithpankaj.com). Happy coding! -------------------------------------------------------------------------------- /17 C++ Object Oriented/Example/Employee: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/17 C++ Object Oriented/Example/Employee -------------------------------------------------------------------------------- /17 C++ Object Oriented/Example/Employee.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | 7 | class Employee 8 | { 9 | 10 | string name = "Rahul"; 11 | int id = 101; 12 | double salary = 10000; 13 | 14 | public : void getdata(){ 15 | 16 | cout<<" Your name is : "< 2 | 3 | using namespace std; 4 | 5 | 6 | 7 | class Student{ 8 | 9 | 10 | public : int age; 11 | public : string name; 12 | 13 | public : Student(){ 14 | cout<<"this is demo...."< 2 | 3 | using namespace std; 4 | 5 | class Client{ 6 | 7 | public: string name; 8 | public : int age; 9 | 10 | void display(){ 11 | cout<<"Name: "< 2 | 3 | using namespace std; 4 | 5 | 6 | class CreditCard 7 | { 8 | 9 | public : string BankName; 10 | public : long CardNumber; 11 | public : string CardHolderName; 12 | public : int ExpiryMonth; 13 | public : double CreditLimit; 14 | 15 | public : void get_CardDetails() 16 | { 17 | cout<< "Bank Name: "<< BankName<< endl; 18 | cout<< "Card Number: "<< CardNumber<< endl; 19 | cout<< "Card Holder Name: "<< CardHolderName<< endl; 20 | cout<< "Expiry Month: "<< ExpiryMonth<< endl; 21 | cout<< "Credit Limit: "<< CreditLimit<< endl; 22 | 23 | } 24 | 25 | }; 26 | 27 | int main() 28 | { 29 | // creating object of CreditCard class 30 | // className objectName; 31 | CreditCard c1; 32 | 33 | // assigning values to the data members 34 | 35 | c1.BankName = "HDFC"; 36 | c1.CardNumber = 1234567890; 37 | c1.CardHolderName = "Rahul"; 38 | c1.ExpiryMonth = 12; 39 | c1.CreditLimit = 100000; 40 | // calling the get_CardDetails function 41 | // objectName.functionName(); 42 | c1.get_CardDetails(); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /18 C++ Classes & Objects/README.md: -------------------------------------------------------------------------------- 1 | 2 | ### Introduction to Classes and Objects in C++ 3 | 4 | #### Classes: 5 | In C++, a class is a blueprint for creating objects. It defines a data structure along with methods that operate on that data. Think of a class as a user-defined data type that encapsulates data and behavior. 6 | 7 | #### Objects: 8 | An object is an instance of a class. It is a concrete realization of the class, created based on the blueprint provided by the class. Objects have attributes (data members) and behaviors (member functions or methods). 9 | 10 | ### Example: 11 | 12 | Let's create a simple C++ program that demonstrates the concepts of classes and objects. Suppose we want to model a "Person" with a name and age. 13 | 14 | ```cpp 15 | #include 16 | #include 17 | 18 | // Define the Person class 19 | class Person { 20 | public: 21 | // Constructor to initialize object's state 22 | Person(std::string n, int a) : name(n), age(a) {} 23 | 24 | // Member function to display information 25 | void displayInfo() { 26 | std::cout << "Name: " << name << ", Age: " << age << " years" << std::endl; 27 | } 28 | 29 | private: 30 | // Data members 31 | std::string name; 32 | int age; 33 | }; 34 | 35 | int main() { 36 | // Create objects of the Person class 37 | Person person1("John Doe", 25); 38 | Person person2("Jane Smith", 30); 39 | 40 | // Access and display information using member functions 41 | std::cout << "Person 1 Details:" << std::endl; 42 | person1.displayInfo(); 43 | 44 | std::cout << "\nPerson 2 Details:" << std::endl; 45 | person2.displayInfo(); 46 | 47 | return 0; 48 | } 49 | ``` 50 | 51 | In this example: 52 | 53 | - We define a class `Person` with private data members `name` and `age`, and a public member function `displayInfo` to print the person's information. 54 | 55 | - The constructor `Person(std::string n, int a)` is used to initialize the object's state when an object is created. 56 | 57 | - In the `main` function, we create two objects of the `Person` class: `person1` and `person2`. 58 | 59 | - We then use the `displayInfo` member function to display the information for each person. 60 | 61 | When you run this program, you should see output similar to the following: 62 | 63 | ``` 64 | Person 1 Details: 65 | Name: John Doe, Age: 25 years 66 | 67 | Person 2 Details: 68 | Name: Jane Smith, Age: 30 years 69 | ``` 70 | 71 | This is a basic example, and in real-world scenarios, classes and objects are used to model more complex systems and structures. -------------------------------------------------------------------------------- /18 C++ Classes & Objects/constructor/Example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | // create class constructor with default value 7 | 8 | class student 9 | { 10 | public:student() 11 | { 12 | cout << "Constructor is called" << endl; 13 | 14 | } 15 | 16 | void display() 17 | { 18 | cout << "Hello World" << endl; 19 | } 20 | }; 21 | 22 | int main() 23 | { 24 | student s1; 25 | s1.display(); 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /18 C++ Classes & Objects/constructor/Example.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/18 C++ Classes & Objects/constructor/Example.exe -------------------------------------------------------------------------------- /18 C++ Classes & Objects/constructor/constructorArg.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // create class constructor with argument 6 | 7 | class student 8 | { 9 | // print student name with constructor argument 10 | 11 | public:student(string name) 12 | { 13 | cout << "Student name is " << name << endl; 14 | } 15 | }; 16 | 17 | int main() 18 | { 19 | student s1("John"); 20 | student s2("Doe"); 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /18 C++ Classes & Objects/constructor/constructorArg.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/18 C++ Classes & Objects/constructor/constructorArg.exe -------------------------------------------------------------------------------- /18 C++ Classes & Objects/constructor/copy_example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class client { 5 | public: 6 | int x; 7 | client(){ 8 | 9 | }; 10 | 11 | // Copy Constructor definition 12 | client (client& obj) { 13 | x = obj.x; 14 | cout << "Copy constructor " 15 | "called" << endl; 16 | } 17 | }; 18 | 19 | int main() { 20 | 21 | // Creating an object of 22 | // class A 23 | client obj1; 24 | obj1.x = 10; 25 | cout << "obj1's x = " << obj1.x << endl; 26 | 27 | // Creating another object by 28 | // copying already created object 29 | client obj2(obj1); 30 | cout << "obj2's x = " << obj2.x; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /18 C++ Classes & Objects/constructor/copy_example.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/18 C++ Classes & Objects/constructor/copy_example.exe -------------------------------------------------------------------------------- /19 C++ Inheritance/Example_01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Client{ 6 | public : void client_info(){ 7 | cout<<"Clinet Name : Joy"< 4 | using namespace std; 5 | class a1_class{ 6 | 7 | public : void getinfoA(){ 8 | 9 | cout<<"this is a class"< 2 | 3 | using namespace std; 4 | 5 | // Hierarchical Inheritance 6 | 7 | 8 | class car{ 9 | public: 10 | void carFunction(){ 11 | cout << "Car Function" << endl; 12 | } 13 | }; 14 | 15 | class bike : public car{ 16 | public: 17 | void bikeFunction(){ 18 | cout << "Bike Function" << endl; 19 | } 20 | }; 21 | 22 | class truck : public car{ 23 | public: 24 | void truckFunction(){ 25 | cout << "Truck Function" << endl; 26 | } 27 | }; 28 | 29 | int main(){ 30 | bike b; 31 | truck t; 32 | b.carFunction(); 33 | b.bikeFunction(); 34 | t.carFunction(); 35 | t.truckFunction(); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /19 C++ Inheritance/Hybrid inheritance/books.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Hybrid Inheritance 6 | 7 | 8 | class Book{ 9 | public: 10 | void bookFunction(){ 11 | cout << "Book Function" << endl; 12 | } 13 | }; 14 | 15 | class Author{ 16 | public: 17 | void authorFunction(){ 18 | cout << "Author Function" << endl; 19 | } 20 | }; 21 | 22 | class Publisher{ 23 | public: 24 | void publisherFunction(){ 25 | cout << "Publisher Function" << endl; 26 | } 27 | }; 28 | 29 | class BookDetails : public Book , public Author , public Publisher{ 30 | public: 31 | void bookDetailsFunction(){ 32 | cout << "Book Details Function" << endl; 33 | } 34 | }; 35 | 36 | class price : public BookDetails{ 37 | public: 38 | void priceFunction(){ 39 | cout << "Price Function" << endl; 40 | } 41 | }; 42 | 43 | int main(){ 44 | price p; 45 | p.bookFunction(); 46 | p.authorFunction(); 47 | p.publisherFunction(); 48 | p.bookDetailsFunction(); 49 | p.priceFunction(); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /19 C++ Inheritance/HybridInheritance.cpp: -------------------------------------------------------------------------------- 1 | // Hybrid Inheritance 2 | 3 | // Hierarchical Inheritance 4 | // Multilevel Inheritance 5 | #include 6 | using namespace std; 7 | class a1_class{ 8 | 9 | public : void getinfoA(){ 10 | 11 | cout<<"this is a class"< 3 | using namespace std; 4 | class a1_class{ 5 | 6 | public : void getinfoA(){ 7 | 8 | cout<<"this is a class"< 2 | 3 | using namespace std; 4 | 5 | // Multilevel Inheritance 6 | 7 | class Person{ 8 | public: 9 | void personFunction(){ 10 | cout << "Person Function" << endl; 11 | } 12 | }; 13 | 14 | class Student : public Person{ 15 | public: 16 | void studentFunction(){ 17 | cout << "Student Function" << endl; 18 | } 19 | }; 20 | 21 | class CollegeStudent : public Student{ 22 | public: 23 | void collegeStudentFunction(){ 24 | cout << "College Student Function" << endl; 25 | } 26 | }; 27 | 28 | int main(){ 29 | CollegeStudent cs; 30 | cs.personFunction(); 31 | cs.studentFunction(); 32 | cs.collegeStudentFunction(); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /19 C++ Inheritance/MultipleInheritance/Food: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/19 C++ Inheritance/MultipleInheritance/Food -------------------------------------------------------------------------------- /19 C++ Inheritance/MultipleInheritance/Food.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Multiple Inheritance 6 | 7 | class Food{ 8 | public: 9 | void foodFunction(){ 10 | cout << "Food Function" << endl; 11 | } 12 | }; 13 | 14 | class Client{ 15 | public: 16 | void clientFunction(){ 17 | cout << "Client Function" << endl; 18 | } 19 | }; 20 | 21 | class Product : public Food , public Client{ 22 | public: 23 | void productFunction(){ 24 | cout << "Product Function" << endl; 25 | } 26 | }; 27 | 28 | int main(){ 29 | Product p; 30 | p.foodFunction(); 31 | p.clientFunction(); 32 | p.productFunction(); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /19 C++ Inheritance/SingleInheritance/Client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/19 C++ Inheritance/SingleInheritance/Client -------------------------------------------------------------------------------- /19 C++ Inheritance/SingleInheritance/Client.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Single Inheritance 6 | 7 | class Client{ 8 | 9 | public: 10 | void clientFunction(){ 11 | cout << "Client Function" << endl; 12 | } 13 | 14 | 15 | }; 16 | 17 | class Product : public Client{ 18 | public: 19 | void productFunction(){ 20 | cout << "Product Function" << endl; 21 | } 22 | 23 | }; 24 | 25 | int main(){ 26 | Product p; 27 | p.clientFunction(); 28 | p.productFunction(); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /19 C++ Inheritance/Single_inheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | class a_class{ 7 | 8 | public : void getinfoA(){ 9 | 10 | cout<<"this is a class"< 2 | 3 | using namespace std; 4 | 5 | class info{ 6 | 7 | 8 | public : info(){ 9 | 10 | cout<<"welcome to p4n.in"< 25 | 26 | class Overloader { 27 | public: 28 | void display(int num) { 29 | std::cout << "Integer: " << num << std::endl; 30 | } 31 | 32 | void display(double num) { 33 | std::cout << "Double: " << num << std::endl; 34 | } 35 | }; 36 | 37 | int main() { 38 | Overloader obj; 39 | obj.display(5); 40 | obj.display(3.14); 41 | 42 | return 0; 43 | } 44 | ``` 45 | 46 | In this example, the `display` function is overloaded to handle both integers and doubles. 47 | 48 | ## Operator Overloading 49 | 50 | Operator overloading allows you to redefine the behavior of operators for user-defined data types. This enhances the expressive power of C++. 51 | 52 | ```cpp 53 | #include 54 | 55 | class Complex { 56 | private: 57 | double real; 58 | double imag; 59 | 60 | public: 61 | Complex() : real(0), imag(0) {} 62 | 63 | Complex operator + (const Complex& obj) { 64 | Complex temp; 65 | temp.real = real + obj.real; 66 | temp.imag = imag + obj.imag; 67 | return temp; 68 | } 69 | 70 | void display() { 71 | std::cout << "Real: " << real << ", Imaginary: " << imag << std::endl; 72 | } 73 | }; 74 | 75 | int main() { 76 | Complex num1, num2, result; 77 | // Set values for num1 and num2 78 | 79 | result = num1 + num2; // Operator overloading 80 | 81 | result.display(); 82 | 83 | return 0; 84 | } 85 | ``` 86 | 87 | In this example, the `+` operator is overloaded for the `Complex` class. 88 | 89 | ## Constructor Overloading 90 | 91 | Constructor overloading involves defining multiple constructors for a class, each with a different parameter list. 92 | 93 | ```cpp 94 | #include 95 | 96 | class Rectangle { 97 | private: 98 | int length; 99 | int width; 100 | 101 | public: 102 | Rectangle() : length(0), width(0) {} 103 | 104 | Rectangle(int l) : length(l), width(l) {} 105 | 106 | Rectangle(int l, int w) : length(l), width(w) {} 107 | 108 | void display() { 109 | std::cout << "Length: " << length << ", Width: " << width << std::endl; 110 | } 111 | }; 112 | 113 | int main() { 114 | Rectangle rect1, rect2(5), rect3(4, 8); 115 | 116 | rect1.display(); 117 | rect2.display(); 118 | rect3.display(); 119 | 120 | return 0; 121 | } 122 | ``` 123 | 124 | In this example, the `Rectangle` class has three constructors, allowing objects to be initialized in different ways. 125 | 126 | ## Conclusion 127 | 128 | C++ overloading is a versatile feature that contributes to the flexibility and expressiveness of your code. Understanding how to overload functions, operators, and constructors provides you with powerful tools for creating efficient and readable programs. 129 | 130 | I hope this comprehensive guide and examples have clarified the concepts of C++ overloading for you. For more tutorials and resources on C++ and programming, don't forget to explore [Codes with Pankaj](http://www.codeswithpankaj.com). Happy coding! -------------------------------------------------------------------------------- /21 C++ Polymorphism/Example1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Overloader { 4 | public: 5 | void display(int num) { 6 | cout << "Integer: " << num << endl; 7 | } 8 | 9 | void display(double num) { 10 | cout << "Double: " << num << endl; 11 | } 12 | void display(float num) { 13 | cout << "float: " << num << endl; 14 | } 15 | }; 16 | 17 | int main() { 18 | Overloader obj; 19 | obj.display(5); 20 | obj.display(3.14); 21 | obj.display(4.5); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /21 C++ Polymorphism/README.md: -------------------------------------------------------------------------------- 1 | # Unleashing the Power of C++ Polymorphism: A Comprehensive Guide 2 | 3 | ## Introduction 4 | 5 | Welcome to an in-depth tutorial on C++ polymorphism! Polymorphism is a fundamental concept in object-oriented programming that allows objects to be treated as instances of their base class, providing flexibility and extensibility. In this tutorial, we'll explore what polymorphism is, its types (compile-time and runtime polymorphism), and how it complements the various types of inheritance in C++. As you embark on this journey, make sure to check out more tutorials and resources on C++ at [Codes with Pankaj](http://www.codeswithpankaj.com). 6 | 7 | ## Table of Contents 8 | 9 | - [Unleashing the Power of C++ Polymorphism: A Comprehensive Guide](#unleashing-the-power-of-c-polymorphism-a-comprehensive-guide) 10 | - [Introduction](#introduction) 11 | - [Table of Contents](#table-of-contents) 12 | - [Understanding Polymorphism](#understanding-polymorphism) 13 | - [Types of Polymorphism](#types-of-polymorphism) 14 | - [Compile-Time Polymorphism (Static Binding)](#compile-time-polymorphism-static-binding) 15 | - [Runtime Polymorphism (Dynamic Binding)](#runtime-polymorphism-dynamic-binding) 16 | - [Polymorphism and Inheritance](#polymorphism-and-inheritance) 17 | - [Example: Polymorphism with Single Inheritance](#example-polymorphism-with-single-inheritance) 18 | - [Example: Polymorphism with Multiple Inheritance](#example-polymorphism-with-multiple-inheritance) 19 | - [Conclusion](#conclusion) 20 | 21 | ## Understanding Polymorphism 22 | 23 | Polymorphism, derived from Greek words "poly" (many) and "morphos" (forms), refers to the ability of a single entity to take many forms. In C++, polymorphism allows objects of different types to be treated as objects of a common base type. This simplifies code design and enhances flexibility. 24 | 25 | ## Types of Polymorphism 26 | 27 | ### Compile-Time Polymorphism (Static Binding) 28 | 29 | Compile-time polymorphism is also known as static binding. It occurs when the method or function to be invoked is determined at compile time. This is achieved through function overloading and operator overloading. 30 | 31 | **Example: Function Overloading** 32 | 33 | ```cpp 34 | #include 35 | 36 | class Calculator { 37 | public: 38 | int add(int a, int b) { 39 | return a + b; 40 | } 41 | 42 | double add(double a, double b) { 43 | return a + b; 44 | } 45 | }; 46 | 47 | int main() { 48 | Calculator calc; 49 | std::cout << calc.add(2, 3) << std::endl; // Calls int add(int, int) 50 | std::cout << calc.add(2.5, 3.5) << std::endl; // Calls double add(double, double) 51 | 52 | return 0; 53 | } 54 | ``` 55 | 56 | In this example, the `add` method is overloaded, and the appropriate version is selected at compile time based on the argument types. 57 | 58 | ### Runtime Polymorphism (Dynamic Binding) 59 | 60 | Runtime polymorphism is also known as dynamic binding or late binding. It occurs when the method to be invoked is determined at runtime. This is achieved through virtual functions and pointers. 61 | 62 | **Example: Virtual Functions** 63 | 64 | ```cpp 65 | #include 66 | 67 | class Shape { 68 | public: 69 | virtual void draw() { 70 | std::cout << "Drawing a shape" << std::endl; 71 | } 72 | }; 73 | 74 | class Circle : public Shape { 75 | public: 76 | void draw() override { 77 | std::cout << "Drawing a circle" << std::endl; 78 | } 79 | }; 80 | 81 | int main() { 82 | Shape* shapePtr; 83 | Circle circle; 84 | 85 | shapePtr = &circle; 86 | shapePtr->draw(); // Calls draw() based on the actual object type at runtime 87 | 88 | return 0; 89 | } 90 | ``` 91 | 92 | In this example, the `draw` function in the base class is marked as virtual, and the appropriate version is selected at runtime based on the actual type of the object. 93 | 94 | ## Polymorphism and Inheritance 95 | 96 | Polymorphism is closely tied to inheritance, another essential concept in object-oriented programming. Let's explore how polymorphism works in different types of inheritance. 97 | 98 | ### Example: Polymorphism with Single Inheritance 99 | 100 | ```cpp 101 | #include 102 | 103 | class Animal { 104 | public: 105 | virtual void sound() { 106 | std::cout << "Animal makes a sound" << std::endl; 107 | } 108 | }; 109 | 110 | class Dog : public Animal { 111 | public: 112 | void sound() override { 113 | std::cout << "Dog barks" << std::endl; 114 | } 115 | }; 116 | 117 | int main() { 118 | Animal* animalPtr; 119 | Dog dog; 120 | 121 | animalPtr = &dog; 122 | animalPtr->sound(); // Calls sound() based on the actual object type at runtime 123 | 124 | return 0; 125 | } 126 | ``` 127 | 128 | In this example, the `sound` function is marked as virtual in the base class `Animal`. When a `Dog` object is treated as an `Animal`, the overridden version of `sound` in the derived class `Dog` is invoked. 129 | 130 | ### Example: Polymorphism with Multiple Inheritance 131 | 132 | ```cpp 133 | #include 134 | 135 | class Engine { 136 | public: 137 | virtual void start() { 138 | std::cout << "Engine started" << std::endl; 139 | } 140 | }; 141 | 142 | class ElectricMotor { 143 | public: 144 | virtual void start() { 145 | std::cout << "Electric motor started" << std::endl; 146 | } 147 | }; 148 | 149 | class HybridCar : public Engine, public ElectricMotor { 150 | public: 151 | void start() override { 152 | std::cout << "Hybrid car started" << std::endl; 153 | } 154 | }; 155 | 156 | int main() { 157 | HybridCar hybridCar; 158 | 159 | hybridCar.Engine::start(); // Calls start() from Engine class 160 | hybridCar.ElectricMotor::start();// Calls start() from ElectricMotor class 161 | hybridCar.start(); // Calls overridden start() in HybridCar class 162 | 163 | return 0; 164 | } 165 | ``` 166 | 167 | In this example, the `HybridCar` class inherits from both `Engine` and `ElectricMotor`. The `start` function is overridden in `HybridCar`, and you can explicitly call the versions from each base class. 168 | 169 | ## Conclusion 170 | 171 | C++ polymorphism is a powerful mechanism that enhances code flexibility and readability. Understanding both compile-time and runtime polymorphism, along with their integration with inheritance, is crucial for designing effective and maintainable object-oriented programs. 172 | 173 | I hope this comprehensive guide and examples have provided you with a solid understanding of C++ polymorphism. For more tutorials and resources on C++ and programming, make sure to explore [Codes with Pankaj](http://www.codeswithpankaj.com). Happy coding! -------------------------------------------------------------------------------- /21 C++ Polymorphism/example2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class customers{ 4 | 5 | public : void info(){ 6 | cout<<"Custromers name : joy "<info(); 25 | 26 | orderObj.info(); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /22 C++ Abstraction/AbstractClasses.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Shape { 4 | public: virtual void draw() = 0; // Pure virtual function 5 | }; 6 | 7 | class Circle : public Shape { 8 | public:void draw() override { 9 | std::cout << "Drawing a circle" << std::endl; 10 | } 11 | }; 12 | 13 | int main() { 14 | Circle circle; 15 | circle.draw(); 16 | 17 | // Shape shape; // Error: Cannot instantiate an abstract class 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /22 C++ Abstraction/AbstractClasses1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Shape { 4 | public: 5 | virtual void draw() = 0; // Pure virtual function 6 | }; 7 | 8 | class Circle : public Shape { 9 | public: 10 | void draw() override { 11 | std::cout << "Drawing a circle" << std::endl; 12 | } 13 | }; 14 | 15 | class Rectangle : public Shape { 16 | public: 17 | void draw() override { 18 | std::cout << "Drawing a rectangle" << std::endl; 19 | } 20 | }; 21 | 22 | int main() { 23 | Circle circle; 24 | Rectangle rectangle; 25 | 26 | Shape* shapePtr = &circle; 27 | shapePtr->draw(); // Calls draw() based on the actual object type at runtime 28 | 29 | shapePtr = &rectangle; 30 | shapePtr->draw(); // Calls draw() based on the actual object type at runtime 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /22 C++ Abstraction/README.md: -------------------------------------------------------------------------------- 1 | Certainly! If you'd like to omit the section on types of inheritance, here's the modified version of the tutorial: 2 | 3 | # Mastering C++ Abstraction: A Comprehensive Guide 4 | 5 | ## Introduction 6 | 7 | Welcome to an in-depth tutorial on C++ Abstraction! Abstraction is a key concept in object-oriented programming that allows you to hide the complex implementation details and focus on essential features. In this tutorial, we'll explore what abstraction is, its importance, and how it is implemented in C++. We'll also delve into how abstraction is utilized in real-world examples. As you embark on this educational journey, be sure to explore more tutorials and resources on C++ at [Codes with Pankaj](http://www.codeswithpankaj.com). 8 | 9 | ## Table of Contents 10 | 11 | - [Mastering C++ Abstraction: A Comprehensive Guide](#mastering-c-abstraction-a-comprehensive-guide) 12 | - [Introduction](#introduction) 13 | - [Table of Contents](#table-of-contents) 14 | - [Understanding Abstraction](#understanding-abstraction) 15 | - [Implementing Abstraction in C++](#implementing-abstraction-in-c) 16 | - [Abstract Classes](#abstract-classes) 17 | - [Abstraction and Inheritance](#abstraction-and-inheritance) 18 | - [Example: Abstraction with Single Inheritance](#example-abstraction-with-single-inheritance) 19 | - [Example: Abstraction with Multiple Inheritance](#example-abstraction-with-multiple-inheritance) 20 | - [Conclusion](#conclusion) 21 | 22 | ## Understanding Abstraction 23 | 24 | Abstraction is the process of hiding the complex implementation details of an object and exposing only the relevant features. It allows you to focus on what an object does rather than how it achieves it. In C++, abstraction is achieved through abstract classes and pure virtual functions. 25 | 26 | ## Implementing Abstraction in C++ 27 | 28 | ### Abstract Classes 29 | 30 | An abstract class is a class that cannot be instantiated and may contain one or more pure virtual functions. A pure virtual function is a virtual function declared in the base class that has no implementation and must be overridden by derived classes. 31 | 32 | ```cpp 33 | #include 34 | 35 | class Shape { 36 | public: 37 | virtual void draw() = 0; // Pure virtual function 38 | }; 39 | 40 | class Circle : public Shape { 41 | public: 42 | void draw() override { 43 | std::cout << "Drawing a circle" << std::endl; 44 | } 45 | }; 46 | 47 | int main() { 48 | Circle circle; 49 | circle.draw(); 50 | 51 | // Shape shape; // Error: Cannot instantiate an abstract class 52 | 53 | return 0; 54 | } 55 | ``` 56 | 57 | In this example, `Shape` is an abstract class with a pure virtual function `draw()`. The `Circle` class, which is derived from `Shape`, must implement the `draw` function. 58 | 59 | ## Abstraction and Inheritance 60 | 61 | Abstraction and inheritance go hand in hand. Abstract classes are often used as base classes in inheritance hierarchies, providing a blueprint for derived classes. 62 | 63 | ### Example: Abstraction with Single Inheritance 64 | 65 | ```cpp 66 | #include 67 | 68 | class Shape { 69 | public: 70 | virtual void draw() = 0; // Pure virtual function 71 | }; 72 | 73 | class Circle : public Shape { 74 | public: 75 | void draw() override { 76 | std::cout << "Drawing a circle" << std::endl; 77 | } 78 | }; 79 | 80 | class Rectangle : public Shape { 81 | public: 82 | void draw() override { 83 | std::cout << "Drawing a rectangle" << std::endl; 84 | } 85 | }; 86 | 87 | int main() { 88 | Circle circle; 89 | Rectangle rectangle; 90 | 91 | Shape* shapePtr = &circle; 92 | shapePtr->draw(); // Calls draw() based on the actual object type at runtime 93 | 94 | shapePtr = &rectangle; 95 | shapePtr->draw(); // Calls draw() based on the actual object type at runtime 96 | 97 | return 0; 98 | } 99 | ``` 100 | 101 | In this example, `Shape` is an abstract class, and both `Circle` and `Rectangle` are derived from it. The `draw` function is implemented differently in each derived class, demonstrating polymorphism. 102 | 103 | ### Example: Abstraction with Multiple Inheritance 104 | 105 | ```cpp 106 | #include 107 | 108 | class Engine { 109 | public: 110 | virtual void start() = 0; // Pure virtual function 111 | }; 112 | 113 | class ElectricMotor { 114 | public: 115 | virtual void start() = 0; // Pure virtual function 116 | }; 117 | 118 | class HybridCar : public Engine, public ElectricMotor { 119 | public: 120 | void start() override { 121 | std::cout << "Hybrid car started" << std::endl; 122 | } 123 | }; 124 | 125 | int main() { 126 | HybridCar hybridCar; 127 | 128 | Engine* enginePtr = &hybridCar; 129 | enginePtr->start(); // Calls start() from Engine class 130 | 131 | ElectricMotor* motorPtr = &hybridCar; 132 | motorPtr->start(); // Calls start() from ElectricMotor class 133 | 134 | return 0; 135 | } 136 | ``` 137 | 138 | In this example, `HybridCar` is derived from both `Engine` and `ElectricMotor`. It implements the `start` function differently, and objects of `HybridCar` can be treated as instances of both base classes. 139 | 140 | ## Conclusion 141 | 142 | C++ abstraction allows you to design programs at a higher level by focusing on essential features and hiding implementation details. Abstract classes and pure virtual functions facilitate the creation of flexible and extensible code. When combined with inheritance, abstraction becomes a powerful tool for designing complex systems. 143 | 144 | I hope this comprehensive guide and examples have provided you with a solid understanding of C++ abstraction. For more tutorials and resources on C++ and programming, make sure to explore [Codes with Pankaj](http://www.codeswithpankaj.com). Happy coding! -------------------------------------------------------------------------------- /23 C++ Encapsulation/CreditCard.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class CreditCard{ 6 | 7 | private : string cardNumber; 8 | private : int cvv; 9 | private : string bankname; 10 | 11 | 12 | // using custom constructor 13 | 14 | public : CreditCard(string cardNumber, int cvv, string bankname){ 15 | this->cardNumber = cardNumber; 16 | this->cvv = cvv; 17 | this->bankname = bankname; 18 | } 19 | 20 | // setter methods 21 | void set_cardNumber(string cardNumber){ 22 | this->cardNumber = cardNumber; 23 | } 24 | 25 | void set_cvv(int cvv){ 26 | this->cvv = cvv; 27 | } 28 | 29 | void set_bankname(string bankname){ 30 | this->bankname = bankname; 31 | } 32 | 33 | // getter methods 34 | 35 | string get_cardNumber(){ 36 | return this->cardNumber; 37 | } 38 | 39 | int get_cvv(){ 40 | return this->cvv; 41 | } 42 | 43 | string get_bankname(){ 44 | return this->bankname; 45 | } 46 | }; 47 | 48 | int main(){ 49 | 50 | CreditCard c1("123456789", 123, "HDFC"); 51 | cout << "Card Number : " << c1.get_cardNumber() << endl; 52 | cout << "CVV : " << c1.get_cvv() << endl; 53 | cout << "Bank Name : " << c1.get_bankname() << endl; 54 | 55 | // update cvv number 56 | c1.set_cvv(456); 57 | 58 | cout << "Card Number : " << c1.get_cardNumber() << endl; 59 | cout << "CVV : " << c1.get_cvv() << endl; 60 | cout << "Bank Name : " << c1.get_bankname() << endl; 61 | 62 | return 0; 63 | 64 | } -------------------------------------------------------------------------------- /23 C++ Encapsulation/CreditCard.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/23 C++ Encapsulation/CreditCard.exe -------------------------------------------------------------------------------- /23 C++ Encapsulation/Employee.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Employee{ 6 | 7 | // employee infomation 8 | 9 | public: string name; 10 | public: int age; 11 | public: string company; 12 | public: int salary; 13 | 14 | // encapsulation 15 | public: void set_name(string Name){ 16 | name = Name; 17 | } 18 | public: void set_age(int Age){ 19 | age = Age; 20 | } 21 | public: void set_company(string Company){ 22 | company = Company; 23 | } 24 | public: void set_salary(int Salary){ 25 | salary = Salary; 26 | } 27 | 28 | public: string get_name(){ 29 | return name; 30 | } 31 | public: int get_age(){ 32 | return age; 33 | } 34 | public: string get_company(){ 35 | return company; 36 | } 37 | public: int get_salary(){ 38 | return salary; 39 | } 40 | 41 | }; 42 | 43 | int main(){ 44 | 45 | Employee emp1; 46 | 47 | emp1.set_name("John"); 48 | emp1.set_age(25); 49 | emp1.set_company("Google"); 50 | emp1.set_salary(100000); 51 | 52 | cout<<"Name: "< 2 | using namespace std; 3 | class data{ 4 | string name; 5 | int age; 6 | public : data(string Name , int Age){ 7 | name = Name; 8 | age = Age; 9 | } 10 | void set_name(string Name){ 11 | name = Name; 12 | } 13 | void set_age(int Age){ 14 | age = Age; 15 | } 16 | string get_name(){ 17 | return name; 18 | } 19 | int get_age(){ 20 | return age; 21 | } 22 | }; 23 | int main(){ 24 | 25 | data dataObj("joy",12); 26 | cout<<"your name : "< 2 | 3 | using namespace std; 4 | 5 | 6 | // create a Encapsulation class 7 | 8 | 9 | class student 10 | { 11 | // private data member 12 | private: 13 | string name; 14 | int age; 15 | int rollno; 16 | 17 | // public member function to set values 18 | public: void setValues(string sname, int sage, int srollno) 19 | { 20 | name = sname; 21 | age = sage; 22 | rollno = srollno; 23 | } 24 | 25 | // public member function to display values 26 | void display() 27 | { 28 | cout << "Name: " << name << endl; 29 | cout << "Age: " << age << endl; 30 | cout << "Roll No: " << rollno << endl; 31 | } 32 | }; 33 | 34 | int main() 35 | { 36 | student s1; 37 | s1.setValues("John", 23, 101); 38 | s1.display(); 39 | 40 | student s2; 41 | s2.setValues("Doe", 25, 102); 42 | s2.display(); 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /23 C++ Encapsulation/EncapsulationExample.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/23 C++ Encapsulation/EncapsulationExample.exe -------------------------------------------------------------------------------- /24 C++ Interfaces/Example2.cpp: -------------------------------------------------------------------------------- 1 | 2 | // C++ program to implement an Interface 3 | #include 4 | #include 5 | using namespace std; 6 | // Interface(Abstract class 7 | // with pure virtual function) 8 | class Scholar 9 | { 10 | public: 11 | virtual string returnString() = 0; 12 | }; 13 | class child : public Scholar 14 | { 15 | public: 16 | string returnString() 17 | { 18 | return "ScholarHat"; 19 | } 20 | }; 21 | int main() 22 | { 23 | child childObj; 24 | Scholar* ptr; 25 | ptr = &childObj; 26 | cout << ptr->returnString(); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /24 C++ Interfaces/InterfaceExample.cpp: -------------------------------------------------------------------------------- 1 | // Demonstrate the working of Data Abstraction 2 | #include 3 | using namespace std; 4 | class implementAbstraction 5 | { 6 | private: 7 | int A, B; 8 | public: 9 | // method to set values of 10 | // private members 11 | void set(int x, int y) 12 | { 13 | A = x; 14 | B = y; 15 | } 16 | void display() 17 | { 18 | cout << "A = " << A << endl; 19 | cout << "B = " << B << endl; 20 | } 21 | }; 22 | int main() 23 | { 24 | implementAbstraction obj; 25 | obj.set(20, 30); 26 | obj.display(); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /24 C++ Interfaces/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/24 C++ Interfaces/README.md -------------------------------------------------------------------------------- /25 C++ Files and Streams/Create_Write_To_File.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | // Create and open a text file 7 | ofstream MyFile("p4n.txt"); 8 | 9 | // Write to the file 10 | MyFile << "Files can be tricky, but it is fun enough!"; 11 | 12 | // Close the file 13 | MyFile.close(); 14 | } -------------------------------------------------------------------------------- /25 C++ Files and Streams/Read_To_File.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | // Create a text string, which is used to output the text file 7 | string myText; 8 | 9 | // Read from the text file 10 | ifstream MyReadFile("p4n.txt"); 11 | 12 | // Use a while loop together with the getline() function to read the file line by line 13 | while (getline (MyReadFile, myText)) { 14 | // Output the text from the file 15 | cout << myText; 16 | } 17 | 18 | // Close the file 19 | MyReadFile.close(); 20 | } -------------------------------------------------------------------------------- /Example/Array/MultiArray: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/Example/Array/MultiArray -------------------------------------------------------------------------------- /Example/Array/MultiArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | int data[3][3]; 9 | 10 | data[0][0] = 21; 11 | data[1][0] = 22; 12 | data[2][0] = 23; 13 | 14 | data[0][1] = 24; 15 | data[1][1] = 25; 16 | data[2][1] = 26; 17 | 18 | data[0][2] = 27; 19 | data[1][2] = 28; 20 | data[2][2] = 29; 21 | 22 | // cout<<"Item from array : -> "< "< 2 | 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | int data[4]; 9 | 10 | data[0] = 27; 11 | data[1] = 26; 12 | data[2] = 25; 13 | data[3] = 24; 14 | 15 | //cout<<"Element on array "<>n; 12 | 13 | int data[n]; 14 | 15 | for(int i = 0 ; i >data[i]; 18 | } 19 | 20 | //cout<<"Element on array "< 2 | 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | 9 | string name; 10 | int age; 11 | float height; 12 | char gender; 13 | 14 | cout<<"Enter Your Name "<>name; 16 | cout<<"Enter Your Age "<>age; 18 | cout<<"Enter Your Height "<>height; 20 | cout<<"Enter Your Gender "<>gender; 22 | 23 | cout<<"your name is "< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | 8 | string name = "joy"; 9 | int age = 23; 10 | float height = 5.5; 11 | char gender = 'M'; 12 | 13 | cout<<"your name is "< 2 | 3 | using namespace std; 4 | 5 | 6 | class Employee{ 7 | 8 | public : void Employee_info(){ 9 | cout <<"Employee Information"<< endl; 10 | cout <<"Name: John Doe"< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // Datatype int 8 | int a = 10; 9 | cout<<"a: "< 2 | 3 | using namespace std; 4 | 5 | 6 | // Encapsulation 7 | 8 | 9 | class customer{ 10 | 11 | 12 | int creditlimit; 13 | string bankname; 14 | 15 | public : customer(int CreditLimit ,string BankName){ 16 | creditlimit = CreditLimit; 17 | bankname = BankName; 18 | } 19 | 20 | public : void setcreditlimit(int CreditLimit){ 21 | creditlimit = CreditLimit; 22 | } 23 | public : int getcreditlimit(){ 24 | return creditlimit; 25 | } 26 | 27 | public : void setbankname(string BankName){ 28 | bankname = BankName; 29 | } 30 | public : string getbankname(){ 31 | return bankname; 32 | } 33 | }; 34 | 35 | 36 | 37 | int main(){ 38 | 39 | customer objcustomer(45000,"SBI"); 40 | cout<<"Credit Limit : "< 2 | 3 | using namespace std; 4 | 5 | class Customers 6 | { 7 | public: void Customer_info(){ 8 | cout<<"Nishant : "< 2 | 3 | using namespace std; 4 | 5 | class client{ 6 | 7 | public: void client_info(){ 8 | cout<<"Client Name: Nishant"< 2 | 3 | using namespace std; 4 | 5 | class student 6 | { 7 | public : void info() 8 | { 9 | cout<<"I am a student"< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | cout<<"Enter Your Name ..."<>name; 10 | cout<<"Enter Your Age ... "<>age; 13 | cout<<"Enter Your Height ..."<>height; 16 | cout<<"Enter Your Weight ..."<>weight; 19 | cout<<"Your Name is "< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | 8 | cout<<"welcome to codeswithpankaj.com"< 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleIdentifier 8 | com.apple.xcode.dsym.basic 9 | CFBundleInfoDictionaryVersion 10 | 6.0 11 | CFBundlePackageType 12 | dSYM 13 | CFBundleSignature 14 | ???? 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleVersion 18 | 1 19 | 20 | 21 | -------------------------------------------------------------------------------- /Example/Lecture01/basic.dSYM/Contents/Resources/DWARF/basic: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/Example/Lecture01/basic.dSYM/Contents/Resources/DWARF/basic -------------------------------------------------------------------------------- /Example/Lecture01/variable: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/Example/Lecture01/variable -------------------------------------------------------------------------------- /Example/Lecture01/variable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | 7 | 8 | int main(){ 9 | 10 | int age = 23; 11 | string name = "joy"; 12 | float height = 4.5; 13 | bool isMarried = false; 14 | char gender = 'M'; 15 | double salary = 50000.0; 16 | long long mobile = 1234567890; 17 | short int weight = 50; 18 | 19 | 20 | //output all variable 21 | 22 | cout<<"your name is : "< 2 | 3 | using namespace std; 4 | 5 | 6 | class Customer { 7 | 8 | public : void CustomerInfo(string name, string address, string phone) { 9 | cout << "Customer Name: " << name << endl; 10 | cout << "Customer Address: " << address << endl; 11 | cout << "Customer Phone: " << phone << endl; 12 | } 13 | 14 | public : void bill(int price,int gst){ 15 | int gst_amount = price * gst / 100; 16 | int Mrp = price + gst_amount; 17 | cout << "Price: " << price << endl; 18 | cout << "GST: " << gst << "%" << endl; 19 | cout << "GST Amount: " << gst_amount << endl; 20 | cout << "MRP: " << Mrp << endl; 21 | cout<<"-------------------"< 2 | 3 | using namespace std; 4 | 5 | class Student{ 6 | 7 | public: int total_marks(int java, int cpp, int python){ 8 | 9 | int total = java + cpp + python; 10 | return total; 11 | 12 | } 13 | 14 | public: float average_marks(int java, int cpp, int python){ 15 | 16 | float average = (java + cpp + python) / 3; 17 | return average; 18 | 19 | } 20 | 21 | public: char grade(int java, int cpp, int python){ 22 | 23 | float average = (java + cpp + python) / 3; 24 | if(average >= 90){ 25 | return 'A'; 26 | }else if(average >= 80){ 27 | return 'B'; 28 | }else if(average >= 70){ 29 | return 'C'; 30 | }else if(average >= 60){ 31 | return 'D'; 32 | }else{ 33 | return 'F'; 34 | } 35 | 36 | } 37 | 38 | public: string result(int java, int cpp, int python){ 39 | 40 | float average = (java + cpp + python) / 3; 41 | if(average >= 60){ 42 | return "Pass"; 43 | }else{ 44 | return "Fail"; 45 | } 46 | 47 | } 48 | 49 | }; 50 | 51 | 52 | int main(){ 53 | 54 | Student student; 55 | cout<<"Total Marks: "< 2 | 3 | using namespace std; 4 | 5 | class Report{ 6 | 7 | public : int JAVAMarks(){ 8 | return 90; 9 | } 10 | 11 | public : float CPPMarks(){ 12 | return 85.5; 13 | } 14 | 15 | public : char Grade(){ 16 | return 'A'; 17 | } 18 | 19 | public : string Result(){ 20 | return "Pass"; 21 | } 22 | 23 | }; 24 | 25 | int main(){ 26 | 27 | Report report; 28 | cout<<"Java Marks: "< 2 | 3 | using namespace std; 4 | 5 | class CreditCard { 6 | 7 | public : void CardInfo() { 8 | cout << "Card Number: 1234 5678 9101 1121" << endl; 9 | cout << "Card Holder: John Doe" << endl; 10 | cout << "Card Type: MasterCard" << endl; 11 | cout << "Card Expiry: 12/2022" << endl; 12 | } 13 | public : void CustomerInfo() { 14 | cout << "Customer Name: John Doe" << endl; 15 | cout << "Customer Address: 1234, 5th Avenue, New York" << endl; 16 | cout << "Customer Phone: 123-456-7890" << endl; 17 | } 18 | 19 | }; 20 | 21 | int main() { 22 | 23 | CreditCard card; 24 | card.CardInfo(); 25 | // call multiple times 26 | card.CardInfo(); 27 | card.CustomerInfo(); 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Example/OPPS/Clients: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/Example/OPPS/Clients -------------------------------------------------------------------------------- /Example/OPPS/Clients.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class foods{ 5 | 6 | public : string foodName = "Burger"; 7 | int price = 100; 8 | 9 | public : void getFood(){ 10 | cout << "Food Name: " << foodName << endl; 11 | cout << "Price: " << price << endl; 12 | } 13 | 14 | }; 15 | 16 | int main(){ 17 | // create a class object 18 | foods f; 19 | f.getFood(); 20 | 21 | // set value into variable 22 | f.foodName = "Pizza"; 23 | f.price = 200; 24 | 25 | // get value from variable 26 | f.getFood(); 27 | 28 | return 0; 29 | 30 | } -------------------------------------------------------------------------------- /Example/Operator: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/Example/Operator -------------------------------------------------------------------------------- /Example/Operator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // Operator Arithmetic 8 | 9 | int a = 10; 10 | int b = 20; 11 | cout<<"a + b: "< e: "<<(d>e)<= e: "<<(d>=e)< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // declare variables 8 | 9 | int a = 10; 10 | int b = 20; 11 | 12 | // print variables 13 | cout<<"a: "< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | cout<<"Hello World!"< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | // find smallest number among three numbers 7 | 8 | int a, b, c; 9 | cout << "Enter three numbers: "; 10 | cin >> a >> b >> c; 11 | 12 | if(a 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // smallest number 8 | 9 | int num1 = 10; 10 | int num2 = 2; 11 | 12 | if(num1 < num2){ 13 | cout << "Smallest number is " << num1 << endl; 14 | }else{ 15 | cout << "Smallest number is " << num2 << endl; 16 | } 17 | 18 | 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Example/output/Loop: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/Example/output/Loop -------------------------------------------------------------------------------- /Example/output/Loop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int main(){ 5 | 6 | // print 1 to 10 number using for loop 7 | for(int i = 1; i <= 10; i++){ 8 | cout << i << " "; 9 | } 10 | 11 | // print 1 to 10 number using while loop 12 | 13 | int i = 1; 14 | while(i <= 10){ 15 | cout << i << " "; 16 | i++; 17 | } 18 | 19 | // print 1 to 10 number using do while loop 20 | 21 | i = 1; 22 | do{ 23 | cout << i << " "; 24 | i++; 25 | }while(i <= 10); 26 | 27 | return 0; 28 | 29 | } -------------------------------------------------------------------------------- /Example/output/SwitchCase: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/Example/output/SwitchCase -------------------------------------------------------------------------------- /Example/output/SwitchCase.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // print month name using switch case 8 | 9 | int month; 10 | cout << "Enter month number: "; 11 | cin >> month; 12 | 13 | switch(month){ 14 | case 1: 15 | cout << "January" << endl; 16 | break; 17 | case 2: 18 | cout << "February" << endl; 19 | break; 20 | case 3: 21 | cout << "March" << endl; 22 | break; 23 | case 4: 24 | cout << "April" << endl; 25 | break; 26 | case 5: 27 | cout << "May" << endl; 28 | break; 29 | case 6: 30 | cout << "June" << endl; 31 | break; 32 | case 7: 33 | cout << "July" << endl; 34 | break; 35 | case 8: 36 | cout << "August" << endl; 37 | break; 38 | case 9: 39 | cout << "September" << endl; 40 | break; 41 | case 10: 42 | cout << "October" << endl; 43 | break; 44 | case 11: 45 | cout << "November" << endl; 46 | break; 47 | case 12: 48 | cout << "December" << endl; 49 | break; 50 | default: 51 | cout << "Invalid month number" << endl; 52 | } 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /Example/output/output/SwitchCase: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/Example/output/output/SwitchCase -------------------------------------------------------------------------------- /Example/output/output/SwitchCase.dSYM/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleIdentifier 8 | com.apple.xcode.dsym.SwitchCase 9 | CFBundleInfoDictionaryVersion 10 | 6.0 11 | CFBundlePackageType 12 | dSYM 13 | CFBundleSignature 14 | ???? 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleVersion 18 | 1 19 | 20 | 21 | -------------------------------------------------------------------------------- /Example/output/output/SwitchCase.dSYM/Contents/Resources/DWARF/SwitchCase: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/Example/output/output/SwitchCase.dSYM/Contents/Resources/DWARF/SwitchCase -------------------------------------------------------------------------------- /Example/output/userInput: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/Example/output/userInput -------------------------------------------------------------------------------- /Example/output/userInput.dSYM/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleIdentifier 8 | com.apple.xcode.dsym.userInput 9 | CFBundleInfoDictionaryVersion 10 | 6.0 11 | CFBundlePackageType 12 | dSYM 13 | CFBundleSignature 14 | ???? 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleVersion 18 | 1 19 | 20 | 21 | -------------------------------------------------------------------------------- /Example/output/userInput.dSYM/Contents/Resources/DWARF/userInput: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/Example/output/userInput.dSYM/Contents/Resources/DWARF/userInput -------------------------------------------------------------------------------- /Example/userInput.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // User input 8 | string name; 9 | cout<<"Enter your name: "; 10 | cin>>name; 11 | int age; 12 | cout<<"Enter your age: "; 13 | cin>>age; 14 | float salary; 15 | cout<<"Enter your salary: "; 16 | cin>>salary; 17 | char ltr; 18 | cout<<"Enter a letter: "; 19 | cin>>ltr; 20 | 21 | cout<<"Name: "< 2 | //#include 3 | //#include 4 | using namespace std; 5 | int main() 6 | //header file for input/output //header file for setprecision function 7 | { 8 | //clrscr(); 9 | float a,b,c,d; 10 | cout<<"Enter any four numbers."<>a; 13 | cout<<"2nd number: "; 14 | cin>>b; 15 | cout<<"3rd number: "; 16 | cin>>c; 17 | cout<<"4th number: "; 18 | cin>>d; 19 | 20 | 21 | 22 | //end of outer else part 23 | //getch(); 24 | return 0; 25 | } 26 | //end of program -------------------------------------------------------------------------------- /Questions/Question_4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/Questions/Question_4 -------------------------------------------------------------------------------- /Questions/Question_4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | 5 | int x , y , z; 6 | x = 5; 7 | y = 6; 8 | z = (++x) + y; // x becomes 6 , so z = 6 + 6 = 12 9 | y = y + x++; // x is 6 so y = 6 + 6 = 12 and then x becomes 7 10 | y++; // y becomes 13 11 | cout<< y << endl; // output is 13 12 | cout<< z << endl; // output is 12 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Questions/Question_5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/Questions/Question_5 -------------------------------------------------------------------------------- /Questions/Question_5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | 5 | int matrix[3][3]; 6 | // adding data row wise 7 | matrix[0][0] = 21; 8 | matrix[0][1] = 22; 9 | matrix[0][2] = 23; 10 | matrix[1][0] = 24; 11 | matrix[1][1] = 25; 12 | matrix[1][2] = 26; 13 | matrix[2][0] = 27; 14 | matrix[2][1] = 28; 15 | matrix[2][2] = 29; 16 | // printing data row wise 17 | //cout<< matrix[1][2] << endl; // output is 26 18 | 19 | // for(int i = 0 ; i < 3; i++){ 20 | // for(int j = 0; j < 3; j++){ 21 | // //cout<< matrix[i][j] << " "; // print the data of the matrix 22 | // cout< 2 | 3 | 4 | #define MAX 100 5 | 6 | using namespace std; 7 | 8 | void sort(int arr[], int n) { 9 | int temp; 10 | for (int i = 0; i < n - 1; i++) { // run n-1 times (number of passes) 11 | for (int j = 0; j < n - i - 1; j++) { // inner looop - compare adjacent elements and swap them if first element is greater than second 12 | if (arr[j] > arr[j + 1]) { // swap if first element is greater than second 13 | temp = arr[j]; // uses temp variable to swap 14 | arr[j] = arr[j + 1]; 15 | arr[j + 1] = temp; 16 | } 17 | } 18 | } 19 | } 20 | 21 | int main() { 22 | 23 | 24 | int arr[MAX], n, i; 25 | 26 | cout << "Enter the number of elements: "; 27 | cin >> n; 28 | 29 | cout << "Enter " << n << " elements:\n"; 30 | for (i = 0; i < n; i++) { 31 | cin >> arr[i]; 32 | } 33 | 34 | cout << "\nUnsorted array:\n"; 35 | for (i = 0; i < n; i++) { 36 | cout << arr[i] << " "; 37 | } 38 | 39 | sort(arr, n); 40 | 41 | cout << "\n\nSorted array in ascending order:\n"; 42 | for (i = 0; i < n; i++) { 43 | cout << arr[i] << " "; 44 | } 45 | 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Questions/Question_Array01.dSYM/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleIdentifier 8 | com.apple.xcode.dsym.Question_Array01 9 | CFBundleInfoDictionaryVersion 10 | 6.0 11 | CFBundlePackageType 12 | dSYM 13 | CFBundleSignature 14 | ???? 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleVersion 18 | 1 19 | 20 | 21 | -------------------------------------------------------------------------------- /Questions/Question_Array01.dSYM/Contents/Resources/DWARF/Question_Array01: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/Questions/Question_Array01.dSYM/Contents/Resources/DWARF/Question_Array01 -------------------------------------------------------------------------------- /Questions/Question_a_1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/Questions/Question_a_1 -------------------------------------------------------------------------------- /Questions/Question_a_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // while loop example 8 | // start point (initialization) 9 | // end point (condition) 10 | // output (print statement) 11 | // increment ++ or decrement -- (update) 12 | 13 | 14 | int i = 1; // initialization 15 | 16 | while(i <= 10){ // condition 17 | cout << i << endl; // output 18 | i++; // update 19 | 20 | } 21 | 22 | return 0; 23 | 24 | } -------------------------------------------------------------------------------- /Questions/Question_a_11: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/Questions/Question_a_11 -------------------------------------------------------------------------------- /Questions/Question_a_11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | // while loop example 5 | // start point (initialization) 6 | // end point (condition) 7 | // output (print statement) 8 | // increment ++ or decrement -- (update) 9 | // ---------Explanation--------- 10 | // the loop starts with i = 1 11 | // it prints of i and increments it in ech iteration 12 | // the loop stops when i is greater than 10 13 | // ----------------------------- 14 | int i = 1; // initialization 15 | 16 | while(i <= 10){ // condition 17 | cout << i << endl; // output 18 | i++; // update 19 | 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Questions/Question_a_2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/Questions/Question_a_2 -------------------------------------------------------------------------------- /Questions/Question_a_2.cpp: -------------------------------------------------------------------------------- 1 | // write a program to find whether a given character is a vowel or not. 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | char ch; 8 | cout << "Enter a character: "; 9 | cin >> ch; 10 | 11 | // convert to lower case 12 | ch = tolower(ch); 13 | 14 | if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') 15 | { 16 | cout << ch << " is a vowel." << endl; 17 | }else{ 18 | cout << ch << " is a not vawel." << endl; 19 | } 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Questions/Question_a_3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/Questions/Question_a_3 -------------------------------------------------------------------------------- /Questions/Question_a_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int a = 10; 6 | int b = 7; 7 | 8 | // find the maximum of two numbers 9 | //a > b ? cout << a : cout << b; 10 | // condition ? true : false 11 | 12 | a > b ? cout << "a is greater" : cout << "b is greater"; 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Questions/Question_a_3.dSYM/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleIdentifier 8 | com.apple.xcode.dsym.Question_a_3 9 | CFBundleInfoDictionaryVersion 10 | 6.0 11 | CFBundlePackageType 12 | dSYM 13 | CFBundleSignature 14 | ???? 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleVersion 18 | 1 19 | 20 | 21 | -------------------------------------------------------------------------------- /Questions/Question_a_3.dSYM/Contents/Resources/DWARF/Question_a_3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pankaj-Str/CPP-Programming/6a004c1a54afe1351cd184ed4d4f659c2cb96704/Questions/Question_a_3.dSYM/Contents/Resources/DWARF/Question_a_3 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C++ Learning Path 2 | 3 | | Day Number | Topic | 4 | |--------------|------------------------------| 5 | | Day 1 | Overview / Introduction of C++ | 6 | | Day 2 | Comments | 7 | | Day 3 | Data Types | 8 | | Day 4 | Variable Types | 9 | | Day 5 | Constants/Literals | 10 | | Day 6 | Storage Classes | 11 | | Day 7 | Operators | 12 | | Day 8 | Loop Types | 13 | | Day 9 | Decision Making | 14 | | Day 10 | Functions | 15 | | Day 11 | Numbers | 16 | | Day 12 | Arrays | 17 | | Day 13 | Strings | 18 | | Day 14 | Pointers | 19 | | Day 15 | References | 20 | | Day 16 | Date & Time | 21 | | Day 17 | Basic Input/Output | 22 | | Day 18 | Inheritance | 23 | | Day 19 | Classes & Objects | 24 | | Day 20 | Overloading | 25 | | Day 21 | Polymorphism | 26 | | Day 22 | Abstraction | 27 | | Day 23 | Encapsulation | 28 | | Day 24 | Interfaces | 29 | | Day 25 | Files and Streams | 30 | | Day 26 | Exception Handling | 31 | | Day 27 | Dynamic Memory | 32 | | Day 28 | Namespaces | 33 | | Day 29 | Templates | 34 | | Day 30 | Preprocessor | 35 | | Day 31 | Signal Handling | 36 | | Day 32 | Multithreading | 37 | | Day 33 | Web Programming | 38 | | Day 34 | Questions and Answers | 39 | --------------------------------------------------------------------------------