⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 page08_14.cpp

📁 C++ interview materials. Very helpful for interview. Including Answer.
💻 CPP
字号:
/*
NIIT 《C++ & CGI PROGRAMMING &SCRRIPTING》 Skill Base
教材P8.14
审核:周永 Laozhou@swpi.edu.cn	23:36 2004-10-18
测试环境:Microsoft Windowsn 2000(VC++);Red Hat Linux 7.3;Red Hat Linux 9.0
*/

//包含头文件
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//************************************************************************************
//************************************************************************************
//父类,被Customer继承
class Person
{
//保护类型的成员变量 子类可以访问
protected:
 char name[26];
 char dateOfBirth[11];
 char city[26];
 char mobileNo[12];
 char phoneNo[11];
//公有类型的成员函数
public:
 //接受用户输入数据的函数
 void accept()
 {
  cout<< endl << "Name: ";
  cin.getline(name, 25);
  cout << endl << "Date of Birth: ";
  cin >> dateOfBirth;
  cin.ignore();
  cout << endl << "City: ";
  cin.getline(city, 25);
  cout << endl << "Mobile phone number: ";
  cin >> mobileNo;
  cin.ignore();
  cout << endl << "Residence phone number: ";
  cin >> phoneNo;
  cin.ignore();
 } //end of accept()
 //显示对象中成员变量的函数
 void display()	
 {
  cout << endl << "Name             : " << name;
  cout << endl << "Date of birth    : " << dateOfBirth;
  cout << endl << "City             : " << city;
  cout << endl << "Mobile number    : " << mobileNo;
  cout << endl << "Residence number : " << phoneNo;
 } //end of  display()

}; //end of class Person


//客户类Customer(Person的子类)
class Customer : public Person
{
//允许FileRead和FileWrite友元类访问自己私有的成员
friend class FileRead;
friend class FileWrite;
private:
 char billingAddress[51];
 float amountOutstanding;

public:
 void accept()
 {
  cout << endl << endl << "ENTER CUSTOMER'S DETAILS";
  Person::accept();
  cout << endl << "Customer's billing address: ";
  cin.getline(billingAddress, 50);
  cout << endl << "Amount due: ";
  cin >> amountOutstanding;
  cin.ignore();
 } //end of accept()

 void display()
 {
  cout << endl << endl << "CUSTOMER'S DETAILS ";
  Person::display();
  cout << endl << "Billing address   : " << billingAddress;
  cout << endl << "Outstanding amount: "<< amountOutstanding << endl;
 } //end of display()

}; //end of class Customer



//写Customer对象到文件中的类
class FileWrite
{
public:
 void accept(Customer &custobj)
 {
 ofstream file;
 custobj.accept();
 file.open("customer.dat", ios::out | ios::app);
 file.write((char *)&custobj, sizeof(custobj));
 file.close();
 }//end of accept()

}; //end of class FileWrite


//从文件读取所有或者特定Customer对象的类
class FileRead
{
public:
 //从文件开头读到结尾 显示所有
 void display_all(Customer &custobj)
 {
  ifstream file;
  file.open("customer.dat", ios::in);
  file.read((char *)&custobj, sizeof(custobj));
  while( ! file.eof() )
  {
    custobj.display();
    file.read((char *)&custobj, sizeof(custobj));
  } //end of while
  file.close();
 } //end of display_all

 //从文件开头读到结尾 只显示某个特定数据(根据输入的手机号码)
 void display_Cust(char *mobile)
 {
  ifstream file;
  Customer custobj;
  file.open("customer.dat", ios::in);
  file.read((char *)&custobj, sizeof(custobj));
  while( ! file.eof() )
  {
    if(strcmp(custobj.mobileNo, mobile) == 0)
    {
      custobj.display();
      file.close();
      return;
    } //end of if
    file.read((char *)&custobj, sizeof(custobj));
  } //end of while
 file.close();
 cout << "A customer with mobile number: " << mobile << " does not exist ! " << endl;
 } //end of display_Cust

};  //end of class   FileRead

//四个类的编写到此结束  下面是main函数
//************************************************************************************
//************************************************************************************


int main()
{
 Customer custobj;
 FileWrite writeobj;
 FileRead readobj;
 int choice = 0;
 while(choice != 4)
 {
  cout << endl << "CUSTOMER MENU" << endl;
  cout << "1. Accept the details of a customer" << endl;
  cout << "2. Display the details of all customers" << endl;
  cout << "3. Display details of one customer" << endl;
  cout << "4. Quit the application" << endl;
  cout << "Enter choice (1 - 4):  ";
  cin >> choice;
  cin.ignore();
  switch(choice)
  {
    case 1:
      writeobj.accept(custobj);
      break;
    case 2:
      readobj.display_all(custobj);
      break;
    case 3:
      cout << "Enter Mobile Number : ";
      char mobile[11];
      cin >> mobile;
      readobj.display_Cust(mobile);
      break;
    case 4:
      continue;
    default:
      cout << "Invalid option! Please enter 1 - 4 only"  << endl;
    } //end of switch

  } //end of while

 return 0;
} //end of main

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -