如何从基类继承结构成员

How to inherit structure members from base class?

本文关键字:结构 成员 继承 基类      更新时间:2023-10-16

我写了一个简单的c++代码,用继承的概念做一些库管理。我遇到了一些继承结构问题和一些全局变量问题。我的代码:

头文件:

#ifndef LIBRARY_H
#define LIBRARY_H
#include <string.h>
#include <iostream>
using namespace std;
static int count_emp;
static int m_nCountBook;
class employee 
{
protected:
    typedef struct
    {
        char employee_name[20];
        int employee_id;
        char employee_dept[20];
        char status_emp[10];
    } stemp;
public:
    stemp emp[10];  
    void add_employee();
    void display_employee();
};
class library : public employee
{
protected:
    typedef struct
    {   
        char book_name[20];
        int book_id;
        char author[20];
        char status_book;
    } stlib;
public:
    stlib lib[10];
    void addBook();
    void showBook();
    void bookTaken();
    void bookReturn();
};
#endif

这个概念是当我在另一个文件中实现bookTaken()函数时,应该检查员工id并添加所取的status_emp。如果以前的员工状态被占用,则不允许。我如何定义使用这两个结构的功能?还有一个问题是static int count_emp;static int m_nCountBook;是静态的。我怎样才能进入另一个文件?

删除两个全局变量。它们应该是库类的实例字段。

如果正确使用stl,很可能根本不需要它们。例如,将所有数组替换为std::vector s,除了应该为std::string的char数组。