For the week 3 assignment, create a class template for a class that holds an obj
ID: 3733163 • Letter: F
Question
For the week 3 assignment, 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. Code.
Add a standard input function for the object that displays a message on the screen such as: “You will be asked to enter X items”, where X is the number of data elements.
Write a main() function that tests your template class with 3 cases: a simple integer and two programmer designed classes. In each case, overload the operations for cout <<, and cin >>.
This is for C++
Explanation / Answer
I have added the code below. I have tested it with three classes - Event,Employee and Student , the code is working fine. Below is the code: -
#include <iostream>
using namespace std;
//template class to hold an object and number of elements.
template<class T,int numA>
class Info{
T object;
int num;
public:
Info(){ this->num = numA; }
void display(){
cout << "You will be ask to enter " << this->num << " details." << endl;
}
};
//Test Class 1
class Student{
int id;
string name;
public:
Student(){ }
};
//Test Class 2
class Event{
string name, date, venue;
int headCount;
public:
Event(){
}
};
//Test Class 3
class Employee{
private:
int EmployeeID;
double EmployeeSalary;
public:
Employee(){
EmployeeID = 0;
EmployeeSalary = 0.00;
}
};
int main(){
Info<Student , 2> studentA;
Info<Event, 4> eventA;
Info<Employee, 2> employeeA;
cout << "Student Class, " << endl;
studentA.display();
cout << "Event Class, " << endl;
eventA.display();
cout<< "Employee Class, "<<endl;
employeeA.display();
}
Sample Run: -
Student Class,
You will be ask to enter 2 details.
Event Class,
You will be ask to enter 4 details.
Employee Class,
You will be ask to enter 2 details.
--------------------------------
Process exited after 0.2555 seconds with return value 0
Press any key to continue . . .