Escolar Documentos
Profissional Documentos
Cultura Documentos
Protásio
Estruturas
▪ É uma coleção de um ou mais tipos de
elementos denominados de membros,
cada um dos quais pode ser um tipo de
dado diferente.
▪ Diferença entre Array e Estrutura
Array ... Mesmo tipo
de dado
struct nome_da_estrutura
{
tipo_de_dado membro1;
tipo_de_dado membro2;
:
tipo_de_dado membro_n;
};
struct aluno
{
char nome_completo[100];
char curso[50];
int matricula;
float CRE;
};
struct aluno
{
char nome_completo[100];
char curso[50];
int matricula;
float CRE;
} joao, maria, jose;
struct aluno
{
char nome_completo[100];
char curso[50];
int matricula;
float CRE;
};
struct aluno
{
char nome_completo[100];
char curso[50];
int matricula;
float CRE;
};
struct aluno{
char nome_completo[100];
char curso[50];
int matricula;
float CRE;
};
int main(){
aluno joao = {"Joao de Jesus", "Engenharia", 10020, 7.4};
aluno maria, jose;
struct aluno{
char nome_completo[100];
char curso[50];
int matricula;
float CRE;
};
int main()
{
aluno joao = {"Joao de Jesus", "Engenharia", 10020, 7.4};
aluno maria, jose;
aluno *joaoponteiro;
joaoponteiro = &joao;
struct TESTE{
int num;
char ch;
};
int main(){
TESTE v1, v2;
v1.num = 2;
v1.ch = 'a';
v2.num = v1.num;
v2.ch = v1.ch;
cout << v1.num << " " << v1.ch << endl;
cout << v2.num << " " << v2.ch << endl;
}
struct TESTE{
int num;
char ch;
};
int main(){
TESTE v1, v2;
Todos os valores v1.num = 2;
dos membros de v1 v1.ch = 'a';
são atribuídos aos v2 = v1;
membros de v2,
respectivamente cout << v1.num << " " << v1.ch << endl;
cout << v2.num << " " << v2.ch << endl;
}
struct pessoal{
aninhadas char nome_completo[100];
char curso[50];
Estrutura geral
int matricula;
▪ Exemplo
};
struct professor{
pessoal dados;
int departamento;
};
int main(){
aluno joao;
professor jose;
int main()
{
ALUNO alunos[3];
struct ALUNO{
char nome_completo[100];
char curso[50];
int matricula;
float CRE;
};
Funções cujo
argumento é uma float obtemCRE(ALUNO x);
void incrementaCRE(ALUNO &x);
estrutura
int main(){
ALUNO joao = {"Joao de Jesus", "Engenharia", 10020, 7.4};
ALUNO maria = {"Maria Aparecida", "Fisica", 10031, 2.6};
struct VETOR{
Dados membros float x,y,z;
float modulo(){
Função membro return sqrt(pow(x,2)+pow(z,2)+pow(z,2));
}
};
int main(){
VETOR a = {2.1, 3, 5.6};
a é uma variável da estrutura
VETOR. cout << a.modulo() << endl;
a também é chamado de }
um OBJETO do tipo VETOR.
struct VETOR{
float modulo(){
return sqrt(pow(x,2)+pow(z,2)+pow(z,2));
}
private:
Membros privados float x,y,z;
};
int main(){
VETOR a;
a.set(2.1, 3, 5.6);
▪ Disposição na memória
i
Byte 0 Byte 1
cr
0 1 ...
▪ Exemplo
▪ enun {VERMELHO, VERDE, AZUL};
int main(){
int temp;
cout << "Digite 0, 1 ou 2: " << endl;
cin >> temp;
int main(){
flutuante temp = 1.2;
cout << temp << endl;
}