#ifndef _MYHEADER_H
#define _MYHEADER_H
#include <stdio.h> // include our extern header files here
#include <iostream>
using namespace std;
struct tMyStruct // declare a new struct
{
int id;
char grade;
bool active;
};
#endif
#include "myheader.h" // include our header file
int main()
{
tMyStruct player;
player.id = 10;
player.active = true;
player.grade = 'a';
cout << player.id << " " << player.active << " " << player.grade;
getchar(); // wait until a key is pressed
return 0;
}
/*
Learn how to use a Struct to ease and systemize your programming.
A structure type is a user-defined composite type. It is composed of fields or
members that can have different types. In C++, a structure is the same as a
class except that its members are public by default.(I will explain class
in the next tutorial)
Its form is the following:
struct struct_type_name
{
type1 member1;
type2 member2;
type3 member3;
.
.
};
or
It's form can also be like this:
struct model_name{
type1 element1;
type2 element2;
type3 element3;
.
.
} object_name;
where model_name is a name for the model of the structure type and the
optional parameter object_name is a valid identifier (or identifiers) for
structure object instantiations. Within curly brackets { } they are the
types and their sub-identifiers corresponding to the elements that compose
the structure.
After you have declared your struct:
struct tMyStruct // declare tMyStruct struct type
{
int id; // declare member types
char grade;
bool active;
};
You can use your struct like this:
Declare a variable containing all the members/elements of your struct.
tMyStruct myVariable; // declare our new variable
myVariable will now contain all the members in the struct.
Example of use:
int id = myVariable.id;
char grade = myVariable.grade;
bool active = myVariable.active;
*/
Download Visual C++ Source Code
I hope you found this c++ tutorial useful!
Don't forget to mention Apron Tutorials in your References!