overload operator>> for class Personal


👍 g++ database.cpp && ./a.out
1. Add 2. Find 3. Modify a rec 4. Exit
Enter an option: 1
in operator>>
Enter an option: 4

👍 cat personal.h
#ifndef PERSONAL
#define PERSONAL

using namespace std;
class Personal {
  friend istream& operator>>(
    istream &i, Personal &p) {
    cout << "in operator>>" << endl;
    return i;
  }
};
#endif

👍 cat database.h 
#ifndef DATABASE
#define DATABASE

template <typename T>
class Database {
public:
  Database();
  void run();
};
#endif

👍 cat database.cpp
#include <iostream>
#include "personal.h"
#include "database.h"

template <typename T>
Database<T>::Database() {
}

template <typename T>
void Database<T>::run() {
  char option[5];
  T rec;
  cout << "1. Add "
       << "2. Find "
       << "3. Modify a record "
       << "4. Exit\n";
  cout << "Enter an option: ";
  while (cin.getline(option, 4)) {
    if (*option == '1') {
      cin >> rec;
    } else if (*option == '2') {
    } else if (*option == '3') {
    } else if (*option != '4')
      cout << "Wrong option\n";
    else return;
    cout << "Enter an option: ";
  }
}

int main() {
  Database<Personal>().run();
}

    

ch 1.10 CASE STUDY: RANDOM ACCESS FILE p43 of

© 拇指 2023