Template and dynamic allocation

Hi. I've got an assignment today, and I'm having trouble with doing it...

The assignment is..

1. Make 3 functions.
2. "add" function saves data.
3. "getSize" function counts the number of data.
4. "output" function outputs data in "add" data.

I've tried for 2 hours and I ended up with a few questions.

1. How can I output in "output" function data from "add" function?
2. Where should I declare dynamic allocation?
3. Why should I count the number of data...?

Please, somebody help me...

I've tried for 2 hours

OK, chuck in whatever you've got down as code/psuedocode from those 2 hours and we'll start off with that
Actually, I deleted the codes that I've tried since I couldn't find way with them.. this may be the best restoration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once
#include <iostream>
using namespace std;
template<typename Z>
class tempClass
{
public:
	void add()
	{
		Z *a = new Z;
		Z *b = new Z;
		Z *c = new Z;
	}
	void output()
	{
		cout << add << endl;
	}
public:
	tempClass() {};
	~tempClass() {};
};
1
2
3
4
5
6
7
8
9
10
11
template<typename T>
std::vector<T> add()//the function returns a vector of all the data you've added
{
//declare the vector
//have a loop to enter data unless user chooses to quit
//or ask user upfront how many elements s/he wants to enter
//push_back vector with the data entered
//when user's done return the vector
//local variable, so return by value
//
}


vector.size() counts number of data
range loop (C++11, google if necessary) over the vector to output it's data assuming opertor << is defined for the datatype, otherwise you have to provide an overload

within main if the datatype is int you call the function as add<int>() etc
Last edited on
Thank you!!

I passed.
Topic archived. No new replies allowed.