A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
Creating a collection class in C++How to use a template to create a custom collection class and using the C++ std::vector STL library as well as the operator. I will expect you to understand how pointers, classes, templates and the operator works |
On Wednesday, March 10th 2004 at 09:11 AM By Johan Strydom (View Profile) ![]() ![]() ![]() ![]() (Rated 3.9 with 13 votes) |
||
IntroductionC++ comes with its own set of containers in the STL (Standard Template Libraries) libraries; (e.g. Why a collection class?"A collection class is a container class that holds one or more items of a certain type" Say for example I want to develop a game where there is a board with 9 buttons. Each button will have a status of on or off. Each button is located on a specific x,y position on the board and is of a certain color depending on the status and player that clicked on the button. The figure below show the board with nine buttons.
Figure.1.On the code side I will thus have a board class containing a collection of buttons. The figure below shows a UML representation of the board class.
Figure.2.The collection class will allow us to have access to each button as well as that button’s properties and functions (e.g status , x and y position, color). Ok lets get coding. The CodeThe code for the collection class is fairly simple. If you understand how pointers, classes, templates and the operator works, then you are already 90% there. The only thing left is for you to bring these technologies together and “voila!” you will have your collection class. In your favorite C++ compiler, create a new win32 project and a header file. I called mine “TCollection.h”. Then add the following line of code to the header: #include After the template class is created, we first add the protected vector member. Protected because we want the member to be available to classes derived from the collection class. template There are basically three things we want to do to our container: Add an Item, Remove an Item and Clear the Container. We also want to know how many items are in the container and for that we will need a The |
|||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current CommentsCongratulations, you have invented the wheel. I see no advantage of using TCollection<T> over vector<T>. TCollection is a wrapper for vector that removes a lot of functionality. I do not see how two function calls (Add()+operator[]) instead of one function call (push_back()) is "better".
though there are situations where the you need to create such a wrapper class for vectors. Nice work.
Related Tutorials
Related Source Code
C++ Job Search