Structures \m/

Whats the use of structures in c++ programming?
Structures are classes with slightly different default access modifiers.

Usually they are used as aggregate classes, or helper structs/tags.
Structures comes from C and is used to group data together.
1
2
3
4
5
6
// Example of a Point struct
struct Point
{
	int x;
	int y;
};

You can now create objects of type Point.
1
2
3
4
Point p; // Create a Point variable named p.
p.x = 2; // Set the x coordinate to 2.
p.y = 4; // Set the y coordinate to 4.
std::cout << p.x << ' ' << p.y << std::endl; // Print the coordinates. 

In C++ a struct is a class so you can do everything with a struct that you can do with a class.
for example this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <sstream>
#include <stdlib.h>
using namespace std;
struct person
{
    string name;
    int age;
};
int main()
{
   struct person x;
   cout<<"Enter Name: ";
   getline(cin,x.name);
   cout<<"Enter Age: ";
   cin>>x.age;
   system("cls");
 cout<<"Name:"<<x.name<<endl;
 cout<<"Age:"<<x.age;
}

why should i use structure if i can use normal variables? i mean just create a new variable for the structure tag
ok .. maybe i should study class first hahaha
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <sstream>

struct person
{
    std::string name;
    int age;
};

std::istream& operator>>(std::istream& in, person& var)
{
    in >> var.age;
    std::getline(in >> std::ws, var.name);
    return in;
}

std::ostream& operator<<(std::ostream& out, const person& var)
{
    return out << "Name: " << var.name << '\n' << " Age: " << var.age;
}


int main()
{
    person x;
    std::cout << "Enter age and a name: ";
    std::cin >> x;
    std::cout << x;
}
Enter age and a name: 18 John Doe
Name: John Doe
 Age: 18


You can create a container of structs easily.
Structs/classes support member funtions.

Actually only reason that you can easily assign/use strings is because string is a class
The benefits are probably more obvious if you have many persons in your program.
1
2
3
4
// Defining three persons using struct
person davez;
person miinipaa;
person peter;

1
2
3
4
5
6
7
// Defining three persons without struct
string davezName;
int davezAge;
string miinipaaName;
int miinipaaAge;
string peterName;
int peterAge;

As you can see you get much more variables to keep track of.

If you want to have an array of persons you would have to have one array for each variable that a person has if you don't use struct.

1
2
// 10 persons using struct
person persons[10];

1
2
3
// 10 persons without struct
string personNames[10];
int personAges[10];


It also gets much easier if you want to pass a person to a function. Using struct you just have to pass 1 argument but if you don't use struct you would have to pass each variable separately and it would be easier to make mistakes, passing variables that belongs to different persons by mistake.
Last edited on
ooooh i see so one of the benefits of using struct is so that i can easily read the code ?
Yes.
@Peter87 what is the advantage and disadvantage of creating a struct in outside and inside the class? and there diff. since struct has a public default access
Topic archived. No new replies allowed.