Structs

What is the purpose or point of using Struct in coding?
furry guy there i nothing display there when searching
@OP
Google (or other search engine of your choice) "What is the purpose or point of using Struct in coding?" or better still "What is the purpose or point of using struct in coding C++?"

https://www.learncpp.com/cpp-tutorial/structs/
A struct is a data structure. They're used for storing multiple attributes of an object. For example:

1
2
3
4
5
struct Person
{
     string name {}; // Name attribute
     int address {};  // Address attribute
};

Then any Person object will have two attributes– Name and Address. Name is a character string and Address is a number. You would also have "get" and "set" functions to give any instances of the Person struct a name and an address.
e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct Person
{
     string name {}; // Name attribute
     int address {};  // Address attribute
     
     void setName (string nameIn); // Name setter function prototype
     string getName (); // Name getter function prototype
};

void Person::setName (string nameIn)
{
     name = nameIn;
}

string Person::getName ()
{
     return name;
}

Just examples of what you might do with a struct. They're very useful for making inventory lists and storing data about certain items, and things like that.

Also pay attention to what @againtry said. Google is your friend when learning about C++ (or any programming language, for that matter).

Hope this helped!
max
Last edited on
Topic archived. No new replies allowed.