请问把结构体写到类里面要怎么写

请问把结构体写到类里面要怎么写,就是不要结构体。

#include
#include
#include

using namespace std;

enum GoodsType//商品类别  
{
    Food=1,//食品
    Cosmetic,//化妆品
    Commodity,//日用品
    Drink//饮料
};

struct Date//入库时间 
{
    int year;
    int month;
    int day;
};

struct Goods//商品基本信息
{
    string code;//商品编号
    string name;//商品名称
    string brand;//生产厂家
    double price;//商品价格
    int num;//商品数量
    GoodsType type;//商品类别
    Date date;//入库时间
    Goods *next;
};

class GoodsManage//商品管理
{
public:
    GoodsManage();
    ~GoodsManage(){}
    void DisplayMainMenu();//主菜单显示
    void AddGoodsInfo();//添加商品信息
    void DisplayGoodsInfo();//浏览商品信息
    void SearchByCode();//按照商品编号搜索商品信息
    void SearchByName();//按照商品名称搜索商品信息
    void SearchByType();//按照商品类别搜索商品信息
    void SearchByBrand();//按照商品品牌搜索商品信息
    void EditGoodsInfo();//编辑商品信息
    void DeleteGoodsInfo();//删除商品信息
    void SellGoodsInfo();//出售商品信息
private:
    int amount;//商品量
    int DeleteAmount;
    Goods *head;
    Goods *DeleteHead;
};

GoodsManage::GoodsManage()//定义构造函数
{
    amount=0;
    DeleteAmount=0;
    head=new Goods;
    head->next=NULL;
    DeleteHead=new Goods;
    DeleteHead->next=NULL;
}

void GoodsManage::DisplayMainMenu()//定义主菜单函数
{
    cout<<" \t\t\t------------------------------------------------------------------\n";
    cout<<" \t\t\t*                                                                *\n";
    cout<<" \t\t\t*                   欢迎使用商品库存管理系统                     *\n";
    cout<<" \t\t\t*                                                                *\n";
    cout<<" \t\t\t*                        【商品进货】…(1)                       *\n";
    cout<<" \t\t\t*                                                                *\n";
    cout<<" \t\t\t*                        【商品编辑】…(2)                       *\n";
    cout<<" \t\t\t*                                                                *\n";
    cout<<" \t\t\t*                        【商品删除】…(3)                       *\n";
    cout<<" \t\t\t*                                                                *\n";
    cout<<" \t\t\t*                     |【按照编号查询】…(4)                     *\n";
    cout<<" \t\t\t*                     |                                          *\n";
    cout<<" \t\t\t*                     |【按照名称查询】…(5)                     *\n";
    cout<<" \t\t\t*         【商品查询】|                                          *\n";
    cout<<" \t\t\t*                     |【按照类别查询】…(6)                     *\n";
    cout<<" \t\t\t*                     |                                          *\n";
    cout<<" \t\t\t*                     |【按照品牌查询】…(7)                     *\n";
    cout<<" \t\t\t*                                                                *\n";
    cout<<" \t\t\t*                        【商品出货】…(8)                       *\n";
    cout<<" \t\t\t*                                                                *\n";
    cout<<" \t\t\t*                        【商品统计】…(9)                       *\n";
    cout<<" \t\t\t*                                                                *\n";
    cout<<" \t\t\t*                                                                *\n";
    cout<<" \t\t\t*                          退出系统…(0)                         *\n";
    cout<<" \t\t\t*                                                                *\n";
    cout<<" \t\t\t------------------------------------------------------------------\n";
    cout<<"\n                      请输入你要进行的操作编号:";           
}

void GoodsManage::AddGoodsInfo()//定义添加商品信息函数
{
    char c;
    int c1;
    Goods *tail=head,*p;
    bool flag;
    while(tail->next!=NULL)
        tail=tail->next;//令tail指向链表中最后一个节点
    do
    {
        flag=0;
        p=new Goods;
        cout<<"请选择商品类别:"<"1.食品 2.化妆品 3.日用品 4.饮料"<"请输入相应编号:";
        do
        {
            cin>>c1;
            if(c1>=1&&c1<=4)//判断用户输入编号是否存在
                flag=1;
            else
            {
                cout<<"您输入的编号不存在!"<"请选择正确的商品类别:"<while(flag==0);//输入编号存在时跳出循环
        if(c1==1)p->type=Food;
        if(c1==2)p->type=Cosmetic;
        if(c1==3)p->type=Commodity;
        if(c1==4)p->type=Drink;
        cout<<"商品类别("<type<<")"<//表示商品类别
        cout<<"请输入商品编号: ";
        cin>>p->code;
        do
        {
            Goods *q=head->next;
            while(q!=NULL&&q->code!=p->code)//当q指向NULL或输入的编号与之前编号重复时跳出循环
                q=q->next;
            if(q==NULL)//当p指向NNULL且编号不重复时
                flag=1;
            else//编号重复时
            {
                cout<<"存在该编号的货物!!!请重新输入编号:";
                cin>>p->code;
            }
        }while(flag==0);
        cout<<"请输入商品名称:";
        cin>>p->name;
        cout<<"请输入生产厂家:"; 
        cin>>p->brand;
        cout<<"请输入商品价格:";
        cin>>p->price;
        cout<<"请输入商品数量:";
        cin>>p->num;
        cout<<"请输入入库时间(年/月/日):";
        cin>>p->date.year>>p->date.month>>p->date.day;
        tail->next=p;//将p插入链表
        p->next=NULL;
        tail=p;
        amount++;//商品量加一
        cout<<"数据输入成功!!!想继续添加吗(y/n):";
        cin>>c;
        while(c!='y'&&c!='n')
        {
            cout<<"指令错误!!!!!<请输入y/n>"<"数据添加成功!!!想继续输入吗(y/n):";
            cin>>c;
        }
    }while(c=='y');
    cout<"信息处理完毕"<"按任意键返回主菜单"<void GoodsManage::EditGoodsInfo()//定义编辑商品信息函数
{
        char c;
        Goods *p;
        bool flag=0;
        string EditCode;
        do
        {
            p=head->next;//令p指向head下一个节点
            flag=0;
            cout<<"请输入您要修改的货物编号:";
            cin>>EditCode;
            while(p->next!=NULL&&p->code!=EditCode)//直到p指向链表中最后一个节点找到相应编号的商品时跳出循环
                p=p->next;
            if(p->code==EditCode)
            {
                flag=1;
                cout<10)<<"编号"<16)<<"名称"<10)<<"生产厂家"<10)<<"价格"<10)<<
                "商品类别"<10)<<"数量"<10)<<"入库时间"<10)<code<16)<name<10)<brand<10)<price
                <10)<type<10)<num<date.year<<"/"<date.month<<"/"<date.day<"确认修改吗?";
                cin>>c;
                while(c!='y'&&c!='n')
                {
                    cout<<"指令错误!!!!<请输入y/n>:";
                    cin>>c;
                }
                if(c=='y')
                {
                    cout<<"请输入商品名称:";
                    cin>>p->name;
                    cout<<"请输入生产厂家:"; 
                    cin>>p->brand;
                    cout<<"请输入商品价格:";
                    cin>>p->price;
                    cout<<"请输入商品数量:";
                    cin>>p->num;
                    cout<<"请输入入库时间(年/月/日):";
                    cin>>p->date.year>>p->date.month>>p->date.day;
                    cout<<"修改成功!"<else cout<<"取消成功!"<if(flag==0)
            {
                cout<<"对不起,您修改的货物不存在!!"<"您想要继续修改吗?(y/n):";
            cin>>c;
            while(c!='y'&&c!='n')
            {
                cout<<"指令错误!!!<请输入y/n>:"<"您想要继续修改吗?(y/n):";
                cin>>c;
            }
        }while(c=='y');
        cout<"信息编辑完毕"<"按任意键返回主菜单"<void GoodsManage::DeleteGoodsInfo()//定义商品信息删除函数
{
    Goods *q=head,*p,*tail=DeleteHead;
    p=new Goods;
    char c;
    string Dename;
    bool flag=0;
    while(tail->next!=NULL)//令tail指向链表中最后一个节点
        tail=tail->next;
    do
    {
        
        cout<<"请输入您要删除的商品名称:";
        cin>>Dename;
        while(q->next!=NULL&&q->next->name!=Dename)//直到q指向链表中最后一个节点或者找到相应名称的商品时跳出循环
            q=q->next;
        if(q->next!=NULL)//找到相应名称的商品
        {
            flag=1;
            cout<<"确认删除吗?";
            cin>>c;
            while(c!='y'&&c!='n')
            {
                cout<<"指令错误!!!!<请输入y/n>:";
                cin>>c;
            }
            if(c=='y')
            {  
                p=q->next;
                q->next=q->next->next;//q的下一个节点指向其后的节点
                tail->next=p;
                tail=p;
                tail->next=NULL;//在链表中删除指定节点
                DeleteAmount++;
                amount--;//商品量减一
                cout<<"删除成功!!"<else cout<<"取消成功!!!"<if(flag==0)
        {
            cout<<"对不起,您删除的商品不存在!!!"<"您想要继续删除吗?(y/n):";
        cin>>c;
        while(c!='y'&&c!='n')
        {
            cout<<"指令错误!!!<请输入y/n>:"<"您想要继续删除吗?(y/n):";
            cin>>c;
        }
        flag=0;
        q=head;//令q指向链表中第一个结点再次搜索相应名称的商品

    }while(c=='y');
    cout<"信息删除完毕"<"按任意键返回主菜单"<void GoodsManage::SearchByCode()//按照商品编号查找商品信息
{    
    Goods *p;
    bool flag;
    string FoundCode;
    p=head;
    flag=0;
    cout<<"请输入您要查找的商品编号:";
    cin>>FoundCode;
       while(p->next!=NULL)
        {
        
                p=p->next;
                if(p->code==FoundCode)//找到相应编号的商品
                {
                 flag=1;
                 cout<10)<<"编号"<16)<<"名称"<10)<<"生产厂家"<10)<<"价格"<10)<<
                "商品类别"<10)<<"数量"<10)<<"入库时间"<10)<code<16)<name<10)<brand<10)<price
                 <10)<type<10)<num<date.year<<"/"<date.month<<"/"<date.day<break;
                }
        }
       if(flag==0)
        {
            cout<<"对不起,您查询的商品不存在!!!"<"信息查找完毕"<"按任意键返回主菜单"<void GoodsManage::SearchByName()//按照商品名称查找商品信息
{    
    Goods *p;
    bool flag;
    string FoundName;
        p=head;
        flag=0;
        cout<<"请输入您要查找的商品名称:";
        cin>>FoundName;
        while(p->next!=NULL)
        {
        
                p=p->next;
                if(p->name==FoundName)//找到相应名称的商品
                {
                 flag=1;
                cout<10)<<"编号"<16)<<"名称"<10)<<"生产厂家"<10)<<"价格"<10)<<
                "商品类别"<10)<<"数量"<10)<<"入库时间"<10)<code<16)<name<10)<brand<10)<price
                <10)<type<10)<num<date.year<<"/"<date.month<<"/"<date.day<break;
                }
        }
        if(flag==0)
        {
            cout<<"对不起,您查询的商品不存在!!!"<"信息查找完毕"<"按任意键返回主菜单"<void GoodsManage::SearchByType()//按照商品类别查找商品信息
{
    
    Goods *p;
    bool flag;
    int FoundType;
        p=head;
        flag=0;
        cout<<"请输入您要查找的商品类别:";
        cin>>FoundType;
        while(p->next!=NULL)
        {
        
                p=p->next;
                if(FoundType==1&&p->type==Food)
                {
                 flag=1;
                 cout<10)<<"编号"<16)<<"名称"<10)<<"生产厂家"<10)<<"价格"<10)<<
                 "商品类别"<10)<<"数量"<10)<<"入库时间"<10)<code<16)<name<10)<brand<10)<price
                 <10)<type<10)<num<date.year<<"/"<date.month<<"/"<date.day<else if(FoundType==2&&p->type==Cosmetic)
                {
                 flag=1;
                  cout<10)<<"编号"<16)<<"名称"<10)<<"生产厂家"<10)<<"价格"<10)<<
                 "商品类别"<10)<<"数量"<10)<<"入库时间"<10)<code<16)<name<10)<brand<10)<price
                 <10)<type<10)<num<date.year<<"/"<date.month<<"/"<date.day<else if(FoundType==3&&p->type==Commodity)
                {
                 flag=1;
                  cout<10)<<"编号"<16)<<"名称"<10)<<"生产厂家"<10)<<"价格"<10)<<
                 "商品类别"<10)<<"数量"<10)<<"入库时间"<10)<code<16)<name<10)<brand<10)<price
                 <10)<type<10)<num<date.year<<"/"<date.month<<"/"<date.day<else if(FoundType==4&&p->type==Drink)
                {
                 flag=1;
                  cout<10)<<"编号"<16)<<"名称"<10)<<"生产厂家"<10)<<"价格"<10)<<
                 "商品类别"<10)<<"数量"<10)<<"入库时间"<10)<code<16)<name<10)<brand<10)<price
                 <10)<type<10)<num<date.year<<"/"<date.month<<"/"<date.day<if(flag==0)
        {
            cout<<"对不起,您查询的商品不存在!!!"<"信息查找完毕"<"按任意键返回主菜单"<void GoodsManage::SearchByBrand()//按照生产厂家查找商品信息
{
    
    Goods *p;
    bool flag;
    string FoundBrand;
        p=head;
        flag=0;
        cout<<"请输入您要查找的商品品牌:";
        cin>>FoundBrand;
        while(p->next!=NULL)
        {
        
                p=p->next;
                if(p->brand==FoundBrand)//找到相应生产厂家所对应的商品
                {
                 flag=1;
                  cout<10)<<"编号"<16)<<"名称"<10)<<"生产厂家"<10)<<"价格"<10)<<
                 "商品类别"<10)<<"数量"<10)<<"入库时间"<10)<code<16)<name<10)<brand<10)<price
                 <10)<type<10)<num<date.year<<"/"<date.month<<"/"<date.day<if(flag==0)
        {
            cout<<"对不起,您查询的商品不存在!!!"<"信息查找完毕"<"按任意键返回主菜单"<void GoodsManage::SellGoodsInfo()//定义商品出库函数
{
        int sellNum,year,month,day;
        double sellPrice,sum=0.0,profit=0.0;;
        char c;
        Goods *p;
        bool flag=0;
        string SellName;
        do
        {
            p=head->next;
            flag=0;
            cout<<"请输入您要出售的商品名称:";
            cin>>SellName;
            while(p->next!=NULL&&p->name!=SellName)
                p=p->next;
            if(p->name==SellName)
            {
                flag=1;
                cout<10)<<"编号"<16)<<"名称"<10)<<"生产厂家"<10)<<"价格"<10)<<
                "商品类别"<10)<<"数量"<10)<<"入库时间"<10)<code<16)<name<10)<brand<10)<price
                <10)<type<10)<num<date.year<<"/"<date.month<<"/"<date.day<"确认出售吗?";
                cin>>c;
                while(c!='y'&&c!='n')
                {
                    cout<<"指令错误!!!!<请输入y/n>:";
                    cin>>c;
                }
                if(c=='y')
                {
                    
                    cout<<"请输入出售的商品数量:";
                    cin>>sellNum;
                    if(sellNum<=p->num)//库存量充足
                    {
                        p->num=p->num-sellNum;//计算商品出库后商品的库存量
                        cout<<"请输入出售的商品价格:";
                        cin>>sellPrice;
                        cout<<"请输入出货日期:";
                        cin>>year>>month>>day;
                        sum=sellNum*sellPrice;
                        profit=sellNum*(sellPrice-p->price);
                        cout<<"此次销售额为: "<"此次利润为: "<"出货日期为:"<"/"<"/"<else
                    {
                        cout<<"库存不足!出库失败!"<else cout<<"取消成功!"<if(flag==0)
            {
                cout<<"对不起,您出售的货物不存在!!"<"您想要继续出售吗?(y/n):";
            cin>>c;
            while(c!='y'&&c!='n')
            {
                cout<<"指令错误!!!<请输入y/n>:"<"您想要继续出售吗?(y/n):";
                cin>>c;
            }
        }while(c=='y');
        cout<"出库完毕"<"按任意键返回主菜单"<void GoodsManage::DisplayGoodsInfo()//定义商品信息浏览函数
{
    Goods *p=head;
    cout<10)<<"编号"<16)<<"名称"<10)<<"生产厂家"<10)<<"价格"<10)<<
        "商品类别"<10)<<"数量"<10)<<"入库时间"<while(p->next!=NULL)//直到p指向链表中最后一个结点
    {
        p=p->next;
        cout<10)<code<16)<name<10)<brand<10)<price
        <10)<type<10)<num<date.year<<"/"<date.month<<"/"<date.day<"信息统计完毕"<"按任意键返回主菜单"<int main()//主函数
{
    int c;
    int i=0;
    bool flag=0;
    GoodsManage goods;//定义GoodsManage类对象
    goods.DisplayMainMenu();
    for(;;)
    {
        do
        {
            cin>>c;
            if(c>=0&&c<=9)//判断用户输入编号是否存在
                flag=1;
            else
            {
                cout<<"您输入的编号不存在!"<"请选择相应的数字进行操作:"<while(flag==0);//输入编号存在时跳出循环进行相应操作
        system("cls");//清屏
        switch(c)
        {
        case 1:goods.AddGoodsInfo();
            break;
        case 2:goods.EditGoodsInfo();
            break;
        case 3:goods.DeleteGoodsInfo();
            break;
        case 4:goods.SearchByCode();
            break;
        case 5:goods.SearchByName();
            break;
        case 6:goods.SearchByType();
            break;
        case 7:goods.SearchByBrand();
            break;
        case 8:goods.SellGoodsInfo();
            break;
        case 9:goods.DisplayGoodsInfo();
            break;
        case 0:exit(0);
            break;
        }
        system("cls");
        goods.DisplayMainMenu();
    }
    return 0;
}

把Date和Goods结构体定义为GoodsManage类的嵌套结构体。使得成为GoodsManage类的成员,通过GoodsManage类访问它们的成员变量。
只需要修改void GoodsManage::DisplayMainMenu()//定义主菜单函数以前的即可。
如下:

#include<iostream>
#include<string>
#include<iomanip>
 
using namespace std;
 
enum GoodsType//商品类别  
{
    Food=1,//食品
    Cosmetic,//化妆品
    Commodity,//日用品
    Drink//饮料
};
 
class Date//入库时间 
{
public:
    int year;
    int month;
    int day;
};
 
class Goods//商品基本信息
{
public:
    string code;//商品编号
    string name;//商品名称
    string brand;//生产厂家
    double price;//商品价格
    int num;//商品数量
    GoodsType type;//商品类别
    Date date;//入库时间
    Goods *next;
};
 
class GoodsManage//商品管理
{
public:
    GoodsManage();
    ~GoodsManage(){}
    void DisplayMainMenu();//主菜单显示
    void AddGoodsInfo();//添加商品信息
    void DisplayGoodsInfo();//浏览商品信息
    void SearchByCode();//按照商品编号搜索商品信息
    void SearchByName();//按照商品名称搜索商品信息
    void SearchByType();//按照商品类别搜索商品信息
    void SearchByBrand();//按照商品品牌搜索商品信息
    void EditGoodsInfo();//编辑商品信息
    void DeleteGoodsInfo();//删除商品信息
    void SellGoodsInfo();//出售商品信息
private:
    int amount;//商品量
    int DeleteAmount;
    Goods *head;
    Goods *DeleteHead;
};
 
GoodsManage::GoodsManage()//定义构造函数
{
    amount=0;
    DeleteAmount=0;
    head=new Goods;
    head->next=NULL;
    DeleteHead=new Goods;
    DeleteHead->next=NULL;
}

如果您希望使用类而不是结构体,则可以在类定义中声明其成员变量和方法,以替代结构体的数据成员和函数。例如,以下代码演示了如何使用类来代替一个简单的结构体:

class Person {
public:
    // 构造函数
    Person(std::string n, int a) : name(n), age(a) {}

    // 成员变量
    std::string name;
    int age;

    // 成员方法
    void say_hello() {
        std::cout << "Hello, my name is " << name << " and I am " << age << " years old." << std::endl;
    }
};

int main() {
    // 创建 Person 对象
    Person person("John", 30);

    // 访问成员变量
    std::cout << "Name: " << person.name << std::endl;
    std::cout << "Age: " << person.age << std::endl;

    // 调用成员方法
    person.say_hello();

    return 0;
}

上述示例中,我们使用类 Person 来替代了一个简单的结构体。类定义中的构造函数、成员变量和成员方法分别对应结构体的初始化、数据成员和函数。使用类的好处在于,它允许您封装数据和行为,并通过访问控制来保护您的代码。

以下答案基于ChatGPT与GISer Liu编写:
我们这里以您给出的结构体为例:

struct Date//入库时间 
{
    int year;
    int month;
    int day;
};

struct Goods//商品基本信息
{
    string code;//商品编号
    string name;//商品名称
    string brand;//生产厂家
    double price;//商品价格
    int num;//商品数量
    GoodsType type;//商品类别
    Date date;//入库时间
    Goods *next;
};

您可以将这个结构体定义为一个类的成员,从而避免使用结构体。下面是一个示例代码:

#include <string>

class Date {
public:
    int year;
    int month;
    int day;
};

class Goods {
public:
    std::string code;//商品编号
    std::string name;//商品名称
    std::string brand;//生产厂家
    double price;//商品价格
    int num;//商品数量
    enum GoodsType {//枚举类型,需要根据实际情况定义
        Type1,
        Type2,
        Type3
    } type;//商品类别
    Date date;//入库时间
    Goods *next;
};

在这个示例代码中,我们将Date和Goods定义为两个类,其中Date只包含三个整型成员变量,而Goods包含了您在结构体定义中使用的所有成员变量,以及一个指向Goods类型的指针成员变量。使用类的方式定义结构体可以更好地封装数据,提高代码的可读性和可维护性。

您好,关于您提出的不想使用结构体的问题,这里给你两个解决方法:
1、将结构体直接换位类,struct替换为class,然后放到下面的clas里面

class Date//入库时间 
{
    int year;
    int month;
    int day;
};
 
class Goods//商品基本信息
{
    string code;//商品编号
    string name;//商品名称
    string brand;//生产厂家
    double price;//商品价格
    int num;//商品数量
    GoodsType type;//商品类别
    Date date;//入库时间
    Goods *next;
};

2、将struct结构体中所有定义的变量属性和方法全部移到下面这个类里面去,这样就改动比较大,所有涉及到之前使用了结构体变量的地方,要修改为直接使用变量

在C++中,可以使用类来替代结构体,以实现相同的功能。要将结构体写入类中,可以使用类的成员变量来定义结构体中的字段,并使用类的成员函数来定义结构体中的函数。例如:


struct Point { int x; int y; };
class Point { public: int x; int y; void move(int dx, int dy); };
void Point::move(int dx, int dy) { x += dx; y += dy; }

如果你想在一个类中定义一个结构体,可以使用C++的内嵌结构体(nested struct)来实现,而不必在类的外部定义一个独立的结构体。

下面是一个简单的示例代码,其中定义了一个名为Person的类,并在其中定义了一个内嵌结构体Address:

class Person {
public:
    // 内嵌结构体
    struct Address {
        std::string street;
        std::string city;
        std::string state;
        std::string zip;
    };
    
    // 类的成员函数
    void set_address(const Address& addr) {
        address_ = addr;
    }
    
private:
    Address address_;
};

在这个例子中,Address结构体嵌套在Person类中。你可以通过创建一个Person对象并调用其中的成员函数set_address来设置其Address成员的值。

Person person;
Person::Address address;

address.street = "123 Main St.";
address.city = "Anytown";
address.state = "CA";
address.zip = "12345";

person.set_address(address);

这样就完成了在类中定义结构体的过程,不需要在类外部单独定义一个结构体。

示例:

class Person {
public:
    // 构造函数
    Person(std::string name, int age, std::string address)
        : name_(name), age_(age), address_(address) {}

    // 成员函数
    void print_info() {
        std::cout << "Name: " << name_ << std::endl;
        std::cout << "Age: " << age_ << std::endl;
        std::cout << "Address: " << address_ << std::endl;
    }

private:
    // 成员变量
    std::string name_;
    int age_;
    std::string address_;
};

不是很明白你题目意思。结构体和类其实是一回事,结构体可以看成所有成员为public的类,所以struct可以直接换成class,然后把成员放入public里