Автор | Сообщение |
|
Отправлено: 30.10.12 07:02. Заголовок: Template for class
#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; } what i done so far and this the question given 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 am i doing correctly until now?
|
|
|
Ответов - 34
, стр:
1
2
All
[только новые]
|
|
|
Отправлено: 30.10.12 11:04. Заголовок: No your code is inco..
No your code is incorrect and does not correspond to the assignment. First of all constructors have no return types. So you may not write as for example T Student(){} Also your definitions of the classes have no default template arguments. So you may not write Student <> theStudent (); I think that according to the assignment the template class should be defined with two template parameters: a type template parameter for denoting an object and a non-type template parameter for the number of members in the type of the object. Thus the definition of the template class should look the following way template <class T, size_t N> class Holder { private: T object; size_t count; public: /* some methods of the class };
|
|
|
|
Отправлено: 30.10.12 11:18. Заголовок: the class Holder if..
the class Holder if in my assignment . the class should be Student? the size_t count , is it T count in the code . or should be int count in the code? andStudent <> thestudent() i sjust a part of code that i didn't finish because I don't know what parameter should pass inside. so for my employee class. am i doing correctly?
|
|
|
|
Отправлено: 30.10.12 11:32. Заголовок: As far as I understa..
As far as I understand your assignment you have to цитата: | "Create a class template for a class that holds an object and the number of data elements in the object" |
| That is the class template will contain either an object of type Employee and the number of data members in this class or class Student and the number of data members of the class. I showed an example of how the template class should look.
|
|
|
|
Отправлено: 30.10.12 12:11. Заголовок: #include <iostre..
#include <iostream> #include <string> using namespace std; template< class T , int numOfData > class Employee{ private: T Employee; int count; T EmployeeID , EmployeeSalary; public: T Employee( T theID , T theSalary ){ EmployeeID = theID; EmployeeSalary = theSalary; } }; template< class T , int numOfData > class Student{ private: T Student; int count; public: T Student(){} }; mind to show how the output look like? am i correct for the code above like what you said just now?
|
|
|
|
Отправлено: 30.10.12 12:40. Заголовок: I think that Employe..
I think that Employee and Student shall not be template classes. It is the class Holder that shall be a template class and shall be instantiated either with Employeee or Stident.
|
|
|
|
Отправлено: 30.10.12 16:21. Заголовок: #include <iostre..
#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; } what do you mean shall be instantiated? isn't Employee :: public Holder ? that is inheritance?
|
|
|
|
Отправлено: 30.10.12 16:40. Заголовок: Class Employee shall..
Class Employee shall have default constructor because tenplate class Holder has data member for which default constructor shall be called. I meant that main has to have something as Holder<Employee, 2> h1; Holder<Student, 12> h2; and so on.
|
|
|
|
Отправлено: 30.10.12 17:22. Заголовок: #include <iostre..
#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(){ 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; } compile successful. isn't correct already? and at the output should look like? or no output for this question actually ? jus tlet us to get understand for the template? thanks for your fast response .
|
|
|
|
Отправлено: 30.10.12 17:30. Заголовок: Data member count of..
Data member count of class Holder is not initialized. I think you can add an explicit default constructor to Holder that will initialize count with non-type template parameter. Also you have to add public function member that will issue the message "“You will be ask to enter X items”". Also each class shall have a methof for inputing values to their data members. This method shall have a common name for the both classes.
|
|
|
|
Отправлено: 30.10.12 17:43. Заголовок: This method shall ha..
This method shall have a common name for the both classes? what this mean actually? and the "“You will be ask to enter X items”". is a function ? which mean example: void inputItems(){ int items = 0; cout << "Enter X items : " ; cin >> items; } then how the function will be call out since we only call this in main? int main(){ Holder <Employee , 2> h1; Holder <Student , 12> h2; system( "pause" ); return 0; }
|
|
|
|
Отправлено: 30.10.12 17:48. Заголовок: Read your assignment..
Read your assignment one more. Class Holder has to have a method that will issue the message and call a method of data member object that will provide input of some values for its data members.
|
|
|
|
|
Отправлено: 30.10.12 17:55. Заголовок: . Write the code fo..
. 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. the question didn't mention about the function should put in class Holder? so do you mean the void inputItems should be in public member of class Holder?
|
|
|
|
Отправлено: 30.10.12 18:11. Заголовок: Well you can ask the..
Well you can ask the question to your senior to make clear what did he mean.
|
|
|
|
Отправлено: 30.10.12 18:27. Заголовок: err . the question i..
err . the question is given by the campus from university of wollogong . we always complain that the question given by them is always foolish-ing us . that's why i keep asking from the website to get some understanding from the code given . erm. mind u ? or u also getting blur? haha
|
|
|
|
Отправлено: 30.10.12 18:36. Заголовок: I see ths sense when..
I see ths sense when an object of the template class calls a method of its member that either has type Employee or Student. It is the template class that knows how many elements the subobject contains. So I think that some method of the template class shall isuue the message and then call an input method of its subobject.
|
|
|
|
Отправлено: 30.10.12 18:56. Заголовок: so do you mean that ..
so do you mean that if i call the Holder in the main . then should out a input message that call user enter for the X item and then return to the Employee or Student object for the subobject contain? so the input message isn't should insert in the constructor of Holder ? because if like what you said earlier put into the public member function , it's no point to call the message out and then return? or else should be like this? template< class T , int numOfData > class Holder{ private: T object; int count; public: Holder(){ count = 0; } void inputItems(){ int items = 0; cout << "Enter X item : "; cin >> items; } }; then how should be call the function out? because in normal way we cannot put older theHolder; isn't correct?
|
|
|
|
Отправлено: 30.10.12 19:18. Заголовок: I think that it is r..
I think that it is required something as template< class T , int numOfData > class Holder { private: T object; int count = numOfData; public: //Holder() { count = NumOfData; } // The consttructor is not required if the compiler supports the initialization of class members in class definition void inputItems() { std::cout << "You will be asked to enter " << count << " items" << std::endl; object.inputItems(); } }; .
|
|
|
|
Отправлено: 31.10.12 04:15. Заголовок: err . but now i cann..
err . but now i cannot see got any input for your count? you just output it ? because user will be enter the count i think?
|
|
|
|
Отправлено: 31.10.12 13:18. Заголовок: I already showed you..
I already showed you. int main() { Holder <Employee , 2> h1; Holder <Student , 12> h2; h1.inputItems(); h2.InputItems(); system( "pause" ); return 0; } The only think that you have to do now is to wtite method inputItems for each class that used as template argument fo Holder. count stores the value of non-type template parameter, It shhall not be changed. In fact you can declare it with cons qualifier. Of course you should use valid values instead of 2 and 12 that is the number of items that will be entered in each class.
|
|
|
|
Отправлено: 31.10.12 14:03. Заголовок: const qualifer? isn&..
const qualifer? isn't mean that my numOfData should be in const value? and after i insert the method inputItems for each class my function inputItems foe each class will be like this class Employee{ private: int EmployeeID; double EmployeeSalary; public: Employee(){ EmployeeID = 0; EmployeeSalary = 0.00; } Employee( int theEmployeeID , double theEmployeeSalary ){ EmployeeID = theEmployeeID; EmployeeSalary = theEmployeeSalary; } void inputItems(){}; }; u mean the number 2 and number 12 is input by user? or ?
|
|
|
|
Отправлено: 31.10.12 14:19. Заголовок: Number 2 is fixed fo..
Number 2 is fixed for your class Student. It is the number of class members that will get values in inputItems.
|
|
|
|
|
Отправлено: 31.10.12 14:25. Заголовок: beside that . how i ..
beside that . how i show out what the object pass in? JUST EXAMPLE: Object : Employee You will be asked to enter 2 items isn't the output should like this too?
|
|
|
|
Отправлено: 31.10.12 14:27. Заголовок: ya i know because of..
ya i know because of the count = numOfData so is it correct? lol
|
|
|
|
Отправлено: 31.10.12 15:03. Заголовок: BasicNewbie пишет: ..
BasicNewbie пишет: цитата: | ya i know because of the count = numOfData so is it correct? lol |
| Yes, you are right. When an object of Holder is created its template arguments are specified including the number of members of a class used as the template argument
|
|
|
|
Отправлено: 31.10.12 15:49. Заголовок: Then how I call the ..
Then how I call the class name out ? I tried a lot of code but fail
|
|
|
|
Отправлено: 31.10.12 17:14. Заголовок: I have not understoo..
I have not understood your question. In fact I showed already the full code. You only need to write member function readItems for each class.
|
|
|
|
Отправлено: 31.10.12 17:23. Заголовок: I mean that how I sh..
I mean that how I show out the object that I pass to the template? Sohow to display out for the object?
|
|
|
|
Отправлено: 31.10.12 17:38. Заголовок: I already wrote all ..
I already wrote all code except the member function readItems of the classes. Reread the thread attentively..
|
|
|
|
Отправлено: 31.10.12 19:04. Заголовок: readItems is when i ..
readItems is when i enter the number 2 in the inputItems . so i can do a looping to call user want to enter what the 2 items are? mind that can u provide what the output should look like ? like that i can get it understand easily .. can?
|
|
|
|
Отправлено: 31.10.12 19:11. Заголовок: One more classes Stu..
One more classes Student and Employee shall have a method with the same name for both classes. You have to call this method.
|
|
|
|
Отправлено: 31.10.12 19:25. Заголовок: i can't get what..
i can't get what you mean for it because i not really understand for the last point that u explaining to me that i asking what should the progrma output look like? sorry for it because my english not so well . and the readItems() isn't read the Holder <Employee , 2> h1; Holder <Student , 12> h2; from the main? if 2 so the readItems() from the function will ask user to enter 2 items for it? if 12 so the readItems() will call user to enter 12 times for the items?
|
|
|
|
|
Отправлено: 31.10.12 20:01. Заголовок: One more in each cla..
One more in each class write a method for entering values for its members. I wil not repeat this any more.
|
|
|
|
Отправлено: 01.11.12 04:42. Заголовок: #include <iostre..
#include <iostream> #include <string> using namespace std; template< class T , int numOfData > class Holder{ private: T object; int count; public: Holder(){ object = obj; count = numOfData; } void inputItems(){ cout << typeid(object).name() << endl; cout << "You will be asked to enter " << count << " items" << endl << endl; object.inputItems(); } void readItems(){ object.readItems(); }; }; class Employee{ private: int EmployeeID; double EmployeeSalary; public: Employee(){ EmployeeID = 0; EmployeeSalary = 0.00; } Employee( int theEmployeeID , double theEmployeeSalary ){ EmployeeID = theEmployeeID; EmployeeSalary = theEmployeeSalary; } void inputItems(){ cout << "Enter Employee ID : "; cin >> EmployeeID; cout << "Enter Employee Salary : "; cin >> EmployeeSalary; }; void readItems(){ cout << "\nEmployee ID : " << EmployeeID << endl << "Employee Salary : " << EmployeeSalary << endl << endl; } }; class Student{ private: int numOfDataElements; public: Student(){ numOfDataElements = 0; } Student( int numOfData ){ numOfDataElements = numOfData; } void inputItems(){}; }; int main(){ Student student; Holder <Employee , 2> h1; Holder <Student , 12> h2; h1.inputItems(); h1.readItems(); h2.inputItems(); system( "pause" ); return 0; } so for my class Employee. am i doing correctly for all? and beside that . i have a seriously question to ask u ya . if i comment out for the constructor for each class . the program still work? so the constructor actually is unused ?
|
|
|
|
Отправлено: 01.11.12 19:25. Заголовок: I showed you already..
I showed you already how class Holder should look. template< class T , int numOfData > class Holder { private: T object; int count = numOfData; public: //Holder() { count = NumOfData; } // The consttructor is not required if the compiler supports the initialization of class members in class definition void inputItems() { std::cout << "You will be asked to enter " << count << " items" << std::endl; object.inputItems(); } }; . So the only thing that you have to do is to define classes Student and Employee and inside them define a method inputItems. That is all.
|
|
|
Ответов - 34
, стр:
1
2
All
[только новые]
|
|