Container Class and Integrators, Proxy Class
Container Class A container class is a data type that is capable of holding a collection of items. A container stores many entities and provide sequential or direct access to them. List, vector and strings are such containers in standard template library. Various operations associated with container class are: Create an empty container Insert an object inside the container Remove an object from container Find the number of objects in the container Sort the objects within the container
Components of STL
Container class Categories of container class: Sequence containers Associative containers Container adapters Sequence container: container class that maintain the ordering of elements in the container. Types of Sequence containers: Vector :dynamic array capable of growing,allows random access to its element. Deque: deck, double ended queue, grow from both sides List: Doubly linked list
Container class Associative containers: automatically sort their inputs when those inputs are inserted into the container. Associative containers compare elements using operator <. Types of associative containers: Multiset- duplicate elements are allowed Map(associative array)- key/value pair Multimap(dictionary)-is a map that allows duplicate keys.(key word, value-meaning)
Container class Container adapters: predefined containers that are adapted to specific uses. Stack : LIFO,operations done at end of the container Queue: FIFO, Insertion: back of the container, Deletion: front Priority queue
Container class Iterator It is an object that can traverse a container class without the user having to know how the container is implemented.
Container class #include<iostream.h> #include<conio.h> class bag public: void insert(int); void show(); int size(); bag()count=0;} private: int data[20]; int count; };
Container class void bag::insert(int val) data[count]=val; count++; } void bag::show() for(int i=0;i<count;i++) cout<<" "<<data[i]; } int bag::size() return count; }
Container class void main() bag o; clrscr(); cout<<"\nthe items in container are:\n"; o.insert(20); o.insert(40); o.insert(60); o.show(); cout<<"\n size of the container"<<o.size(); cout<<"\n items in container are\n"; o.insert(80);
Container class o.insert(100); o.show(); cout<<" \n size of container is"<<o.size(); cout<<"\n"; getch(); }
Container class
Proxy Class Proxy class: Used to hide implementation details of a class Class that knows only the public interface of the class being hidden Enables clients to use class services without giving access to class s implementation Forward class declaration: Used when class definition only uses a pointer to another class Prevents the need for including header file Declares a class before it is referenced
Proxy Class: Creation of myclass.h class myclass private: int value; public: myclass(int v):value(v) } void setvalue(int v) value=v; } int getvalue() return value; } };
Proxy Class:myinterface.cpp class myclass; class myinterface private: myclass *ptr; public: myinterface(int); void setvalue(int); int getvalue(); ~myinterface(); };
Proxy Class: Creation of Myinterface.cpp #include"c:\turboc4\tc\bin\myinterf.h" #include"c:\turboc4\tc\bin\myclass.h" myinterface::myinterface(int v):ptr(new myclass(v)) } void myinterface::setvalue(int v) ptr->setvalue(v); } int myinterface::getvalue() return ptr->getvalue(); }
Proxy Class :Creation of Myinterface.cpp myinterface::~myinterface() delete ptr; }
Proxy Class: Creation of test.cpp #include<iostream.h> #include"c:\tc\bin\myinterf.h" void main() myinterf obj(10); cout<<"\n interface contains(before):"<<obj.getvalue(); obj.setvalue(20); cout<<"\n interface contains(after):"<<obj.getvalue(); }