Professional Documents
Culture Documents
Punteros - Referencias
Clases
Y ahora, a trabajar!
Propiedades basicas
int x = 0;
x += 1 0 ; // e q u i v a l e a x=x +10;
x++; // e q u i v a l e a x=x +1;
Nombre: x
Propiedades basicas
int x = 0;
x += 1 0 ; // e q u i v a l e a x=x +10;
x++; // e q u i v a l e a x=x +1;
Nombre: x
Tipo: int
Propiedades basicas
int x = 0;
x += 1 0 ; // e q u i v a l e a x=x +10;
x++; // e q u i v a l e a x=x +1;
Nombre: x
Tipo: int
Valor: para este ejemplo, 11
Propiedades basicas
int x = 0;
x += 1 0 ; // e q u i v a l e a x=x +10;
x++; // e q u i v a l e a x=x +1;
Nombre: x
Tipo: int
Valor: para este ejemplo, 11
Su ubicacion en la memoria (?)
int h = 4;
f o r ( i n t i =0; i <10; i ++){
h = 25;
}
c o u t << h << e n d l ;
int h = 4;
f o r ( i n t i =0; i <10; i ++){
int g = 75;
}
c o u t << g << e n d l ; // e s t o e s t a b i e n ??
int h = 4;
f o r ( i n t i =0; i <10; i ++){
double h = 2 9 ; // c o m p i l a ??
}
c o u t << h << e n d l ;
int h = 4;
f o r ( i n t i =0; i <10; i ++){
double h = 2 9 ; // c o m p i l a ??
}
c o u t << h << e n d l ;
Veamos un mapa
Punteros
1101
777
int * p
pos: 1101
int **q
Ejemplos
// p : en e l s t a c k , pk l o va a a p u n t a r
void f u n c A b u r r i d a ( ) {
long k = 1 2 3 4 ;
long pk ; // pk > ???
pk = &k ; // pk > k
c o u t << k << == << pk << == << &k e n d l ;
pk = new long ; // nuevo l o n g en heap
pk = 1 0 ;
( pk)++; // e s e l o n g a h o r a v a l e 11
d e l e t e pk ;
}
Ejemplos (2)
void funcMasAburrida ( ) {
s t r u c t punto { i n t x ; i n t y ; } ;
punto p = new punto ;
c o u t << p>x << == ( p ) . x << e n d l ;
}
Referencias
Ejemplos (2)
void funcMortalmenteAburrida ( ) {
int olerante = 10;
int & loco = olerante ;
i n t & e r r o r D e C o m p i l a c i o n ; //No i n i c i a l i z a d o !
o l e r a n t e += 1 0 ;
l o c o += 1 0 ; // e s una r e f , p e r o s e u s a i g u a l
// c u a n t o v a l e n a h o r a ?
}
long & f u n c M o r t a l ( ) {
long a n i z a = 1 0 ;
return aniza ; // boom . . . p o r que ?
}
Veremos...
El const
Constructor por defecto
Constructor por copia
Listas de inicializacion
Operador de asignacion
Destructor
Veremos...
El const
Constructor por defecto
Constructor por copia
Listas de inicializacion
Operador de asignacion
Destructor
Pero primero, un poco de motivacion
Ejemplo estructuras
const!
const y punteros
const y punteros
const - conversiones
const - conversiones
const - conversiones
const - ejemplos
// Una c o n s t a n t e n u m e r i c a
const double PI = 3 . 1 4 1 6 ;
// C a l c u l a e l a r e a : no m o d i f i c a a c
double a r e a ( const C i r c u l o & c ) ;
// E l punto d e v u e l t o s e m i r a y no s e t o c a .
// Tampoco m o d i f i c a a l c i r c u l o
const punto2d & C i r c u l o : : c e n t r o ( ) const ;
// e s t e punto s i s e puede m o d i f i c a r ! ( o j o ! )
// tampoco m o d i f i c a a l c i r c u l o
punto2d & C i r c u l o : : c e n t r o ( ) const ;
c l a s s GatoMontes {
public :
int vidas ();
};
// e s t o no va a c o m p i l a r . . . p o r ?
v o i d m o s t r a r V i d a s ( const GatoMontes& g ) {
c o u t << g . v i d a s ( ) << e n d l ;
}
c l a s s GatoMontes {
public :
i n t v i d a s ( ) const ;
};
// Ahora s i !
v o i d m o s t r a r V i d a s ( const GatoMontes& g ) {
c o u t << g . v i d a s ( ) << e n d l ;
}
c l a s s Test {
// Aca tenemos T e s t ( ) i m p l i c i t o
int a ;
};
c l a s s Test2 {
// Al d e c l a r a r e s t e , no hay
// c o n s t r u c t o r T e s t 2 ( ) i m p l i c i t o
T e s t 2 ( long l ) ;
};
class copiable {
c o p i a b l e ( const c o p i a b l e& c2 ) {
t h i s >a = NULL ;
// Lo tenemos que c o p i a r
// e x p l i c i t a m e n t e ( s i c o r r e s p o n d e )
i f ( c2 . a != NULL)
t h i s >a = new i n t ( c2 . a ) ;
}
int a ;
};
Listas de inicializacion
Sintaxis usada en los constructores para construir variables
miembro (algunas o todas)
Se vuelven indispensables cuando tenemos miembros sin
constructor por defecto
// E s f e r a . h
c l a s s punto3d {
double x , y , z ;
punto3d ( double x0 , double y0 , double z0 )
: x ( x0 ) , y ( y0 ) , z ( z0 ) { }
};
class esfera {
double r a d i o , punto3d c e n t r o ;
e s f e r a ( ) ; // n e c e s i t a l i s t a !
};
Memoria Dinamica en C++
Memoria en C++ estructuras de datos
Punteros - Referencias const
Clases Construccion
Y ahora, a trabajar! Operador =
// E s f e r a . cpp
esfera : : esfera ()
: radio (1) , centro (0 ,0 ,0)
{
}
// o e q u i v a l e n t e m e n t e :
esfera : : esfera ()
: centro (0 ,0 ,0)
{
radio = 1;
}
c l a s s Pp {
private :
i n t &v a l o r ;
public :
// Compila ?
Pp ( i n t &v )
{
valor = v ;
}
i n t sumarYRetornar ( ) ;
};
c l a s s Pp {
private :
i n t &v a l o r ;
public :
// Ahora s .
Pp ( i n t &v ) : v a l o r ( v ) ;
i n t sumarYRetornar ( ) ;
};
Operador de asignacion
Operador de asignacion
Destructor
Destructor
class leaker {
int p ;
l e a k e r ( i n t tam )
{ p = new i n t [ tam ] ; }
l e a k e r ( ) { / Dejo l o s p i n t s en e l l i m b o / }
};
Destructor
class limpita {
int p ;
l i m p i t a ( i n t tam ) { p = new i n t [ tam ] ; }
l i m p i t a () { delete [ ] p ; }
// d e l e t e p h a c e l o mismo . . .
// . . . p e r o dep e n d e d e l c o m p i l a d o r ! !
// d e l e t e [ ] e s e l c o r r e c t o
}
Regla de tres
Clase ListaAlumno
t y p e d e f u n s i g n e d l o n g Nat ;
s t r u c t Alumno {
i n t LU , t e l ;
};
c l a s s ListaAlumnos {
// C o n s t r u c t o r e s . . .
// O b s e r v a d o r e s
Nat l o n g i t u d ( ) c o n s t ;
c o n s t Alumno & i e s i m o ( Nat i ) c o n s t ;
Alumno & i e s i m o ( Nat i ) ;
// O p e r a c i o n e s
v o i d a g A d e l a n t e ( c o n s t Alumno & e l e m ) ;
v o i d a g A t r a s ( c o n s t Alumno & e l e m ) ;
v o i d e l i m i n a r ( Nat i ) ;
private :
s t r u c t Nodo { // . . .
}
};