Il programma è molto semplice sono sono una classi base oggetto e due classi derivata libro e cellulare e una classe contenitore composta da un vettore di oggetti base un libreria di errori e un main di prova. Il problema è il main non capisco perchè non mi stampi nulla ho creato un vettore del tipo della classe base in cui ho inserito oggi derivati e poi creato dei tipi contenitori in cui ho inserito gli oggetti ma niente non stampa niente no mi da nemmeno nessun errore.
C++:
#ifndef OGGETTOLIB_H
#define OGGETTOLIB_H
#include <cstring>
#include <iomanip>
#include <iostream>
#include <cstdlib>
#define DIM 100
using namespace std;
namespace Myprog{
typedef char Buffer [DIM];
class Oggetto{
friend ostream &operator<<(ostream &, const Oggetto &);
friend istream &operator>>(istream &, Oggetto &);
private:
int Codice;
char *Descrizione;
char *Forma;
protected:
virtual ostream &stampa(ostream &)const;
virtual istream &leggi(istream &);
public:
Oggetto(const int =0, const char * ="", const char * ="");
Oggetto(const Oggetto &);
~Oggetto(){ delete[] Descrizione; delete[] Forma; };
void set_Oggetto(const Oggetto &);
void set_Codice(const int COD){ Codice=COD; };
void set_Descrizione(const char *);
void set_Forma(const char *);
const int get_Codice() { return Codice; };
const char *get_Descrizione() { return Descrizione; };
const char *get_Forma() { return Forma; };
const Oggetto &operator=(const Oggetto &);
bool operator==(const Oggetto &)const;
bool operator!=(const Oggetto &)const;
};
}
#endif
C++:
#include "Oggettolib.h"
namespace Myprog{
Oggetto::Oggetto(const int COD, const char *D, const char *F):Codice(COD), Descrizione(new char [strlen(D)+1]), Forma(new char [strlen(F)+1]){
strcpy(Descrizione,D);
strcpy(Forma,F);
}
Oggetto::Oggetto(const Oggetto &O):Codice(O.Codice), Descrizione(new char [strlen(O.Descrizione)+1]), Forma(new char [strlen(O.Forma)+1]){
strcpy(Descrizione,O.Descrizione);
strcpy(Forma,O.Forma);
}
void Oggetto::set_Oggetto(const Oggetto &O){
set_Codice(O.Codice);
set_Descrizione(O.Descrizione);
set_Forma(O.Forma);
}
void Oggetto::set_Descrizione(const char *D){
if(Descrizione) delete [] Descrizione;
Descrizione=new char [strlen(D)+1];
strcpy(Descrizione,D);
}
void Oggetto::set_Forma(const char *F){
if(Forma) delete [] Forma;
Forma=new char [strlen(F)+1];
strcpy(Forma,F);
}
const Oggetto &Oggetto::operator=(const Oggetto &O){
if(&O!=this) set_Oggetto(O);
}
bool Oggetto::operator==(const Oggetto &O)const{
return (Codice==O.Codice && !strcmp(Descrizione,O.Descrizione) && !strcmp(Forma, O.Forma) );
}
bool Oggetto::operator!=(const Oggetto &O)const{
return(!this->operator==(O));
}
ostream &operator<<(ostream &out,const Oggetto &O){
return O.stampa(out);
}
istream &operator>>(istream &in, Oggetto &O){
return O.leggi(in);
}
ostream &Oggetto::stampa(ostream &out)const{
out<<"\n |Codice: "<<Codice<<"| Descrizione: "<<Descrizione<<"| Forma: "<<Forma<<endl;
return out;
}
istream &Oggetto::leggi(istream &in){
Buffer b;
cout<<"\n Inserisci Codice :";
in>>Codice;
in.ignore();
cout<<"\n Inserisci Descrizione :";
in.getline(b, DIM-1, '\n');
set_Descrizione(b);
in.ignore();
cout<<"\n Inserisci Forma :";
in.getline(b, DIM-1, '\n');
set_Forma(b);
in.ignore();
return in;
}
}
C++:
#ifndef LIBROLIB_H
#define LIBROELIB_H
#include "Oggettolib.h"
namespace Myprog{
class Libro: public Oggetto{
friend ostream &operator<<(ostream &, const Libro &);
friend istream &operator>>(istream &, Libro &);
private:
char *Titolo;
int Pagine;
protected:
virtual ostream &stampa(ostream &)const;
virtual istream &leggi(istream &);
public:
Libro(const int =0, const char * ="", const char * ="", const char * ="", const int =0);
Libro(const Libro &);
~Libro(){ delete[] Titolo; };
void set_Libro(const Libro &);
void set_Pagine(const float P){ Pagine=P; };
void set_Titolo(const char *);
const int get_Pagine() { return Pagine; };
const char *get_Titolo() { return Titolo; };
const Libro &operator=(const Libro &);
bool operator==(const Libro &)const;
bool operator!=(const Libro &)const;
};
}
#endif
C++:
#include "Librolib.h"
namespace Myprog{
Libro::Libro(const int COD, const char *D, const char *F, const char *T, const int P):Oggetto(COD, D, F), Pagine(P), Titolo(new char [strlen(T)+1]) {
strcpy(Titolo,T);
}
Libro::Libro(const Libro &L):Oggetto(L), Pagine(L.Pagine), Titolo(new char [strlen(L.Titolo)+1]) {
strcpy(Titolo,L.Titolo);
}
void Libro::set_Libro(const Libro &L){
set_Pagine(L.Pagine);
set_Titolo(L.Titolo);
}
void Libro::set_Titolo(const char *T){
if(Titolo) delete [] Titolo;
Titolo=new char [strlen(T)+1];
strcpy(Titolo,T);
}
const Libro &Libro::operator=(const Libro &L){
if(&L!=this) set_Libro(L);
}
bool Libro::operator==(const Libro &L)const{
return (Pagine==L.Pagine && !strcmp(Titolo,L.Titolo) && Oggetto::operator==(L) );
}
bool Libro::operator!=(const Libro &L)const{
return(!this->operator==(L));
}
ostream &operator<<(ostream &out,const Libro &L){
return L.stampa(out);
}
istream &operator>>(istream &in, Libro &L){
return L.leggi(in);
}
ostream &Libro::stampa(ostream &out)const{
Oggetto::stampa(out);
out<<"\n |Titolo: "<<Titolo<<"| Pagine: "<<Pagine<<endl;
return out;
}
istream &Libro::leggi(istream &in){
Oggetto::leggi(in);
Buffer b;
cout<<"\n Inserisci Titolo :";
in.getline(b, DIM-1, '\n');
set_Titolo(b);
in.ignore();
cout<<"\n Inserisci Pagine :";
in>>Pagine;
in.ignore();
return in;
}
}
C++:
#ifndef CELLULARELIB_H
#define CELLULARELIB_H
#include "Oggettolib.h"
namespace Myprog{
class Cellulare: public Oggetto{
friend ostream &operator<<(ostream &, const Cellulare &);
friend istream &operator>>(istream &, Cellulare &);
private:
char *Modello;
float Costo;
protected:
virtual ostream &stampa(ostream &)const;
virtual istream &leggi(istream &);
public:
Cellulare(const int =0, const char * ="", const char * ="", const char * ="", const float =0);
Cellulare(const Cellulare &);
~Cellulare(){ delete[] Modello; };
void set_Cellulare(const Cellulare &);
void set_Costo(const float COS){ Costo=COS; };
void set_Modello(const char *);
const float get_Costo() { return Costo; };
const char *get_Modello() { return Modello; };
const Cellulare &operator=(const Cellulare &);
bool operator==(const Cellulare &)const;
bool operator!=(const Cellulare &)const;
};
}
#endif
C++:
#include "Cellularelib.h"
namespace Myprog{
Cellulare::Cellulare(const int COD, const char *D, const char *F, const char *M, const float COS):Oggetto(COD, D, F), Costo(COS), Modello(new char [strlen(M)+1]) {
strcpy(Modello,M);
}
Cellulare::Cellulare(const Cellulare &C):Oggetto(C), Costo(C.Costo), Modello(new char [strlen(C.Modello)+1]) {
strcpy(Modello,C.Modello);
}
void Cellulare::set_Cellulare(const Cellulare &C){
set_Costo(C.Costo);
set_Modello(C.Modello);
}
void Cellulare::set_Modello(const char *M){
if(Modello) delete [] Modello;
Modello=new char [strlen(M)+1];
strcpy(Modello,M);
}
const Cellulare &Cellulare::operator=(const Cellulare &C){
if(&C!=this) set_Cellulare(C);
}
bool Cellulare::operator==(const Cellulare &C)const{
return (Costo==C.Costo && !strcmp(Modello,C.Modello) && Oggetto::operator==(C) );
}
bool Cellulare::operator!=(const Cellulare &C)const{
return(!this->operator==(C));
}
ostream &operator<<(ostream &out,const Cellulare &C){
return C.stampa(out);
}
istream &operator>>(istream &in, Cellulare &C){
return C.leggi(in);
}
ostream &Cellulare::stampa(ostream &out)const{
Oggetto::stampa(out);
out<<"\n |Modello: "<<Modello<<"| Costo: "<<Costo<<endl;
return out;
}
istream &Cellulare::leggi(istream &in){
Oggetto::leggi(in);
Buffer b;
cout<<"\n Inserisci Modello :";
in.getline(b, DIM-1, '\n');
set_Modello(b);
in.ignore();
cout<<"\n Inserisci Costo :";
in>>Costo;
in.ignore();
return in;
}
}
C++:
#ifndef CONTENITORELIB_H
#define CONTENITORELIB_H
#include "Cellularelib.h"
#include "Librolib.h"
#include "Errorelib.h"
#include <fstream>
#include <string>
namespace Myprog{
typedef Oggetto T;
class Contenitore{
private:
static const int N=20;
int Count;
T Vett[N];
public:
Contenitore(){ for(int i=0; i<N; i++)Vett[i]; };
bool Empty()const {return Count==0;};
bool Full()const {return Count==N;};
bool inElenco(const T &)const;
int Posizione(const T &);
bool Inserisci(const T &);
bool Elimina(const T &);
Contenitore operator/(Contenitore )const;
Contenitore operator+(Contenitore )const;
Contenitore operator-(Contenitore )const;
void StampaVideo()const;
void StampaFile(const char *)throw(Errore);
};
}
#endif
C++:
#include "Contenitorelib.h"
namespace Myprog{
bool Contenitore::inElenco(const T &E)const{
bool trovato=false;
for(int i=0; i<Count; i++){
if(Vett[i]==E) trovato=true;
}
return trovato;
}
int Contenitore::Posizione(const T &E){
for(int i=0; i<Count; i++){
if(Vett[i]==E) return i;
}
}
bool Contenitore::Inserisci(const T &E){
if(Full()) return false;
if(inElenco(E)){
throw("\n Elemento già presente in elenco", E);
}
Vett[Count]=E;
Count++;
return true;
}
bool Contenitore::Elimina(const T &E){
if(!inElenco(E)) return false;
int i=Posizione(E);
for(int j=i; i<Count; i++){
Vett[j]=Vett[j+1];
}
}
void Contenitore::StampaVideo()const{
for(int i=0; i<Count; i++){
cout<<i<<")"<<Vett[i]<<endl;
}
}
void Contenitore::StampaFile(const char *NomeF)throw(Errore){
fstream File;
File.open(NomeF);
if(!File){
throw("\n Errore durante l'apertura del file!");
}
for(int i=0; i<Count; i++){
if(&Vett[i]){
File<<Vett[i]<<endl;
}
}
}
Contenitore Contenitore::operator+(const Contenitore I)const{
Contenitore Cbuff;
for(int i=0; i<I.Count; i++){
Cbuff.Inserisci(I.Vett[i]);
}
for(int i=0; i<this->Count; i++){
try{
Cbuff.Inserisci(this->Vett[i]);
}
catch(Errore e){
cout<<e.what()<<' '<<e.return_elem()<<endl;
}
}
return Cbuff;
}
Contenitore Contenitore::operator-(const Contenitore I)const{
Contenitore Cbuff;
for(int i=0; i<this->Count; i++){
Cbuff.Inserisci(this->Vett[i]);
}
for(int i=0; i<I.Count; i++){
Cbuff.Elimina(I.Vett[i]);
}
return Cbuff;
}
Contenitore Contenitore::operator/(const Contenitore I)const{
Contenitore Cbuff;
for(int i=0; i<this->Count; i++){
if(this->inElenco(I.Vett[i])){
Cbuff.Inserisci(I.Vett[i]);
}
return Cbuff;
}
}
}
C++:
#ifndef ERRORELIB_H
#define ERRORELIB_H
#include <string>
#include <cstdlib>
using namespace std;
namespace Myprog{
typedef Oggetto T;
class Errore{
private:
string error;
T Elem;
public:
Errore(const string Err, const T &EL): error(Err), Elem(EL){ };
const string what()const;
const T return_elem()const {return Elem;};
};
}
#endif
C++:
#include "Errorelib.h"
namespace Myprog{
const string Errore::what()const{
string msg;
msg="\n Errore!"+error;
msg.c_str();
}
}
C++:
#include "Contenitorelib.h"
using namespace std;
using namespace Myprog;
int main(int argc, char** argv) {
Libro L1(1, "Libro", "quadrato", "gnigni", 500);
Libro L2(1, "Libro", "quadrato", "gnigni", 500);
Libro L3(2, "Libro", "rettangolare", "cucca", 200);
Cellulare C1(3, "Cellulare", "rettangolare", "fiif", 20.20);
Cellulare C2(4, "Cellulare", "quadrato", "CUU", 333.20);
Cellulare C3(3, "Cellulare", "rettangolare", "fiif", 20.20);
Oggetto O1[4];
O1[1]=L1;
O1[2]=L2;
O1[3]=C1;
O1[4]=C3;
Oggetto O2[4];
O2[1]=L1;
O2[2]=L3;
O2[3]=C2;
O2[4]=C3;
Contenitore E1;
Contenitore E2;
Contenitore E3;
Contenitore E4;
Contenitore E5;
for(int i=0; i<4; i++ ){
try{
E1.Inserisci(O1[i]);
}catch (Errore e){
cout<<e.what()<<' '<<e.return_elem()<<endl;
}
}
E1.StampaVideo();
for(int i=0; i<4; i++ ){
try{
E2.Inserisci(O2[i]);
}catch (Errore e){
cout<<e.what()<<' '<<e.return_elem()<<endl;
}
E2.StampaVideo();
E3=E1+E2;
E3.StampaVideo();
E4=E1-E2;
E4.StampaVideo();
E5=E1/E2;
E5.StampaVideo();
}
}