0
Follow
0
View

How do I write a structure into a class

dgqs2000 注册会员
2023-02-27 22:51

example:

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_;
};
zhh1127 注册会员
2023-02-27 22:51

I'm not sure what you mean. struct and class are the same thing. A struct can be regarded as a class whose members are public. Therefore, a struct can be replaced directly with a class and put its members into public

.
ricesky 注册会员
2023-02-27 22:51

In C++, you can use classes instead of structs to achieve the same functionality. To write a structure to a class, you can use member variables of the class to define fields in the structure and member functions of the class to define functions in the structure. For example:


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; }
dujunting80 注册会员
2023-02-27 22:51

If you want to define a structure in a class, you can do so using C++ nested structures(nested struct) instead of defining a separate structure outside the class.

Here is a simple example of code that defines a class named Person with an embedded structure 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_;
};

In this example, the Address structure is nested within the Person class. You can set the value of its Address member by creating a Person object and calling its member function set_address.

Person person;
Person::Address address;

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

person.set_address(address);

This completes the process of defining a structure in the class, without the need to define a separate structure outside the class.

cuibojason 注册会员
2023-02-27 22:51

The following answer is based on ChatGPT and GISer Liu:
We will take the structure you gave here as an example:

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;
};

You can avoid using structs by defining this struct as a member of a class. Here is a sample code:

#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;
};

In this example code, we define Date and Goods as two classes, where Date contains only three integer member variables, and Goods contains all the member variables you use in the struct definition, as well as a pointer member variable to the type of Goods. Defining structures in class terms can better encapsulate data and improve code readability and maintainability.

x309007873 注册会员
2023-02-27 22:51

How should we use the enumerated types below? was not declared in this scope

Was not declared in this scope

ddk35785 注册会员
2023-02-27 22:51

Hello, here are two solutions to your problem of not wanting to use structs:
1. Replace the struct directly with a class and place it in the following 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. Move all the variable attributes and methods defined in the struct structure to the following class, so that the changes are relatively large. All the aspects related to the use of structure variables before should be modified to directly use the variable

.
hxpflch 注册会员
2023-02-27 22:51

emm, then you say, write the structure into the class, this is not in?

hemyname 注册会员
2023-02-27 22:51

If you want to use a class instead of a structure, you can declare its member variables and methods in the class definition instead of the structure's data members and functions. For example, the following code demonstrates how to use a class instead of a simple struct:

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;
}

In the example above, we used the class Person instead of a simple structure. The constructor, member variable, and member method in the class definition correspond to the initializer, data member, and function of the structure, respectively. The advantage of using classes is that they allow you to encapsulate data and behavior and protect your code through access control.

loadstar_kun 注册会员
2023-02-27 22:51
< div class = "md_content_show e397 data - v - 3967" = "" >

run successful:

img

About the Author

Question Info

Publish Time
2023-02-27 22:51
Update Time
2023-02-27 22:51

Related Question