Professional Documents
Culture Documents
1. [001.txt] Sobrecarga
Define una clase que represente a un array de enteros, usando memoria dinamica
Agrega un constructor que permita que si das un tamaño, sea el tamaño inicial, y que, si no lo das, sea 10.
Crea un constructor de copias
Redefine el operador de asignación
Define el destructor.
Haz que el operador - elimine todas las instancias de un entero si esta presente
Haz que el operador += agregue al final un entero
Haz que el operador == compare correctamente dos objetos
Haz que el operador [] devuelva una referencia al elemento indicado
Sobrecarga el insertor para que muestre los elementos y la posicion que ocupan
Sobrecarga el extractor para que lea pares de posición y número hasta que la posición sea negativa.
Solución:
Página 1 de 11
Sobrecarga
}
void Array : : a g r e g a r ( int n ) {
l e n ++;
i f ( l e n >= s i z e ) {
s i z e += INC SIZE ;
int ∗ newdata = new int [ s i z e ] ;
f or ( int n=0; n<l e n ; n++){
newdata [ n ] = data [ n ] ;
}
delete [ ] data ;
data = newdata ;
}
data [ l e n −1] = n ;
}
void Array : : a g r e g a r ( const Array &c ) {
int ∗ newdata ;
int n ;
i f ( l e n+c . getLen ( ) > s i z e ) {
s i z e += l e n ;
newdata = new int [ s i z e ] ;
f or ( n=0; n<l e n ; n++){
newdata [ n ] = data [ n ] ;
}
f or ( int m=0 ; n<s i z e ; n++, m++){
newdata [ n ] = c . data [m] ;
l e n ++;
}
delete [ ] data ;
data = newdata ;
} else {
n=l e n ;
f or ( int m=0 ; m<c . getLen ( ) ; n++, m++){
data [ n ] = c . data [m] ;
l e n ++;
}
}
}
int Array : : g e t S i z e ( ) const {
return s i z e ;
}
int Array : : getLen ( ) const {
return l e n ;
}
Array& Array : : operator = ( const Array &b ) {
i f ( t h i s == &b ) {
return ∗ t h i s ;
}
int ∗ newdata ;
len = b . len ;
size = b. size ;
newdata = new int [ s i z e ] ;
f or ( int n=0; n<l e n ; n++){
newdata [ n ] = b . data [ n ] ;
}
delete [ ] data ;
data = newdata ;
return ∗ t h i s ;
Página 2 de 11
Sobrecarga
}
ostream & operator << ( ostream & out , const Array & c ) {
f or ( int n=0; n<c . getLen ( ) ; n++){
out << c [ n ] << ” , ” ;
}
out << e n d l ;
return out ;
}
void Array : : operator += ( int n ) {
agregar (n) ;
}
bool operator == ( const Array & a , const Array & b ) {
i f ( a . s i z e != b . s i z e ) return f a l s e ;
i f ( a . l e n != b . l e n ) return f a l s e ;
f or ( int c o n t =0; cont<a . l e n ; c o n t++){
i f ( a . data [ c o n t ] != b . data [ c o n t ] ) {
return f a l s e ;
}
}
return true ;
}
Array operator − ( const Array & a , int n ) {
Array tmp ( a . s i z e ) ;
f or ( int c o n t =0; cont<a . l e n ; c o n t++){
i f ( a . data [ c o n t ] != n ) {
tmp . a g r e g a r ( a . data [ c o n t ] ) ;
}
}
return tmp ;
}
Array operator + ( const Array &a , const Array &b ) {
Array tmp ;
tmp . a g r e g a r ( a ) ;
tmp . a g r e g a r ( b ) ;
return tmp ;
}
int & Array : : operator [ ] ( int i n d e x ) const {
return ∗ ( data+i n d e x ) ;
// o b i e n :
// r e t u r n d a t a [ i n d e x ] ;
}
int main ( void ) {
Array a=Array ( ) ; Array b=Array ( ) ; Array c=Array ( ) ;
a . agregar (1) ; a . agregar (10) ; a . agregar (100) ;
b . agregar (2) ; b . agregar (20) ; b . agregar (200) ;
c . agregar (3) ; c . agregar (30) ; c . agregar (300) ;
c o u t << ”a+b : ” << a+b << e n d l ;
c o u t << ”b+c : ” << b+c << e n d l ;
c o u t << ”a+c : ” << a+c << e n d l ;
c o u t << ”a+b+c : ” << a+b+c << e n d l ;
Array x = Array ( ) ;
x . agregar (1) ; x . agregar (2) ; x . agregar (3) ; x . agregar (4) ;
Array n = x −2;
c o u t << n << e n d l ;
Array y = Array ( ) ;
y . agregar (1) ; y . agregar (2) ; y . agregar (9) ; y . agregar (4) ;
c o u t << ( x == a ) << e n d l ; c o u t << ( a == x ) << e n d l ;
Página 3 de 11
Sobrecarga
2. [002.txt] Tenemos un tipo Vehiculo que tiene una cantidad variable de objetos Destino. Tenemos una clase
Destino que representa una población con su código como entero y un bool que dice si el vehı́culo la ha visitado
hoy o no.
Haz, mediante la sobrecarga de operadores, que se pueda crear un programa como este.
// Crear d e s t i n o s con un c o d i g o
D e s t i n o d1 ( 1 2 3 ) , d2 ( 4 5 6 ) ;
// Crear v e h i c u l o s con una c a n t i d a d de d e s t i n o s i n i c i a l
V e h i c u l o v1 ( 1 0 ) , v2 ( 3 0 ) ;
// Agregar un d e s t i n o a un v e h i c u l o
v1 += d1 ;
v1 += d2 ;
// Asignar un d e s t i n o en una p o s i c i o n c o n c r e t a a un v e h i c u l o
v2 [ 0 ] = d2 ;
// Crear un v e h i c u l o a con l o s d e s t i n o s de o t r o
V e h i c u l o v3 ( v2 ) ;
// Crear un v e h i c u l o nuevo con l o s d e s t i n o s de o t r o s
V e h i c u l o v4 = v1 + v2 + v3 ;
// Hacer que un v e h i c u l o e x i s t e n t e s e i g u a l e a o t r o
v2 = v3 ;
// Agregar a un v e h i c u l o 100 p o s i c i o n e s mas para d e s t i n o s
v1 += 1 0 0 ;
// Agregar a un v e h i c u l o l o s d e s t i n o s de o t r o
v1 += v2 ;
// Mostrar en p a n t a l l a un v e h i c u l o con s u s d e s t i n o s
c o u t << ” v1 y v2 ” << v1 << v2 << e n d l ;
// Comparar s i un v e h i c u l o t i e n e mas d e s t i n o s que o t r o
i f ( v1 > v2 )
c o u t << ” v1 e s mayor a v2 ” << v1 << v2<< e n d l ;
// D e c i d i r s i un v e h i c u l o ha v i s i t a d o a t o d o s s u s d e s t i n o s
i f ( ! v1 )
c o u t << ” v1 no ha v i s i t a d o a t o d o s ” << v1<< e n d l ;
// Marcar e l d e s t i n o 2 d e l v e h i c u l o como v i s i t a d o
v1 ∗= 2 ;
Solución:
int c o d i g o ;
bool v i s i t a d o ;
};
D e s t i n o : : D e s t i n o ( const D e s t i n o & d )
Página 4 de 11
Sobrecarga
: codigo (d . codigo ) , v i s i t a d o (d . v i s i t a d o ){
}
D e s t i n o : : D e s t i n o ( int c o d i g o =0)
: codigo ( codigo ) , v i s i t a d o ( false ) {
}
D e s t i n o & D e s t i n o : : operator= ( const D e s t i n o & d e s t i n o ) {
codigo = destino . codigo ;
visitado = destino . visitado ;
return ∗ t h i s ;
}
ostream & operator <<(ostream & out , D e s t i n o & d ) {
out << ” D e s t i n o : ” << d . c o d i g o
<< ” v i s i t a d o : ” << ( d . v i s i t a d o ? ” SI ” : ”NO” )
<< e n d l ;
return out ;
}
class Vehiculo {
public :
V e h i c u l o ( const int a r r a y S i z e ) ;
V e h i c u l o ( const V e h i c u l o & v ) ;
V e h i c u l o & operator=(const V e h i c u l o & v e h i c u l o ) ;
˜ Vehiculo () ;
void operator +=(const D e s t i n o & d e s t i n o ) ;
D e s t i n o & operator [ ] ( int numDestino ) ;
void operator +=(int numDestinos ) ;
void operator +=(const V e h i c u l o & v e h i c u l o ) ;
bool operator >(const V e h i c u l o & v ) ;
bool operator ! ( ) ;
void operator ∗=( int numDestino ) ;
friend ostream & operator <<(ostream & out , V e h i c u l o & v ) ;
private :
Destino ∗ d e s t i n o s ;
int numDestinos ;
int a r r a y S i z e ;
Página 5 de 11
Sobrecarga
i f ( d e s t i n o s != 0 ) {
delete [ ] d e s t i n o s ;
}
}
void V e h i c u l o : : operator +=(const V e h i c u l o & v e h i c u l o ) {
i f ( numDestinos+ v e h i c u l o . numDestinos >= a r r a y S i z e ) {
D e s t i n o ∗ newDestinos = new D e s t i n o [ a r r a y S i z e ∗ 2 ] ;
f or ( int n= 0 ; n< a r r a y S i z e ; n++){
newDestinos [ n]= d e s t i n o s [ n ] ;
}
delete [ ] d e s t i n o s ;
d e s t i n o s = newDestinos ;
}
f or ( int n= 0 ; n< v e h i c u l o . numDestinos ; n++){
d e s t i n o s [ numDestinos++]= v e h i c u l o . d e s t i n o s [ n ] ;
}
}
void V e h i c u l o : : operator +=(const D e s t i n o & d e s t i n o ) {
i f ( numDestinos >= a r r a y S i z e ) {
D e s t i n o ∗ newDestinos = new D e s t i n o [ a r r a y S i z e ∗ 2 ] ;
f or ( int n= 0 ; n< a r r a y S i z e ; n++){
newDestinos [ n]= d e s t i n o s [ n ] ;
}
delete [ ] d e s t i n o s ;
d e s t i n o s = newDestinos ;
}
d e s t i n o s [ numDestinos++] = d e s t i n o ;
}
void V e h i c u l o : : operator +=(int i n c ) {
D e s t i n o ∗ newDestinos = new D e s t i n o [ a r r a y S i z e + i n c ] ;
f or ( int n= 0 ; n< a r r a y S i z e ; n++){
newDestinos [ n]= d e s t i n o s [ n ] ;
}
delete [ ] d e s t i n o s ;
d e s t i n o s = newDestinos ;
a r r a y S i z e+=i n c ;
}
D e s t i n o & V e h i c u l o : : operator [ ] ( int numDestino ) {
return d e s t i n o s [ numDestino ] ;
}
V e h i c u l o operator+(const V e h i c u l o & a , const V e h i c u l o & b ) {
V e h i c u l o newVehiculo ( a . a r r a y S i z e + b . a r r a y S i z e ) ;
newVehiculo . numDestinos = a . numDestinos+ b . numDestinos ;
int n=0;
f or ( n= 0 ; n< a . numDestinos ; n++){
newVehiculo . d e s t i n o s [ n]= a . d e s t i n o s [ n ] ;
}
f or ( int m = 0 ; m< b . numDestinos ; m++){
newVehiculo . d e s t i n o s [ n+m]= b . d e s t i n o s [m] ;
}
return newVehiculo ;
}
V e h i c u l o & V e h i c u l o : : operator=(const V e h i c u l o & v e h i c u l o ) {
// p r o t e c c i o n c o n t r a ’ x = x ; ’
i f (& v e h i c u l o == t h i s ) {
return ∗ t h i s ;
Página 6 de 11
Sobrecarga
}
i f ( d e s t i n o s != 0 ) {
delete [ ] d e s t i n o s ;
}
d e s t i n o s = new D e s t i n o [ v e h i c u l o . a r r a y S i z e ] ;
numDestinos = v e h i c u l o . numDestinos ;
f or ( int n= 0 ; n< numDestinos ; n++){
d e s t i n o s [ n]= v e h i c u l o . d e s t i n o s [ n ] ;
}
return ∗ t h i s ;
}
bool V e h i c u l o : : operator >(const V e h i c u l o & v ) {
return numDestinos > v . numDestinos ;
}
bool V e h i c u l o : : operator ! ( ) {
f or ( int n= 0 ; n< numDestinos ; n++){
if (! destinos [n ] . visitado )
return f a l s e ;
}
return true ;
}
void V e h i c u l o : : operator ∗=( int numDestino ) {
d e s t i n o s [ numDestino ] . v i s i t a d o = true ;
}
ostream & operator <<(ostream & out , V e h i c u l o & v ) {
out << e n d l << ” V e h i c u l o para ” << v . a r r a y S i z e
<< ” d e s t i n o s , con ” << v . numDestinos << ” . ” <<e n d l ;
f or ( int n= 0 ; n< v . numDestinos ; n++){
out << ”\ t ” << v . d e s t i n o s [ n ] ;
}
out << e n d l ;
return out ;
}
int main ( ) {
D e s t i n o d1 ( 1 2 3 ) , d2 ( 4 5 6 ) ;
c o u t << ” D e s t i n o s i n i c i a l e s ” << d1 << d2 << e n d l ;
V e h i c u l o v1 ( 1 0 ) , v2 ( 3 0 ) ;
c o u t << ” V e h i c u l o s i n i c i a l e s ” << v1 << v2 << e n d l ;
v1 += d1 ;
v1 += d2 ;
c o u t << ” v1 agregando d e s t i n o s d1 y d2” << v1 << e n d l ;
c o u t << ” v2 o r i g i n a l ” << v2 << e n d l ;
v2 += d1 ;
v2 [ 0 ] = d2 ;
c o u t << ” v2 cambiando l a 0 por d2” << v2 << e n d l ;
V e h i c u l o v3 ( v2 ) ;
c o u t << ” v3 c r e a d o a p a r t i r de v2 ” << v3 << e n d l ;
V e h i c u l o v4 = v1 + v2 + v3 ;
c o u t << ”v4 , suma de v1 , v2 y v3 ” << v4 << e n d l ;
v2 = v3 ;
c o u t << ” v2 a s i g n a n d o l e v3 ” << v2 << e n d l ;
v1 += 1 0 0 ;
c o u t << ” v1 agregando con += e l e n t e r o 100 ” << v1 << e n d l ;
v1 += v2 ;
c o u t << ” v1 agregando con += v2 ” << v1 << e n d l ;
c o u t << ” v1 y v2 ” << v1 << v2 << e n d l ;
i f ( v1 > v2 )
Página 7 de 11
Sobrecarga
3. [003.txt] Implementa la clase punto, y la clase polı́gono. Cada objeto de la clase punto tiene dos coordenadas
de tipo entero (x e y). Cada polı́gono está formado por un numero determinado pero variable de puntos. Utiliza
reserva dinámica de memoria para almacenar los puntos que forman el polinomio. Implementa las funciones
necesarias (constructores, destructores, ...) y además en la clase polı́gono:
Solución:
P o l i g o n o ( const P o l i g o n o & p ) {
numPuntos = p . numPuntos ;
puntos = new Punto [ numPuntos ] ;
f or ( int n=0; n< numPuntos ; n++){
puntos [ n ] . x = p . puntos [ n ] . x ;
puntos [ n ] . y = p . puntos [ n ] . y ;
}
}
P o l i g o n o ( int n=0) : numPuntos ( n ) {
i f ( n>0){
puntos = new Punto [ numPuntos ] ;
}
}
P o l i g o n o & operator= ( P o l i g o n o &p ) {
i f ( puntos ) delete [ ] puntos ;
numPuntos = p . numPuntos ;
puntos = new Punto [ numPuntos ] ;
f or ( int n=0; n< numPuntos ; n++){
puntos [ n ] . x = p . puntos [ n ] . x ;
puntos [ n ] . y = p . puntos [ n ] . y ;
}
Página 8 de 11
Sobrecarga
return ∗ t h i s ;
}
˜ Poligono () {
i f ( puntos )
delete [ ] puntos ;
}
friend P o l i g o n o & operator +( int i , P o l i g o n o &p ) ;
};
P o l i g o n o & operator +( int i , P o l i g o n o &p ) {
f or ( int n=0; n< p . numPuntos ; n++){
p . puntos [ n ] . x+=i ;
p . puntos [ n ] . y+=i ;
}
return p ;
}
Solución:
private :
int ∗∗ d a t o s ;
int rows , c o l s ;
void r e s e r v a D a t o s ( ) ;
void l i b e r a D a t o s ( ) ;
void c o p i a D a t o s ( int ∗ ∗ ) ;
Página 9 de 11
Sobrecarga
Matriz : : ˜ Matriz ( ) {
liberaDatos () ;
}
Página 10 de 11
Sobrecarga
delete [ ] d a t o s [ row ] ;
}
delete [ ] d a t o s ;
}
void Matriz : : c o p i a D a t o s ( int ∗∗ d a t o s ) {
f or ( int row = 0 ; row < rows ; row++){
f or ( int c o l = 0 ; c o l < c o l s ; c o l ++){
this−>d a t o s [ row ] [ c o l ] = d a t o s [ row ] [ c o l ] ;
}
}
}
void Matriz : : r e s e r v a D a t o s ( ) {
d a t o s = new int ∗ [ rows ] ;
f or ( int n= 0 ; n<rows ; n++){
d a t o s [ n]= new int [ c o l s ] ;
f or ( int c o l= 0 ; c o l <c o l s ; c o l ++){
datos [ n ] [ c o l ] = 0;
}
}
}
Matriz sumarAInt ( int n , const Matriz & m) {
Matriz r e t (m) ;
f or ( int row = 0 ; row < m. rows ; row++){
f or ( int c o l = 0 ; c o l < m. c o l s ; c o l ++){
r e t . d a t o s [ row ] [ c o l ] +=n ;
}
}
return r e t ;
}
ostream & operator << ( ostream & os , Matriz &m) {
f or ( int row = 0 ; row < m. rows ; row++){
f or ( int c o l = 0 ; c o l < m. c o l s ; c o l ++){
o s << m[ row ] [ c o l ] << ” , ” ;
}
o s << e n d l ;
}
o s << e n d l ;
return o s ;
}
int main ( ) {
Matriz m( 2 , 2 ) ;
m[ 0 ] [ 1 ] = 1 0 ;
m[ 1 ] [ 1 ] = 2 0 ;
c o u t << ”M: ”<<e n d l ;
c o u t << m;
Matriz z = sumarAInt ( 2 0 0 , m) ;
c o u t << ”Z : ”<<e n d l ;
c o u t << z<<e n d l ;
Matriz x = m−z ;
c o u t << ”X: ”<<e n d l ;
c o u t << x<<e n d l ;
i f ( x == z ) c o u t << ” e r r o r ”<<e n d l ;
z = x;
i f ( x == z ) c o u t << ” son i g u a l e s ”<<e n d l ;
}
Página 11 de 11