Containing the Type You Specify in Classes - C++ template

C++ examples for template:template class

Description

Containing the Type You Specify in Classes

Demo Code

#include <iostream> 
#include <vector> 
#include <string> 
using namespace std; 

class Employee //  ww w.j a  v a2 s  . co  m
{ 
public: 
   string Name; 
   string FireDate; 
   int GoofoffDays; 

   Employee(string aname, string afiredate, int agoofdays) : Name(aname), FireDate(afiredate), GoofoffDays(agoofdays) {} 
}; 

int main() { 
   // A vector that holds strings 
   vector<string> MyAliases; 
   MyAliases.push_back(string("A")); 
   MyAliases.push_back(string("B")); 
   MyAliases.push_back(string("C")); 
   
   cout << MyAliases[0] << endl; 
   cout << MyAliases[1] << endl; 
   cout << MyAliases[2] << endl; 

   // A vector that holds integers 
   vector<int> LuckyNumbers; 
   LuckyNumbers.push_back(1); 
   LuckyNumbers.push_back(2); 
   LuckyNumbers.push_back(3); 
   cout << LuckyNumbers[0] << endl; 
   cout << LuckyNumbers[1] << endl; 
   cout << LuckyNumbers[2] << endl; 

   // A vector that holds Employee instances 
   vector<Employee> GreatWorkers; 
   GreatWorkers.push_back(Employee("Z","1", 50)); 
   GreatWorkers.push_back(Employee("T","0", 40)); 
   cout << GreatWorkers[0].Name << endl; 
   cout << GreatWorkers[1].Name << endl; 

   return 0; 
}

Result


Related Tutorials