#include <bits/stdc++.h> using namespace std; /* class definition for each record */ class Employee{ int id; string name; public: Employee(int Id, string Name){ id = Id; name = Name; } int getid(){ return id; } void print(){ cout<<"Id : "<<id<<"\n"; cout<<"Name : "<<name<<"\n"; } }; /* class definition which stores multiple records of 'Employee' type */ class EmployeeFile { vector<Employee> file; public: void add(int id, string name){ file.push_back(Employee(id,name)); } Employee search(int id){ for(auto f : file){ if(f.getid() == id) return f; } return {0,""}; } void print(){ for(auto f : file){ f.print(); } } }; int main() { EmployeeFile emp; /* input number of records */ int n; cin>>n; for(int i=0;i<n;i++){ int id; string name; cin>>id>>name; /* add record */ emp.add(id,name); } cout<<"List of employees\n"; /* print records */ emp.print(); int key; cin>>key; /* search record with key */ Employee e = emp.search(key); cout<<"Searched employee\n"; e.print(); return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter