Template in class

Here the work i done so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>

using namespace std;

template< class T >
class Employee{
private:
	T EmployeeID , EmployeeSalary;
public:
	T Employee( T theID , T theSalary ){
		EmployeeID = theID;
		EmployeeSalary = theSalary;
	}
};

template< class T >
class Student{
private:

public:
	T Student(){}

};

int main(){

	Employee <> theEmployee( 2 , 3000 );
	Student <> theStudent ();

	system( "pause" );
	return 0;
}


And here is the question given
Create a class template for a class that holds an object and the number of data elements in the object. For example, if an Employee class has two data elements, an ID number and a salary, then the class template holds the number 2 and an Employee object; if a Student class contains 12 data elements, then the class template holds 12 and a Student object. Write the code for standard input function for the object that displays a message on the screen – “You will be ask to enter X items” – where X is the number of data elements. Write a main() function that tests your template class with an integer and two programmer-defined classes


So can i ask that , am i do correctly so far?
what is the mistake i made ?
constructors dont return anything, when declaring variable of a template class you need to pass the type to it Employee <> theEmployee( 2 , 3000 ); should look something like Employee <int> theEmployee( 2 , 3000 );

closed account (DETpfSEw)
Number 1: you should remove return type from constructor of first class. because constructor returns nothing.
Number 2: specify data type in main function when instantiating the template based classes.
ENJOY!!!
Did i fullfill what question want me to do?
err . remove return type ?
did u mean create accessor or mutator?
He means this
1
2
3
4
	T Employee( T theID , T theSalary ){
		EmployeeID = theID;
		EmployeeSalary = theSalary;
	}


Should be this
1
2
3
4
	Employee( T theID , T theSalary ){
		EmployeeID = theID;
		EmployeeSalary = theSalary;
	}
okay. i changed it . i get what he mean .
but now
if a Student class contains 12 data elements, then the class template holds 12 and a Student object. Write the code for standard input function for the object that displays a message on the screen – “You will be ask to enter X items” – what mean for this sentences?

how should the class Student pass the parameter to constructor? abit blur with it
mind to help for me template?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>

using namespace std;

template< class T , int numOfData >
class Holder{
private:
	T object;
	int count;
public:

};

class Employee{
private:
	int EmployeeID;
	double EmployeeSalary;
public:
	Employee( int theEmployeeID , double theEmployeeSalary ){
		EmployeeID = theEmployeeID;
		EmployeeSalary = theEmployeeSalary;
	}
};

class Student{
private:
	int numOfDataElements;
public:
	Student();

};

int main(){

	Employee <int> theEmployee( 2 , 3000 );
	Student <> theStudent ();

	system( "pause" );
	return 0;
}


the work i done so far . so any advise?
You have a basic misunderstanding of templates. Please read and understand this:
http://www.cplusplus.com/doc/tutorial/templates/

You seem to also have an issue with passing parameters to the constructor, so read this:
http://www.cplusplus.com/doc/tutorial/classes/

After reading those and changing your code based on what you learned, then post back and show us what you've done.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>

using namespace std;

template< class T , int numOfData >
class Holder{
private:
	T object;
	int count;
public:
	Holder(){
		count = numOfData;
	}
	void inputItems(){
		cout << "You will be asked to enter " << count << "items" << endl;
		object.inputItems();
	}
};

class Employee{
private:
	int EmployeeID;
	double EmployeeSalary;
public:
	Employee(){
		EmployeeID = 0;
		EmployeeSalary = 0.00;
	}
	Employee( int theEmployeeID , double theEmployeeSalary ){
		EmployeeID = theEmployeeID;
		EmployeeSalary = theEmployeeSalary;
	}
};

class Student{
private:
	int numOfDataElements;
public:
	Student(){
		numOfDataElements = 0;
	}
	Student( int numOfData ){
		numOfDataElements = numOfData;
	}
};

int main(){
	
	Holder <Employee , 2> h1;
	Holder <Student , 12> h2;

	system( "pause" );
	return 0;
}


the work i done so far. last code is incomplete . just get some concept.
hmm. i read slide from my class and base on that to do some changes on it
mind a senior teach me ?
Topic archived. No new replies allowed.