CS-212 Object Oriented Programming LAB MANUAL - 03 Trait Obtained Marks R1 R2 Application Functionality and Specification 30% Readability Maximum Marks 3 1 10% R3 Reusability 1 10% R4 Object 3 Oriented Design 30% R5 Efficiency 1 10% R6 Delivery 1 10% R7 Plagiarism 1 below 70% Total 10 Task 1: 1. Design a datatype to represent polynomials. Where a polynomial can be represented by the degree and coefficients. Provide copy, default, and parameterized constructors to your class. Also write destructor. Write functions to add, subtract, and multiply polynomials. Test your datatype by instantiating objects using all constructors and display result by adding, subtracting, and multiplying them. Code: 1 #include <iostream> using namespace std; class polynomial { int* coeff; int degree; public: // Default constructor polynomial() : degree(0), coeff(nullptr) {} // Parametrized constructor polynomial(int deg, int* coef) : degree(deg) { coeff = new int[degree + 1]; for (int i = 0; i <= degree; i++) { coeff[i] = coef[i]; } } // Copy constructor polynomial(const polynomial& other) : degree(other.degree) { coeff = new int[degree + 1]; for (int i = 0; i <= degree; i++) { coeff[i] = other.coeff[i]; } } // Destructor ~polynomial() { delete[] coeff; } // GETTERS int getCoeff(int index) { if (index >= 0 && index <= degree) { return coeff[index]; } return 0; // Return 0 for out-of-range index } int getDegree() { return degree; } // Setters 2 void setCoeff(int index, int value) { if (index >= 0 && index <= degree) { coeff[index] = value; } } void add(polynomial& other) { int max_degree = max(degree, other.degree); int* resultCoeff = new int[max_degree + 1]; for (int i = 0; i <= max_degree; i++) { int a = getCoeff(i); int b = other.getCoeff(i); resultCoeff[i] = a + b; } polynomial result(max_degree, resultCoeff); cout << "Result of Addition: "; result.display(); delete[] resultCoeff; } void sub(polynomial& other) { int max_degree = max(degree, other.degree); int* resultCoeff = new int[max_degree + 1]; for (int i = 0; i <= max_degree; i++) { int a = getCoeff(i); int b = other.getCoeff(i); resultCoeff[i] = a - b; } polynomial result(max_degree, resultCoeff); cout << "Result of Subtraction: "; result.display(); delete[] resultCoeff; } void multiply(const polynomial& other) { int result_degree = degree + other.degree; int* resultCoeff = new int[result_degree + 1]; for (int i = 0; i <= degree; i++) { for (int j = 0; j <= other.degree; j++) { resultCoeff[i + j] += coeff[i] * other.coeff[j]; 3 } } polynomial result(result_degree, resultCoeff); cout << "Result of Multiplication: "; result.display(); delete[] resultCoeff; } void display() { for (int i = degree; i >= 0; i--) { if (coeff[i] != 0) { if (i != degree) { if (coeff[i] > 0) { cout << " + "; } else { cout << " - "; } } if (i == 0 || abs(coeff[i]) != 1) { cout << abs(coeff[i]); } if (i > 0) { cout << "x"; if (i > 1) { cout << "^" << i; } } } } cout << endl; } }; int main() { int deg1, deg2; cout << "Enter the Degree of polynomial 1: "; cin >> deg1; int* coef1 = new int[deg1 + 1]; for (int i = 0; i <= deg1; i++) { cout << "Enter Coefficient for x^" << i << ": "; cin >> coef1[i]; } polynomial p1(deg1, coef1); 4 cout << "Enter the Degree of polynomial 2: "; cin >> deg2; int* coef2 = new int[deg2 + 1]; for (int i = 0; i <= deg2; i++) { cout << "Enter Coefficient for x^" << i << ": "; cin >> coef2[i]; } polynomial p2(deg2, coef2); cout << "Polynomial p1: "; p1.display(); cout << "Polynomial p2: "; p2.display(); p1.add(p2); // Add p1 and p2 p1.sub(p2); // Subtract p2 from p1 p1.multiply(p2); // Multiply p1 and p2 delete[] coef1; delete[] coef2; return 0; } 5 Output: Task2: Create a class dataArray where each object of dataArray can hold a dynamic array of any size given by user and passed in overloaded constructor. a. Default, copy and overloaded constructors. b. c. d. e. f. SetValues() function that sets value to array by taking input from user. ShowArray() to display the array. ResizeArray(int Size) to resize array. Copy Constructor Destructor Code: #include <iostream> using namespace std; // Define a class for a dynamic integer array class DynamicArray { private: int* data; // Pointer to store the array data int size; // Size of the array public: // Default constructor: Creates an empty dynamic array DynamicArray() { data = nullptr; size = 0; } 6 // Parameterized constructor: Creates a dynamic array with the given initial size DynamicArray(int initialSize) { size = initialSize; data = new int[size]; } // Copy constructor: Creates a copy of a dynamic array DynamicArray(const DynamicArray& other) { size = other.size; data = new int[size]; for (int i = 0; i < size; i++) { data[i] = other.data[i]; } } // Destructor: Deallocates memory used by the dynamic array ~DynamicArray() { delete[] data; } // Function to set values in the dynamic array void setValues() { for (int i = 0; i < size; i++) { cout << "Enter value for element " << i << ": "; cin >> data[i]; } } // Function to display the contents of the dynamic array void displayArray() { for (int i = 0; i < size; i++) { cout << data[i] << " "; } cout << endl; } // Function to resize the dynamic array to a new size void resizeArray(int newSize) { int* temp = new int[newSize]; int minSize = (newSize < size) ? newSize : size; for (int i = 0; i < minSize; i++) { temp[i] = data[i]; } 7 delete[] data; // Deallocate the old array data = temp; // Point to the resized array size = newSize; // Update the size } }; int main() { int initialSize; cout << "Enter the initial size of the dynamic array: "; cin >> initialSize; DynamicArray arr1(initialSize); // Create a dynamic array with the specified initial size cout << "Setting values for arr1:" << endl; arr1.setValues(); // Input values into arr1 cout << "arr1 contents: "; arr1.displayArray(); // Display the contents of arr1 DynamicArray arr2 = arr1; // Copy constructor: Create arr2 as a copy of arr1 cout << "Copying arr1 to arr2..." << endl; cout << "arr2 contents: "; arr2.displayArray(); // Display the contents of arr2 int newSize; cout << "Enter the new size for arr2: "; cin >> newSize; arr2.resizeArray(newSize); // Resize arr2 to the new size cout << "Resized arr2: "; arr2.displayArray(); // Display the resized arr2 return 0; } Output: 8 Task3: Design a data type to represent N input OR gate. Provide following functions: Get input from console. Provide parametrized, default, and copy constructor to class. Evaluate and display the result of each gate using objects. Code: #include <iostream> using namespace std; // Define a class for an OR gate class ORGate { private: int numInputs; // Number of input signals bool* inputs; // Array to store the input signals bool output; // Result of the OR operation public: // Parametrized constructor to initialize with a specific number of inputs ORGate(int num) : numInputs(num) { inputs = new bool[numInputs]; // Dynamically allocate memory for inputs } // Default constructor with a single input ORGate() : ORGate(1) {} // Delegate to the parametrized constructor // Copy constructor to create a copy of an ORGate object ORGate(const ORGate& other) : numInputs(other.numInputs) { inputs = new bool[numInputs]; for (int i = 0; i < numInputs; ++i) { inputs[i] = other.inputs[i]; // Copy input values from the other object } } // Destructor to free the dynamically allocated memory ~ORGate() { delete[] inputs; // Deallocate the memory used for inputs } // Function to get inputs from the console void getInput() { 9 for (int i = 0; i < numInputs; ++i) { cout << "Enter input " << i + 1 << " (0 or 1): "; cin >> inputs[i]; // Read input values from the user } outPut(); } // Function to evaluate the OR gate void outPut() { output= false; // Initialize the result to false for (int i = 0; i < numInputs; ++i) { if (inputs[i]) { output= true; // If any input is true, set the result to true and exit the loop break; } } display(); } // Function to display the result void display() { cout << "OR gate result: " << output<< endl; // Display the result of the OR gate } }; int main() { int numInputs; cout << "Enter the number of inputs for the OR gate: "; cin >> numInputs; // Read the number of inputs from the user ORGate gate(numInputs); // Create an ORGate object with the specified number of inputs gate.getInput(); // Prompt the user to input values return 0; } Output: 10 Q1) How do you pass a dynamic array as a function argument in C++? To pass a dynamic array as a function argument using pointers in C++, you simply pass a pointer to the array along with its size. This allows the function to work with the array's elements without copying them. However, it's important to remember to deallocate the memory for the dynamic array using delete[] when you're done to prevent memory leaks. Passing the array's size is essential for the function to safely access the elements within the allocated memory. Q2) Which is better deep copy or shallow copy and why? The choice between deep copy and shallow copy depends on the particular requirements of your program. Shallow copy is a faster and memory-efficient option, as it merely copies references to the underlying data, making it suitable when you want multiple objects to share the same data, and changes should reflect across all copies. However, this efficiency comes at a cost; it can lead to unintended side effects, as modifications in one copy will affect all other copies, which may not be the desired behavior. On the other hand, deep copy involves duplicating the entire data structure, ensuring that each copy is entirely independent, and changes in one copy do not affect others. This approach offers safety and isolation but can be slower and consume more memory, particularly for large or complex data structures. 11