On-line: гостей 0. Всего: 0 [подробнее..]
Программисты всех стран, объединяйтесь!

АвторСообщение



ссылка на сообщение  Отправлено: 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?

Спасибо: 0 
ПрофильЦитата Ответить
Ответов - 34 , стр: 1 2 All [только новые]





ссылка на сообщение  Отправлено: 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.

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 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?

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 31.10.12 14:27. Заголовок: ya i know because of..


ya i know because of the
count = numOfData so is it correct? lol

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 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



Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 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

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 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.

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 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?

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 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..

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 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?

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 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.

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 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?


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 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.

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 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 ?

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 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.

Спасибо: 0 
ПрофильЦитата Ответить
Ответов - 34 , стр: 1 2 All [только новые]
Ответ:
1 2 3 4 5 6 7 8 9
большой шрифт малый шрифт надстрочный подстрочный заголовок большой заголовок видео с youtube.com картинка из интернета картинка с компьютера ссылка файл с компьютера русская клавиатура транслитератор  цитата  кавычки моноширинный шрифт моноширинный шрифт горизонтальная линия отступ точка LI бегущая строка оффтопик свернутый текст

показывать это сообщение только модераторам
не делать ссылки активными
Имя, пароль:      зарегистрироваться    
Тему читают:
- участник сейчас на форуме
- участник вне форума
Все даты в формате GMT  3 час. Хитов сегодня: 20
Права: смайлы да, картинки да, шрифты да, голосования нет
аватары да, автозамена ссылок вкл, премодерация откл, правка нет