c++ program, please include all details Create a class template for a class that
ID: 3834790 • Letter: C
Question
c++ program, please include all details 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 a standard input function for the object that displays a message on the screen You will be asked to enter X itemswhere X is the number of data elements. Write a main()function that tests your template class with an integer and two programmerdesigned classes.
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 . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.