├── Exercises ├── leap_year.cpp └── palindrom.cpp ├── Loops ├── forLoop.cpp ├── whileLoop.cpp └── doWhileLoop.cpp ├── breakAndContinue.cpp ├── OOP └── class.cpp ├── array.cpp ├── strings.cpp ├── switchStatement.cpp ├── userInput.cpp ├── function.cpp ├── variableAndDataTypes.cpp ├── conditionsAndOperators.cpp └── README.md /Exercises/leap_year.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int year; 6 | cout << "Enter the year: "; 7 | cin >> year; 8 | 9 | if (year < 1000 || year > 9999) { 10 | cout << "Invalid year"; 11 | } 12 | else if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { 13 | cout << "Leap year"; 14 | } 15 | else { 16 | cout << "Not a leap year"; 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Loops/forLoop.cpp: -------------------------------------------------------------------------------- 1 | // for loop is used when we know the exact number of iterations 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | 8 | // for (initialization; condition; update) { 9 | // body 10 | // } 11 | 12 | // initialization - initialize a variable 13 | // condition - if true the loop is executed 14 | // update - increment or decrement of initialize variable 15 | 16 | for(int i=0; i<10; i++) { 17 | cout<< i<< " Hello, World!"<< endl; 18 | } 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Exercises/palindrom.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool is_palindrome(string text){ 5 | 6 | int len = text.length(); 7 | 8 | int j = len - 1; 9 | 10 | for(int i = 0; i < len; i++){ 11 | if(text[i] == text[j]){ 12 | return true; 13 | } 14 | j--; 15 | } 16 | return false; 17 | 18 | } 19 | 20 | int main(){ 21 | 22 | cout << is_palindrome("bo ob") << "\n"; 23 | cout << is_palindrome("ad a") << "\n"; 24 | cout << is_palindrome("level") << "\n"; 25 | cout << is_palindrome("thomas")<< "\n"; 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /breakAndContinue.cpp: -------------------------------------------------------------------------------- 1 | // The break statement used to jump out of a loop. 2 | // The continue statement breaks one iteration in a loop. 3 | 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | 9 | cout<< "Break Statement"<< endl; 10 | int count = 0; 11 | for(int i=0; i<10; i++) { 12 | if(i == 5) { 13 | break; 14 | } 15 | else { 16 | cout<< "Counting "<< i<< endl; 17 | } 18 | } 19 | 20 | cout<< "Continue Statement"<< endl; 21 | count = 0; 22 | for(int i=0; i<10; i++) { 23 | if(i==5) { 24 | continue; 25 | } 26 | else { 27 | cout<< "Counting "<< i<< endl; 28 | } 29 | } 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Loops/whileLoop.cpp: -------------------------------------------------------------------------------- 1 | // While Loop in C++ is used in situations 2 | // where we do not know the exact number of iterations of the loop beforehand. 3 | // The loop execution is terminated on the basis of the condition. 4 | // Condition may change during each iteration. 5 | // 'while' loop is more flexible for dynamic condition. 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main() { 11 | 12 | int i = 1; 13 | cout<< "Value of i = "<< i<< endl; 14 | 15 | while(i < 6) { 16 | cout<< "BD Cricket Team sucks"<< endl; 17 | 18 | i++; 19 | } 20 | 21 | cout<< "Value of i = "< -5) { 24 | cout<< "Watching BD Cricket Team playing is a waste of time. Now I have stones in my heart not in kidney."<< endl; 25 | 26 | i--; 27 | } 28 | 29 | cout<< "Value of i = "< 5 | using namespace std; 6 | 7 | int main() { 8 | 9 | int i = 10; 10 | do { 11 | // body 12 | cout<< "Value of i: "<< i<< endl; 13 | i++; 14 | } 15 | while(i<5); 16 | // The value of i is greater than 5. But still the loop executed once using do...while loop. 17 | // using while loop 18 | while (i<5) 19 | { 20 | cout<< "Hello, World!"<< endl; 21 | } 22 | // The condition did not met so the loop did not execute using while loop. 23 | 24 | 25 | int j = 10; 26 | do { 27 | // body 28 | cout<< "Value of j: "<< j<< endl; 29 | j++; 30 | } 31 | while(j<20); 32 | // The condition met so the loop executed. 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /OOP/class.cpp: -------------------------------------------------------------------------------- 1 | // class is a blueprint for creating objects 2 | // a template that describes how objects of that class should work 3 | // It defines the structure and behavior of objects by specifying their attributes and methods 4 | // attributes -> variables 5 | // methods -> functions 6 | 7 | #include 8 | using namespace std; 9 | 10 | class Student 11 | { 12 | public: 13 | int id; 14 | string name; 15 | double cgpa; 16 | 17 | void displayInfo() 18 | { 19 | cout<< "Name: "<< name<< endl; 20 | cout<< "Id: "<< id<< endl; 21 | cout<< "CGPA: "<< cgpa<< endl; 22 | } 23 | }; 24 | 25 | int main() 26 | { 27 | // creating an object of student class 28 | Student s1; 29 | 30 | // setting value of attributes 31 | s1.name = "Wasik Ahmed"; 32 | s1.id = 47698; 33 | s1.cgpa = 3.7; 34 | 35 | // calling methods 36 | s1.displayInfo(); 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /array.cpp: -------------------------------------------------------------------------------- 1 | // array is a collection of elements of the same data type 2 | // stored in contiguous memory locations 3 | // each element is accessed using an index 4 | // fixed size 5 | 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | 11 | int nums[5]; 12 | nums[0] = 10; 13 | nums[1] = 20; 14 | nums[2] = 30; 15 | nums[3] = 40; 16 | nums[4] = 50; 17 | 18 | cout<< "Elements in nums(array): "; 19 | cout<< nums[0]<< " "<< nums[1]<< " "<< nums[2]<< " "<< nums[3]<< " "<< nums[4]<< endl; 20 | 21 | // finding the size of an array 22 | int size = sizeof(nums) / sizeof(nums[0]); 23 | cout<< "Size of nums(array): "<< size<< endl; 24 | 25 | // using for loop 26 | int n=5; 27 | string friends[n]; 28 | cout<< "Enter your friend's name(Total 5): "<< endl; 29 | 30 | for(int i=0; i> friends[i]; 32 | } 33 | 34 | cout<< "Your friends: "; 35 | for(int i=0; i 2 | #include // library 3 | using namespace std; 4 | 5 | // Strings are used for storing text. 6 | // string variable contains a collection of characters 7 | // string in c++ is an object 8 | 9 | int main() { 10 | 11 | string message = "Hello, "; 12 | string firstName = "Wasik"; 13 | string lastName = "Ahmed"; 14 | 15 | // Concatenation of strings variables 16 | // using '+' operator 17 | string welcome = message + firstName + " " + lastName; 18 | cout<< welcome<< endl; 19 | 20 | // using append() function 21 | string appendName = firstName.append(" ").append(lastName); 22 | cout<< appendName<< endl; 23 | 24 | // string length 25 | // functions: length(), size() 26 | string fullName = "Wasik Ahmed"; 27 | int nameLength = fullName.length(); 28 | // int nameLength = fullName.size(); 29 | cout<< "Length: "<< endl; 30 | 31 | // Accessing characters 32 | cout<< "First letter(Full Name): "<< fullName[0]<< endl; // at zero index 33 | cout<< "First letter(Full Name): "<< fullName.at(0)<< endl; // using str.at(index) function 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /switchStatement.cpp: -------------------------------------------------------------------------------- 1 | // switch statement is used to make decisions based on the value of an expression. 2 | 3 | #include 4 | using namespace std; 5 | 6 | // switch(expression) { 7 | // case value: 8 | // code block 9 | // break; 10 | // default: 11 | // default code block 12 | // } 13 | 14 | int main() { 15 | 16 | // get order number (1-5) 17 | // print the order 18 | 19 | int order; 20 | cout<< "Please order your food (Choose beween 1-5)"<< endl; 21 | cout<< "Input order number: "; 22 | cin>> order; 23 | 24 | switch(order) { 25 | case 1: 26 | cout<< "You ordered Pizza!"<< endl; 27 | break; 28 | case 2: 29 | cout<< "You ordered Tacos!"<< endl; 30 | break; 31 | case 3: 32 | cout<< "You ordered Hamburger!"<< endl; 33 | break; 34 | case 4: 35 | cout<< "You ordered Sushi!"<< endl; 36 | break; 37 | case 5: 38 | cout<< "You ordered Chocolate Cake!"<< endl; 39 | break; 40 | default: 41 | cout<< "Invalid order number. Please try again!"<< endl; 42 | } 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /userInput.cpp: -------------------------------------------------------------------------------- 1 | // cin is a predefined variable that reads data from the keyboard 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | 8 | int id; 9 | string firstName, lastName, address; 10 | cout<< "Enter Id: "; 11 | cin>> id; 12 | 13 | cout<< "Enter first name: "; 14 | cin>> firstName; 15 | cout<< "Enter last name: "; 16 | cin>> lastName; 17 | 18 | // difference between getline() and cin 19 | // getline only works on strings, cin works for all data types. 20 | // With getline you can specify the delimiter (by default '\n' is used). 21 | // cin information uses any whitespace as a delimiter - this includes spaces, newlines, tabs etc. 22 | // getline removes the delimiter from the input stream, cin does not. 23 | 24 | cout<< "Enter address: "; 25 | 26 | cin.ignore(); 27 | // cin.ignore() clears or consumes the next character in the input stream, typically used to remove newline characters or other unwanted characters from the stream. 28 | getline(cin, address); 29 | 30 | // printing all informations with welcome message 31 | cout<< "Welcome "<< lastName<< endl; 32 | cout<< "Your Id: "<< id<< endl; 33 | cout<< "Full name: "<< firstName <<" "<< lastName<< endl; 34 | cout<< "Address: "<< address<< endl; 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /function.cpp: -------------------------------------------------------------------------------- 1 | // function 2 | // a block of code that performs a specific task 3 | 4 | #include 5 | using namespace std; 6 | 7 | // return_data_type function_name(parameters) { 8 | // code block 9 | // } 10 | 11 | // defining a function that returns no value (void) 12 | void printMenu() 13 | { 14 | cout<< " Tasty Bites "<< endl; 15 | cout<< "1. Spaghetti Carbonara - $12.99\n"; 16 | cout<< "2. Margherita Pizza - $10.99\n"; 17 | cout<< "3. Chicken Alfredo - $14.99\n"; 18 | cout<< "4. Grilled Salmon - $16.99\n"; 19 | cout<< "5. Vegetable Stir-Fry - $11.99\n"; 20 | } 21 | 22 | // defining a function that returns integer value (int) 23 | int takeOrder() 24 | { 25 | cout<< "Please input order number: "; 26 | int orderNo; 27 | cin>> orderNo; 28 | 29 | return orderNo; // returning an integer value 30 | } 31 | 32 | void serve(int orderNo) // parameterized function 33 | { 34 | switch (orderNo) 35 | { 36 | case 1: 37 | cout<< "Spaghetti Carbonara is served!"<< endl; 38 | break; 39 | case 2: 40 | cout<< "Margherita Pizza is served!"<< endl; 41 | break; 42 | case 3: 43 | cout<< "Chicken Alfredo is served!"<< endl; 44 | break; 45 | case 4: 46 | cout<< "Grilled Salmon is served!"<< endl; 47 | break; 48 | case 5: 49 | cout<< "Vegetable Stir-Fry is served!"<< endl; 50 | break; 51 | default: 52 | cout<< "Please choose correct order no from Menu"<< endl; 53 | break; 54 | } 55 | } 56 | 57 | int main() { 58 | 59 | // calling function 60 | printMenu(); 61 | 62 | int orderNo = takeOrder(); // storing the return value of takeOrder() function 63 | 64 | cout<< "Your order no: "<< orderNo<< endl; 65 | 66 | serve(orderNo); 67 | 68 | return 0; 69 | } -------------------------------------------------------------------------------- /variableAndDataTypes.cpp: -------------------------------------------------------------------------------- 1 | // variables are containers for storing data values 2 | 3 | // Data Types and Constraints 4 | // int - stores integer numbers 5 | // float - stores floating point numbers - size 4 bytes - 7 decimal digits precision - must add suffix f at end of value 6 | // double - stores floating point numbers - size 8 bytes - 15 decimal digits precision 7 | // char - stores single characters 8 | // string - stores text 9 | // bool - Boolean - true or false 10 | // const - declare constant variable - unchangeable 11 | 12 | #include 13 | using namespace std; 14 | 15 | int main() { 16 | 17 | int myIntegerNum = 10; 18 | float myFloatNum = 20.12345678; 19 | double myDoubleNum = 10.1234567890123456; 20 | char myCharacter = 'a'; 21 | string myString = "Hello, World"; 22 | bool myBool = true; 23 | const int myConstantInteger = 100; 24 | // myConstantInteger = 555; //-- this operation will not work 25 | // myIntegerNum = 555; //-- this operation will work 26 | // int x=10, y=20, z=30; //-- declare more than one variable of same data type 27 | // int x, y, z; 28 | // x = y = z = 100; //-- assing same value to multiple variables 29 | 30 | // print variable 31 | cout<< myIntegerNum<< endl; 32 | cout<< myFloatNum<< endl; 33 | cout<< myDoubleNum<< endl; 34 | cout<< myCharacter<< endl; 35 | cout<< myString<< endl; 36 | cout<< myBool<< endl; 37 | cout<< myConstantInteger<< endl; 38 | 39 | // print data type 40 | cout<< typeid(myIntegerNum).name()<< endl; 41 | cout<< typeid(myFloatNum).name()<< endl; 42 | cout<< typeid(myDoubleNum).name()<< endl; 43 | cout<< typeid(myCharacter).name()<< endl; 44 | cout<< typeid(myString).name()<< endl; 45 | cout<< typeid(myBool).name()<< endl; 46 | cout<< typeid(myConstantInteger).name()<< endl; 47 | 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /conditionsAndOperators.cpp: -------------------------------------------------------------------------------- 1 | // Operators for conditions and conditional statements 2 | 3 | // Common operators 4 | // assignment | increment | arithmetic | logical | comparison | member | other 5 | // | decrement | | | | access | 6 | // ----------------------------------------------------------------------------- 7 | // a = b | ++a | +a | !a | a == b | a[b] | a(...) 8 | // a += b | --a | -a | a && b | a != b | *a | a, b 9 | // a -= b | a++ | a + b | a || b | a < b | &a | ? : 10 | // a *= b | a-- | a - b | | a > b | a->b | 11 | // a /= b | | a * b | | a <= b | a.b | 12 | // a %= b | | a / b | | a >= b | a->*b | 13 | // a &= b | | a % b | | a <=> b | a.*b | 14 | // a |= b | | ~a | | | | 15 | // a ^= b | | a & b | | | | 16 | // a <<= b | | a | b | | | | 17 | // a >>= b | | a ^ b | | | | 18 | // | | a << b | | | | 19 | // | | a >> b | | | | 20 | 21 | // Conditional statements 22 | // if - if a condition is true 23 | // else - if a condition is false 24 | // else if - if the first condition is false 25 | 26 | #include 27 | using namespace std; 28 | 29 | int main() { 30 | 31 | int a = 10; 32 | int b = 5; 33 | 34 | // using if 35 | if(a > b) { 36 | cout<< "a is greater than b"<< endl; 37 | } 38 | 39 | if(a < b) { 40 | cout<< "a is less than b"<< endl; 41 | } 42 | 43 | // using else 44 | if(a < b) { 45 | cout<< "a is less than b"<< endl; 46 | } 47 | else { 48 | cout<< "a is greater than b"<< endl; 49 | } 50 | 51 | // using else if 52 | if(a == b) { 53 | cout<< "a is equal to b"<< endl; 54 | } 55 | else if(a > b) { 56 | cout<< "a is greater than b"<< endl; 57 | } 58 | else { 59 | cout<< "a is less than b"<< endl; 60 | } 61 | 62 | return 0; 63 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Learning-CPP 3 | 4 | This repository serves as a personal learning space for revising the basics of programming using C++. Here, you'll find a collection of code and resources related to C++ fundamentals. 5 | 6 | 7 | ## Table of Contents 8 | 9 | - [Installation](#installation) 10 | - [Code Illustrations](#code-illustrations) 11 | - [Contributing](#contributing) 12 | - [Contact Information](#contact-information) 13 | - [Authors](#authors) 14 | 15 | --- 16 | ## Installation 17 | 18 | To use the C++ codes in this repository, please follow these steps: 19 | 20 | 1. **Clone the repository** 21 | - Clone this repository to your local machine using Git: 22 | ```shell 23 | git clone https://github.com/WasikAhmed/Learning-CPP.git 24 | ``` 25 | 26 | 2. **Compile and Run Codes:** 27 | - Navigate to the directory where you cloned the repository: 28 | ```bash 29 | cd Learning-CPP 30 | ``` 31 | - Compile and run the C++ codes as needed. For example: 32 | ```bash 33 | g++ variableAndDataTypes.cpp -o variableAndDataTypes 34 | ./variableAndDataTypes 35 | ``` 36 | 37 | ## Code Illustrations 38 | 39 | This repository contains the following: 40 | 41 | - [Variables and Data Types](https://github.com/WasikAhmed/Learning-CPP/blob/main/variableAndDataTypes.cpp) 42 | - [User Input](https://github.com/WasikAhmed/Learning-CPP/blob/main/userInput.cpp) 43 | - [Conditions and Operators](https://github.com/WasikAhmed/Learning-CPP/blob/main/conditionsAndOperators.cpp) 44 | - [Strings](https://github.com/WasikAhmed/Learning-CPP/blob/main/strings.cpp) 45 | - [Break and Continue Statement](https://github.com/WasikAhmed/Learning-CPP/blob/main/breakAndContinue.cpp) 46 | - [Switch Statement](https://github.com/WasikAhmed/Learning-CPP/blob/main/switchStatement.cpp) 47 | - [Array](https://github.com/WasikAhmed/Learning-CPP/blob/main/array.cpp) 48 | 49 | **Loops** 50 | - [For Loop](https://github.com/WasikAhmed/Learning-CPP/blob/main/Loops/forLoop.cpp) 51 | - [While Loop](https://github.com/WasikAhmed/Learning-CPP/blob/main/Loops/whileLoop.cpp) 52 | - [Do...While Loop](https://github.com/WasikAhmed/Learning-CPP/blob/main/Loops/doWhileLoop.cpp) 53 | 54 | **Object-Oriented Programming (OOP)** 55 | 56 | 57 | 58 | ## Contributing 59 | 60 | Contributions are always welcome! If you'd like to contribute to this repository or improve the existing code, please follow these guidelines: 61 | 62 | 1. Fork the repository. 63 | 2. Create a new branch with a descriptive name related to your contribution or issue you're addressing (e.g., `git checkout -b update-fundamentals`). 64 | 3. Make your changes and commit them: `git commit -m "Updated array"`. 65 | 4. Push to your forked repository: `git push origin update-fundamentals`. 66 | 5. Create a pull request with a clear description of your changes. 67 | 68 | 69 | ## Contact Information 70 | 71 | If you have any questions, suggestions, or feedback feel free to reach out: 72 | 73 | - Email: [22-47698-2@student.aiub.edu](mailto:22-47698-2@student.aiub.edu) 74 | 75 | 76 | 77 | ## Authors 78 | 79 | - [@WasikAhmed](https://www.github.com/WasikAhmed) 80 | --------------------------------------------------------------------------------