├── Beginners_1 ├── Stage_1 │ ├── manual.docx │ ├── task1.cpp │ ├── task2.cpp │ ├── task3.cpp │ ├── task4.cpp │ └── task5.cpp ├── Stage_2 │ ├── manual.docx │ ├── task1.cpp │ ├── task2.cpp │ └── task3.cpp ├── Stage_3 │ ├── manual.docx │ ├── task1.cpp │ └── task2.cpp └── Stage_4 │ ├── manual.docx │ ├── task1.cpp │ ├── task2.cpp │ ├── task3.cpp │ └── task4.cpp ├── Beginners_2 ├── Stage_5 │ ├── manual.docx │ ├── task1.cpp │ └── task2.cpp ├── Stage_6 │ ├── manual.docx │ └── task1.cpp ├── Stage_7 │ ├── manual.docx │ ├── task1.cpp │ └── task2.cpp └── Stage_8 │ ├── manual.docx │ ├── task1.cpp │ └── task2.cpp ├── Beginners_3 ├── Stage_10 │ ├── manual.docx │ └── task1.cpp ├── Stage_11 │ ├── manual.docx │ ├── task1.cpp │ └── task2.cpp ├── Stage_12 │ ├── manual.docx │ ├── task1.cpp │ ├── task2.cpp │ ├── task3.cpp │ └── task4.cpp └── Stage_9 │ ├── manual.docx │ ├── task1.cpp │ ├── task2.cpp │ └── task3.cpp ├── README.md ├── Testing_Assignment_1 ├── manual.docx ├── synonyms(task1).txt ├── task1.cpp └── task2.cpp ├── Testing_Assignment_2 ├── input.txt ├── manual.docx └── task1.cpp └── Testing_Assignment_3 ├── manual.docx ├── task1.cpp ├── task2.cpp └── task3.cpp /Beginners_1/Stage_1/manual.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mabdullahatif/Object_Oriented_Programming/3b43c05ed9e394f7147c379a50757f600d31f21b/Beginners_1/Stage_1/manual.docx -------------------------------------------------------------------------------- /Beginners_1/Stage_1/task1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | - Declare two integer variables 'a' and 'b'. 3 | - Create two integer pointers 'ptrA' and 'ptrB' and assign them the addresses of 'a' and 'b' respectively. 4 | - Prompt the user to input two integer values, which are stored in 'a' and 'b'. 5 | - Display the values of 'a' and 'b' using the pointers 'ptrA' and 'ptrB' respectively. 6 | - Update the values of 'a' and 'b' by setting the values at the memory addresses pointed to by 'ptrA' and 'ptrB' to 30 and 40 respectively. 7 | - Display the updated values of 'a' and 'b' using the pointers 'ptrA' and 'ptrB' respectively. 8 | */ 9 | #include 10 | using namespace std; 11 | int main(){ 12 | int a,b; 13 | int *ptrA=&a; 14 | int *ptrB=&b; 15 | 16 | cout<<"Enter first number = "; 17 | cin>>a; 18 | 19 | cout<<"Enter second number = "; 20 | cin>>b; 21 | 22 | cout<<"the values are = "<<*ptrA<<" & "<<*ptrB< 18 | using namespace std; 19 | int main(){ 20 | int *ptr; 21 | ptr = new int; 22 | 23 | cout<<"Enter a number for the evaluation = "; 24 | cin>>*ptr; 25 | 26 | cout<<"Square of a number is = "; 27 | cout<<(*ptr)*(*ptr)<>arr[i]; 45 | } 46 | int largest=0,secondmax=0; 47 | 48 | for (int i=0;iarr[largest]){ 50 | secondmax=largest; 51 | largest=i; 52 | } 53 | } 54 | cout<>arr[i]; 32 | } 33 | 34 | int *ptr; 35 | ptr=arr; 36 | 37 | for(int i=0;i 21 | using namespace std; 22 | 23 | int main() { 24 | const int a = 5; 25 | const int b = 12; 26 | const int c = 10; 27 | const int *pa = &a; 28 | const int *pb = &b; 29 | const int *pc = &c; 30 | 31 | int median = 0; 32 | 33 | if ((*pa <= *pb && *pa >= *pc) || (*pa >= *pb && *pa <= *pc)) { 34 | median = *pa; 35 | const_cast(a) = -1; // Updating median value to -1 through pointers 36 | } else if ((*pb <= *pa && *pb >= *pc) || (*pb >= *pa && *pb <= *pc)) { 37 | median = *pb; 38 | const_cast(b) = -1; // Updating median value to -1 through pointers 39 | } else { 40 | median = *pc; 41 | const_cast(c) = -1; // Updating median value to -1 through pointers 42 | } 43 | 44 | cout << "Median is = " << median << endl; 45 | cout << "Updated values of a, b, and c are: " << a << ", " << b << ", " << c << endl; 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Beginners_1/Stage_2/manual.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mabdullahatif/Object_Oriented_Programming/3b43c05ed9e394f7147c379a50757f600d31f21b/Beginners_1/Stage_2/manual.docx -------------------------------------------------------------------------------- /Beginners_1/Stage_2/task1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program demonstrates the dynamic input, compression, and output of an integer array. 3 | 4 | - The function 'InputArray' takes a reference to an integer 'size' and dynamically allocates memory for an integer array. 5 | It prompts the user to input the size and elements of the array and returns a pointer to the dynamically created array. 6 | 7 | - The function 'OutputArray' takes a constant integer pointer 'myarray' and a constant integer reference 'size'. 8 | It displays the elements of the array pointed to by 'myarray' using pointer arithmetic. 9 | 10 | - The function 'CompressArray' takes an integer pointer 'originalArr' and a reference to an integer 'size'. 11 | It creates a new dynamically allocated integer array 'compressedArr' to store the compressed version of the original array. 12 | The function compresses the original array by removing duplicate consecutive elements. 13 | It then updates the 'size' parameter with the new compressed size, deallocates the memory of the original array, and returns the compressed array. 14 | 15 | - In the 'main' function, the user is prompted to input the length of the array. 16 | It calls the 'InputArray' function to dynamically create the array and fill it with user-provided values. 17 | It then displays the original array using the 'OutputArray' function. 18 | 19 | - The 'CompressArray' function is called to obtain the compressed version of the original array. 20 | The compressed array is then displayed using the 'OutputArray' function. 21 | 22 | - Finally, memory allocated for the compressed array is deallocated using 'delete[]' to prevent memory leaks. 23 | 24 | Note: The program showcases dynamic memory allocation and manipulation of arrays using pointers. 25 | Remember to free up the memory allocated using 'new' with 'delete[]' to prevent memory leaks. 26 | */ 27 | #include 28 | using namespace std; 29 | 30 | // Function to input an array dynamically 31 | int* InputArray(int& size) { 32 | cout << "Enter size of the array: "; 33 | cin >> size; 34 | 35 | int* array = new int[size]; 36 | 37 | cout << "Enter " << size << " elements: "; 38 | for (int i = 0; i < size; i++) { 39 | cin >> array[i]; 40 | } 41 | 42 | return array; 43 | } 44 | 45 | // Function to output an array 46 | void OutputArray(const int* myarray, const int& size) { 47 | for (int i = 0; i < size; i++) { 48 | cout << *(myarray + i) << " "; 49 | } 50 | } 51 | 52 | // Function to compress an array 53 | int* CompressArray(int* originalArr, int& size) { 54 | int* compressedArr = new int[size]; 55 | int compressedSize = 0; 56 | 57 | compressedArr[compressedSize++] = originalArr[0]; 58 | for (int i = 1; i < size; i++) { 59 | if (originalArr[i] != originalArr[i - 1]) { 60 | compressedArr[compressedSize++] = originalArr[i]; 61 | } 62 | } 63 | 64 | // Update the size parameter with the compressed size 65 | size = compressedSize; 66 | 67 | // Delete the original array to prevent memory leaks 68 | delete[] originalArr; 69 | 70 | return compressedArr; 71 | } 72 | 73 | int main() { 74 | int length; 75 | cout << "Enter length of the array: "; 76 | cin >> length; 77 | 78 | int* array = InputArray(length); 79 | 80 | cout << "The values are: "; 81 | OutputArray(array, length); 82 | cout << endl; 83 | 84 | int compressedSize = length; 85 | int* compressedArray = CompressArray(array, compressedSize); 86 | 87 | cout << "Array after Compression: "; 88 | OutputArray(compressedArray, compressedSize); 89 | cout << endl; 90 | 91 | // Don't forget to deallocate the memory 92 | delete[] compressedArray; 93 | 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /Beginners_1/Stage_2/task2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program takes an input string, counts and categorizes various elements in the string. 3 | 4 | - The 'Counter' function takes a character array 'array' as input and counts the following elements: 5 | - 'spaces': Counts the number of space characters in the input array. 6 | - 'uppercase': Counts the number of uppercase letters in the input array. 7 | - 'lowercase': Counts the number of lowercase letters in the input array. 8 | - 'vowels': Counts the number of vowels (a, e, i, o, u, both uppercase and lowercase) in the input array. 9 | - 'consonant': Calculates the number of consonants (total letters - vowels) in the input array. 10 | 11 | - In the 'main' function, the user is prompted to enter an array of characters (string) using 'cin.getline()'. 12 | The 'Counter' function is then called with this array as an argument to analyze the content of the input string. 13 | 14 | - The 'Counter' function prints the total number of letters (uppercase and lowercase combined), spaces, 15 | uppercase letters, lowercase letters, vowels, and consonants in the input string. 16 | 17 | Note: The program showcases basic string manipulation using character arrays and various character count calculations. 18 | The 'Counter' function iterates through the input array and counts the desired elements based on specific conditions. 19 | The use of 'cin.getline()' allows the program to read spaces and accept input of multiple words in a single line. 20 | */ 21 | #include 22 | #include 23 | #include 24 | #include 25 | using namespace std; 26 | void Counter(char array[]){ 27 | int spaces=0; 28 | int uppercase=0; 29 | int lowercase=0; 30 | int vowels=0; 31 | int cosonant=0; 32 | int i=0; 33 | for(int i=0;i='A' && array[i]<='Z'){ 38 | uppercase++; 39 | // cout<='a' && array[i]<='z'){ 42 | lowercase++; 43 | //cout< 26 | #include 27 | using namespace std; 28 | 29 | int main() { 30 | char* c1 = new char[100]; 31 | char* c2 = new char[100]; 32 | int len1, len2; 33 | 34 | do { 35 | cout << "Enter first string: "; 36 | cin.ignore(); 37 | cin.getline(c1, 100); 38 | len1 = strlen(c1); 39 | 40 | cout << "Enter second string: "; 41 | cin.getline(c2, 100); 42 | len2 = strlen(c2); 43 | 44 | if (strstr(c1, c2)) { 45 | cout << "The entered substring is present in the array!!" << endl; 46 | } else { 47 | cout << "Sorry, the entered substring is not present in the array!!" << endl; 48 | } 49 | } while (!strstr(c1, c2)); 50 | 51 | // Clean up the dynamically allocated memory 52 | delete[] c1; 53 | delete[] c2; 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Beginners_1/Stage_3/manual.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mabdullahatif/Object_Oriented_Programming/3b43c05ed9e394f7147c379a50757f600d31f21b/Beginners_1/Stage_3/manual.docx -------------------------------------------------------------------------------- /Beginners_1/Stage_3/task1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program demonstrates encryption using a Caesar cipher with a user-provided encryption key. 3 | 4 | - The function 'isValidEncryptionKey' takes a single character 'key' and checks if it is a valid encryption key. 5 | The function returns 'true' if the key is an alphabet (a-z or A-Z) and 'false' otherwise. 6 | 7 | - The function 'Encrypt' takes a character array 'arr', an integer 'size', and a character 'key' as inputs. 8 | It encrypts the characters in the 'arr' array using the Caesar cipher technique, shifting each character by the value of the 'key'. 9 | If the 'key' is a valid alphabet (a-z or A-Z), the characters in the array are modified in place. 10 | 11 | - In the 'main' function: 12 | - The user is prompted to enter the size of the character array ('arr') dynamically. 13 | - The 'arr' array is dynamically allocated based on the user-provided size. 14 | - The user is prompted to input the elements of the 'arr' array. 15 | - The 'actualSize' variable is used to store the actual number of characters entered by excluding the null terminator. 16 | - The user is prompted to input the encryption key ('key'). 17 | - A new character array 'resizedArr' is dynamically allocated with a size of 'actualSize + 1' to hold the modified string. 18 | - The original 'arr' array is copied into 'resizedArr' character by character, and a null terminator is added. 19 | - The 'Encrypt' function is called with 'resizedArr', 'actualSize', and 'key' to encrypt the array. 20 | - The original 'arr' and encrypted 'resizedArr' are displayed. 21 | 22 | - The program deallocates the dynamic memory allocated for 'arr' and 'resizedArr' using 'delete[]' to avoid memory leaks. 23 | 24 | Note: The program showcases dynamic memory allocation for character arrays, input handling for encryption key, and the implementation of a simple Caesar cipher for encryption. 25 | */ 26 | #include 27 | using namespace std; 28 | 29 | bool isValidEncryptionKey(char key) { 30 | return ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z')); 31 | } 32 | 33 | void Encrypt(char* arr, int size, char key) { 34 | if (!isValidEncryptionKey(key)) { 35 | cout << "Invalid encryption key! Please enter a valid alphabet (a-z or A-Z)." << endl; 36 | return; 37 | } 38 | 39 | for (int i = 0; i < size; i++) { 40 | arr[i] = (arr[i] + key - 'a') % 26 + 'a'; 41 | } 42 | } 43 | 44 | int main() { 45 | int size; 46 | cout << "Enter Size of array: "; 47 | cin >> size; 48 | 49 | // Dynamically allocate char array 50 | char* arr = new char[size]; 51 | 52 | cout << "Enter the array elements: "; 53 | cin.ignore(); // Ignore the newline character left in the input buffer 54 | cin.getline(arr, size); 55 | 56 | // Get the actual number of characters entered 57 | int actualSize = cin.gcount() - 1; // Excluding the null terminator 58 | 59 | cout << "Enter the encryption key: "; 60 | char key; 61 | cin >> key; 62 | 63 | // Resize the original array to the number of characters entered 64 | char* resizedArr = new char[actualSize + 1]; // +1 for null terminator 65 | for (int i = 0; i < actualSize; i++) { 66 | resizedArr[i] = arr[i]; 67 | } 68 | resizedArr[actualSize] = '\0'; // Add null terminator 69 | 70 | Encrypt(resizedArr, actualSize, key); 71 | 72 | cout << "Original array: " << arr << endl; 73 | cout << "Encrypted array: " << resizedArr << endl; 74 | 75 | // Deallocate dynamic memory 76 | delete[] arr; 77 | delete[] resizedArr; 78 | 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /Beginners_1/Stage_3/task2.cpp: -------------------------------------------------------------------------------- 1 | 2 | /*This C++ program demonstrates matrix manipulation to separate different types of characters (alphabets, numbers, 3 | and special characters) from the user-provided matrix. It uses dynamic memory allocation for 2D character arrays 4 | and functions to allocate, input, display, and separate characters in the matrix. The 'SeparateCharacters' function 5 | counts occurrences of alphabets, numbers, and special characters and dynamically allocates separate arrays for each 6 | type. The program prevents memory leaks by deallocating dynamic memory after use.*/ 7 | #include 8 | using namespace std; 9 | 10 | char** AllocateMemory(int& rows, int& cols) { 11 | cout << "Enter the number of rows: "; 12 | cin >> rows; 13 | cout << "Enter the number of columns: "; 14 | cin >> cols; 15 | 16 | char** matrix = new char* [rows]; 17 | for (int i = 0; i < rows; i++) { 18 | matrix[i] = new char[cols]; 19 | } 20 | 21 | return matrix; 22 | } 23 | 24 | char** InputMatrix(char** matrix, const int rows, const int cols) { 25 | cout << "Enter the elements of the matrix:" << endl; 26 | for (int i = 0; i < rows; i++) { 27 | for (int j = 0; j < cols; j++) { 28 | cin >> matrix[i][j]; 29 | } 30 | } 31 | 32 | return matrix; 33 | } 34 | 35 | void DisplayMatrix(char** matrix, const int& rows, const int& cols) { 36 | cout << "Sample Matrix is:" << endl; 37 | for (int i = 0; i < rows; i++) { 38 | for (int j = 0; j < cols; j++) { 39 | cout << matrix[i][j] << "\t"; 40 | } 41 | cout << endl; 42 | } 43 | } 44 | 45 | void SeparateCharacters(char** matrix, const int rows, const int cols, 46 | char*& alphabets, char*& numbers, char*& specialchar) { 47 | // Count the occurrences of each character type 48 | int alphabetCount = 0, numberCount = 0, specialCount = 0; 49 | for (int i = 0; i < rows; i++) { 50 | for (int j = 0; j < cols; j++) { 51 | if (isalpha(matrix[i][j])) { 52 | alphabetCount++; 53 | } else if (isdigit(matrix[i][j])) { 54 | numberCount++; 55 | } else { 56 | specialCount++; 57 | } 58 | } 59 | } 60 | 61 | // Resize the arrays 62 | alphabets = new char[alphabetCount]; 63 | numbers = new char[numberCount]; 64 | specialchar = new char[specialCount]; 65 | 66 | // Separate the characters 67 | int alphabetIndex = 0, numberIndex = 0, specialIndex = 0; 68 | for (int i = 0; i < rows; i++) { 69 | for (int j = 0; j < cols; j++) { 70 | if (isalpha(matrix[i][j])) { 71 | alphabets[alphabetIndex++] = matrix[i][j]; 72 | } else if (isdigit(matrix[i][j])) { 73 | numbers[numberIndex++] = matrix[i][j]; 74 | } else { 75 | specialchar[specialIndex++] = matrix[i][j]; 76 | } 77 | } 78 | } 79 | } 80 | 81 | int main() { 82 | int rows, cols; 83 | char** matrix = AllocateMemory(rows, cols); 84 | 85 | InputMatrix(matrix, rows, cols); 86 | DisplayMatrix(matrix, rows, cols); 87 | 88 | char* alphabets; 89 | char* numbers; 90 | char* specialchar; 91 | 92 | SeparateCharacters(matrix, rows, cols, alphabets, numbers, specialchar); 93 | 94 | // Display the separated arrays 95 | cout << endl; 96 | cout << "alphabets = "; 97 | for (int i = 0; i < rows * cols; i++) { 98 | cout << alphabets[i] << " "; 99 | } 100 | cout << endl; 101 | 102 | cout << "numbers = "; 103 | for (int i = 0; i < rows * cols; i++) { 104 | cout << numbers[i] << " "; 105 | } 106 | cout << endl; 107 | 108 | cout << "specialchar = "; 109 | for (int i = 0; i < rows * cols; i++) { 110 | cout << specialchar[i] << " "; 111 | } 112 | cout << endl; 113 | 114 | // Deallocate dynamic memory 115 | for (int i = 0; i < rows; i++) { 116 | delete[] matrix[i]; 117 | } 118 | delete[] matrix; 119 | 120 | delete[] alphabets; 121 | delete[] numbers; 122 | delete[] specialchar; 123 | 124 | return 0; 125 | } 126 | -------------------------------------------------------------------------------- /Beginners_1/Stage_4/manual.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mabdullahatif/Object_Oriented_Programming/3b43c05ed9e394f7147c379a50757f600d31f21b/Beginners_1/Stage_4/manual.docx -------------------------------------------------------------------------------- /Beginners_1/Stage_4/task1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program defines a 'Student' class to store and display student data. 3 | The class has private data members 'name', 'eng' (English marks), 'math' (Math marks), 'science' (Science marks), and 'total' (total marks). 4 | It includes member functions: 5 | 6 | 1. 'TakeData': To input student details such as name, marks in English, Math, and Science, and calculate the total marks. 7 | 8 | 2. 'ShowData': To display the student's name, marks in English, Math, Science, and the total marks. 9 | 10 | The 'main' function creates two instances of the 'Student' class ('s1' and 's2'). 11 | It then calls the 'TakeData' function for both instances to input student data and calculates the total marks. 12 | After that, it calls the 'ShowData' function for both instances to display the student details, including the total marks. 13 | 14 | Note: The program showcases the use of a class to represent student data, member functions to interact with class data, 15 | and the concept of encapsulation to keep data private. 16 | */ 17 | #include 18 | using namespace std; 19 | 20 | class Student { 21 | private: 22 | string name; 23 | float eng, math, science; 24 | float total; 25 | 26 | public: 27 | void TakeData() { 28 | cout << "Enter name: "; 29 | cin >> name; 30 | cout << "Enter marks in English: "; 31 | cin >> eng; 32 | cout << "Enter marks in Math: "; 33 | cin >> math; 34 | cout << "Enter marks in Science: "; 35 | cin >> science; 36 | total = cTotal(); 37 | } 38 | 39 | void ShowData() { 40 | cout << "Name: " << name << endl; 41 | cout << "English: " << eng << endl; 42 | cout << "Math: " << math << endl; 43 | cout << "Science: " << science << endl; 44 | cout << "Total: " << total << endl; 45 | } 46 | 47 | float cTotal() { 48 | return eng + math + science; 49 | } 50 | }; 51 | 52 | int main() { 53 | Student s1, s2; 54 | s1.TakeData(); 55 | s2.TakeData(); 56 | cout << "\nStudent 1 Data:\n"; 57 | s1.ShowData(); 58 | cout << "\nStudent 2 Data:\n"; 59 | s2.ShowData(); 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Beginners_1/Stage_4/task2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program defines a 'Phone' class to store and display phone numbers. 3 | The class has private data members 'areaCode', 'exchange', and 'number' to represent the phone number components. 4 | 5 | - The default constructor is provided to initialize the phone number components to 0. 6 | 7 | - The 'Input' function allows the user to input the area code, exchange, and number. 8 | 9 | - The 'Display' function displays the phone number in the format (areaCode) exchange-number. 10 | 11 | Note: The program demonstrates the use of a class to represent phone numbers and the member functions to interact with class data. 12 | The 'Phone' class encapsulates the phone number details and provides methods to input and display phone numbers. 13 | */ 14 | #include 15 | using namespace std; 16 | 17 | class Phone { 18 | private: 19 | int areaCode; 20 | int exchange; 21 | int number; 22 | 23 | public: 24 | Phone() : areaCode(0), exchange(0), number(0) {} 25 | 26 | void Input() { 27 | cout << "Enter your area code, exchange, and number: "; 28 | cin >> areaCode >> exchange >> number; 29 | } 30 | 31 | void Display() { 32 | cout << "My number is (" << areaCode << ") " << exchange << "-" << number << endl; 33 | } 34 | }; 35 | 36 | int main() { 37 | Phone myNumber; 38 | Phone yourNumber; 39 | 40 | myNumber.Display(); 41 | 42 | yourNumber.Input(); 43 | 44 | cout << "Your number is "; 45 | yourNumber.Display(); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Beginners_1/Stage_4/task3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program defines a 'Date' class to represent dates in day/month/year format. 3 | The class has private data members 'day', 'month', and 'year' to store the date components. 4 | 5 | - The default constructor initializes the date to 1/1/1926 and displays a message indicating its call. 6 | 7 | - The 'Print' function displays the date in day/month/year format. 8 | 9 | - The 'Input' function allows the user to input the day, month, and year. 10 | 11 | - The 'SetDay', 'SetMonth', and 'SetYear' functions set the day, month, and year, respectively. 12 | 13 | - The 'GetDay', 'GetMonth', and 'GetYear' functions return the day, month, and year, respectively. 14 | 15 | 16 | Note: The program demonstrates the use of a class to represent dates and the member functions to interact with date components. 17 | It also showcases the constructor and how member functions can be used to set and get the date components. 18 | */ 19 | #include 20 | using namespace std; 21 | 22 | class Date { 23 | private: 24 | int day; 25 | int month; 26 | int year; 27 | 28 | public: 29 | Date() : day(1), month(1), year(1926) { 30 | cout << "Default Constructor Called" << endl; 31 | } 32 | 33 | void Print() { 34 | cout << day << "/" << month << "/" << year << endl; 35 | } 36 | 37 | void Input() { 38 | cout << "Enter day, month, and year: "; 39 | cin >> day >> month >> year; 40 | } 41 | 42 | void SetDay(int d) { 43 | day = d; 44 | } 45 | 46 | void SetMonth(int m) { 47 | month = m; 48 | } 49 | 50 | void SetYear(int y) { 51 | year = y; 52 | } 53 | 54 | int GetDay() { 55 | return day; 56 | } 57 | 58 | int GetMonth() { 59 | return month; 60 | } 61 | 62 | int GetYear() { 63 | return year; 64 | } 65 | }; 66 | 67 | int main() { 68 | Date date1; 69 | date1.Print(); 70 | 71 | date1.Input(); 72 | date1.Print(); 73 | 74 | Date xmasDay; 75 | xmasDay.Print(); 76 | 77 | xmasDay.SetDay(25); 78 | xmasDay.SetMonth(12); 79 | xmasDay.SetYear(2020); 80 | 81 | cout << "Updated Christmas Day: "; 82 | xmasDay.Print(); 83 | 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /Beginners_1/Stage_4/task4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program defines a 'Sample' class with multiple constructors to demonstrate constructor overloading. 3 | 4 | The 'Sample' class has private data members 'x' (an integer) and 'y' (a double). 5 | 6 | The class includes the following constructors: 7 | 1. Default constructor: Initializes 'x' to 0 and 'y' to 0.0. 8 | 2. Constructor with one integer parameter: Initializes 'x' with the provided integer value and 'y' to 0.0. 9 | 3. Constructor with two integer parameters: Initializes 'x' and 'y' with the provided integer values. 10 | 4. Constructor with one integer and one double parameter: Initializes 'x' with the integer value and 'y' with the double value. 11 | 12 | In the 'main' function: 13 | - Four instances of the 'Sample' class ('s1', 's2', 's3', and 's4') are created using different constructors. 14 | Note: The program showcases constructor overloading, which allows the 'Sample' class to be initialized with different combinations of values for 'x' and 'y'. 15 | */ 16 | #include 17 | using namespace std; 18 | 19 | class Sample { 20 | private: 21 | int x; 22 | double y; 23 | 24 | public: 25 | Sample() : x(0), y(0.0) {} 26 | Sample(int val) : x(val), y(0.0) {} 27 | Sample(int val1, int val2) : x(val1), y(val2) {} 28 | Sample(int val1, double val2) : x(val1), y(val2) {} 29 | }; 30 | 31 | int main() { 32 | Sample s1; 33 | Sample s2(10); 34 | Sample s3(20, 30); 35 | Sample s4(40, 3.14); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Beginners_2/Stage_5/manual.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mabdullahatif/Object_Oriented_Programming/3b43c05ed9e394f7147c379a50757f600d31f21b/Beginners_2/Stage_5/manual.docx -------------------------------------------------------------------------------- /Beginners_2/Stage_5/task1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program defines a 'Set' class that represents a set of integers and provides various set operations. 3 | 4 | - The 'Set' class has private data members 'data', 'noOfElements', and 'capacity' to store the set elements, 5 | the number of elements in the set, and the capacity of the set, respectively. 6 | 7 | - The class has constructors to create a set object and initialize its properties. 8 | The destructor is provided to free the dynamically allocated memory for the set. 9 | 10 | - The 'insert' function inserts a new element into the set. 11 | 12 | - The 'remove' function removes an element from the set. 13 | 14 | - The 'getCardinality' function returns the number of elements in the set. 15 | 16 | - The 'calcUnion', 'calcSymmetricDifference', and 'calcDifference' functions perform set operations 17 | Union, Symmetric Difference, and Difference, respectively, and return a new set object with the result. 18 | 19 | - The 'isMember' function checks if an element is present in the set. 20 | 21 | - The 'isSubSet' function checks if the current set is a proper or improper subset of another set. 22 | 23 | - The 'reSize' function resizes the set to the specified new capacity. 24 | 25 | - The 'update' function updates the value of an element in the set with a new value. 26 | 27 | - The 'show' function displays the elements of the set. 28 | 29 | Note: The program demonstrates the implementation of a set using dynamic memory allocation. 30 | It showcases various set operations and how elements can be inserted, removed, and updated in the set. 31 | */ 32 | #include 33 | using namespace std; 34 | 35 | class Set { 36 | private: 37 | int* data; 38 | int noOfElements; 39 | int capacity; 40 | 41 | public: 42 | Set(int cap = 0); 43 | Set(const Set& ref); 44 | ~Set(); 45 | void insert(int element); 46 | void remove(int element); 47 | int getCardinality() const; 48 | Set calcUnion(const Set& s2) const; 49 | Set calcSymmetricDifference(const Set& s2) const; 50 | Set calcDifference(const Set& s2) const; 51 | int isMember(int val) const; 52 | int isSubSet(const Set& s2) const; 53 | void reSize(int newcapacity); 54 | void update(int prVal, int curVal); 55 | void show() const; 56 | }; 57 | 58 | Set::Set(int cap) { 59 | // Ensure capacity is not negative 60 | if (cap < 0) { 61 | cap = 0; 62 | } 63 | 64 | this->data = new int[cap]; 65 | this->capacity = cap; 66 | this->noOfElements = 0; 67 | } 68 | 69 | Set::Set(const Set& ref) { 70 | noOfElements = ref.noOfElements; 71 | data = new int[ref.capacity]; 72 | for (int i = 0; i < noOfElements; i++) { 73 | data[i] = ref.data[i]; 74 | } 75 | } 76 | 77 | Set::~Set() { 78 | delete[] data; 79 | } 80 | 81 | void Set::insert(int element) { 82 | int* ptr = new int[this->noOfElements + 1]; 83 | for (int i = 0; i < noOfElements; i++) { 84 | ptr[i] = data[i]; 85 | } 86 | ptr[noOfElements] = element; 87 | delete[] data; 88 | data = ptr; 89 | noOfElements++; 90 | } 91 | 92 | void Set::remove(int element) { 93 | int* ptr = new int[this->noOfElements - 1]; 94 | int j = 0; 95 | for (int i = 0; i < noOfElements; i++) { 96 | if (data[i] != element) { 97 | ptr[j] = data[i]; 98 | j++; 99 | } 100 | } 101 | delete[] data; 102 | data = ptr; 103 | noOfElements--; 104 | 105 | // Check if the Set needs to be resized 106 | if (noOfElements <= capacity / 2) { 107 | reSize(capacity - capacity / 4); 108 | } 109 | } 110 | 111 | int Set::getCardinality() const { 112 | return noOfElements; 113 | } 114 | 115 | Set Set::calcUnion(const Set& s2) const { 116 | Set result(this->capacity + s2.capacity); 117 | for (int i = 0; i < this->noOfElements; i++) { 118 | result.insert(this->data[i]); 119 | } 120 | for (int i = 0; i < s2.noOfElements; i++) { 121 | if (!this->isMember(s2.data[i])) { 122 | result.insert(s2.data[i]); 123 | } 124 | } 125 | return result; 126 | } 127 | 128 | Set Set::calcSymmetricDifference(const Set& s2) const { 129 | Set result(this->capacity + s2.capacity); 130 | for (int i = 0; i < this->noOfElements; i++) { 131 | if (!s2.isMember(this->data[i])) { 132 | result.insert(this->data[i]); 133 | } 134 | } 135 | for (int i = 0; i < s2.noOfElements; i++) { 136 | if (!this->isMember(s2.data[i])) { 137 | result.insert(s2.data[i]); 138 | } 139 | } 140 | return result; 141 | } 142 | 143 | Set Set::calcDifference(const Set& s2) const { 144 | Set result(this->capacity); 145 | for (int i = 0; i < this->noOfElements; i++) { 146 | if (s2.isMember(this->data[i])) { 147 | result.insert(this->data[i]); 148 | } 149 | } 150 | return result; 151 | } 152 | 153 | int Set::isMember(int val) const { 154 | for (int i = 0; i < this->noOfElements; i++) { 155 | if (this->data[i] == val) { 156 | return 1; 157 | } 158 | } 159 | return 0; 160 | } 161 | 162 | int Set::isSubSet(const Set& s2) const { 163 | int count = 0; 164 | for (int i = 0; i < s2.noOfElements; i++) { 165 | if (this->isMember(s2.data[i])) { 166 | count++; 167 | } 168 | } 169 | if (count == this->noOfElements && count < s2.noOfElements) { 170 | return 1; // Proper Subset 171 | } else if (count == this->noOfElements && count == s2.noOfElements) { 172 | return 2; // Improper Subset 173 | } else { 174 | return 0; // Not a Subset 175 | } 176 | } 177 | 178 | void Set::reSize(int newcapacity) { 179 | if (newcapacity < 0) { 180 | newcapacity = 0; 181 | } 182 | 183 | if (newcapacity >= this->noOfElements) { 184 | int* newdata = new int[newcapacity]; 185 | for (int i = 0; i < this->noOfElements; i++) { 186 | newdata[i] = data[i]; 187 | } 188 | 189 | delete[] data; 190 | data = newdata; 191 | capacity = newcapacity; 192 | } 193 | } 194 | 195 | void Set::update(int prVal, int curVal) { 196 | if (!isMember(prVal)) { 197 | cout << "Target value not found." << endl; 198 | return; 199 | } 200 | 201 | if (isMember(curVal)) { 202 | cout << "Violation set property. Can't Modify the data." << endl; 203 | return; 204 | } 205 | 206 | for (int i = 0; i < noOfElements; i++) { 207 | if (data[i] == prVal) { 208 | data[i] = curVal; 209 | } 210 | } 211 | cout << "Record update successfully." << endl; 212 | } 213 | 214 | void Set::show() const { 215 | for (int i = 0; i < noOfElements; i++) { 216 | cout << data[i] << " "; 217 | } 218 | } 219 | 220 | int main() { 221 | Set s(10); 222 | 223 | s.insert(5); 224 | s.insert(10); 225 | s.insert(15); 226 | 227 | Set s2 = s; 228 | s2.insert(20); 229 | 230 | Set s3 = s.calcUnion(s2); 231 | Set s4 = s.calcSymmetricDifference(s2); 232 | Set s5 = s.calcDifference(s2); 233 | 234 | s.show(); // Output: 5 10 15 235 | cout << endl; 236 | 237 | s2.show(); // Output: 5 10 15 20 238 | cout << endl; 239 | 240 | s3.show(); // Output: 5 10 15 20 241 | cout << endl; 242 | 243 | s4.show(); // Output: 20 244 | cout << endl; 245 | 246 | s5.show(); // Output: 5 10 15 247 | cout << endl; 248 | 249 | s.update(5, 25); // Record update successfully. 250 | 251 | s.show(); // Output: 25 10 15 252 | cout << endl; 253 | 254 | return 0; 255 | } 256 | -------------------------------------------------------------------------------- /Beginners_2/Stage_5/task2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program defines a 'fraction' class that represents fractions and provides various operations on them. 3 | 4 | - The 'fraction' class has private data members 'numerator' and 'denominator' to store the fraction components. 5 | 6 | - The class has a 'fractionsCount' static data member that keeps track of the number of fraction objects created. 7 | 8 | - The class has constructors to create a fraction object and initialize its numerator and denominator. 9 | It also has setter and getter functions for the numerator and denominator. 10 | 11 | - The class overloads the '+' and '*' operators to perform addition and multiplication of fractions, respectively. 12 | The overloaded '>' operator is provided to compare fractions. 13 | 14 | - The 'isProper' function checks if the fraction is a proper fraction. 15 | 16 | - The 'getCount' function returns the total number of fraction objects created. 17 | 18 | - The 'show' function displays the fraction in reduced form, whether it is proper or not, and the total number of fractions. 19 | 20 | In the 'main' function: 21 | - Two fraction objects ('f1' and 'f2') are created and initialized with different values. 22 | 23 | - The overloaded '+' and '*' operators are used to perform addition and multiplication of fractions. 24 | The 'show' function is called to display the results. 25 | 26 | - The overloaded '>' operator is used to compare two fractions, and the comparison result is displayed. 27 | 28 | - The values of 'f1' and 'f2' are changed using the setter functions, and the addition and multiplication operations are performed again. 29 | 30 | - The overloaded '>' operator is used again to compare the new fractions, and the comparison result is displayed. 31 | 32 | Note: The program demonstrates the implementation of a 'fraction' class with various member functions to manipulate and compare fractions. 33 | It shows how overloaded operators can be used to perform arithmetic and comparison operations on custom data types like fractions. 34 | */ 35 | 36 | #include 37 | using namespace std; 38 | 39 | class fraction { 40 | private: 41 | static int fractionsCount; 42 | int numerator; 43 | int denominator; 44 | 45 | public: 46 | fraction(int n = 1, int d = 1); 47 | void setNum(int n); 48 | void setDen(int d); 49 | int getNum() const; 50 | int getDen() const; 51 | fraction operator+(const fraction& f) const; 52 | fraction operator*(const fraction& f) const; 53 | bool operator>(const fraction& f) const; 54 | bool isProper() const; 55 | static int getCount(); 56 | void show() const; 57 | }; 58 | 59 | int fraction::fractionsCount = 0; 60 | 61 | fraction::fraction(int n, int d) { 62 | if (d == 0) { 63 | d = 1; // Ensure denominator is not zero 64 | } 65 | 66 | this->numerator = n; 67 | this->denominator = d; 68 | fractionsCount++; 69 | } 70 | 71 | void fraction::setNum(int n) { 72 | this->numerator = n; 73 | } 74 | 75 | void fraction::setDen(int d) { 76 | if (d != 0) { 77 | this->denominator = d; 78 | } 79 | } 80 | 81 | int fraction::getNum() const { 82 | return numerator; 83 | } 84 | 85 | int fraction::getDen() const { 86 | return denominator; 87 | } 88 | 89 | fraction fraction::operator+(const fraction& f) const { 90 | fraction result; 91 | result.numerator = (numerator * f.denominator) + (f.numerator * denominator); 92 | result.denominator = denominator * f.denominator; 93 | 94 | int div = 2; 95 | while (div <= result.numerator && div <= result.denominator) { 96 | if (result.numerator % div == 0 && result.denominator % div == 0) { 97 | result.numerator /= div; 98 | result.denominator /= div; 99 | } else { 100 | div++; 101 | } 102 | } 103 | return result; 104 | } 105 | 106 | fraction fraction::operator*(const fraction& f) const { 107 | fraction result; 108 | result.numerator = numerator * f.numerator; 109 | result.denominator = denominator * f.denominator; 110 | 111 | int div = 2; 112 | while (div <= result.numerator && div <= result.denominator) { 113 | if (result.numerator % div == 0 && result.denominator % div == 0) { 114 | result.numerator /= div; 115 | result.denominator /= div; 116 | } else { 117 | div++; 118 | } 119 | } 120 | return result; 121 | } 122 | 123 | bool fraction::operator>(const fraction& f) const { 124 | if (denominator == f.denominator) { 125 | return numerator > f.numerator; 126 | } else { 127 | return numerator * f.denominator > f.numerator * denominator; 128 | } 129 | } 130 | 131 | bool fraction::isProper() const { 132 | return numerator > denominator; 133 | } 134 | 135 | int fraction::getCount() { 136 | return fractionsCount; 137 | } 138 | 139 | void fraction::show() const { 140 | cout << "Reduced Form: " << numerator << "/" << denominator << endl; 141 | if (isProper()) { 142 | cout << "The Fraction Is Proper" << endl; 143 | } else { 144 | cout << "The Fraction Is Not Proper" << endl; 145 | } 146 | cout << "Number of Fractions: " << getCount() << endl; 147 | } 148 | 149 | int main() { 150 | fraction f1(4, 5), f2(6, 7); 151 | 152 | cout << "Hard-coded using constructor: " << endl; 153 | cout << "Fractions are 4/5 + 6/7" << endl; 154 | fraction f3 = f1 + f2; 155 | fraction f4 = f1 * f2; 156 | cout << "Addition of fractions: "; 157 | f3.show(); 158 | cout << "Multiplication of fractions: "; 159 | f4.show(); 160 | cout << endl; 161 | 162 | cout << "> Operator: " << endl; 163 | cout << "f1: 4/5, f2: 6/7" << endl; 164 | if (f1 > f2) { 165 | cout << "Fraction One > Fraction Two !!" << endl; 166 | } else { 167 | cout << "Fraction One Not > Fraction Two !!" << endl; 168 | } 169 | 170 | cout << "Hard-coded using setter function: " << endl; 171 | f1.setNum(9); 172 | f1.setDen(4); 173 | f2.setNum(8); 174 | f2.setDen(5); 175 | cout << "Fractions are 9/4 + 8/5" << endl; 176 | fraction f5 = f1 + f2; 177 | fraction f6 = f1 * f2; 178 | cout << "Addition of fractions: "; 179 | f5.show(); 180 | cout << "Multiplication of fractions: "; 181 | f6.show(); 182 | 183 | cout << "> Operator: " << endl; 184 | cout << "f5: 9/4, f6: 8/5" << endl; 185 | if (f5 > f6) { 186 | cout << "Fraction Five > Fraction Six !!" << endl; 187 | } else { 188 | cout << "Fraction Five Not > Fraction Six !!" << endl; 189 | } 190 | 191 | return 0; 192 | } 193 | -------------------------------------------------------------------------------- /Beginners_2/Stage_6/manual.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mabdullahatif/Object_Oriented_Programming/3b43c05ed9e394f7147c379a50757f600d31f21b/Beginners_2/Stage_6/manual.docx -------------------------------------------------------------------------------- /Beginners_2/Stage_6/task1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program defines a class 'BiggerInt' to work with arrays of integers representing large integers. 3 | The class provides various member functions to perform operations like assignment, appending, comparison, and display. 4 | 5 | The 'BiggerInt' class has the following private data members: 6 | - 'big_int_': A pointer to an integer to store the dynamic array of integers representing a large integer. 7 | - 'int_length_': An integer to store the number of elements in the 'big_int_' array. 8 | 9 | The class has the following public member functions: 10 | - 'BiggerInt()': Default constructor to initialize 'big_int_' to NULL and 'int_length_' to 0. 11 | - 'BiggerInt(const int* obj, int size)': Constructor to create a 'BiggerInt' object and initialize it with the given array of integers and its size. 12 | - 'BiggerInt(const BiggerInt& obj)': Copy constructor to create a deep copy of a 'BiggerInt' object. 13 | - 'void assign(const BiggerInt& obj)': Function to assign the 'big_int_' array from another 'BiggerInt' object. 14 | - 'void assign(const int* big_int, int size)': Function to assign the 'big_int_' array from a given array of integers and its size. 15 | - 'void append(const BiggerInt& obj)': Function to append the 'big_int_' array from another 'BiggerInt' object to the current object's 'big_int_'. 16 | - 'void append(const int* big_int, int size)': Function to append a given array of integers and its size to the current object's 'big_int_'. 17 | - 'int compareTo(const BiggerInt& obj)': Function to compare the 'int_length_' of the current object and another 'BiggerInt' object. 18 | Returns 2 if the current object has a smaller 'int_length_', 0 if both have the same 'int_length_', and 1 if the current object has a larger 'int_length_'. 19 | - 'int compareTo(const int* big_int, int size)': Function to compare the 'int_length_' of the current object and a given array of integers and its size. 20 | Returns 2 if the current object has a smaller 'int_length_', 0 if both have the same 'int_length_', and 1 if the current object has a larger 'int_length_'. 21 | - 'void display()': Function to display the elements of the 'big_int_' array in a space-separated format. 22 | If the 'big_int_' array is empty, it prints "No value assigned." 23 | 24 | The 'BiggerInt' class also has a destructor 'BiggerInt::~BiggerInt()' to deallocate the dynamically allocated 'big_int_' array. 25 | 26 | In the 'main' function: 27 | - Two integer arrays 'arr' and 'arr2' are defined to hold sample large integers. 28 | - Two 'BiggerInt' objects 'pro' and 'pro1' are created using different constructors and initialized with the sample arrays. 29 | - The 'display' function is called to show the elements of 'pro' and 'pro1'. 30 | - The 'append' function is called to append the 'big_int_' array of 'pro1' to the 'big_int_' array of 'pro'. 31 | - The 'display' function is called again to show the updated elements of 'pro'. 32 | 33 | Note: The program demonstrates the use of a custom class 'BiggerInt' to work with large integers represented as arrays of integers. 34 | It showcases various member functions that handle assignment, appending, and comparison of large integers. 35 | */ 36 | #include 37 | using namespace std; 38 | 39 | class BiggerInt{ 40 | private: 41 | int* big_int_; 42 | int int_length_; 43 | public: 44 | BiggerInt(); //task1 45 | 46 | BiggerInt(const int * obj, int size); //task2 47 | 48 | BiggerInt(const BiggerInt &obj); //task3 49 | 50 | void assign(const BiggerInt &obj); //task4 51 | 52 | void assign(const int * big_int, int size); //task5 53 | 54 | void append(const BiggerInt &obj); //task6 55 | 56 | void append(const int* big_int, int size); //task7 57 | 58 | int compareTo(const BiggerInt &obj); //task8 59 | 60 | int compareTo(const int* big_int, int size); //task9 61 | 62 | void display(); //task10 63 | 64 | ~BiggerInt(); //task11 65 | 66 | }; 67 | BiggerInt::BiggerInt(){ //task1 68 | big_int_ = 0; 69 | int_length_ =0; 70 | } 71 | 72 | BiggerInt::BiggerInt(const int * obj, int size){ //task2 73 | int_length_=size; 74 | big_int_=new int [int_length_]; 75 | for(int i=0;iint_length_){ 149 | return 2; 150 | } 151 | else if(obj.int_length_==int_length_){ 152 | return 0; 153 | } 154 | else{ 155 | return 1; 156 | } 157 | } 158 | 159 | int BiggerInt::compareTo(const int* big_int, int size){ //task9 160 | if(size>int_length_){ 161 | return 2; 162 | } 163 | else if(size==int_length_){ 164 | return 0; 165 | } 166 | else{ 167 | return 1; 168 | } 169 | } 170 | 171 | void BiggerInt::display(){ //task10 172 | if(int_length_==0){ 173 | cout<<"No value assigned"<=' (greater than or equal to), '<=' (less than or equal to), 22 | // '==' (equal to), '!=' (not equal to). 23 | 24 | // In the 'main' function: 25 | // - Demonstrates operations on 'ComplexNumber' objects, such as addition, subtraction, and comparison. 26 | 27 | #include 28 | using namespace std; 29 | 30 | class ComplexNumber { 31 | private: 32 | int real; 33 | int imaginary; 34 | static int count; //will count the total number of objects created 35 | 36 | public: 37 | ComplexNumber(int real = 0, int imaginary = 0); //with default arguments 38 | ComplexNumber(const ComplexNumber& other); // copy constructor 39 | ~ComplexNumber(); //Does Nothing. 40 | 41 | void Input(); 42 | void Output(); 43 | 44 | static int countDisplay(); //will return the total number of objects created by incrementing as the object is created 45 | bool IsEqual(const ComplexNumber& other); 46 | ComplexNumber Conjugate(); 47 | 48 | // Adding two complex numbers (a + bi) and (c + di) yields ((a+b) + (c+d)i) 49 | ComplexNumber operator+ (const ComplexNumber& num); 50 | 51 | // Subtracting two complex numbers (a + bi) and (c + di) yields ((a-b) + (c-d)i). 52 | ComplexNumber operator- (const ComplexNumber& num); 53 | 54 | // Multiplying two complex numbers (a + bi) and (c + di) yields ((ac-bd) + (ad+bc)i). 55 | ComplexNumber operator* (const ComplexNumber& num); 56 | 57 | // Increment and decrement operators should only add 1 or subtract 1 from the real part 58 | ComplexNumber& operator++(); // pre-increment 59 | ComplexNumber& operator--(); // pre-decrement 60 | ComplexNumber operator++(int); // post-increment 61 | ComplexNumber operator--(int); // post-decrement 62 | 63 | bool operator>=(const ComplexNumber& num); 64 | bool operator<=(const ComplexNumber& num); 65 | bool operator!=(const ComplexNumber& num); 66 | bool operator==(const ComplexNumber& num); 67 | }; 68 | 69 | int ComplexNumber::count = 0; 70 | 71 | ComplexNumber::ComplexNumber(int real, int imaginary) { 72 | this->real = real; 73 | this->imaginary = imaginary; 74 | count++; 75 | } 76 | 77 | ComplexNumber::ComplexNumber(const ComplexNumber& other) { 78 | this->real = other.real; 79 | this->imaginary = other.imaginary; 80 | count++; 81 | } 82 | 83 | ComplexNumber::~ComplexNumber() { 84 | } 85 | 86 | void ComplexNumber::Input() { 87 | cout << "Enter real part: "; 88 | cin >> this->real; 89 | cout << "Enter imaginary part: "; 90 | cin >> this->imaginary; 91 | } 92 | 93 | void ComplexNumber::Output() { 94 | cout << "Answer: "; 95 | cout << this->real << "+"; 96 | cout << "(" << this->imaginary << "i)"; 97 | } 98 | 99 | int ComplexNumber::countDisplay() { 100 | return count; 101 | } 102 | 103 | bool ComplexNumber::IsEqual(const ComplexNumber& other) { 104 | return (this->real == other.real && this->imaginary == other.imaginary); 105 | } 106 | 107 | ComplexNumber ComplexNumber::Conjugate() { 108 | ComplexNumber c; 109 | c.real = this->real; 110 | c.imaginary = -this->imaginary; 111 | return c; 112 | } 113 | 114 | ComplexNumber ComplexNumber::operator+(const ComplexNumber& num) { 115 | ComplexNumber c; 116 | c.real = this->real + num.real; 117 | c.imaginary = this->imaginary + num.imaginary; 118 | return c; 119 | } 120 | 121 | ComplexNumber ComplexNumber::operator-(const ComplexNumber& num) { 122 | ComplexNumber c; 123 | c.real = this->real - num.real; 124 | c.imaginary = this->imaginary - num.imaginary; 125 | return c; 126 | } 127 | 128 | ComplexNumber ComplexNumber::operator*(const ComplexNumber& num) { 129 | ComplexNumber c; 130 | c.real = this->real * num.real - this->imaginary * num.imaginary; 131 | c.imaginary = this->real * num.imaginary + this->imaginary * num.real; 132 | return c; 133 | } 134 | 135 | ComplexNumber& ComplexNumber::operator++() { 136 | this->real++; 137 | return *this; 138 | } 139 | 140 | ComplexNumber& ComplexNumber::operator--() { 141 | this->real--; 142 | return *this; 143 | } 144 | 145 | ComplexNumber ComplexNumber::operator++(int) { 146 | ComplexNumber c = *this; 147 | this->real++; 148 | return c; 149 | } 150 | 151 | ComplexNumber ComplexNumber::operator--(int) { 152 | ComplexNumber c = *this; 153 | this->real--; 154 | return c; 155 | } 156 | 157 | bool ComplexNumber::operator>=(const ComplexNumber& num) { 158 | return (this->real >= num.real && this->imaginary >= num.imaginary); 159 | } 160 | 161 | bool ComplexNumber::operator<=(const ComplexNumber& num) { 162 | return (this->real <= num.real && this->imaginary <= num.imaginary); 163 | } 164 | bool ComplexNumber::operator==(const ComplexNumber& num) { 165 | return (this->real == num.real && this->imaginary == num.imaginary); 166 | } 167 | bool ComplexNumber::operator!=(const ComplexNumber& num) { 168 | return !(*this == num); 169 | } 170 | 171 | 172 | 173 | 174 | int main() { 175 | ComplexNumber c1(1, 3), c2(3, 7), c3, c4, c5; 176 | cout << "Sum of C1 and C2 is: "; c3 = c1 + c2; cout << endl; 177 | c3.Output(); 178 | cout << endl; 179 | 180 | cout << "Subtraction of C1 and C2 is: "; c4 = c1 - c2; cout << endl; 181 | c4.Output(); 182 | cout << endl; 183 | 184 | cout << "Multiplication of C1 and C2 is: "; c5 = c1 * c2; cout << endl; 185 | c5.Output(); 186 | cout << endl; 187 | 188 | cout << endl; 189 | cout << c1.IsEqual(c2) << endl; 190 | cout << endl; 191 | 192 | cout << "Increment of C1: "; c1++; ComplexNumber c6 = c1; cout << endl; c6.Output(); cout << endl; 193 | cout << "Decrement of C1: "; c1--; ComplexNumber c7 = c1; cout << endl; c7.Output(); cout << endl; 194 | 195 | cout << endl; 196 | cout << endl; 197 | 198 | c4.Input(); 199 | c4.Output(); 200 | return 0; 201 | } 202 | -------------------------------------------------------------------------------- /Beginners_2/Stage_7/task2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program defines a class called 'Quadratic' that represents quadratic equations 3 | of the form ax² + bx + c. The class has three private data members: 'a', 'b', and 'c', 4 | to store the coefficients of the quadratic equation. The class provides a default constructor 5 | to initialize the coefficients to zero and an overloaded constructor to allow custom initialization 6 | of the coefficients. The copy constructor is also implemented to create a new 'Quadratic' object with 7 | the same coefficient values as an existing object. The destructor is provided but does not perform any 8 | specific actions as there is no dynamic memory allocation. 9 | 10 | The class supports various arithmetic operations through overloaded operators, including addition, 11 | subtraction, and multiplication by a constant. Additionally, it provides comparison operators for checking 12 | the equality and inequality of 'Quadratic' objects. A friend function 'operator>>' is used to allow user input 13 | of 'Quadratic' objects, prompting the user to enter the values of 'a', 'b', and 'c'. Another friend function 'operator<<' 14 | is implemented to display the quadratic equation in the standard form (ax² + bx + c). 15 | 16 | In the 'main' function, the program demonstrates the use of 'Quadratic' objects by performing arithmetic operations 17 | (addition, multiplication) on them. It also allows user input to set the coefficients of a 'Quadratic' object and displays 18 | the result. The program then compares two 'Quadratic' objects for equality and prints the respective messages based on the comparison result. 19 | */ 20 | 21 | #include 22 | using namespace std; 23 | 24 | class Quadratic { 25 | private: 26 | int a; // First part of the quadratic equation 27 | int b; // Second part of the equation 28 | int c; // Third part of the quadratic equation (ax² + bx + c) 29 | 30 | public: 31 | Quadratic(); 32 | Quadratic(int a, int b, int c); // overloaded constructor 33 | Quadratic(const Quadratic& other); // copy constructor 34 | ~Quadratic(); // destructor 35 | 36 | Quadratic operator+(const Quadratic& other) const; // add two quadratic objects 37 | Quadratic operator-(const Quadratic& other) const; // subtract one quadratic object from another 38 | Quadratic operator*(int constant) const; // multiply a constant with Quadratic object 39 | 40 | friend istream& operator>>(istream& in, Quadratic& quad); // input a quadratic object 41 | friend ostream& operator<<(ostream& out, const Quadratic& quad); // output a quadratic object 42 | 43 | bool operator==(const Quadratic& other) const; // equality operator 44 | bool operator!=(const Quadratic& other) const; // inequality operator 45 | 46 | Quadratic& operator=(const Quadratic& other); // assignment operator 47 | }; 48 | 49 | Quadratic::Quadratic() : a(0), b(0), c(0) {} 50 | 51 | Quadratic::Quadratic(int a, int b, int c) : a(a), b(b), c(c) {} 52 | 53 | Quadratic::Quadratic(const Quadratic& other) { 54 | this->a = other.a; 55 | this->b = other.b; 56 | this->c = other.c; 57 | } 58 | 59 | Quadratic::~Quadratic() {} 60 | 61 | Quadratic Quadratic::operator+(const Quadratic& other) const { 62 | Quadratic result; 63 | result.a = this->a + other.a; 64 | result.b = this->b + other.b; 65 | result.c = this->c + other.c; 66 | return result; 67 | } 68 | 69 | Quadratic Quadratic::operator-(const Quadratic& other) const { 70 | Quadratic result; 71 | result.a = this->a - other.a; 72 | result.b = this->b - other.b; 73 | result.c = this->c - other.c; 74 | return result; 75 | } 76 | 77 | Quadratic Quadratic::operator*(int constant) const { 78 | Quadratic result; 79 | result.a = this->a * constant; 80 | result.b = this->b * constant; 81 | result.c = this->c * constant; 82 | return result; 83 | } 84 | 85 | istream& operator>>(istream& in, Quadratic& quad) { 86 | cout << "Enter coefficients (a b c): "; 87 | in >> quad.a >> quad.b >> quad.c; 88 | return in; 89 | } 90 | 91 | ostream& operator<<(ostream& out, const Quadratic& quad) { 92 | out << quad.a << "x^2 + " << quad.b << "x + " << quad.c; 93 | return out; 94 | } 95 | 96 | bool Quadratic::operator==(const Quadratic& other) const { 97 | return (this->a == other.a && this->b == other.b && this->c == other.c); 98 | } 99 | 100 | bool Quadratic::operator!=(const Quadratic& other) const { 101 | return !(*this == other); 102 | } 103 | 104 | Quadratic& Quadratic::operator=(const Quadratic& other) { 105 | if (this == &other) { 106 | return *this; 107 | } 108 | 109 | this->a = other.a; 110 | this->b = other.b; 111 | this->c = other.c; 112 | return *this; 113 | } 114 | 115 | 116 | int main() { 117 | Quadratic q1(1, 2, 3), q2(2, 3, 4), q3, q4; 118 | q3 = q1 + q2; 119 | cout << "q1 + q2 = " << q3 << endl; 120 | 121 | q4 = q1 * 2; 122 | cout << "q1 * 2 = " << q4 << endl; 123 | 124 | cout << "Enter values for a, b, c for q3: "; 125 | cin >> q3; 126 | cout << "q3 = " << q3 << endl; 127 | 128 | if (q1 == q2) { 129 | cout << "q1 and q2 are equal" << endl; 130 | } else { 131 | cout << "q1 and q2 are not equal" << endl; 132 | } 133 | 134 | return 0; 135 | } -------------------------------------------------------------------------------- /Beginners_2/Stage_8/manual.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mabdullahatif/Object_Oriented_Programming/3b43c05ed9e394f7147c379a50757f600d31f21b/Beginners_2/Stage_8/manual.docx -------------------------------------------------------------------------------- /Beginners_2/Stage_8/task1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program defines a class called 'BiggerInt' that represents a dynamic array of integers. 3 | The class has private data members 'big_int_' and 'int_length_' to store the array and its size, 4 | respectively. The class provides a default constructor to initialize the array as nullptr and size to zero, 5 | an overloaded constructor to allow the creation of an array with specified values, and a copy constructor 6 | to create a new 'BiggerInt' object with the same data as an existing object. The destructor is implemented to 7 | release the dynamically allocated memory when the object is destroyed. 8 | 9 | The class supports various operations, including assignment, pre-increment, post-increment, pre-decrement, 10 | and post-decrement using overloaded operators. It also provides getter and setter functions for the array and its size, 11 | as well as an overloaded '[]' operator for element access. 12 | 13 | In the 'main' function, the program demonstrates the use of the 'BiggerInt' class by creating two 'BiggerInt' 14 | objects, 'e1' and 'e2', and initializing them with different arrays. It then performs pre-increment and post-increment 15 | operations on the arrays using the overloaded operators and displays the results. Similarly, it demonstrates pre-decrement 16 | and post-decrement operations and their corresponding results. The program finally prints the modified arrays after 17 | the increment and decrement operations. 18 | */ 19 | 20 | #include 21 | using namespace std; 22 | 23 | class BiggerInt { 24 | private: 25 | int* big_int_; 26 | int int_length_; 27 | 28 | public: 29 | BiggerInt(); 30 | BiggerInt(const int* obj, int size); 31 | BiggerInt(const BiggerInt& obj); 32 | BiggerInt operator=(const BiggerInt& c); 33 | ~BiggerInt(); 34 | 35 | void display(); 36 | void setbig_int_(int*); 37 | void setint_length_(int); 38 | int* getbig_int_(); 39 | int getint_length_(); 40 | int& operator[](int); 41 | 42 | BiggerInt operator++(); // pre-increment 43 | BiggerInt operator++(int); // post-increment 44 | BiggerInt operator--(); // pre-decrement 45 | BiggerInt operator--(int); // post-decrement 46 | }; 47 | 48 | BiggerInt::BiggerInt() { 49 | big_int_ = nullptr; 50 | int_length_ = 0; 51 | } 52 | 53 | BiggerInt::BiggerInt(const int* obj, int size) { 54 | int_length_ = size; 55 | big_int_ = new int[int_length_]; 56 | for (int i = 0; i < int_length_; i++) { 57 | big_int_[i] = obj[i]; 58 | } 59 | } 60 | 61 | BiggerInt::BiggerInt(const BiggerInt& obj) { 62 | int_length_ = obj.int_length_; 63 | big_int_ = new int[obj.int_length_]; 64 | for (int i = 0; i < int_length_; i++) { 65 | big_int_[i] = obj.big_int_[i]; 66 | } 67 | } 68 | 69 | BiggerInt BiggerInt::operator=(const BiggerInt& c) { 70 | if (this == &c) { 71 | return *this; 72 | } 73 | 74 | if (big_int_ != nullptr) { 75 | delete[] big_int_; 76 | } 77 | 78 | int_length_ = c.int_length_; 79 | big_int_ = new int[int_length_]; 80 | for (int i = 0; i < int_length_; i++) { 81 | big_int_[i] = c.big_int_[i]; 82 | } 83 | 84 | return *this; 85 | } 86 | 87 | BiggerInt::~BiggerInt() { 88 | delete[] big_int_; 89 | } 90 | 91 | void BiggerInt::display() { 92 | if (int_length_ == 0) { 93 | cout << "No value assigned"; 94 | } else { 95 | for (int i = 0; i < int_length_; i++) { 96 | cout << big_int_[i]; 97 | } 98 | cout << endl; 99 | } 100 | } 101 | 102 | void BiggerInt::setbig_int_(int* big) { 103 | big_int_ = big; 104 | } 105 | 106 | void BiggerInt::setint_length_(int len) { 107 | int_length_ = len; 108 | } 109 | 110 | int* BiggerInt::getbig_int_() { 111 | return big_int_; 112 | } 113 | 114 | int BiggerInt::getint_length_() { 115 | return int_length_; 116 | } 117 | 118 | int& BiggerInt::operator[](int index) { 119 | if (index >= int_length_) { 120 | cout << "Array index out of bounds, exiting"; 121 | exit(0); 122 | } 123 | return big_int_[index]; 124 | } 125 | 126 | BiggerInt BiggerInt::operator++() { 127 | for (int i = 0; i < int_length_; i++) { 128 | ++big_int_[i]; 129 | } 130 | return *this; 131 | } 132 | 133 | BiggerInt BiggerInt::operator++(int) { 134 | BiggerInt copy = *this; 135 | for (int i = 0; i < int_length_; i++) { 136 | big_int_[i]++; 137 | } 138 | return copy; 139 | } 140 | 141 | BiggerInt BiggerInt::operator--() { 142 | for (int i = 0; i < int_length_; i++) { 143 | --big_int_[i]; 144 | } 145 | return *this; 146 | } 147 | 148 | BiggerInt BiggerInt::operator--(int) { 149 | BiggerInt copy = *this; 150 | for (int i = 0; i < int_length_; i++) { 151 | big_int_[i]--; 152 | } 153 | return copy; 154 | } 155 | int main() { 156 | int arr1[5] = {1, 2, 3, 4, 5}; 157 | int arr2[4] = {6, 7, 8, 9}; 158 | 159 | BiggerInt e1(arr1, 5); 160 | BiggerInt e2(arr2, 4); 161 | 162 | cout << "Array 1: "; 163 | e1.display(); 164 | 165 | cout << "Array 2: "; 166 | e2.display(); 167 | 168 | cout << "Using pre-increment on Array 1: "; 169 | (++e1).display(); 170 | 171 | cout << "Using post-increment on Array 2: "; 172 | (e2++).display(); 173 | 174 | cout << "After post-increment, Array 2: "; 175 | e2.display(); 176 | 177 | cout << "Using pre-decrement on Array 1: "; 178 | (--e1).display(); 179 | 180 | cout << "Using post-decrement on Array 2: "; 181 | (e2--).display(); 182 | 183 | cout << "After post-decrement, Array 2: "; 184 | e2.display(); 185 | 186 | return 0; 187 | } -------------------------------------------------------------------------------- /Beginners_2/Stage_8/task2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program defines three classes: 'Point', 'Circle', and 'Cylinder'. 3 | The 'Point' class represents a point in 2D space with x and y coordinates. 4 | It has constructors to initialize the coordinates and getters to access them. 5 | 6 | The 'Circle' class represents a circle in 2D space and is defined using its center point (an instance of the 'Point' class) and radius. 7 | The class contains functions to get the radius and center coordinates of the circle, as well as a function to check if a given point lies on the circle. 8 | 9 | The 'Cylinder' class represents a cylinder in 3D space. It is defined by its height, two circles (top and bottom) to represent the base, and their respective radii. 10 | The class includes a constructor to initialize the cylinder and functions to get its height. 11 | It also has a function to check if a given point lies on the cylinder, either on its top and bottom circles or between them along the height. 12 | 13 | In the 'main' function, the program demonstrates the usage of these classes. 14 | It creates an instance of the 'Point' class, sets its coordinates, and displays them. 15 | It then creates an instance of the 'Circle' class, sets its center point and radius, and checks if the previously created point lies on the circle. 16 | Similarly, it creates an instance of the 'Cylinder' class, sets its attributes, and checks if the point lies on the cylinder. 17 | The program then prints the results of these checks. 18 | */ 19 | 20 | #include 21 | using namespace std; 22 | 23 | class Point { 24 | private: 25 | int* x; 26 | int* y; 27 | 28 | public: 29 | // Constructors, Getters and Destructor 30 | Point(int, int); 31 | int getx(); 32 | int gety(); 33 | ~Point(); 34 | void display(); 35 | void setx(int); 36 | void sety(int); 37 | }; 38 | 39 | Point::Point(int X, int Y) { 40 | this->x = new int; 41 | this->y = new int; 42 | 43 | *x = X; 44 | *y = Y; 45 | } 46 | 47 | int Point::getx() { 48 | return *x; 49 | } 50 | 51 | int Point::gety() { 52 | return *y; 53 | } 54 | 55 | Point::~Point() { 56 | delete x, y; 57 | } 58 | 59 | void Point::display() { 60 | cout << "(" << *x << "," << *y << ")"; 61 | } 62 | 63 | float square(int sqr) { 64 | return sqr * sqr; 65 | } 66 | 67 | class Circle { 68 | private: 69 | float* radius; 70 | Point* coordinate; 71 | Point Pin; 72 | 73 | public: 74 | // Constructors, Getters, Destructor 75 | Circle(float, int, int); 76 | float getradius(); 77 | int getcenterX(); 78 | int getcenterY(); 79 | ~Circle(); 80 | bool CheckOnCircle(Point& p1); // Returns true if point lies in circle, false otherwise 81 | }; 82 | 83 | Circle::Circle(float R, int x, int y) : Pin(x, y) { 84 | this->radius = new float; 85 | *radius = R; 86 | coordinate = &Pin; 87 | } 88 | 89 | float Circle::getradius() { 90 | return *radius; 91 | } 92 | 93 | int Circle::getcenterX() { 94 | return coordinate->getx(); 95 | } 96 | 97 | int Circle::getcenterY() { 98 | return coordinate->gety(); 99 | } 100 | 101 | Circle::~Circle() { 102 | delete radius; 103 | } 104 | 105 | bool Circle::CheckOnCircle(Point& p1) { 106 | float length = square(p1.getx() - coordinate->getx()) - square(p1.gety() - coordinate->gety()); 107 | if (length <= square(*radius)) { 108 | return true; 109 | } else { 110 | return false; 111 | } 112 | } 113 | 114 | class Cylinder { 115 | private: 116 | int height; 117 | Circle* top; // Circle on top of Cylinder 118 | Circle* bottom; // Circle on bottom of Cylinder 119 | 120 | public: 121 | // Constructors, Destructor 122 | Cylinder(int, float, int, int, float, int, int); 123 | int getheight(); 124 | ~Cylinder(); 125 | bool CheckOnCylinder(Point& p1); // Returns true if point lies in cylinder, false otherwise 126 | }; 127 | 128 | Cylinder::Cylinder(int h, float rad1, int x1, int y1, float rad2, int x2, int y2) 129 | : top(new Circle(rad1, x1, y1)), bottom(new Circle(rad2, x2, y2)) { 130 | height = h; 131 | } 132 | 133 | int Cylinder::getheight() { 134 | return height; 135 | } 136 | 137 | Cylinder::~Cylinder() { 138 | delete top; 139 | delete bottom; 140 | } 141 | 142 | bool Cylinder::CheckOnCylinder(Point& p1) { 143 | bool isOnTop = top->CheckOnCircle(p1); 144 | bool isOnBottom = bottom->CheckOnCircle(p1); 145 | 146 | if (isOnTop && isOnBottom) { 147 | return true; 148 | } 149 | 150 | // Check if point lies between the top and bottom circles along the height of the cylinder 151 | int centerY = (top->getcenterY() + bottom->getcenterY()) / 2; 152 | int heightHalf = height / 2; 153 | int pointY = p1.gety(); 154 | return (pointY >= centerY - heightHalf && pointY <= centerY + heightHalf); 155 | } 156 | 157 | int main() { 158 | cout << "x and y coordinate : "; 159 | Point a(3, 4); 160 | a.display(); 161 | cout << endl; 162 | 163 | cout << "Check on circle : "; 164 | Circle c(5.3, 2, 3); 165 | cout << c.CheckOnCircle(a) << endl; 166 | 167 | cout << "Check on cylinder : "; 168 | Cylinder cl(7, 5.3, 2, 9, 5.3, 2, 3); 169 | cout << cl.CheckOnCylinder(a) << endl; 170 | 171 | return 0; 172 | } 173 | -------------------------------------------------------------------------------- /Beginners_3/Stage_10/manual.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mabdullahatif/Object_Oriented_Programming/3b43c05ed9e394f7147c379a50757f600d31f21b/Beginners_3/Stage_10/manual.docx -------------------------------------------------------------------------------- /Beginners_3/Stage_10/task1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program demonstrates the concept of polymorphism using a simple banking system. 3 | 4 | There is a base class 'Account' that represents a generic bank account with attributes 'AN' (Account Number) and 'AB' (Account Balance). 5 | It has member functions to get and set these attributes, and also virtual functions 'Print', 'Credit', and 'Debit'. 6 | 7 | Two derived classes, 'CurrentAccount' and 'SavingAccount', are created from the 'Account' class. 8 | The 'CurrentAccount' class adds attributes 'SC' (Service Charges) and 'MB' (Minimum Balance) and overrides the 'Print', 'Credit', and 'Debit' functions. 9 | The 'SavingAccount' class adds an attribute 'IR' (Interest Rate) and also overrides the 'Print', 'Credit', and 'Debit' functions. 10 | 11 | In the 'main' function, an array of pointers to the 'Account' base class is created, and three different account objects (one 'SavingAccount', one 'CurrentAccount', and one 'Account') are dynamically allocated and assigned to the array elements. 12 | 13 | Polymorphism is demonstrated by calling the 'Print', 'Credit', and 'Debit' functions on the array elements, which results in calling the appropriate functions based on the object type. 14 | 15 | Sample output: 16 | ------------------ 17 | Account Number : 0 18 | Account Balance : 0 19 | Interest Rate : 5 20 | Account Number : 0 21 | Account Balance : 0 22 | Services Charges : 100 23 | Minimum Balance : 1000 24 | Account Number : 0 25 | Account Balance : 0 26 | Account destroyed !! 27 | Saving Account destroyed !! 28 | Current Account destroyed !! 29 | ------------------ 30 | */ 31 | 32 | #include 33 | using namespace std; 34 | 35 | class Account { 36 | private: 37 | int AN, AB; 38 | 39 | public: 40 | int getAN() { 41 | return AN; 42 | } 43 | 44 | int getAB() { 45 | return AB; 46 | } 47 | 48 | void setAN(int acn) { 49 | AN = acn; 50 | } 51 | 52 | void setAB(int acb) { 53 | AB = acb; 54 | } 55 | 56 | virtual void Print() { 57 | cout << "Account Number : " << AN << endl 58 | << "Account Balance : " << AB << endl; 59 | } 60 | 61 | virtual void Credit(float) { 62 | } 63 | 64 | virtual void Debit(float) { 65 | } 66 | 67 | virtual ~Account() { 68 | cout << "Account destroyed !!" << endl; 69 | } 70 | }; 71 | 72 | class CurrentAccount : public Account { 73 | private: 74 | int SC, MB; 75 | 76 | public: 77 | CurrentAccount() { 78 | SC = 100; // Default service charges 79 | MB = 1000; // Default minimum balance 80 | } 81 | 82 | void Print() { 83 | Account::Print(); 84 | cout << "Services Charges : " << SC << endl 85 | << "Minimum Balance : " << MB << endl; 86 | } 87 | 88 | void Credit(float amount) { 89 | setAB(getAB() + amount); 90 | } 91 | 92 | void Debit(float amount) { 93 | if (amount <= getAB()) { 94 | setAB(getAB() - amount); 95 | if (getAB() < MB) { 96 | setAB(getAB() - SC); 97 | } 98 | } 99 | } 100 | 101 | ~CurrentAccount() { 102 | cout << "Current Account destroyed !!" << endl; 103 | } 104 | }; 105 | 106 | class SavingAccount : public Account { 107 | private: 108 | int IR; // Interest Rate 109 | 110 | public: 111 | SavingAccount() { 112 | IR = 5; // Default interest rate 113 | } 114 | 115 | void Print() { 116 | Account::Print(); 117 | cout << "Interest Rate : " << IR << endl; 118 | } 119 | 120 | void Credit(float amount) { 121 | setAB(getAB() + amount); 122 | } 123 | 124 | void Debit(float amount) { 125 | if (amount <= getAB()) { 126 | setAB(getAB() - amount); 127 | } 128 | } 129 | 130 | ~SavingAccount() { 131 | cout << "Saving Account destroyed !!" << endl; 132 | } 133 | }; 134 | 135 | int main() { 136 | // Array of base pointers 137 | Account** alist = new Account*[3]; 138 | alist[0] = new SavingAccount; 139 | alist[1] = new CurrentAccount; 140 | alist[2] = new Account; 141 | 142 | // Print data of all accounts polymorphic behavior 143 | for (int i = 0; i < 3; i++) 144 | alist[i]->Print(); 145 | 146 | // Credit and debit polymorphic behavior 147 | alist[0]->Credit(50); 148 | alist[2]->Debit(333); 149 | 150 | // Freeing the dynamically allocated objects 151 | for (int i = 0; i < 3; i++) { 152 | delete alist[i]; 153 | } 154 | delete[] alist; 155 | 156 | return 0; 157 | } 158 | -------------------------------------------------------------------------------- /Beginners_3/Stage_11/manual.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mabdullahatif/Object_Oriented_Programming/3b43c05ed9e394f7147c379a50757f600d31f21b/Beginners_3/Stage_11/manual.docx -------------------------------------------------------------------------------- /Beginners_3/Stage_11/task1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program demonstrates the use of a function template to compare two values for equality. 3 | 4 | The function template 'isEqualTo' is defined, which takes two parameters of the same data type 'T'. 5 | It compares the values of 'a' and 'b' using the '==' operator and returns true if they are equal, otherwise false. 6 | 7 | In the 'main' function, we have variables of different data types: 'num1' and 'num2' of type int, 'f1' and 'f2' of type float, and 'ch1' and 'ch2' of type char. 8 | 9 | The 'isEqualTo' function is called with each pair of variables, and the result is printed to the console. 10 | 11 | Sample output: 12 | ------------------ 13 | Is num1 equal to num2? 0 14 | Is f1 equal to f2? 1 15 | Is ch1 equal to ch2? 0 16 | ------------------ 17 | Here, num1 is not equal to num2, f1 is equal to f2, and ch1 is not equal to ch2. 18 | */ 19 | 20 | #include 21 | using namespace std; 22 | 23 | template 24 | bool isEqualTo(T a, T b) { 25 | return a == b; 26 | } 27 | 28 | int main() { 29 | int num1 = 5, num2 = 10; 30 | float f1 = 3.14, f2 = 3.14; 31 | char ch1 = 'A', ch2 = 'B'; 32 | 33 | cout << "Is num1 equal to num2? " << isEqualTo(num1, num2) << endl; 34 | cout << "Is f1 equal to f2? " << isEqualTo(f1, f2) << endl; 35 | cout << "Is ch1 equal to ch2? " << isEqualTo(ch1, ch2) << endl; 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Beginners_3/Stage_11/task2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program demonstrates the use of abstract classes and inheritance to create different types of bank accounts. 3 | 4 | The 'Account' class is defined as an abstract class with two pure virtual functions: 'Debit' and 'Credit'. 5 | The class also has two member variables: 'accountNumber' to store the account number and 'accountBalance' to store the current balance. 6 | 7 | Two derived classes 'CurrentAccount' and 'SavingAccount' inherit from the 'Account' class and provide their implementations of the 'Debit' and 'Credit' functions. 8 | Additionally, a new class 'BasicAccount' is created that also inherits from the 'Account' class and implements its own version of 'Debit' and 'Credit'. 9 | 10 | In the 'main' function, we create an array of pointers to 'Account' class objects to hold different types of accounts. 11 | We create instances of 'CurrentAccount', 'SavingAccount', and 'BasicAccount' using dynamic memory allocation. 12 | We then print the account details using the 'Print' function for each account type and finally, free the dynamically allocated memory. 13 | 14 | Sample output: 15 | ------------------ 16 | Account Number: 456 17 | Account Balance: 2000 18 | Minimum Balance: 500 19 | Service Charges: 50 20 | Account Number: 789 21 | Account Balance: 3000 22 | Interest Rate: 5 23 | Account Number: 123 24 | Account Balance: 1000 25 | ------------------ 26 | Here, we see the details for each account, including the minimum balance and service charges for the current account and the interest rate for the saving account. 27 | The basic account has no additional features, and its details only include the account number and balance. 28 | */ 29 | 30 | #include 31 | using namespace std; 32 | 33 | class Account { 34 | protected: // Change access specifier to protected 35 | int accountNumber; 36 | float accountBalance; 37 | 38 | public: 39 | Account(int num, float balance) : accountNumber(num), accountBalance(balance) {} 40 | 41 | int getAccountNumber() const { 42 | return accountNumber; 43 | } 44 | 45 | float getAccountBalance() const { 46 | return accountBalance; 47 | } 48 | 49 | virtual void Debit(float amount) = 0; 50 | virtual void Credit(float amount) = 0; 51 | 52 | virtual void Print() { 53 | cout << "Account Number: " << accountNumber << endl; 54 | cout << "Account Balance: " << accountBalance << endl; 55 | } 56 | 57 | virtual ~Account() {} 58 | }; 59 | 60 | class CurrentAccount : public Account { 61 | private: 62 | float serviceCharges; 63 | float minBalance; 64 | 65 | public: 66 | CurrentAccount(int num, float balance, float charges, float minBal) 67 | : Account(num, balance), serviceCharges(charges), minBalance(minBal) {} 68 | 69 | void Debit(float amount) override { 70 | if (amount <= getAccountBalance()) { 71 | accountBalance -= amount; 72 | } else { 73 | cout << "Insufficient balance!" << endl; 74 | } 75 | } 76 | 77 | void Credit(float amount) override { 78 | accountBalance += amount; 79 | if (accountBalance < minBalance) { 80 | accountBalance -= serviceCharges; 81 | } 82 | } 83 | 84 | void Print() override { 85 | Account::Print(); 86 | cout << "Minimum Balance: " << minBalance << endl; 87 | cout << "Service Charges: " << serviceCharges << endl; 88 | } 89 | }; 90 | 91 | class SavingAccount : public Account { 92 | private: 93 | float interestRate; 94 | 95 | public: 96 | SavingAccount(int num, float balance, float rate) 97 | : Account(num, balance), interestRate(rate) {} 98 | 99 | void Debit(float amount) override { 100 | if (amount <= getAccountBalance()) { 101 | accountBalance -= amount; 102 | } else { 103 | cout << "Insufficient balance!" << endl; 104 | } 105 | } 106 | 107 | void Credit(float amount) override { 108 | accountBalance += amount; 109 | } 110 | 111 | void Print() override { 112 | Account::Print(); 113 | cout << "Interest Rate: " << interestRate << endl; 114 | } 115 | }; 116 | 117 | class BasicAccount : public Account { 118 | public: 119 | BasicAccount(int num, float balance) 120 | : Account(num, balance) {} 121 | 122 | void Debit(float amount) override { 123 | if (amount <= getAccountBalance()) { 124 | accountBalance -= amount; 125 | } else { 126 | cout << "Insufficient balance!" << endl; 127 | } 128 | } 129 | 130 | void Credit(float amount) override { 131 | accountBalance += amount; 132 | } 133 | }; 134 | 135 | int main() { 136 | Account* accounts[4]; 137 | accounts[0] = new CurrentAccount(456, 2000, 50, 500); 138 | accounts[1] = new SavingAccount(789, 3000, 5); 139 | accounts[2] = new BasicAccount(123, 1000); 140 | 141 | for (int i = 0; i < 3; ++i) { 142 | accounts[i]->Print(); 143 | delete accounts[i]; 144 | } 145 | 146 | return 0; 147 | } 148 | -------------------------------------------------------------------------------- /Beginners_3/Stage_12/manual.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mabdullahatif/Object_Oriented_Programming/3b43c05ed9e394f7147c379a50757f600d31f21b/Beginners_3/Stage_12/manual.docx -------------------------------------------------------------------------------- /Beginners_3/Stage_12/task1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program demonstrates the use of a template class to create a fixed-size sequence container. 3 | 4 | The 'Sequence' class is defined as a template class, which means it can work with different data types. 5 | The class takes two template parameters: 'T' (the data type) and 'N' (the size of the sequence). 6 | The class has a private member array 'memblock' of type 'T' and size 'N'. 7 | 8 | The class provides two member functions: 'setmember' and 'getmember'. 9 | - 'setmember' takes an index 'x' and a value of type 'T', and it sets the value at the given index in the 'memblock' array. 10 | - 'getmember' takes an index 'x' and returns the value at that index in the 'memblock' array. 11 | 12 | In the 'main' function, two instances of the 'Sequence' class are created: 'myints' with the data type 'int' and size 5, 13 | and 'myfloats' with the data type 'double' and size 5. 14 | 15 | We use the 'setmember' function to set the value 100 at index 0 in 'myints' and the value 3.1416 at index 3 in 'myfloats'. 16 | 17 | Then, we use the 'getmember' function to retrieve the values set earlier and print them using 'cout'. 18 | 19 | Sample output: 20 | ------------------ 21 | 100 22 | 3.1416 23 | ------------------ 24 | Here, we see the values 100 and 3.1416 printed, which were set in 'myints' and 'myfloats', respectively. 25 | */ 26 | 27 | #include 28 | using namespace std; 29 | 30 | template 31 | class Sequence { 32 | private: 33 | T memblock[N]; 34 | 35 | public: 36 | void setmember(int x, T value) { 37 | memblock[x] = value; 38 | } 39 | 40 | T getmember(int x) { 41 | return memblock[x]; 42 | } 43 | }; 44 | 45 | int main() { 46 | Sequence myints; 47 | Sequence myfloats; 48 | 49 | myints.setmember(0, 100); 50 | myfloats.setmember(3, 3.1416); 51 | 52 | cout << myints.getmember(0) << '\n'; 53 | cout << myfloats.getmember(3) << '\n'; 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Beginners_3/Stage_12/task2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program demonstrates two template functions: 'range' and 'shiftColumns'. 3 | 4 | 1. range: 5 | The 'range' function takes a 2D array 'array', along with the number of 'rows' and 'columns' in the array. 6 | It calculates the range of values in the 2D array, which is defined as the difference between the maximum and minimum values. 7 | The function then returns the value that is one-fourth of the range, added to the minimum value. 8 | 9 | 2. shiftColumns: 10 | The 'shiftColumns' function takes a 2D array 'array', along with the number of 'rows' and 'columns' in the array. 11 | This function shifts the elements of each row in the array one position to the left. 12 | The leftmost element of each row wraps around to the rightmost position. 13 | 14 | In the 'main' function, a 3x3 integer matrix 'matrix' is declared and initialized. 15 | A dynamic 2D integer array 'dynamicMatrix' is created using dynamic memory allocation and is initialized with the values from the 'matrix' array. 16 | 17 | The 'range' function is called on 'dynamicMatrix', and the result is printed. 18 | 19 | Then, the 'shiftColumns' function is called on 'dynamicMatrix', and the resulting matrix is printed. 20 | 21 | Finally, the dynamically allocated memory for 'dynamicMatrix' is deallocated. 22 | 23 | Sample output: 24 | ------------------ 25 | Range of matrix: 5 26 | Shifted matrix: 27 | 2 3 1 28 | 5 6 4 29 | 8 9 7 30 | ------------------ 31 | The range of values in the matrix is 5, and the matrix is shifted one position to the left. 32 | */ 33 | 34 | 35 | #include 36 | using namespace std; 37 | 38 | template 39 | T range(T** array, int rows, int columns) { 40 | T min = array[0][0]; 41 | T max = array[0][0]; 42 | 43 | for (int i = 0; i < rows; ++i) { 44 | for (int j = 0; j < columns; ++j) { 45 | if (array[i][j] < min) { 46 | min = array[i][j]; 47 | } 48 | if (array[i][j] > max) { 49 | max = array[i][j]; 50 | } 51 | } 52 | } 53 | 54 | return ((max - min) / static_cast(4)) + min; 55 | } 56 | 57 | template 58 | void shiftColumns(T** array, int rows, int columns) { 59 | for (int i = 0; i < rows; ++i) { 60 | T temp = array[i][0]; 61 | for (int j = 0; j < columns - 1; ++j) { 62 | array[i][j] = array[i][j + 1]; 63 | } 64 | array[i][columns - 1] = temp; 65 | } 66 | } 67 | 68 | int main() { 69 | const int rows = 3; 70 | const int columns = 3; 71 | int matrix[rows][columns] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; 72 | 73 | int** dynamicMatrix = new int* [rows]; 74 | for (int i = 0; i < rows; ++i) { 75 | dynamicMatrix[i] = new int[columns]; 76 | for (int j = 0; j < columns; ++j) { 77 | dynamicMatrix[i][j] = matrix[i][j]; 78 | } 79 | } 80 | 81 | cout << "Range of matrix: " << range(dynamicMatrix, rows, columns) << endl; 82 | 83 | shiftColumns(dynamicMatrix, rows, columns); 84 | 85 | cout << "Shifted matrix:\n"; 86 | for (int i = 0; i < rows; ++i) { 87 | for (int j = 0; j < columns; ++j) { 88 | cout << dynamicMatrix[i][j] << " "; 89 | } 90 | cout << endl; 91 | } 92 | 93 | // Deallocate dynamic memory 94 | for (int i = 0; i < rows; ++i) { 95 | delete[] dynamicMatrix[i]; 96 | } 97 | delete[] dynamicMatrix; 98 | 99 | return 0; 100 | } 101 | -------------------------------------------------------------------------------- /Beginners_3/Stage_12/task3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program demonstrates the implementation of a simple dynamic array-like class called 'Vector' using templates. 3 | 4 | The 'Vector' class has the following features: 5 | 1. Dynamic Resizing: When the number of elements exceeds the capacity, the array is dynamically resized to double its current capacity. 6 | 2. Insert Element: It allows inserting elements at the end of the array. 7 | 3. Copy Constructor: It has a copy constructor that creates a new vector by copying the elements from another vector. 8 | 4. Destructor: It deallocates the dynamically allocated memory for the array when the vector goes out of scope. 9 | 10 | In the 'main' function: 11 | 1. An 'int' type vector 'iv1' with an initial capacity of 2 is created. 12 | 2. Three elements (5, 6, and 1) are inserted into 'iv1'. 13 | 3. A new vector 'iv2' is created by using the copy constructor with 'iv1' as the argument. 14 | 4. The 'iv2' vector is printed using the overloaded '<<' operator, which prints the elements of the vector. 15 | 16 | Sample output: 17 | ------------------ 18 | 5 6 1 19 | ------------------ 20 | The 'iv2' vector is a copy of 'iv1', and it contains the same elements as 'iv1'. 21 | */ 22 | 23 | #include 24 | using namespace std; 25 | 26 | template 27 | class Vector { 28 | private: 29 | T* arrayPtr; 30 | int capacity; 31 | int totalElements; 32 | 33 | public: 34 | Vector(int cap) : capacity(cap), totalElements(0) { 35 | arrayPtr = new T[capacity]; 36 | } 37 | 38 | Vector(const Vector& other) : capacity(other.capacity), totalElements(other.totalElements) { 39 | arrayPtr = new T[capacity]; 40 | for (int i = 0; i < totalElements; ++i) { 41 | arrayPtr[i] = other.arrayPtr[i]; 42 | } 43 | } 44 | 45 | ~Vector() { 46 | delete[] arrayPtr; 47 | } 48 | 49 | void insertElement(const T& param) { 50 | if (totalElements == capacity) { 51 | capacity *= 2; 52 | T* newArrayPtr = new T[capacity]; 53 | for (int i = 0; i < totalElements; ++i) { 54 | newArrayPtr[i] = arrayPtr[i]; 55 | } 56 | delete[] arrayPtr; 57 | arrayPtr = newArrayPtr; 58 | } 59 | arrayPtr[totalElements++] = param; 60 | } 61 | 62 | friend ostream& operator<<(ostream& os, const Vector& vec) { 63 | for (int i = 0; i < vec.totalElements; ++i) { 64 | os << vec.arrayPtr[i] << " "; 65 | } 66 | return os; 67 | } 68 | 69 | int getTotalElements() const { 70 | return totalElements; 71 | } 72 | 73 | int getCapacity() const { 74 | return capacity; 75 | } 76 | }; 77 | 78 | int main() { 79 | Vector iv1(2); 80 | iv1.insertElement(5); 81 | iv1.insertElement(6); 82 | iv1.insertElement(1); 83 | 84 | Vector iv2(iv1); 85 | cout << iv2 << endl; 86 | 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /Beginners_3/Stage_12/task4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program takes a person's date of birth in numeric form (e.g., 8-27-1980) and parses it to validate the day, month, and year. 3 | 4 | The 'parseDate' function: 5 | 1. Parses the input string to extract the day, month, and year components. 6 | 2. Checks if the day, month, and year are valid according to the given rules. 7 | 3. If any component is invalid, it throws the corresponding exception: invalidDay, invalidMonth, or invalidYear. 8 | 4. If the date is valid, it prints the date in a more readable format (e.g., "August 27, 1980"). 9 | 10 | The 'isLeapYear' function: 11 | 1. Determines whether a year is a leap year or not based on the leap year rules. 12 | 13 | In the 'main' function: 14 | 1. Asks the user to enter a person's date of birth in numeric form. 15 | 2. Calls the 'parseDate' function to validate and parse the input date. 16 | 3. Catches any exceptions thrown during the parsing process and prints an error message accordingly. 17 | 18 | Sample output 1: 19 | ------------------ 20 | Enter a person's date of birth in numeric form (e.g., 8-27-1980): 12-31-2000 21 | December 31, 2000 22 | ------------------ 23 | 24 | Sample output 2: 25 | ------------------ 26 | Enter a person's date of birth in numeric form (e.g., 8-27-1980): 2-30-1995 27 | Invalid day! 28 | ------------------ 29 | */ 30 | 31 | #include 32 | #include 33 | using namespace std; 34 | 35 | class invalidDay {}; 36 | class invalidMonth {}; 37 | class invalidYear {}; 38 | 39 | bool isLeapYear(int year) { 40 | return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 41 | } 42 | 43 | void parseDate(const string& date) { 44 | int month, day, year; 45 | char dash1, dash2; 46 | 47 | if (sscanf(date.c_str(), "%d%c%d%c%d", &month, &dash1, &day, &dash2, &year) != 5) { 48 | throw invalidDay(); 49 | } 50 | 51 | if (month < 1 || month > 12) { 52 | throw invalidMonth(); 53 | } 54 | 55 | int daysInMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 56 | 57 | if (isLeapYear(year)) { 58 | daysInMonth[1] = 29; 59 | } 60 | 61 | if (day < 1 || day > daysInMonth[month - 1]) { 62 | throw invalidDay(); 63 | } 64 | 65 | if (year < 0) { 66 | throw invalidYear(); 67 | } 68 | 69 | string monthNames[] = { "January", "February", "March", "April", "May", "June", "July", 70 | "August", "September", "October", "November", "December" }; 71 | cout << monthNames[month - 1] << " " << day << ", " << year << endl; 72 | } 73 | 74 | int main() { 75 | string date; 76 | cout << "Enter a person's date of birth in numeric form (e.g., 8-27-1980): "; 77 | cin >> date; 78 | 79 | try { 80 | parseDate(date); 81 | } 82 | catch (const invalidDay&) { 83 | cout << "Invalid day!" << endl; 84 | } 85 | catch (const invalidMonth&) { 86 | cout << "Invalid month!" << endl; 87 | } 88 | catch (const invalidYear&) { 89 | cout << "Invalid year!" << endl; 90 | } 91 | 92 | return 0; 93 | } 94 | -------------------------------------------------------------------------------- /Beginners_3/Stage_9/manual.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mabdullahatif/Object_Oriented_Programming/3b43c05ed9e394f7147c379a50757f600d31f21b/Beginners_3/Stage_9/manual.docx -------------------------------------------------------------------------------- /Beginners_3/Stage_9/task1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program defines two classes: 'Tyre' and 'Car'. 3 | The 'Tyre' class represents a car tire and contains private attributes for width, aspect ratio, and diameter. 4 | It has constructors to initialize the tire attributes and getters to access them. 5 | The 'Tyre' class also includes a 'PrintTyre' function to display the tire's width, aspect ratio, and diameter. 6 | 7 | The 'Car' class represents a car and contains private attributes for model (year) and company (manufacturer). 8 | It also has an attribute for a 'Tyre' object, which is a composition to represent the car's tires. 9 | The 'Car' class includes a constructor that takes the model, company name, and a 'Tyre' object as arguments to initialize the car attributes. 10 | The 'Car' class also includes a 'PrintCar' function to display the car's model, company, and tire information. 11 | 12 | In the 'main' function, the program demonstrates the usage of these classes. 13 | It creates a 'Tyre' object named 'tNew' with width 12, aspect ratio 10, and diameter 13, and then displays its information using 'PrintTyre'. 14 | Next, it creates a 'Car' object named 'cNew' with model year 2016, company name "Honda", and the 'tNew' tire object. 15 | Finally, it displays the car's information using 'PrintCar', which also includes the details of the tire using the 'PrintTyre' function. 16 | */ 17 | 18 | #include 19 | using namespace std; 20 | class Tyre{ 21 | private: 22 | int* width; 23 | int* aspect_ratio; 24 | int* diameter; 25 | public: 26 | Tyre(); 27 | Tyre(int,int,int); 28 | ~Tyre(); 29 | int getwidth(); 30 | int getaspect_ratio(); 31 | int getdiameter(); 32 | //Constructors, Getters and Destructor 33 | void PrintTyre(){ 34 | cout<<"WIDTH : "<<*width<<" ASPECT_RATIO : "<<*aspect_ratio<<" DIAMETER : "<<*diameter; 35 | } 36 | }; 37 | Tyre::Tyre(){ 38 | width=0; 39 | aspect_ratio=0; 40 | diameter=0; 41 | } 42 | Tyre::Tyre(int W,int A,int D){ 43 | this->width=new int; 44 | this->aspect_ratio=new int; 45 | this->diameter=new int; 46 | *width=W; 47 | *aspect_ratio=A; 48 | *diameter=D; 49 | } 50 | int Tyre::getwidth(){ 51 | return *width; 52 | } 53 | 54 | int Tyre::getaspect_ratio(){ 55 | return *aspect_ratio; 56 | } 57 | 58 | int Tyre::getdiameter(){ 59 | return *diameter; 60 | } 61 | Tyre::~Tyre(){ 62 | delete width,aspect_ratio,diameter; 63 | } 64 | 65 | 66 | class Car{ 67 | private: 68 | int* model; 69 | string* company; 70 | Tyre* t1; 71 | public: 72 | // Car(); 73 | Car(int M,string C,Tyre& T){ 74 | this->model=new int; 75 | this->company=new string; 76 | *model=M; 77 | *company=C; 78 | t1=new Tyre(T.getwidth(), T.getaspect_ratio(), T.getdiameter()); 79 | } 80 | ~Car(){ 81 | delete model,company,t1; 82 | } 83 | //Constructors, Destructor 84 | void PrintCar(){ 85 | cout<<"MODEL : "<<*model<<" COMPANY : "<<*company<<" "; 86 | t1->PrintTyre(); 87 | 88 | } 89 | }; 90 | 91 | 92 | int main(){ 93 | Tyre tNew(12, 10, 13); 94 | tNew.PrintTyre();endl(cout); 95 | Car cNew(2016,"Honda",tNew); 96 | cNew.PrintCar(); 97 | return 0; 98 | } 99 | -------------------------------------------------------------------------------- /Beginners_3/Stage_9/task2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program defines three classes: 'LocalPhone', 'natphone', and 'intphone'. 3 | The 'LocalPhone' class represents a local phone number and contains a private attribute 'Phone' to store the phone number. 4 | It has a constructor to initialize the 'Phone' attribute and functions to set and display the phone number. 5 | 6 | The 'natphone' class is derived from the 'LocalPhone' class and represents a national phone number. 7 | It adds an additional private attribute 'citycode' to store the city code. 8 | The 'natphone' class has a constructor that initializes both 'citycode' and the 'Phone' attribute inherited from the base class. 9 | It also includes functions to set and display the city code. 10 | 11 | The 'intphone' class is derived from the 'natphone' class and represents an international phone number. 12 | It adds an additional private attribute 'countrycode' to store the country code. 13 | The 'intphone' class has a constructor that initializes both 'countrycode' and the 'citycode' attribute inherited from the base class. 14 | It also includes functions to set and display the country code. 15 | 16 | In the 'main' function, the program demonstrates the usage of these classes by creating objects of each class. 17 | It creates a 'LocalPhone' object 'lp' with phone number "1234567", a 'natphone' object 'np' with city code 999 and phone number "1234567", 18 | and an 'intphone' object 'ip' with country code 42, city code 999, and phone number "1234567". 19 | 20 | The program then displays the local phone number using 'showphone', the city code using 'showcity', the country code using 'showcountry', 21 | and the final result, which shows the country code followed by the city code and the phone number, using 'showcountrycode'. 22 | */ 23 | 24 | #include 25 | using namespace std; 26 | class LocalPhone{ 27 | private: 28 | string Phone; 29 | public: 30 | LocalPhone(string n){ 31 | Phone=n; 32 | } 33 | void setphone(string n){ 34 | Phone=n; 35 | } 36 | void showphone(){ 37 | cout<Phone; 38 | } 39 | }; 40 | class natphone:public LocalPhone{ 41 | private: 42 | int citycode; 43 | public: 44 | natphone(int c,string n):LocalPhone(n){ 45 | citycode=c; 46 | } 47 | void setcitycode(int c){ 48 | citycode=c; 49 | } 50 | void showcitycode(){ 51 | cout<citycode; 52 | showphone(); 53 | } 54 | void showcity(){ 55 | cout<citycode; 56 | } 57 | }; 58 | class intphone:public natphone{ 59 | private: 60 | int countrycode; 61 | public: 62 | intphone(int country,int c,string n):natphone(c,n){ 63 | countrycode=country; 64 | } 65 | void setcountrycode(int c){ 66 | countrycode=c; 67 | } 68 | void showcountrycode(){ 69 | cout<countrycode; 70 | showcitycode(); 71 | } 72 | void showcountry(){ 73 | cout<countrycode; 74 | } 75 | }; 76 | int main(){ 77 | LocalPhone lp("1234567"); 78 | natphone np(999,"1234567"); 79 | intphone ip(42,999,"1234567"); 80 | cout<<"Local Phone Number : "; 81 | lp.showphone();endl(cout); 82 | cout<<"City Code : "; 83 | np.showcity();endl(cout); 84 | cout<<"International Code : "; 85 | ip.showcountry();endl(cout); 86 | cout<<"Final result : "; 87 | ip.showcountrycode(); 88 | 89 | return 0; 90 | 91 | } 92 | -------------------------------------------------------------------------------- /Beginners_3/Stage_9/task3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program defines several classes related to employees, including 'Employee', 'Employee2', 'manager', 'scientist', and 'laborer'. 3 | 4 | The 'Employee' class has two protected attributes 'name' and 'number' and two member functions 'getdata' and 'putdata'. 5 | 'getdata' is used to display the employee's name and number, while 'putdata' is used to set the 'name' and 'number' attributes. 6 | 7 | The 'Employee2' class is derived from 'Employee' and has two additional protected attributes 'compensation' and 'time'. 8 | It also has two member functions, 'input' to set the compensation and time, and 'show' to display these attributes. 9 | 10 | The 'manager', 'scientist', and 'laborer' classes are all derived from 'Employee2' and represent different types of employees. 11 | Each of these classes has a 'input' function to set the attributes inherited from the base class as well as their own attributes. 12 | They also have an 'output' function to display all the attributes including name, number, compensation, and time for the respective employee type. 13 | 14 | In the 'main' function, the program creates objects of 'manager', 'scientist', and 'laborer' classes, and uses the 'input' function to set their attributes. 15 | Then it calls the 'output' function for each employee type to display their information. 16 | */ 17 | 18 | #include 19 | using namespace std; 20 | class Employee{ 21 | protected: 22 | string name; 23 | string number; 24 | public: 25 | void getdata(){ 26 | cout<name=name; 30 | this->number=num; 31 | } 32 | }; 33 | class Employee2:public Employee{ 34 | protected: 35 | double compensation; 36 | string time; 37 | public: 38 | void input(double compens,string time){ 39 | this->compensation=compens; 40 | this->time=time; 41 | } 42 | void show(){ 43 | cout<name=name; 50 | this->number=num; 51 | this->compensation=compens; 52 | this->time=time; 53 | } 54 | void output(){ 55 | cout<name=name; 66 | this->number=num; 67 | this->compensation=compens; 68 | this->time=time; 69 | } 70 | void output(){ 71 | cout<name=name; 82 | this->number=num; 83 | this->compensation=compens; 84 | this->time=time; 85 | } 86 | void output(){ 87 | cout<, ==) to compare objects. 43 | - Overloading prefix and postfix increment and decrement operators (++ and --) for custom behavior. 44 | 45 | ## Friend Functions 46 | 47 | - Use of friend functions to allow non-member functions access to private members of a class. 48 | 49 | ## Encapsulation 50 | 51 | - Encapsulation of data and behavior within class methods. 52 | - Data hiding and encapsulation of member variables using private access specifiers. 53 | 54 | ## Member Functions 55 | 56 | - Definition and implementation of member functions to perform operations on objects. 57 | 58 | ## Dynamic Memory Allocation 59 | 60 | - Usage of dynamic memory allocation (new/delete) to manage objects' memory. 61 | 62 | ## Input/Output Operator Overloading 63 | 64 | - Overloading input and output stream operators (<< and >>) to enable custom input and output operations. 65 | 66 | ## Conversion Functions 67 | 68 | - Creating conversion functions to convert Roman numerals to decimal numbers and vice versa. 69 | 70 | ## Conditional Statements and Loops 71 | 72 | - Use of conditional statements (if-else) to perform specific actions based on conditions. 73 | - Use of loops (for, while) to iterate over strings and arrays. 74 | 75 | ## C++ Standard Library 76 | 77 | - Utilization of standard library functions (e.g., strcpy, strlen, strcmp) for string manipulation. 78 | - Usage of standard library containers (e.g., string, ostream, istream) and algorithms. 79 | 80 | ## How to Use 81 | 82 | 1. Clone the repository to your local machine using `git clone`or simply download `.zip file`. 83 | 2. Navigate to the directory of the desired C++ file in your terminal. 84 | 3. Compile the C++ file using a C++ compiler (e.g., g++). For example: 85 | 4. Run the compiled executable to see the output of the program. 86 | 87 | ## Contributing 88 | 89 | Contributions to this repository are welcome! If you have any new C++ code examples or improvements to existing ones, feel free to create a pull request. 90 | 91 | ## Initial Contributor 92 | 93 | So far, all the work in this repository has been done by me. 94 | 95 | Thank you for visiting this repository and happy coding! 96 | 97 | 98 | ## LinkedIn             Facebook             Instagram             Twitter 99 | 100 | 101 |                             102 | 103 | 104 | 105 |                                106 | 107 | 108 | 109 |                                  110 | 111 | 112 | 113 | 114 | -------------------------------------------------------------------------------- /Testing_Assignment_1/manual.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mabdullahatif/Object_Oriented_Programming/3b43c05ed9e394f7147c379a50757f600d31f21b/Testing_Assignment_1/manual.docx -------------------------------------------------------------------------------- /Testing_Assignment_1/synonyms(task1).txt: -------------------------------------------------------------------------------- 1 | abandon discontinue vacate absent missing unavailable cable wire 2 | calculate compute determine measure safety security refuge 3 | happy joyful delighted content 4 | sad unhappy sorrowful 5 | -------------------------------------------------------------------------------- /Testing_Assignment_1/task1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This C++ program reads synonyms from a file, stores them in parallel arrays, and then paraphrases the input text using the synonyms. 3 | 4 | The 'readSynonymsFromFile' function: 5 | 1. Takes a filename, a vector of words, and a vector of vectors of synonyms as input. 6 | 2. Opens the input file and reads its contents line by line. 7 | 3. For each line, extracts the word and its associated synonyms. 8 | 4. Stores the word in the 'words' vector and its synonyms in a nested vector 'synonyms'. 9 | 5. Closes the file after reading. 10 | 11 | The 'paraphraseText' function: 12 | 1. Takes the input text, the 'words' vector, and the 'synonyms' vector as input. 13 | 2. Tokenizes the input text into words using a stringstream. 14 | 3. For each word, checks if it matches any of the words in the 'words' vector. 15 | 4. If a match is found, randomly selects a synonym for that word and replaces it. 16 | 5. Appends the paraphrased word (either the original or the synonym) to the 'output' string. 17 | 18 | In the 'main' function: 19 | 1. Creates vectors 'words' and 'synonyms' to store the synonyms read from the file. 20 | 2. Calls 'readSynonymsFromFile' to populate these vectors with data from the file "synonyms.txt". 21 | 3. Asks the user to enter the text to paraphrase. 22 | 4. Calls 'paraphraseText' to paraphrase the input text using the synonyms read from the file. 23 | 5. Prints the paraphrased text to the console. 24 | 25 | Sample 'synonyms.txt' file contents: 26 | ----------------------------------- 27 | hello hi hey 28 | run jog sprint 29 | big large huge 30 | quick fast rapid 31 | 32 | Sample input and output: 33 | ------------------------ 34 | Input text: "Hello, please run quickly." 35 | Paraphrased text: "Hi, please jog rapidly." 36 | */ 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | 45 | using namespace std; 46 | 47 | // Function to read synonyms from the file and store them in parallel arrays 48 | void readSynonymsFromFile(const string& filename, vector& words, vector>& synonyms) { 49 | ifstream inFile(filename); 50 | if (!inFile) { 51 | cout << "Error: Unable to open the file " << filename << endl; 52 | exit(1); 53 | } 54 | 55 | string line; 56 | while (getline(inFile, line)) { 57 | istringstream iss(line); 58 | string word; 59 | iss >> word; 60 | words.push_back(word); 61 | 62 | vector syns; 63 | string synonym; 64 | while (iss >> synonym) { 65 | syns.push_back(synonym); 66 | } 67 | synonyms.push_back(syns); 68 | } 69 | 70 | inFile.close(); 71 | } 72 | 73 | // Function to paraphrase the input text using synonyms 74 | string paraphraseText(const string& input, const vector& words, const vector>& synonyms) { 75 | string output = ""; 76 | istringstream iss(input); 77 | string word; 78 | 79 | while (iss >> word) { 80 | bool paraphrased = false; 81 | 82 | for (size_t i = 0; i < words.size(); i++) { 83 | if (word == words[i]) { 84 | size_t numSynonyms = synonyms[i].size(); 85 | if (numSynonyms > 0) { 86 | srand(time(0)); 87 | int randomIndex = rand() % numSynonyms; 88 | word = synonyms[i][randomIndex]; 89 | paraphrased = true; 90 | break; 91 | } 92 | } 93 | } 94 | 95 | if (!paraphrased) { 96 | output += word + " "; 97 | } 98 | } 99 | 100 | return output; 101 | } 102 | 103 | int main() { 104 | vector words; 105 | vector> synonyms; 106 | 107 | // Read synonyms from the file 108 | readSynonymsFromFile("synonyms.txt", words, synonyms); 109 | 110 | string inputText; 111 | cout << "Enter the text to paraphrase: "; 112 | getline(cin, inputText); 113 | 114 | string paraphrasedText = paraphraseText(inputText, words, synonyms); 115 | cout << "Paraphrased text: " << paraphrasedText << endl; 116 | 117 | return 0; 118 | } 119 | -------------------------------------------------------------------------------- /Testing_Assignment_1/task2.cpp: -------------------------------------------------------------------------------- 1 | /*The program is designed to convert a 2D array into a 1D array and then sort 2 | the elements of the 1D array in ascending order. It utilizes dynamic memory 3 | allocation to handle arrays of varying sizes and avoids memory leaks through proper deallocation. 4 | 5 | Main Functions: 6 | 7 | Fillarray: This function takes user input to create a 2D array. It asks the user 8 | for the number of rows and then, for each row, the number of columns. It allocates 9 | memory for the 2D array and stores the input elements provided by the user. 10 | 11 | TwoDtoOneD: This function takes the 2D array and converts it into a 1D array. It calculates 12 | the total number of elements in the 2D array to determine the size of the 1D array. Then, it traverses 13 | the 2D array row by row and copies each element into the 1D array. 14 | 15 | showOneD: This function displays the elements of the 1D array on the console. 16 | 17 | sortArray: This function sorts the elements of the 1D array in ascending order using the 18 | std::sort function from the library. 19 | 20 | Main Execution: 21 | 22 | The user is prompted to enter the number of rows and columns for each row of the 2D array. 23 | After entering the elements for the 2D array, the program converts it into a 1D array and sorts it. 24 | Finally, the sorted elements of the 1D array are displayed on the console. 25 | Memory Management: 26 | 27 | Dynamic memory allocation is used to create the 2D array, 1D array, and an array to store the number 28 | of columns in each row of the 2D array. 29 | After sorting and displaying the 1D array, the memory allocated for the arrays is properly deallocated 30 | to prevent memory leaks. 31 | Error Handling: 32 | 33 | The program handles the case where the user enters invalid inputs, such as negative row or column values, 34 | to ensure valid memory allocation and access.*/ 35 | 36 | #include 37 | #include 38 | using namespace std; 39 | 40 | void Fillarray(int**& arr, int& r, int*& cArr, int& size) { 41 | cout << "Enter the number of rows: "; 42 | cin >> r; 43 | 44 | arr = new int*[r]; 45 | cArr = new int[r]; 46 | 47 | for (int i = 0; i < r; i++) { 48 | cout << "Enter Number Of Columns For Row #" << i << ": "; 49 | cin >> cArr[i]; 50 | arr[i] = new int[cArr[i]]; 51 | for (int j = 0; j < cArr[i]; j++) { 52 | cin >> arr[i][j]; 53 | size++; 54 | } 55 | } 56 | } 57 | 58 | int* TwoDtoOneD(int** arr, int r, int* cArr, const int size) { 59 | int* oneD = new int[size]; 60 | int index = 0; 61 | 62 | for (int i = 0; i < r; i++) { 63 | for (int j = 0; j < cArr[i]; j++) { 64 | oneD[index] = arr[i][j]; 65 | index++; 66 | } 67 | } 68 | 69 | return oneD; 70 | } 71 | 72 | void showOneD(int* arr, const int size) { 73 | cout << "The elements of the array are: "; 74 | for (int i = 0; i < size; i++) { 75 | cout << arr[i] << " "; 76 | } 77 | } 78 | 79 | void sortArray(int* arr, const int size) { 80 | sort(arr, arr + size); // Using the standard sorting function from the algorithm library 81 | } 82 | 83 | int main() { 84 | int** arr = nullptr; 85 | int* cArr = nullptr; 86 | int* oneD = nullptr; 87 | int r, size = 0; 88 | 89 | Fillarray(arr, r, cArr, size); 90 | oneD = TwoDtoOneD(arr, r, cArr, size); 91 | 92 | sortArray(oneD, size); 93 | showOneD(oneD, size); 94 | 95 | // Deallocate memory for 2D array and 1D array 96 | for (int i = 0; i < r; i++) { 97 | delete[] arr[i]; 98 | } 99 | delete[] arr; 100 | delete[] cArr; 101 | delete[] oneD; 102 | 103 | return 0; 104 | } 105 | -------------------------------------------------------------------------------- /Testing_Assignment_2/input.txt: -------------------------------------------------------------------------------- 1 | Arif 2 | 12-A, Street 6, Lahore 3 | 0301-1234567 4 | arif@example.com 5 | NUST 6 | Computer Science 7 | P 8 | Assistant Professor 9 | CPS,OOP,LA,CAL 10 | 4,4,4,4 11 | 4000 12 | 2 13 | 0 14 | Masters 15 | 16 | Arham 17 | 45-B, Avenue 8, Karachi 18 | 0312-9876543 19 | arham@example.com 20 | FAST 21 | Electrical Engineering 22 | V 23 | Lecturer 24 | DS,Algo,PF 25 | 3,3,3 26 | 2000 27 | 1 28 | 2 29 | Masters 30 | -------------------------------------------------------------------------------- /Testing_Assignment_2/manual.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mabdullahatif/Object_Oriented_Programming/3b43c05ed9e394f7147c379a50757f600d31f21b/Testing_Assignment_2/manual.docx -------------------------------------------------------------------------------- /Testing_Assignment_2/task1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Description: This C++ code defines a class called "teacher," which represents 3 | a teacher with various attributes and functionalities. The class has private 4 | member variables such as Name, Address, Contact, Email, Institution, 5 | Department, Type, Designation, Allocated_Courses, Credit_Hours, Experience, 6 | Publications, salary, and Qualifications, each representing different aspects 7 | of a teacher's profile. 8 | 9 | Public member functions include setters and getters to modify and access 10 | these attributes, allowing for easy interaction with the teacher objects. 11 | Additionally, there are functions to calculate the teacher's salary based on 12 | their type and designation, as well as methods to display the teacher's 13 | information and the overall design of the program. 14 | 15 | The teacher class constructor enables the creation of teacher objects with 16 | optional default values for the attributes. This provides flexibility when 17 | creating teacher instances, as some attributes can be initialized with default 18 | values if not explicitly provided during object creation. 19 | 20 | The main function demonstrates the usage of the teacher class. It reads teacher 21 | data from a specified file, creates teacher objects based on the data read, and 22 | calculates their salaries based on their type (V - Visiting, P - Permanent) and 23 | designation (Lecturer, Assistant Professor, Associate Professor, Professor). The 24 | program then displays the information of each teacher, including their updated 25 | salary after the calculation. 26 | 27 | Furthermore, the main function allows users to interact with the teacher objects. 28 | It prompts the user to select a teacher by name and choose which attribute they 29 | want to update (qualification, contact, or institution). Based on the user's choice, 30 | the program updates the selected teacher's information accordingly and displays 31 | the updated data. 32 | 33 | The code is well-structured, modular, and efficiently handles data using C++ 34 | vectors to initialize Allocated_Courses and Credit_Hours arrays, making it easier 35 | to manage and modify teacher information without dealing with fixed-size arrays. 36 | 37 | Overall, this code showcases object-oriented programming concepts, file handling, 38 | data manipulation, and user interaction, making it a comprehensive example of 39 | managing teacher information in a simulated environment. 40 | */ 41 | 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | using namespace std; 48 | 49 | class teacher { 50 | private: 51 | string Name; 52 | string Address; 53 | string Contact; 54 | string Email; 55 | string Institution; 56 | string Department; 57 | char Type; 58 | string Designation; 59 | string Allocated_Courses[4]; 60 | int Credit_Hours[4]; 61 | int Experience; 62 | int Publications; 63 | double salary; 64 | string Qualifications; 65 | 66 | public: 67 | teacher(string = "", string = "", string = "", string = "", string = "", string = "", 68 | char = '\0', string = "", const vector& = { "", "", "" }, const vector& = { 0, 0, 0 }, int = 0, 69 | int = 0, string = ""); 70 | 71 | void setName(string); 72 | void setAddress(string); 73 | void setContact(string); 74 | void setEmail(string); 75 | void setInstitution(string); 76 | void setDepartment(string); 77 | void setType(char); 78 | void setDesignation(string); 79 | void setAllocated_Courses(string[]); 80 | void setCredit_Hours(int[]); 81 | void setExperience(int); 82 | void setPublications(int); 83 | void setQualifications(string); 84 | void setSalary(double); 85 | 86 | string getName() const; 87 | string getAddress() const; 88 | string getContact() const; 89 | string getEmail() const; 90 | string getInstitution() const; 91 | string getDepartment() const; 92 | char getType() const; 93 | string getDesignation() const; 94 | void getAllocated_Courses() const; 95 | void getCredit_Hours() const; 96 | int getExperience() const; 97 | int getPublications() const; 98 | string getQualifications() const; 99 | double getSalary(); 100 | 101 | void showData(); 102 | void showDesign(); 103 | void calSalary(); 104 | void updateRecord(); 105 | }; 106 | 107 | teacher::teacher(string name, string address, string contact, string email, string institution, 108 | string department, char type, string designation, const vector& Allocated_Courses, const vector& Credit_Hours, 109 | int experience, int publications, string qualifications) { 110 | 111 | Name = name; Address = address; Email = email; Institution = institution; Department = department; Contact = contact; 112 | Type = type; Designation = designation; Experience = experience; Publications = publications; Qualifications = qualifications; 113 | 114 | for (int i = 0; i < 4; i++) 115 | this->Allocated_Courses[i] = Allocated_Courses[i]; 116 | 117 | for (int i = 0; i < 4; i++) 118 | this->Credit_Hours[i] = Credit_Hours[i]; 119 | } 120 | 121 | void teacher::setName(string name) { 122 | Name = name; 123 | } 124 | void teacher::setAddress(string add) { 125 | Address = add; 126 | } 127 | void teacher::setContact(string cont) { 128 | Contact = cont; 129 | } 130 | void teacher::setEmail(string email) { 131 | Email = email; 132 | } 133 | void teacher::setInstitution(string insti) { 134 | Institution = insti; 135 | } 136 | void teacher::setDepartment(string depart) { 137 | Department = depart; 138 | } 139 | void teacher::setType(char type) { 140 | Type = type; 141 | } 142 | void teacher::setDesignation(string desig) { 143 | Designation = desig; 144 | } 145 | void teacher::setAllocated_Courses(string array[]) { 146 | for (int i = 0; i < 4; i++) 147 | Allocated_Courses[i] = array[i]; 148 | } 149 | void teacher::setCredit_Hours(int cred[]) { 150 | for (int i = 0; i < 4; i++) 151 | Credit_Hours[i] = cred[i]; 152 | } 153 | void teacher::setPublications(int pub) { 154 | Publications = pub; 155 | } 156 | void teacher::setExperience(int experi) { 157 | Experience = experi; 158 | } 159 | void teacher::setQualifications(string qualifi) { 160 | Qualifications = qualifi; 161 | } 162 | 163 | string teacher::getName() const { 164 | return Name; 165 | } 166 | string teacher::getAddress() const { 167 | return Address; 168 | } 169 | string teacher::getContact() const { 170 | return Contact; 171 | } 172 | string teacher::getEmail() const { 173 | return Email; 174 | } 175 | string teacher::getInstitution() const { 176 | return Institution; 177 | } 178 | string teacher::getDepartment() const { 179 | return Department; 180 | } 181 | char teacher::getType() const { 182 | return Type; 183 | } 184 | string teacher::getDesignation() const { 185 | return Designation; 186 | } 187 | void teacher::getAllocated_Courses() const { 188 | for (int i = 0; i < 4; i++) 189 | cout << Allocated_Courses[i] << " "; 190 | } 191 | void teacher::getCredit_Hours() const { 192 | for (int i = 0; i < 4; i++) 193 | cout << Credit_Hours[i] << " "; 194 | } 195 | 196 | int teacher::getExperience() const { 197 | return Experience; 198 | } 199 | int teacher::getPublications() const { 200 | return Publications; 201 | } 202 | string teacher::getQualifications() const { 203 | return Qualifications; 204 | } 205 | double teacher::getSalary() { 206 | return salary; 207 | } 208 | 209 | void teacher::showDesign() { 210 | cout << "**************************************************************************************" << endl; 211 | cout << "** OOP Assignment 2 || Created by L21-999 **" << endl; 212 | cout << "** **" << endl; 213 | cout << "** ************************************************** **" << endl; 214 | cout << "**---------------** Class Of Teacher And Their Basic Information **-----------------**" << endl; 215 | cout << "** ************************************************** **" << endl; 216 | cout << "** **" << endl; 217 | cout << "** Deadline 99th April || TA : Sir Salman Arshad **" << endl; 218 | cout << "**************************************************************************************" << endl; 219 | } 220 | 221 | void teacher::updateRecord() { 222 | cout << "|| Enter 1 to update qualification ||" << endl; 223 | cout << "|| Enter 2 to update contact ||" << endl; 224 | cout << "|| Enter 3 to update institution ||" << endl; 225 | } 226 | 227 | void teacher::showData() { 228 | cout << endl 229 | << " Name of teacher: " << getName() << endl 230 | << " Address of teacher: " << getAddress() << endl 231 | << " Contact of teacher: " << getContact() << endl 232 | << " Email of teacher: " << getEmail() << endl 233 | << " Institution of teacher: " << getInstitution() << endl 234 | << " Department of teacher: " << getDepartment() << endl 235 | << " Type of teacher: " << getType() << endl; 236 | cout << " Courses of teacher: "; 237 | getAllocated_Courses(); 238 | cout << endl; 239 | cout << " Credit Hours of teacher: "; 240 | getCredit_Hours(); 241 | cout << endl << " Designation of teacher: " << getDesignation() << endl 242 | << " Experience of teacher: " << getExperience() << endl 243 | << " Publication of teacher: " << getPublications() << endl 244 | << " Qualification of teacher: " << getQualifications() << endl; 245 | cout << "**************************************************************************************" << endl 246 | << " Salary of teacher: " << getSalary() << endl; 247 | cout << "**************************************************************************************"; 248 | cout << endl << endl; 249 | } 250 | 251 | void teacher::setSalary(double sal) { 252 | salary = sal; 253 | } 254 | 255 | void teacher::calSalary() { 256 | double sal = salary; 257 | int creditHoursPerWeek = 0; 258 | salary = 0; 259 | 260 | if (Type == 'V' || Type == 'v') { 261 | for (int i = 0; i < 4; i++) 262 | creditHoursPerWeek += Credit_Hours[i]; 263 | 264 | if (Designation == "Lecturer") { 265 | salary += creditHoursPerWeek * 2000; 266 | } 267 | else if (Designation == "Assistant Professor") { 268 | salary += creditHoursPerWeek * 4000; 269 | } 270 | else if (Designation == "Associate Professor") { 271 | salary += creditHoursPerWeek * 7000; 272 | } 273 | else if (Designation == "Professor") { 274 | salary += creditHoursPerWeek * 9000; 275 | } 276 | } 277 | else if (Type == 'P' || Type == 'p') { 278 | if (Designation == "Lecturer") { 279 | salary += 100000; 280 | } 281 | else if (Designation == "Assistant Professor") { 282 | salary += 175000; 283 | } 284 | else if (Designation == "Associate Professor") { 285 | salary += 250000; 286 | } 287 | else if (Designation == "Professor") { 288 | salary += 325000; 289 | } 290 | } 291 | 292 | salary += Publications * 8000; 293 | 294 | if (Experience >= 1 && Experience <= 4) { 295 | salary += double(Experience) * 0.05 * salary; 296 | } 297 | } 298 | 299 | int main() { 300 | 301 | string allocatedCourses[4] = { "CPS", "OOP", "LA", "CAL" }; 302 | int Creds[4] = { 4, 4, 4, 4 }; 303 | teacher e[5]; 304 | 305 | string path; 306 | 307 | cout << "Enter a path: "; 308 | cin >> path; 309 | 310 | ifstream FILE(path); 311 | 312 | if (!FILE.is_open()) 313 | cout << "FILE cannot be opened!" << endl; 314 | 315 | else { 316 | string value; 317 | string arr[4]; 318 | int cred[4]; 319 | int temp; 320 | char ch; 321 | e[0].showDesign(); 322 | 323 | for (int i = 0; i < 2; i++) { 324 | FILE >> value; e[i].setName(value); 325 | FILE >> value; e[i].setAddress(value); 326 | FILE >> value; e[i].setContact(value); 327 | FILE >> value; e[i].setEmail(value); 328 | FILE.ignore(); 329 | getline(FILE, value); 330 | e[i].setInstitution(value); 331 | FILE >> value; e[i].setDepartment(value); 332 | FILE >> ch; e[i].setType(ch); 333 | FILE.ignore(); 334 | getline(FILE, value); 335 | e[i].setDesignation(value); 336 | 337 | getline(FILE, value); 338 | stringstream S(value); 339 | 340 | for (int i = 0; getline(S, value, ','); i++) 341 | arr[i] = value; 342 | 343 | e[i].setAllocated_Courses(arr); 344 | 345 | getline(FILE, value); 346 | stringstream C(value); 347 | for (int i = 0; getline(C, value, ','); i++) 348 | cred[i] = stoi(value); 349 | 350 | e[i].setCredit_Hours(cred); 351 | 352 | FILE >> temp; 353 | e[i].setSalary(temp); 354 | FILE >> temp; 355 | e[i].setExperience(temp); 356 | FILE >> temp; 357 | e[i].setPublications(temp); 358 | 359 | FILE >> value; 360 | e[i].setQualifications(value); 361 | } 362 | e[3].setSalary(175000); 363 | e[3].setName("Abdullah"); 364 | e[3].setAddress("Lahore"); 365 | e[3].setContact("00000000009"); 366 | e[3].setEmail("l21-9999@lhr.nu.edu.pk"); 367 | e[3].setInstitution("FAST NUCES"); 368 | e[3].setDepartment("CS"); 369 | e[3].setType('P'); 370 | e[3].setDesignation("Lecturer"); 371 | e[3].setAllocated_Courses(allocatedCourses); 372 | e[3].setCredit_Hours(Creds); 373 | e[3].setExperience(4); 374 | e[3].setPublications(3); 375 | e[3].setQualifications("Under Graduate"); 376 | } 377 | 378 | for (int i = 0; i < 2; i++) { 379 | e[i].calSalary(); 380 | e[i].showData(); 381 | cout << endl; 382 | } 383 | e[3].showData(); 384 | 385 | int choice = 0; 386 | string Name; 387 | e[0].updateRecord(); 388 | cout << "Enter choice : "; 389 | cin >> choice; 390 | cout << "Enter Name : "; 391 | cin >> Name; 392 | if (choice == 1 && Name == "Arif") { 393 | char Quali[25]; 394 | cout << "ENTER QUALIFICATION : "; 395 | cin.ignore(); 396 | cin.getline(Quali, 25); 397 | e[0].setQualifications(Quali); 398 | } 399 | else if (choice == 2 && Name == "Arif") { 400 | string Cont; 401 | cout << "ENTER CONTACT NUMBER : "; 402 | cin >> Cont; 403 | e[0].setContact(Cont); 404 | } 405 | else if (choice == 3 && Name == "Arif") { 406 | char inst[25]; 407 | cout << "ENTER INSTITUTION : "; 408 | cin.ignore(); 409 | cin.getline(inst, 25); 410 | e[0].setInstitution(inst); 411 | } 412 | else if (choice == 1 && Name == "Arham") { 413 | char Quali[25]; 414 | cout << "ENTER QUALIFICATION : "; 415 | cin.ignore(); 416 | cin.getline(Quali, 25); 417 | e[1].setQualifications(Quali); 418 | } 419 | else if (choice == 2 && Name == "Arham") { 420 | string Cont; 421 | cout << "ENTER CONTACT NUMBER : "; 422 | cin >> Cont; 423 | e[1].setContact(Cont); 424 | } 425 | else if (choice == 3 && Name == "Arham") { 426 | char inst[25]; 427 | cout << "ENTER INSTITUTION : "; 428 | cin.ignore(); 429 | cin.getline(inst, 25); 430 | e[1].setInstitution(inst); 431 | } 432 | else { 433 | cout << endl; 434 | cout << "!!!!!! INVALID CHOICES ENTERED !!!!!!"; 435 | cout << endl; 436 | return 0; 437 | } 438 | 439 | if (1) { 440 | cout << endl; 441 | cout << "**************************************************************************************" << endl; 442 | cout << "** **" << endl; 443 | cout << "** UPDATED DATA **" << endl; 444 | cout << "** **" << endl; 445 | cout << "**************************************************************************************"; 446 | cout << endl; 447 | e[0].showData(); 448 | e[1].showData(); 449 | e[3].showData(); 450 | } 451 | else { 452 | return 0; 453 | } 454 | 455 | return 0; 456 | } 457 | -------------------------------------------------------------------------------- /Testing_Assignment_3/manual.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mabdullahatif/Object_Oriented_Programming/3b43c05ed9e394f7147c379a50757f600d31f21b/Testing_Assignment_3/manual.docx -------------------------------------------------------------------------------- /Testing_Assignment_3/task1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Description: This C++ code defines a class "VectorType" that represents a 3D vector. 3 | The class implements various operations on vectors, such as addition, subtraction, 4 | dot product, length calculation, and finding the angle between two vectors. 5 | 6 | The class contains private member variables "x," "y," and "z," which represent the 7 | vector's components. Public member functions include constructors, a copy constructor, 8 | destructor, and overloaded operators for assignment, addition, subtraction, dot product, 9 | pre/post-increment, pre/post-decrement, equality, and inequality. The class also 10 | provides functions to calculate the vector's length and the angle between two vectors. 11 | 12 | In the main function, the user is prompted to enter the x, y, and z values for two 13 | vectors (c1 and c2). The code then demonstrates various operations on these vectors, 14 | including printing their components, calculating dot product, vector length, and the 15 | angle between them. Additionally, it displays the sum and difference of vectors c1 and 16 | c2, as well as the result of pre/post-increment and pre/post-decrement operations on c1 17 | and c2. Finally, it checks for equality and inequality between the vectors. 18 | 19 | The VectorType class provides a convenient and reusable implementation for 3D vector 20 | manipulation, and the main function demonstrates its functionality with user-provided 21 | vector values. The code showcases object-oriented programming, operator overloading, 22 | and mathematical vector operations in C++. 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | class VectorType { 31 | private: 32 | double *x; 33 | double *y; 34 | double *z; 35 | 36 | public: 37 | VectorType(); 38 | VectorType(double, double, double); 39 | VectorType(const VectorType&); // Copy constructor 40 | ~VectorType(); 41 | 42 | VectorType& operator=(const VectorType&); 43 | 44 | double operator*(const VectorType&); 45 | 46 | VectorType operator+(const VectorType&); 47 | VectorType operator-(const VectorType&); 48 | 49 | VectorType& operator++(); // Pre-increment 50 | VectorType operator++(int); // Post-increment 51 | VectorType& operator--(); // Pre-decrement 52 | VectorType operator--(int); // Post-decrement 53 | 54 | bool operator==(const VectorType&); 55 | bool operator!=(const VectorType&); 56 | 57 | friend istream& operator>>(istream&, VectorType&); 58 | friend ostream& operator<<(ostream&, const VectorType&); 59 | 60 | double length()const; 61 | double findAngle(const VectorType&); 62 | }; 63 | 64 | VectorType::VectorType() { 65 | x = new double; 66 | y = new double; 67 | z = new double; 68 | *x = 0; 69 | *y = 0; 70 | *z = 0; 71 | } 72 | 73 | VectorType::VectorType(double X, double Y, double Z) { 74 | x = new double; 75 | y = new double; 76 | z = new double; 77 | *x = X; 78 | *y = Y; 79 | *z = Z; 80 | } 81 | 82 | VectorType::VectorType(const VectorType& vec) { 83 | x = new double; 84 | y = new double; 85 | z = new double; 86 | *x = *vec.x; 87 | *y = *vec.y; 88 | *z = *vec.z; 89 | } 90 | 91 | VectorType::~VectorType() { 92 | delete x; 93 | delete y; 94 | delete z; 95 | } 96 | 97 | VectorType& VectorType::operator=(const VectorType& vec) { 98 | if (this == &vec) return *this; 99 | 100 | *x = *vec.x; 101 | *y = *vec.y; 102 | *z = *vec.z; 103 | return *this; 104 | } 105 | 106 | double VectorType::operator*(const VectorType& vec) { 107 | return (*x * (*vec.x)) + (*y * (*vec.y)) + (*z * (*vec.z)); 108 | } 109 | 110 | VectorType VectorType::operator+(const VectorType& vec) { 111 | VectorType result(*x + (*vec.x), *y + (*vec.y), *z + (*vec.z)); 112 | return result; 113 | } 114 | 115 | VectorType VectorType::operator-(const VectorType& vec) { 116 | VectorType result(*x - (*vec.x), *y - (*vec.y), *z - (*vec.z)); 117 | return result; 118 | } 119 | 120 | VectorType& VectorType::operator++() { 121 | ++(*x); 122 | ++(*y); 123 | ++(*z); 124 | return *this; 125 | } 126 | 127 | VectorType VectorType::operator++(int) { 128 | VectorType temp(*this); 129 | ++(*x); 130 | ++(*y); 131 | ++(*z); 132 | return temp; 133 | } 134 | 135 | VectorType& VectorType::operator--() { 136 | --(*x); 137 | --(*y); 138 | --(*z); 139 | return *this; 140 | } 141 | 142 | VectorType VectorType::operator--(int) { 143 | VectorType temp(*this); 144 | --(*x); 145 | --(*y); 146 | --(*z); 147 | return temp; 148 | } 149 | 150 | bool VectorType::operator==(const VectorType& vec) { 151 | return (*x == *vec.x) && (*y == *vec.y) && (*z == *vec.z); 152 | } 153 | 154 | bool VectorType::operator!=(const VectorType& vec) { 155 | return (*x != *vec.x) || (*y != *vec.y) || (*z != *vec.z); 156 | } 157 | 158 | istream& operator>>(istream& is, VectorType& vec) { 159 | is >> *vec.x >> *vec.y >> *vec.z; 160 | return is; 161 | } 162 | 163 | ostream& operator<<(ostream& os, const VectorType& vec) { 164 | os << "(" << *vec.x << "i," << *vec.y << "j," << *vec.z << "k)"; 165 | return os; 166 | } 167 | 168 | double VectorType::length()const { 169 | return sqrt((*x * *x) + (*y * *y) + (*z * *z)); 170 | } 171 | 172 | double VectorType::findAngle(const VectorType& vec) { 173 | double dotProduct = *x * (*vec.x) + *y * (*vec.y) + *z * (*vec.z); 174 | double lengthProduct = length() * vec.length(); 175 | double angleInRadians = acos(dotProduct / lengthProduct); 176 | return angleInRadians * (180 / 3.14159265358979323846); // Convert radians to degrees 177 | } 178 | 179 | int main() { 180 | VectorType c1, c2; 181 | 182 | cout << "Enter x, y, z values for Vector 1: "; 183 | cin >> c1; 184 | cout << "Enter x, y, z values for Vector 2: "; 185 | cin >> c2; 186 | 187 | cout << "Vector 1: " << c1 << endl; 188 | cout << "Vector 2: " << c2 << endl; 189 | 190 | cout << "Dot Product: " << (c1 * c2) << endl; 191 | cout << "Length of Vector 1: " << c1.length() << endl; 192 | cout << "Length of Vector 2: " << c2.length() << endl; 193 | cout << "Angle between Vector 1 and Vector 2: " << c1.findAngle(c2) << " degrees" << endl; 194 | 195 | VectorType c3 = c1 + c2; 196 | cout << "Sum of Vectors: " << c3 << endl; 197 | 198 | VectorType c4 = c1 - c2; 199 | cout << "Difference of Vectors: " << c4 << endl; 200 | 201 | VectorType c5 = c1++; 202 | VectorType c6 = ++c2; 203 | cout << "Post-increment of Vector 1: " << c5 << endl; 204 | cout << "Pre-increment of Vector 2: " << c6 << endl; 205 | 206 | VectorType c7 = c1--; 207 | VectorType c8 = --c2; 208 | cout << "Post-decrement of Vector 1: " << c7 << endl; 209 | cout << "Pre-decrement of Vector 2: " << c8 << endl; 210 | 211 | cout << "Vector 1 == Vector 2? " << (c1 == c2 ? "Yes" : "No") << endl; 212 | cout << "Vector 1 != Vector 2? " << (c1 != c2 ? "Yes" : "No") << endl; 213 | 214 | return 0; 215 | } 216 | -------------------------------------------------------------------------------- /Testing_Assignment_3/task2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Description: This C++ code defines a class "String" that represents a dynamic string. 3 | The class provides various string manipulation functionalities, including conversion 4 | to uppercase and lowercase, string concatenation, substring extraction, index search, 5 | comparison, and overloaded operators for concatenation, assignment, comparison, and 6 | indexing. 7 | 8 | The "String" class contains a private member variable "text," which is a dynamically 9 | allocated character array to store the string. Public member functions include 10 | constructors (default, parameterized, and copy), a destructor, and methods for string 11 | manipulation. The class also overloads operators for concatenation (+), assignment (=), 12 | comparison (==, !=, <, >), and indexing ([]). 13 | 14 | In the main function, the code demonstrates the usage of the "String" class. It creates 15 | two "String" objects "str1" and "str2," performs string concatenation using the + operator, 16 | and stores the result in "str3." The code then prompts the user to enter a string and 17 | displays the entered string using the overloaded insertion (<<) operator. 18 | 19 | The "String" class provides a flexible and efficient implementation for string 20 | manipulation and is suitable for applications requiring dynamic string handling. The 21 | main function showcases the functionality of the "String" class through string 22 | concatenation and user input, making it a practical and reusable code snippet for string 23 | processing in C++. 24 | */ 25 | 26 | #include 27 | #include 28 | #include 29 | using namespace std; 30 | 31 | class String { 32 | private: 33 | char* text; 34 | 35 | public: 36 | String(); // Default constructor 37 | String(const char* str); // Parameterized constructor 38 | String(const String& other); // Copy constructor 39 | ~String(); // Destructor 40 | 41 | // Operations 42 | int length() const; 43 | String upper() const; 44 | String lower() const; 45 | char at(int index) const; 46 | String substring(int start, int end) const; 47 | int index(const char* substr) const; 48 | int compare(const String& other) const; 49 | void concat(const char* str); 50 | void concat(const String& other); 51 | void concat(char ch); 52 | void concat(int num); 53 | void concat(float num); 54 | void prepend(const char* str); 55 | void prepend(const String& other); 56 | void prepend(char ch); 57 | void prepend(int num); 58 | void prepend(float num); 59 | 60 | // Operators 61 | String operator+(const String& other) const; 62 | String operator+(const char* str) const; 63 | String operator+(char ch) const; 64 | String operator+(int num) const; 65 | String operator+(float num) const; 66 | String& operator=(const String& other); 67 | bool operator==(const String& other) const; 68 | bool operator!=(const String& other) const; 69 | bool operator<(const String& other) const; 70 | bool operator>(const String& other) const; 71 | char operator[](int index) const; 72 | char& operator[](int index); 73 | friend istream& operator>>(istream& is, String& str); 74 | friend ostream& operator<<(ostream& os, const String& str); 75 | }; 76 | 77 | // Default constructor 78 | String::String() { 79 | text = new char[1]; 80 | text[0] = '\0'; 81 | } 82 | 83 | // Parameterized constructor 84 | String::String(const char* str) { 85 | int len = strlen(str); 86 | text = new char[len + 1]; 87 | strcpy(text, str); 88 | } 89 | 90 | // Copy constructor 91 | String::String(const String& other) { 92 | int len = other.length(); 93 | text = new char[len + 1]; 94 | strcpy(text, other.text); 95 | } 96 | 97 | // Destructor 98 | String::~String() { 99 | delete[] text; 100 | } 101 | 102 | int String::length() const { 103 | return strlen(text); 104 | } 105 | 106 | String String::upper() const { 107 | String result(*this); 108 | for (int i = 0; i < result.length(); i++) { 109 | result.text[i] = toupper(result.text[i]); 110 | } 111 | return result; 112 | } 113 | 114 | String String::lower() const { 115 | String result(*this); 116 | for (int i = 0; i < result.length(); i++) { 117 | result.text[i] = tolower(result.text[i]); 118 | } 119 | return result; 120 | } 121 | 122 | char String::at(int index) const { 123 | if (index >= 0 && index < length()) { 124 | return text[index]; 125 | } 126 | return '\0'; 127 | } 128 | 129 | String String::substring(int start, int end) const { 130 | if (start < 0 || start >= length() || end < 0 || end >= length() || start > end) { 131 | return String(); 132 | } 133 | 134 | int len = end - start + 1; 135 | char* sub = new char[len + 1]; 136 | strncpy(sub, text + start, len); 137 | sub[len] = '\0'; 138 | 139 | String result(sub); 140 | delete[] sub; 141 | return result; 142 | } 143 | 144 | int String::index(const char* substr) const { 145 | char* pos = strstr(text, substr); 146 | if (pos != nullptr) { 147 | return pos - text; 148 | } 149 | return -1; 150 | } 151 | 152 | int String::compare(const String& other) const { 153 | return strcmp(text, other.text); 154 | } 155 | 156 | void String::concat(const char* str) { 157 | int len1 = length(); 158 | int len2 = strlen(str); 159 | char* temp = new char[len1 + len2 + 1]; 160 | strcpy(temp, text); 161 | strcat(temp, str); 162 | delete[] text; 163 | text = temp; 164 | } 165 | 166 | void String::concat(const String& other) { 167 | concat(other.text); 168 | } 169 | 170 | void String::concat(char ch) { 171 | int len1 = length(); 172 | char* temp = new char[len1 + 2]; 173 | strcpy(temp, text); 174 | temp[len1] = ch; 175 | temp[len1 + 1] = '\0'; 176 | delete[] text; 177 | text = temp; 178 | } 179 | 180 | void String::concat(int num) { 181 | char buffer[20]; 182 | sprintf(buffer, "%d", num); 183 | concat(buffer); 184 | } 185 | 186 | void String::concat(float num) { 187 | char buffer[20]; 188 | sprintf(buffer, "%f", num); 189 | concat(buffer); 190 | } 191 | 192 | void String::prepend(const char* str) { 193 | int len1 = strlen(str); 194 | int len2 = length(); 195 | char* temp = new char[len1 + len2 + 1]; 196 | strcpy(temp, str); 197 | strcat(temp, text); 198 | delete[] text; 199 | text = temp; 200 | } 201 | 202 | void String::prepend(const String& other) { 203 | prepend(other.text); 204 | } 205 | 206 | void String::prepend(char ch) { 207 | int len1 = 1; 208 | int len2 = length(); 209 | char* temp = new char[len1 + len2 + 1]; 210 | temp[0] = ch; 211 | strcpy(temp + 1, text); 212 | delete[] text; 213 | text = temp; 214 | } 215 | 216 | void String::prepend(int num) { 217 | char buffer[20]; 218 | sprintf(buffer, "%d", num); 219 | prepend(buffer); 220 | } 221 | 222 | void String::prepend(float num) { 223 | char buffer[20]; 224 | sprintf(buffer, "%f", num); 225 | prepend(buffer); 226 | } 227 | 228 | String String::operator+(const String& other) const { 229 | String result(*this); 230 | result.concat(other); 231 | return result; 232 | } 233 | 234 | String String::operator+(const char* str) const { 235 | String result(*this); 236 | result.concat(str); 237 | return result; 238 | } 239 | 240 | String String::operator+(char ch) const { 241 | String result(*this); 242 | result.concat(ch); 243 | return result; 244 | } 245 | 246 | String String::operator+(int num) const { 247 | String result(*this); 248 | result.concat(num); 249 | return result; 250 | } 251 | 252 | String String::operator+(float num) const { 253 | String result(*this); 254 | result.concat(num); 255 | return result; 256 | } 257 | 258 | String& String::operator=(const String& other) { 259 | if (this != &other) { 260 | delete[] text; 261 | int len = other.length(); 262 | text = new char[len + 1]; 263 | strcpy(text, other.text); 264 | } 265 | return *this; 266 | } 267 | 268 | bool String::operator==(const String& other) const { 269 | return compare(other) == 0; 270 | } 271 | 272 | bool String::operator!=(const String& other) const { 273 | return compare(other) != 0; 274 | } 275 | 276 | bool String::operator<(const String& other) const { 277 | return compare(other) < 0; 278 | } 279 | 280 | bool String::operator>(const String& other) const { 281 | return compare(other) > 0; 282 | } 283 | 284 | char String::operator[](int index) const { 285 | return at(index); 286 | } 287 | 288 | char& String::operator[](int index) { 289 | if (index >= 0 && index < length()) { 290 | return text[index]; 291 | } 292 | // Return the first character of the string if the index is out of bounds 293 | return text[0]; 294 | } 295 | 296 | istream& operator>>(istream& is, String& str) { 297 | char buffer[1000]; 298 | is.getline(buffer, sizeof(buffer)); 299 | str = buffer; 300 | return is; 301 | } 302 | 303 | ostream& operator<<(ostream& os, const String& str) { 304 | os << str.text; 305 | return os; 306 | } 307 | 308 | int main() { 309 | String str1("Hello"); 310 | String str2("World"); 311 | String str3 = str1 + " " + str2; // Concatenation using + operator 312 | cout << str3 << endl; // Output: "Hello World" 313 | 314 | String str4; 315 | cout << "Enter a string: "; 316 | cin >> str4; 317 | cout << "You entered: " << str4 << endl; 318 | 319 | return 0; 320 | } 321 | -------------------------------------------------------------------------------- /Testing_Assignment_3/task3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Description: This C++ code defines a class "Roman" to represent Roman numerals and their operations. 3 | The class provides functionalities to convert Roman numerals to decimal numbers and vice versa. 4 | It also overloads arithmetic operators (+, -, *, /), comparison operators (>, ==), and 5 | prefix/postfix increment and decrement operators (++ and --). 6 | 7 | The "Roman" class contains private member variables "RomanV" (string for Roman numeral) and 8 | "RomanN" (integer for decimal value). It also has public member variables "variable" (string for Roman numeral) 9 | and "number" (integer for decimal value). 10 | 11 | The main function demonstrates the usage of the "Roman" class. It prompts the user to enter two Roman numerals 12 | and performs various operations on them, such as addition, subtraction, multiplication, and division. The result 13 | of each operation is displayed in Roman numerals. 14 | 15 | Additionally, the code tests the comparison operators (== and >) and the prefix/postfix increment and decrement 16 | operators for Roman numerals. 17 | 18 | The "Roman" class provides a convenient way to work with Roman numerals and perform basic arithmetic operations 19 | on them. It's a useful tool for handling Roman numerals in C++ programs. 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | using namespace std; 27 | 28 | class Roman { 29 | private: 30 | string RomanV; 31 | int RomanN; 32 | 33 | public: 34 | Roman(); 35 | Roman(string, int); 36 | 37 | string variable; 38 | int number = 0; 39 | 40 | friend Roman operator+(const Roman&, const Roman&); 41 | friend Roman operator-(const Roman&, const Roman&); 42 | friend Roman operator*(const Roman&, const Roman&); 43 | friend Roman operator/(const Roman&, const Roman&); 44 | friend bool operator>(const Roman&, const Roman&); 45 | friend bool operator==(const Roman&, const Roman&); 46 | 47 | Roman& operator++(); // Prefix increment 48 | Roman operator++(int); // Postfix increment 49 | Roman& operator--(); // Prefix decrement 50 | Roman operator--(int); // Postfix decrement 51 | 52 | int ConvertToDnumber(string) const; 53 | string ConvertToRvariable(int) const; 54 | }; 55 | 56 | Roman::Roman() { 57 | RomanV = '\0'; 58 | RomanN = 0; 59 | } 60 | 61 | Roman::Roman(string v, int n) { 62 | RomanV = v; 63 | RomanN = n; 64 | } 65 | 66 | Roman operator+(const Roman& V1, const Roman& V2) { 67 | Roman V; 68 | V.number = V1.number + V2.number; 69 | return V; 70 | } 71 | 72 | Roman operator-(const Roman& V1, const Roman& V2) { 73 | Roman V; 74 | if (V1.number - V2.number > 1) { 75 | V.number = V1.number - V2.number; 76 | return V; 77 | } 78 | else { 79 | cout << " (It is less than 1 so no Roman Variable)"; 80 | return V; 81 | } 82 | } 83 | 84 | Roman operator*(const Roman& V1, const Roman& V2) { 85 | Roman V; 86 | V.number = V1.number * V2.number; 87 | return V; 88 | } 89 | 90 | Roman operator/(const Roman& V1, const Roman& V2) { 91 | Roman V; 92 | if (V1.number / V2.number > 1) { 93 | V.number = V1.number / V2.number; 94 | return V; 95 | } 96 | else { 97 | cout << " (It is less than 1 so no Roman Variable)"; 98 | return V; 99 | } 100 | } 101 | 102 | bool operator>(const Roman& V1, const Roman& V2) { 103 | if (V1.variable > V2.variable) { 104 | return true; 105 | } 106 | else { 107 | return false; 108 | } 109 | } 110 | 111 | bool operator==(const Roman& V1, const Roman& V2) { 112 | if (V1.variable == V2.variable) { 113 | return true; 114 | } 115 | else { 116 | return false; 117 | } 118 | } 119 | 120 | int Roman::ConvertToDnumber(string str) const { 121 | Roman decimal; 122 | for (int i = 0; i < str.length(); i++) { 123 | switch (str[i]) { 124 | case 'M': 125 | decimal.number += 1000; 126 | break; 127 | case 'm': 128 | decimal.number += 1000; 129 | break; 130 | case 'D': 131 | decimal.number = decimal.number + 500; 132 | break; 133 | case 'd': 134 | decimal.number = decimal.number + 500; 135 | break; 136 | case 'C': 137 | decimal.number = decimal.number + 100; 138 | break; 139 | case 'c': 140 | decimal.number = decimal.number + 100; 141 | break; 142 | case 'L': 143 | decimal.number = decimal.number + 50; 144 | break; 145 | case 'l': 146 | decimal.number = decimal.number + 50; 147 | break; 148 | case 'X': 149 | decimal.number = decimal.number + 10; 150 | break; 151 | case 'x': 152 | decimal.number = decimal.number + 10; 153 | break; 154 | case 'V': 155 | decimal.number = decimal.number + 5; 156 | break; 157 | case 'v': 158 | decimal.number = decimal.number + 5; 159 | break; 160 | case 'I': 161 | decimal.number = decimal.number + 1; 162 | break; 163 | case 'i': 164 | decimal.number = decimal.number + 1; 165 | break; 166 | default: 167 | cout << "\n\n\n!!!Invalid Input!!!"; 168 | } 169 | } 170 | return decimal.number; 171 | } 172 | 173 | string Roman::ConvertToRvariable(int num) const { 174 | Roman roman; 175 | while (num > 0) { 176 | if (num >= 1000) { 177 | roman.variable = roman.variable + "M"; 178 | num = num - 1000; 179 | } 180 | else if (num >= 500) { 181 | roman.variable = roman.variable + "D"; 182 | num = num - 500; 183 | } 184 | else if (num >= 100) { 185 | roman.variable = roman.variable + "C"; 186 | num = num - 100; 187 | } 188 | else if (num >= 50) { 189 | roman.variable = roman.variable + "L"; 190 | num = num - 50; 191 | } 192 | else if (num >= 10) { 193 | roman.variable = roman.variable + "X"; 194 | num = num - 10; 195 | } 196 | else if (num >= 5) { 197 | roman.variable = roman.variable + "V"; 198 | num = num - 5; 199 | } 200 | else if (num >= 1) { 201 | roman.variable = roman.variable + "I"; 202 | num = num - 1; 203 | } 204 | else { 205 | exit(0); 206 | } 207 | } 208 | roman.number = num; 209 | return roman.variable; 210 | } 211 | 212 | Roman& Roman::operator++() { 213 | // Prefix increment 214 | this->number++; 215 | this->variable = ConvertToRvariable(this->number); 216 | return *this; 217 | } 218 | 219 | Roman Roman::operator++(int) { 220 | // Postfix increment 221 | Roman temp = *this; 222 | this->number++; 223 | this->variable = ConvertToRvariable(this->number); 224 | return temp; 225 | } 226 | 227 | Roman& Roman::operator--() { 228 | // Prefix decrement 229 | if (this->number > 1) { 230 | this->number--; 231 | this->variable = ConvertToRvariable(this->number); 232 | } 233 | else { 234 | cout << " (It is less than 1 so no Roman Variable)"; 235 | } 236 | return *this; 237 | } 238 | 239 | Roman Roman::operator--(int) { 240 | // Postfix decrement 241 | Roman temp = *this; 242 | if (this->number > 1) { 243 | this->number--; 244 | this->variable = ConvertToRvariable(this->number); 245 | } 246 | else { 247 | cout << " (It is less than 1 so no Roman Variable)"; 248 | } 249 | return temp; 250 | } 251 | 252 | int main() { 253 | endl(cout); 254 | string str1, str2; 255 | Roman obj1, obj2; 256 | Roman result; 257 | 258 | cout << "Enter The First Roman Variable : "; 259 | cin >> str1; 260 | endl(cout); 261 | cout << "Enter The Second Roman Variable : "; 262 | cin >> str2; 263 | endl(cout); 264 | 265 | obj1.variable = str1; 266 | obj2.variable = str2; 267 | 268 | int num1 = obj1.ConvertToDnumber(str1); 269 | int num2 = obj2.ConvertToDnumber(str2); 270 | 271 | obj1.number = num1; 272 | obj2.number = num2; 273 | 274 | int num = num1 + num2; 275 | cout << "Decimal Value Of Both Roman variables : "; 276 | cout << num1 << " & " << num2; 277 | cout << "\n\n"; 278 | result = obj1 + obj2; 279 | result.variable = result.ConvertToRvariable(num); 280 | cout << "Decimal Addition Of Both Roman Variables : "; 281 | cout << result.variable << "\n\n"; 282 | 283 | cout << "Decimal Subtraction Of Both Roman Variables : "; 284 | result = obj1 - obj2; 285 | num = num1 - num2; 286 | result.variable = result.ConvertToRvariable(num); 287 | cout << result.variable << "\n\n"; 288 | 289 | num = num1 * num2; 290 | result.variable = result.ConvertToRvariable(num); 291 | cout << "Decimal Multiplication Of Both Roman Variables : "; 292 | cout << result.variable << "\n\n"; 293 | 294 | cout << "Decimal Division Of Both Roman Variables : "; 295 | result = obj1 / obj2; 296 | num = num1 / num2; 297 | result.variable = result.ConvertToRvariable(num); 298 | cout << result.variable << "\n\n"; 299 | 300 | if (obj1.variable == obj2.variable) { 301 | cout << "== Condition Check For Both Roman Variables !! "; 302 | endl(cout); 303 | cout << "The Roman Value " << str1 << " == " << str2 << endl; 304 | } 305 | else { 306 | cout << "== Condition Check For Both Roman Variables !! "; 307 | endl(cout); 308 | cout << "The Roman Value " << str1 << " != " << str2 << endl; 309 | } 310 | 311 | endl(cout); 312 | if (obj1.number > obj2.number) { 313 | cout << "> Condition Check For Both Roman Variables !! "; 314 | endl(cout); 315 | cout << "The Larger Roman Number Is " << obj1.variable << " : " << obj1.number << " >= " << obj2.number; 316 | } 317 | else { 318 | cout << "> Condition Check For Both Roman Variables !! "; 319 | endl(cout); 320 | cout << "The Larger Roman Number Is " << obj2.variable << " : " << obj2.number << " >= " << obj1.number; 321 | } 322 | 323 | endl(cout); 324 | cout << "Testing Prefix Increment" << endl; 325 | Roman inc_result = ++result; 326 | cout << "After Prefix Increment: " << inc_result.variable << endl; 327 | 328 | cout << "Testing Postfix Increment" << endl; 329 | inc_result = result++; 330 | cout << "After Postfix Increment: " << inc_result.variable << endl; 331 | 332 | cout << "Testing Prefix Decrement" << endl; 333 | Roman dec_result = --result; 334 | cout << "After Prefix Decrement: " << dec_result.variable << endl; 335 | 336 | cout << "Testing Postfix Decrement" << endl; 337 | dec_result = result--; 338 | cout << "After Postfix Decrement: " << dec_result.variable << endl; 339 | 340 | cout << "\n\n********************************END OF PROGRAM THANK YOU*********************************"; 341 | return 0; 342 | } 343 | --------------------------------------------------------------------------------