Here is the OOP Assignment No 04 for Template Class
Question:
Define a template class Container for storing different type of values e.g. int, double, float etc. This container would keep same type of values/elements at a time. The Container class should consist of the following member variables:
T * ptr_Container: A pointer to a set of values it contains. This member points to the dynamically allocated array (which you will be allocating in constructor).
Capacity: An integer that shows total capacity of the container
Counter: An integer counter variable which increments upon every insertion operation and decrements upon removal of any element from the container; representing total number of elements currently present in the container.
The container should consist of following functions:
- constructor with capacity as parameter
- isFull which will return true if counter reaches capacity otherwise false
- insert which will receive a value of some type (int/double/float) and insert into the container if it’s not already full
- search which will return index number of the value if the container contains the value passed as parameter, otherwise it should return -1.
- remove which will remove a value, if exists, from the container and shifts all subsequent values one index back.
- print which will print all values contained in the container
Now create a test function to create three instances of Container for types: int, float, double and call all functions separately for each instance. Your output must be properly formatted.
Code:
The below is the code for OOP Assignment No 04 for Template Class
#include<iostream> using namespace std; template class container { T *ptr; int capacity; int counter; public: container(T a,int counter = 0) { capacity=a; ptr=new T[capacity]; } void insert(T b) { if(counter>=capacity) { cout<<"Capacity Full!!"<<endl; } else { ptr[counter]=b; counter++; cout<<counter<<"th Elements Are Added Which is "<<b<<endl; } } void search(T c) { bool status; for(int i=0;i<capacity;i++) { if(ptr[i]==c) { cout<<c<<" is Found Succesfully at index "<<i<<endl; return; } else { status=false; } } if(status==false) { cout<<"It is at index -1"; } return ; } void remove(T d) { bool status = 0; for(int i=0;i<counter;i++) { if(ptr[i]==d) { status= 1; ptr[i]=0; for(int j=i;j<counter;j++) { ptr[i]=ptr[j+1]; i++; } cout<<d<<" is Removed Sucessfully"<<endl<<endl; counter--; } } if(status==0) { cout<<d<<" is not found !!"<<endl<<endl; } } void print() { for(int i=0;i<counter;i++) { cout<<"The "<<i+1<<" Value is : "<<ptr[i]<<endl; }} }; int main() { cout<<"------------------------- FOR INTEGER ------------------------"< T1(10); T1.insert(3); T1.insert(4); T1.insert(5); T1.insert(6); cout<<"\n------------------------- Removing of Values ------------------------"<<endl<<endl; T1.remove(4); cout<<"\n------------------------After Removal of Values ------------------------"<<endl<<endl; T1.print(); cout<<"\n------------------------Searching for a Value ------------------------"<<endl<<endl; T1.search(10); cout<<"\n\n------------------------ FOR FLOAT ------------------------"< T2(10); T2.insert(4.6); T2.insert(5.7); T2.insert(3.9); T2.insert(9.8); cout<<"\n------------------------- Removing of Values ------------------------"< T3(10); T3.insert(3.8887); T3.insert(9.778); T3.insert(1.880); T3.insert(6.665); cout<<"\n------------------------- Removing of Values ------------------------"<<endl<<endl; T3.remove(3.8887); cout<<"\n------------------------After Removal of Values ------------------------"<<endl<<endl; T3.print(); cout<<"\n------------------------Searching for a Value ------------------------"<<endl<<endl; T3.search(6.665); return 0; }
For more details about Huffman coding click here
For other assignments and quizzes click here
Output: