Q14Object Oriented Programming
Question
Write a class template to represent generic vector. Include member functions to perform the following tasks: a) To create the vector b) To modify the value of a given element c) To multiply by a scalar value d) To display the vector in the form (10, 20, 30...... )
Answer
A fully functional, highly advanced C++ Class Template program demonstrating generic programming by creating a dynamic, mathematically rigorous Vector capable of handling any data type (int, float), including element modification, scalar multiplication, and formatted stream output.
A generic vector class utilizes advanced C++ Template architecture (template <class T>) to create a highly flexible mathematical array structure. This allows the exact same code block to flawlessly handle integers, floating-point decimals, or even complex user-defined objects without requiring any code duplication.
The Core C++ Code
``cpp
#include <iostream>
using namespace std;
// Aggressive Template Declaration: 'T' represents any valid data type
template <class T>
class GenericVector {
private:
T* data; // Dynamic pointer to hold the array on the Heap
int capacity; // Total allocated size of the mathematical vector
public:
// a) Constructor: To mathematically create the vector in Heap memory
GenericVector(int size) {
capacity = size;
data = new T[capacity]; // Violent allocation of dynamic memory
for (int i = 0; i < capacity; i++) {
data[i] = 0; // Initialize memory to prevent garbage data
}
}
// Destructor to strictly prevent catastrophic memory leaks
~GenericVector() {
delete[] data;
}
// b) Method: To precisely modify the value of a given element
void modifyElement(int index, T newValue) {
// Strict array bounds checking to prevent Segmentation Faults
if (index >= 0 && index < capacity) {
data[index] = newValue;
} else {
cout << "FATAL ERROR: Index mathematically out of bounds!" << endl;
}
}
// c) Method: To violently multiply the entire vector by a scalar value
void multiplyScalar(T scalar) {
for (int i = 0; i < capacity; i++) {
data[i] = data[i] * scalar; // Iterate and apply mathematical scaling
}
}
// d) Method: To display the vector in the strict aesthetic form (10, 20, 30...)
void displayVector() {
cout << "(";
for (int i = 0; i < capacity; i++) {
cout << data[i];
if (i < capacity - 1) {
cout << ", "; // Print comma for all elements except the absolute last one
}
}
cout << ")" << endl;
}
};
// Main function demonstrating the polymorphic capability of the Template
int main() {
// Instantiate an Integer Vector
cout << "--- Integer Vector Operations ---" << endl;
GenericVector<int> intVec(3); // Force 'T' to become 'int'
intVec.modifyElement(0, 10);
intVec.modifyElement(1, 20);
intVec.modifyElement(2, 30);
cout << "Original: ";
intVec.displayVector(); // Expected Output: (10, 20, 30)
intVec.multiplyScalar(2);
cout << "After Scalar (*2): ";
intVec.displayVector(); // Expected Output: (20, 40, 60)
// Instantiate a Floating-Point Vector
cout << "\n--- Float Vector Operations ---" << endl;
GenericVector<float> floatVec(2); // Force 'T' to become 'float'
floatVec.modifyElement(0, 5.5);
floatVec.modifyElement(1, 9.1);
cout << "Original: ";
floatVec.displayVector(); // Expected Output: (5.5, 9.1)
floatVec.multiplyScalar(10.0);
cout << "After Scalar (*10.0): ";
floatVec.displayVector(); // Expected Output: (55, 91)
return 0;
}
``
Architectural Review
The program successfully satisfies all complex requirements. The template <class T> directive forces the compiler to generate completely independent, optimized binary code paths for the int vector and the float vector during compilation. The new T[capacity] command ensures the memory footprint dynamically perfectly matches the required data type size.