3
I wonder if there’s a way I could create a class, to store 2 variables, for example, a Vector2, where I would instantiate and use it like this:
Vector2 tile;
int posX, posY;
posX = tile.x;
posY = tile.Y;
or something like.
3
I wonder if there’s a way I could create a class, to store 2 variables, for example, a Vector2, where I would instantiate and use it like this:
Vector2 tile;
int posX, posY;
posX = tile.x;
posY = tile.Y;
or something like.
5
In C, with struct (C has not class), do so
/* define struct Vector2 com dois elementos */
struct Vector2 {
int x;
int y;
};
/* define tile como uma variavel pertencente ao tipo struct Vector2 */
struct Vector2 tile;
/* atribui valores aos membros de tile */
tile.x = 42;
tile.y = -1;
0
In C, Classes are not used, as is done in C++.
When we want to store data of different types, we use struct:
struct estrutura {
int nBase;
int nAltura;
double fArea; };
and then we can create structures like struct estrutura:
estrutura triangulo1;
estrutura retangulo1;
and access their independent elements to each other:
triangulo1.nBase = 10;
triangulo1.nAltura = 5;
triangulo1.fArea = triangulo1.nBase * triangulo1.nAltura / 2.0;
// triangulo1.fArea terá valor de 7.5
retangulo1.nBase = 7;
retangulo1.nAltura = 3;
retangulo1.fArea = nBase * nAltura;
// retangulo1.fArea terá valor de 21.0
I hope I’ve helped!
Hmmm ... the type estrutura does not exist without a typedef. The comment concerning fArea has a value different from 25.
Browser other questions tagged c variables abstract-classes
You are not signed in. Login or sign up in order to post.
Some people like to use
typedefto specify another name for the type without having to typestruct. I see no great advantage in that (writestructdoes not cost and can make the program more readable), so do not use thetypedefto that effect.– pmg