Sie sind auf Seite 1von 7

Práctica

1. [001.txt] Partiendo del programa desarrollado en el ejercicio anterior (Bloque II), se realizaran varias modifi-
caciones relacionadas con herencia, composicion, polimorfismo, y gestion de excepciones.
El ejercicio anterior suponı́a la creación de una clase Grupo, formada por un numero variable de usuarios, y que
se implementó como un array dinámico de objetos de la clase Usuario. El primer cambio va a consistir en que
la parte dinámica del grupo va a estar formada por fotos y vı́deos. La idea es que en una red social cualquiera
de los miembros de un grupo podrı́a publicar y compartir archivos de fotos y vı́deos. La clase Archivo podrı́a ser
similar a esta:
c l a s s Archivo {
public :
const s t r i n g &g e t T i t u l o ( ) ;
const s t r i n g &getRuta ( ) ;
v i r t u a l const s t r i n g &t o S t r i n g ( ) = 0 ;
Archivo ( const s t r i n g &r , const s t r i n g &t ) ;
private :
s t r i n g ruta ;
string titulo ;
};

Las transformaciones a realizar serán las siguientes:


Definir las clases Foto y Video como especializaciones de la clase Archivo. La clase Foto tendrá una variable
miembro adicional album, y la clase Video una variable codigoInsertar (código HTML para incrustar el
vı́deo en una Web). El contenido de esta variable se generará por una función que reciba el ancho y alto
seleccionado. También habrá una función que devuelva dicho código HTML.
El vector de archivos de cada grupo se alojará en memoria dinámica, encargándose la propia clase Grupo de
su gestión. Este vector dinámico debe ser creado utilizando un vector de la STL. Como se quiere aprovechar
la flexibilidad de la ligadura dinámica, el tipo base del contenedor será un puntero a la clase Archivo, que
permitirá que el vector contenga indistintamente vı́deos o fotos.
El método virtual toString()es similar al operador , devolviendo el contenido del objeto, que será diferente
para las clases Foto y Video.
Todos los errores que en el ejercicio anterior se mostraban por consola y hacı́an que el programa se detuviera,
deben ser gestionados mediante excepciones. Ası́, los errores del operador – (no hay archivos que eliminar)
deben ser tratados ahora mediante el lanzamiento de excepciones.
El main (sin incluir tratamiento de excepciones) podı́a ser algo ası́:
unsigned int num ;
char cad [ 3 ] ;

Grupo g1 ( ”AVIA” ) ;
Grupo g2 ( ”LIMIA” ) ;

c o u t << ” \nVamos a l e e r l o s d a t o s ( i n c l u i d o s l o s a r c h i v o s ) de l o s dos gr u p os


. \ n” ;
c o u t << ” \ nPara e l p r i m e r grupo . \ n” ;
c i n >> g1 ;
c o u t << ” \ nPara e l segundo grupo . \ n” ;
c i n >> g2 ;

c o u t << ” \nComparamos e l numero de a r c h i v o s de l o s dos gr u p os . \ n” ;


i f ( g1 < g2 )
c o u t << ”\ t E l grupo ” << g1 . obtenerNombre ( )
<< ” t i e n e menos a r c h i v o s que e l grupo ”
<< g2 . obtenerNombre ( ) << e n d l ;
e l s e c o u t << ”\ t E l grupo ” << g2 . obtenerNombre ( )
<< ” t i e n e menos a r c h i v o s que e l grupo ”
<< g1 . obtenerNombre ( ) << e n d l ;

c o u t << endl<< ” Se va a e l i m i n a r e l u l t i m o a r c h i v o d e l grupo ”

Página 1 de 7
Práctica

<< g1 . obtenerNombre ( ) << e n d l ;


−−g1 ;
c o u t << ” \ nDespues de q u i t a r l e e l u l t i m o a r c h i v o l o s d a t o s d e l grupo ”
<< g1 . obtenerNombre ( ) << ” son : \ n” ;
c o u t << g1 ;

c o u t << ” \ nCuantos a r c h i v o s d e s e a s a g r e g a r a l grupo ”


<< g2 . obtenerNombre ( ) << ” ?\n” ;
c i n . g e t l i n e ( cad , 3 ) ;
num = a t o i ( cad ) ;
g2 = num + g2 ;
c o u t << ” \ nDespues de a g r e g a r l e a r c h i v o s a l grupo ”
<< g2 . obtenerNombre ( ) << ” s u s d a t o s son : \ n” ;
c o u t << g2 ;

Solución:

// f o t o s Y V i d e o s . h
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
#i f n d e f FOTOSYVIDEOS H INCLUDED
#define FOTOSYVIDEOS H INCLUDED
#include <v e c t o r >
#include <s t r i n g >
#include <i o s t r e a m >
#include <c s t r i n g >
#include <c s t d l i b >

using namespace s t d ;
const int TAM MAXIMO=100;

c l a s s Archivo ;

c l a s s Grupo {
private :
char nombre [TAM MAXIMO ] ;
char imagen [TAM MAXIMO ] ;
char d e s c r i p c i o n [TAM MAXIMO ] ;
char web [TAM MAXIMO ] ;
v e c t o r <Archivo ∗> al m ac e n A r c h i v os ;
public :
Grupo ( const char ∗ s ) ;
s t r i n g obtenerNombre ( ) ;
friend i s t r e a m & operator >>(i s t r e a m &i , Grupo & g ) ;
friend ostream & operator <<(ostream &i , Grupo & g ) ;
friend bool operator <(Grupo &a , Grupo& b ) ;
Grupo& operator −−() ;
friend Grupo& operator+( int n , Grupo & g ) ;
};

c l a s s Archivo {
public :
v i r t u a l ˜ Archivo ( ) ;
const s t r i n g &g e t T i t u l o ( ) {
return t i t u l o ;
}
const s t r i n g &getRuta ( ) {
return r u t a ;

Página 2 de 7
Práctica

}
v i r t u a l const s t r i n g t o S t r i n g ( ) = 0 ;
Archivo ( const s t r i n g &r , const s t r i n g &t ) ;
private :
s t r i n g ruta ;
string titulo ;
};

c l a s s Foto : public Archivo {


public :
v i r t u a l ˜ Foto ( ) ;
Foto ( s t r i n g u r l , s t r i n g t i t u l o , s t r i n g album ) ;
const s t r i n g t o S t r i n g ( ) ;
s t r i n g getAlbum ( ) {
return album ;
}
friend i s t r e a m & operator >>(i s t r e a m &i , Grupo & g ) ;
private :
s t r i n g album ;

};

c l a s s Video : public Archivo {


public :
v i r t u a l ˜ Video ( ) ;
const s t r i n g t o S t r i n g ( ) ;
Video ( s t r i n g u r l , s t r i n g t i t u l o , s t r i n g c o d i g o I n s e r t a r ) ;
string getCodigoInsertar () {
return c o d i g o I n s e r t a r ;
}
friend i s t r e a m & operator >>(i s t r e a m &i , Grupo & g ) ;
private :
string codigoInsertar ;

};

i s t r e a m & operator >>(i s t r e a m &i , Grupo & g ) ;


s t r i n g codigoHTML ( int ancho , int a l t o ) ;
ostream & operator << ( ostream &o , const Grupo &g ) ;

#endif // FOTOSYVIDEOS H INCLUDED

// f o t o s Y V i d e o s . cpp
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
//#i n c l u d e ” f o t o s Y v i d e o s . h”

Archivo : : Archivo ( const s t r i n g &ruta , const s t r i n g &t i t u l o )


: ruta ( ruta ) , t i t u l o ( t i t u l o ) {
}
Archivo : : ˜ Archivo ( ) {

}
Foto : : Foto ( s t r i n g u r l , s t r i n g t i t u l o , s t r i n g album )
: Archivo ( u r l , t i t u l o ) , album ( album ) {
}
Foto : : ˜ Foto ( ) {

Página 3 de 7
Práctica

}
s t r i n g Grupo : : obtenerNombre ( ) {
return nombre ;
}

const s t r i n g Foto : : t o S t r i n g ( ) {
string ret ;
r e t+= g e t T i t u l o ( ) ;
r e t+=” , ” ;
r e t+= getRuta ( ) ;
r e t+=” , ” ;
r e t+= getAlbum ( ) ;
return r e t ;

Video : : ˜ Video ( ) {

Video : : Video ( s t r i n g u r l , s t r i n g t i t u l o , s t r i n g c o d i g o I n s e r t a r )
: Archivo ( u r l , t i t u l o ) , c o d i g o I n s e r t a r ( c o d i g o I n s e r t a r ) {

};

const s t r i n g Video : : t o S t r i n g ( ) {
string ret ;
r e t+= g e t T i t u l o ( ) ;
r e t+=” , ” ;
r e t+= getRuta ( ) ;
r e t+=” , ” ;
r e t+= g e t C o d i g o I n s e r t a r ( ) ;
return r e t ;

Grupo : : Grupo ( const char ∗ s ) {


s t r c p y ( nombre , s ) ;

i s t r e a m & operator >>(i s t r e a m &i , Grupo & g ) {


cout<<” I n t r o d u c e e l nombre d e l grupo : ” ;
i . g e t l i n e ( g . nombre ,TAM MAXIMO) ;
cout<<” I n t r o d u c e l a imagen d e l grupo : ” ;
i . g e t l i n e ( g . imagen ,TAM MAXIMO) ;
cout<<” I n t r o d u c e l a d e s c r i p c i o n d e l grupo : ” ;
i . g e t l i n e ( g . d e s c r i p c i o n ,TAM MAXIMO) ;
cout<<” I n t r o d u c e l a web d e l grupo : ”<<e n d l ;
i . g e t l i n e ( g . web ,TAM MAXIMO) ;
cout<<”Ahora i n t r o d u c i r e m o s l o s d i s t i n t o s a r c h i v o s . ”<<e n d l ;
Archivo ∗ tmp=0;
char c ;
s t r i n g u r l , t i t u l o , album , c o d i g o I n s e r t a r ;
int ancho , a l t o ;
do {
cout<<”F : f o t o , V: video , S : f i n ”<<e n d l ;

Página 4 de 7
Práctica

c i n >>c ;
i f ( c==’F ’ or c==’V ’ or c==’ S ’ ) {
switch ( c ) {
case ’F ’ :

cout<<” I n t r o d u c e l a u r l de l a f o t o : ” ;
i . ignore () ;
getline ( i , url ) ;
cout<<” I n t r o d u c e e l t i t u l o de l a f o t o : ” ;
getline ( i , titulo ) ;
cout<<” I n t r o d u c e e l album de l a f o t o : ” ;
g e t l i n e ( i , album ) ;
tmp=new Foto ( u r l , t i t u l o , album ) ;
g . al m ac e n A r c h i v os . push back ( tmp ) ;
break ;

case ’V ’ :

cout<<” I n t r o d u c e l a u r l d e l v i d e o : ” ;
i . ignore () ;
getline ( i , url ) ;
cout<<” I n t r o d u c e e l t i t u l o d e l v i d e o : ” ;
getline ( i , titulo ) ;
cout<<” I n t r o d u c e e l ancho d e l v i d e o : ” ;
c i n >>ancho ;
cout<<” I n t r o d u c e e l a l t o d e l v i d e o : ” ;
c i n >>a l t o ;
c o d i g o I n s e r t a r=codigoHTML ( ancho , a l t o ) ;
tmp=new Video ( u r l , t i t u l o , c o d i g o I n s e r t a r ) ;
g . al m ac e n A r c h i v os . push back ( tmp ) ;
break ;

} else {
cout<<” El s i m b o l o i n t r o d u c i o d o e s i n c o r r e c t o : ”<<e n d l ;
}
} while ( c != ’ S ’ ) ;

return i ;
}

s t r i n g codigoHTML ( int ancho , int a l t o ) {


s t r i n g cadena=” Esto e s e l c o d i g o HTML d e l v i d e o . ” ;

return cadena ;
}
bool operator <(Grupo &a , Grupo& b ) {
return a . al m ac e n A r c h i v os . s i z e ( ) < b . al m ac e n A r c h i v os . s i z e ( ) ;
}
Grupo& Grupo : : operator −−() {
al m ac e n A r c h i v os . pop back ( ) ;
return ∗ t h i s ;
}
Grupo& operator+( int n , Grupo & g ) {
//Que s e supone que hacemos a q u i ?
// El v e c t o r s e amplia s o l o

Página 5 de 7
Práctica

//No podemos a g r e g a r s i no sabemos


// s i s e desean f o t o s o v i d e o s
return g ;
}
ostream & operator << ( ostream &o , Grupo &g ) {
o
<<”Nombre : ”<<g . nombre<<e n d l
<<” Imagen : ”<<g . imagen<<e n d l
<<” D e s c r i p c i o n : ”<<g . d e s c r i p c i o n <<e n d l
<<”Web : ”<<g . web<<e n d l
<<” A r c h i v o s d e l grupo : ”<<e n d l ;
// F a l t a mostrar l o s a r c h i v o s con e l i t e r a t o r
v e c t o r <Archivo ∗ >:: i t e r a t o r i ;
i = g . al m ac e n A r c h i v os . b e g i n ( ) ;
while ( i != g . al m ac e n A r c h i v os . end ( ) ) {
c o u t << ( ∗ i )−>t o S t r i n g ( )<<e n d l ;
i ++;
}

return o ;

}
// main . cpp
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
#include <i o s t r e a m >
//#i n c l u d e ” f o t o s Y v i d e o s . h”
using namespace s t d ;

int main ( ) {
unsigned int num ;
char cad [ 3 ] ;

Grupo g1 ( ”AVIA” ) ;
Grupo g2 ( ”LIMIA” ) ;

c o u t << ”\nVamos a l e e r l o s d a t o s ( i n c l u i d o s l o s a r c h i v o s ) de l o s dos


gr u p os . \ n” ;
c o u t << ”\ nPara e l p r i m e r grupo . \ n” ;
c i n >> g1 ;
c o u t << ”\ nPara e l segundo grupo . \ n” ;
c i n >> g2 ;

c o u t << ”\nComparamos e l numero de a r c h i v o s de l o s dos gr u p os . \ n” ;


i f ( g1 < g2 )
c o u t << ”\ t E l grupo ” << g1 . obtenerNombre ( )
<< ” t i e n e menos a r c h i v o s que e l grupo ”
<< g2 . obtenerNombre ( ) << e n d l ;
e l s e c o u t << ”\ t E l grupo ” << g2 . obtenerNombre ( )
<< ” t i e n e menos a r c h i v o s que e l grupo ”
<< g1 . obtenerNombre ( ) << e n d l ;

c o u t << ”\ nSe va a e l i m i n a r e l u l t i m o a r c h i v o d e l grupo ”


<< g1 . obtenerNombre ( ) << e n d l ;
−−g1 ;
c o u t << ”\ nDespues de q u i t a r l e e l u l t i m o a r c h i v o l o s d a t o s d e l grupo ”
<< g1 . obtenerNombre ( ) << ” son : \ n” ;
c o u t << g1 ;

Página 6 de 7
Práctica

c o u t << ”\ nCuantos a r c h i v o s d e s e a s a g r e g a r a l grupo ”


<< g2 . obtenerNombre ( ) << ” ?\n” ;
c i n . g e t l i n e ( cad , 3 ) ;
num = a t o i ( cad ) ;
g2 = num + g2 ;
c o u t << ”\ nDespues de a g r e g a r l e a r c h i v o s a l grupo ”
<< g2 . obtenerNombre ( ) << ” s u s d a t o s son : \ n” ;
c o u t << g2 ;
}

Página 7 de 7

Das könnte Ihnen auch gefallen