#pragma once
#include<string>
using namespace std;
// 普通用户类,只能查看、统计、显示车辆,无法实现增删改
class User {
public:
void checkCar(); // 普通用户登录菜单
user.cpp
#include<iostream>
#include<Windows.h>
#include"user.h"
#include"car.h"
using namespace std;
void User::checkCar() {
Car car;
while (1) {
system("cls"); // 清空屏幕
cout << "1.显示车辆状况" << endl;
cout << "2.查询车辆信息" << endl;
cout << "3.统计车辆" << endl;
cout << "4.退出普通用户" << endl;
int c;
cout << "输入要执行的操作:";
cin >> c;
switch (c) {
case 1: car.showInfor(); break;
case 2: car.findCar(); break;
case 3: car.timeAmount(); break;
case 4: return;
default: cout << "请输入正确的操作" << endl;
system("pause");
3.3、管理员用户类及方法实现
admin.h
#pragma once // 避免同一个头文件被包含多次
#include<string>
#include"user.h"
using namespace std;
// 管理员类,公有继承普通用户类,可以添加,修改,删除
class Admin:public User {
public:
void Manager(); // 显示管理员登录的菜单
admin.cpp
#include"admin.h"
#include"car.h"
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void Admin::Manager() {
Car car;
while (1) {
system("cls"); // 清空屏幕
cout << "1.增加车辆" << endl;
cout << "2.显示所有车辆信息" << endl;
cout << "3.查询" << endl;
cout << "4.修改" << endl;
cout << "5.删除" << endl;
cout << "6.统计" << endl;
cout << "7.退出管理用户" << endl;
int choice;
cout << "请输入要执行的操作:";
cin >> choice;
switch (choice) {
case 1: car.addCar(); break;
case 2: car.showInfor(); break;
case 3: car.findCar(); break;
case 4: car.modCar(); break;
case 5: car.delCar(); break;
case 6: car.timeAmount(); break;
case 7: return;
default: cout << "输入错误!" << endl;
system("pause");
3.4、主函数调用情况
#include"user.h"
#include"admin.h"
#include<iostream>
using namespace std;
int main() {
User user; // 普通用户对象
Admin admin; // 管理员对象
int choice;
while (1) {
system("cls");
cout << "1.普通用户登录" << endl;
cout << "2.管理员登录" << endl;
cout << "3.退出系统" << endl;
cout << "请输入要执行的操作:" << endl;
cin >> choice;
switch (choice) {
case 1: user.checkCar(); break;
case 2: admin.Manager(); break;
case 3: exit(0); // 退出系统
default: cout << "请输入正确的操作" << endl;
system("pause");
return 0;