Estructura_clase
description
Transcript of Estructura_clase
![Page 1: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/1.jpg)
Estructura_clase
Ing. Eduard Antonio Lozano Córdoba
![Page 2: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/2.jpg)
ESTRUCTURAS ANIDADAS
• Las estructuras anidadas ahorran tiempo en la escritura de programas que utilizan estructuras similares.
![Page 3: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/3.jpg)
• Se han de definir los miembros comunes sólo una vez en su propia estructura y a continuación utilizar esa estructura como un miembro de otra estructura.
ESTRUCTURAS ANIDADAS
![Page 4: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/4.jpg)
• Gráficamente se podrían mostrar estructuras anidadas
ESTRUCTURAS ANIDADAS
![Page 5: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/5.jpg)
• Este ejemplo define la estructura persona, en la que el miembro fecha_nacimiento es a su vez una estructura.
Para acceder, por ejemplo, al miembro anyo de persona escribiremos,
![Page 6: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/6.jpg)
Ejercicio _ parte_1
#include <stdio.h>struct fecha1. {2. int dia, mes, anyo;3. } tfecha;
4. struct ficha5. {6. char nombre[40];7. char direccion[40];8. long telefono;9. struct fecha fecha_nacimiento;10.} tficha;
![Page 7: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/7.jpg)
Ejercicio _ parte_2
1. void main ( )2. {3. struct ficha persona, otra_persona;4. ///* Introducir datos */5. printf("Nombre: ");6. gets (persona. nombre); 7. printf("Dirección: ");8. gets (persona.direccion );9. printf ( "Teléfono: " );10.scanf ("%ld", &persona.telefono) ;11.printf ("Fecha de nacimiento: \n" );12. printf (" Día: " ) ;13.scanf ( "%d", &persona. fecha_nacimiento.dia) ; 14.printf(" Mes: ");15.scanf ( "%d", &persona.fecha_nacimiento.mes) ;16.printf(" Año: ");17.scanf ( "%d" , &persona.fecha_nacimiento.anyo) ;
![Page 8: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/8.jpg)
Ejercicio _ parte_3
1. ///* Copiar una estructura en otra * /
2. otra_persona = persona;
3. /* Escribir los datos de la nueva estructura */
4. printf("\n\n");
5. printf ( "Nombre: %s\n", otra_persona.nombre);
6. printf ( "Dirección: %s\n", otra_persona.direccion);
7. printf ( "Teléfono: %ld\n" , otra_persona. telefono) ;
8. printf ("Fecha de nacimiento: \n,") ;
9. printf(" Día: %d\n", otra_persona.fecha_nacimiento.dia);
10.printf (" Mes: %d\n", otra_persona.fecha_nacimiento.mes);
11.printf ( " Año: %d\n" , otra_persona.fecha_nacimiento.anyo);
12.}
![Page 9: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/9.jpg)
EJERCICIO
![Page 10: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/10.jpg)
![Page 11: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/11.jpg)
![Page 12: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/12.jpg)
struct fecha{int mes, dia, anyo;};
struct tiempo{int horas, minutos;};
struct registro{char numero-cuenta;float cantidad;int tipo-operación;struct fecha f;struct tiempo t;};
![Page 13: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/13.jpg)
![Page 14: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/14.jpg)
UNIONES
• Una unión es una variable que puede contener, en distintos instantes de la ejecución del programa, datos de diferentes tipos. Esto permite manipular diferentes tipos de datos utilizando una misma zona de memoria, la reservada para la variable unión.
![Page 15: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/15.jpg)
La cantidad de memoria reservada para una unión es igual a la anchura de la variable más grande.
En el tipo union, cada uno de los miembros dato comparten memoria con los otros miembros de la unión. La cantidad total de memoria utilizada por la unión comparte es de 8 bytes, ya que el elemento double es el miembro dato mayor de la unión.
Una razón para utilizar una unión es ahorrar memoria. En muchos programas se deben tener varias variables, pero no necesitan utilizarse todas al mismo tiempo. Considérese la situación en que se
necesitan tener diversas cadenas de caracteres de entrada. Se pueden crear varios arrays de cadenas e caracteres, tales como las siguientes:
Estas tres variables ocupan 240 bytes de memoria. Sin embargo, si su programa no necesita utilizar las tres variables simultáneamente, ¿por qué no permitirle compartir la memoria utilizando una unión?
Cuando se combinan en el tipo union frases, estas variables ocupan un total de sólo 80 bytes.
![Page 16: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/16.jpg)
Unión y estructura
![Page 17: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/17.jpg)
Ejercicio• #include "iostream.h"
• union tunion• {• int a;• int b;• int c;• };
• void main ( )• {• union tunion var1;• var1.a = 10;• var1.b = 100;• var1.c = 1000;• cout<<var1.a<<endl;• cout<<var1.b<<endl;• cout<<var1.c<<endl;• }
![Page 18: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/18.jpg)
Análisis del ejercicio anterior
![Page 19: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/19.jpg)
El tamaño de una estructura
• sizeof
![Page 20: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/20.jpg)
![Page 21: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/21.jpg)
GRACIAS
![Page 22: Estructura_clase](https://reader036.fdocumento.com/reader036/viewer/2022062519/56814fa0550346895dbd5eb0/html5/thumbnails/22.jpg)
• matrices de estructuras