Now we have to learn about classes. But it is better to get an overture of structure before knowing the classes. When we accumulate dissimilar variables having unlike types by giving them one name then it is called Structure. It is trouble-free and mostly used in database programming.
The syntax of structure is given here:
Members
};
Tag is used for structure. For making one structure, the format is written here:
Tag name_of_single_structure;
In order to contact a variable in the structure, you have to write the given pattern:
name_of_single_structure.name_of_variable;
For instance:
int y;
};
Example an_example; //taking it as a common variable type
an_example.y = 33;
Another example is given here:
int id_number;
int age;
float salary;
};
int main()
{
database employee; //Now it has an employee variable that has changable
// variables inside it.
employee.id_number = 2;
employee.salary = 120570.21;
}
Here database is consists of 3 variables which are id_number, age and salary. It is possible for you to perform various functions with this database. You can bring into play it as variable type. Even you cam make a worker. Then you can give information about worker, in order to bring change. It is also possible to get back structures from functions. Example is given here:
Another thing which is similar to structures is unions. The difference is that the whole family of variables has to use one similar memory. When we announce a union then compiler allots a wide range of memory for union. It is a gigantic storage area which restricts you to either use bulky items or the miniatures. But it is not allowed to store different size of data in one memory/
Last but not least, if it is necessary for you to create pointer for getting data that is saved in the mentioned structure, you have to write ->. Even now, all the steps related to pointer are applied.
For instance:
using namespace std;
struct xampl {
int y;
};
int main()
{
xampl structure;
xampl *ptr;
structure.y = 12;
ptr = &structure; // It is true that you have to think about the & when dealing with structures
cout<< ptr->y; // The -> acts a bit like the * when used with pointers
// take whatever is at that memory address
// Not "get what that memory address is"
cin.get();
}
cin.get();
}
Leave a Reply