Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,173 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,942 people online right now. Registration is fast and FREE... Join Now!




Help with creating a template class

 
Reply to this topicStart new topic

Help with creating a template class

ibaraku
6 Dec, 2007 - 04:14 PM
Post #1

D.I.C Head
Group Icon

Joined: 12 May, 2007
Posts: 164



Thanked: 1 times
My Contributions
Hello everyone, I have a question about Templates, I'm supposed to write a template-based class that implements a set of items. The class should allow the user to
a)add a new item to the set
b)get the number of items in the set
c)get a pointer to a dynamically created array containing each item in the set, the caller of this of this function is responsible for de-allocating the memory

I'm mostly confused about part c, and don't really know what they exactly mean. I just need help on how to start this project, thank you guys.
User is offlineProfile CardPM
+Quote Post

Bench
RE: Help With Creating A Template Class
6 Dec, 2007 - 04:32 PM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 617



Thanked: 14 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Well, then worry about the dynamic memory allocation later on - see if you can create your class with a fixed size array to begin with.

Dynamically created arrays are usually those which are created with new. the syntax goes along the following lines (example - an array of 20 int objects)
CODE
int* my_array = new int[20];


Edit - Its important to make sure you clean up your array when you're done with it. deletion of the array must happen using the delete[] operator.

This post has been edited by Bench: 6 Dec, 2007 - 04:40 PM
User is offlineProfile CardPM
+Quote Post

ibaraku
RE: Help With Creating A Template Class
6 Dec, 2007 - 04:40 PM
Post #3

D.I.C Head
Group Icon

Joined: 12 May, 2007
Posts: 164



Thanked: 1 times
My Contributions
QUOTE(Bench @ 6 Dec, 2007 - 05:32 PM) *

Well, then worry about the dynamic memory allocation later on - see if you can create your class with a fixed size array to begin with.

Dynamically created arrays are usually those which are created with new. the syntax goes along the following lines (example - an array of 20 int objects)
CODE
int* my_array = new int[20];


Edit - deletion of the array must happen using the delete[] operator.


ok, so I wrote the header file and declared my array as so
[code]
#ifndef TEMPLATE_H
#define TEMPLATE_H

#include<iostream>
#include<string>

template<class T>
class TemplateSet{
public:
TemplateSet();//constructor
TemplateSet(T newValue);
void setValue(T newValue);
T getValue()const;
void getNumberOfItems();
T *array;
private:
T value;
};
#endif;
[\code]

and then just define in my implementation file, correct???

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:46AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month