Before discussing classes, this lesson will be a preface to statistics structures similar to classes. Structures are a means of storing numerous unusual values in variables of potentially unusual types below the identical name. This makes it a more modular program, which is easier to alter as its design makes things more compacted. Structs are normally helpful when a lot of records need to be grouped mutually -- such as, they can be used to hold records from a database or to store information regarding contacts in an address book. In the contacts example, a struct could be used that would hold all of the information regarding a single contact--name, address, phone number, as well as so forth.
The format for defining a structure is
Code:
struct Tag {
Members
}; Where label is the given name of the complete type of structure as well as Members are the variables in the struct. To essentially make a single structure the syntax is
Code:
struct Tag name_of_single_structure;
To access a variable of the structure it goes
Code:
name_of_single_structure.name_of_variable;
For example:
Code:
struct example {
int x;
};
struct example an_example; //Treating it like a normal variable type
an_example.x = 33; //How to access it's members Here is an example program:
Code:
struct database {
int id_number;
int age;
float salary;
};
int main()
{
database employee; //There is now an employee variable that has modifiable
// variables inside it.
employee.age = 22;
employee.id_number = 1;
employee.salary = 12000.21;
} The struct database states that database has three variables in it, age, id_number, along with salary. You can make use of record like a variable type like int. You can make an member of staff with the database type as I did on top of. Then, to alter it you call the whole thing with the 'employee.' in front of it. You can also return structures from functions by defining their return type as a structure type. For example:
I will speak only a little bit regarding unions also. Unions are like structures apart from that all the variables share the similar memory. When a union is affirmed the compiler allocates sufficient memory for the biggest data-type in the union. It’s like a huge storage chest where you can store one huge item, or else a small item, but not at all the both at the equal time.
The '.' operator is used to access unusual variables in a union also.
As a final note, if you desire to contain a pointer to a structure, to really access the information stored within the structure that is pointed to, you make use of the -> operator in place of the . operator. All points about pointers still apply.
A quick example:
Code:
#include <iostream>
using namespace std;
struct xampl {
int x;
};
int main()
{
xampl structure;
xampl *ptr;
structure.x = 12;
ptr = &structure; // Yes, you need the & when dealing with structures
// and using pointers to them
cout<< ptr->x; // The -> acts somewhat like the * when used with pointers
// It says, get whatever is at that memory address
// Not "get what that memory address is"
cin.get();
}
Bookmarks