File originale in java
PHP Code:
http://www.xgiovio.com/forum/forum/programming/java/447-gerarchie-object-persona-studente 
main
PHP Code:
#include <iostream>
#include <string>

#include "Studente.h"
#include "Persona.h"



int main () {

        
Studente a ("mario"1960"math");
        
Studente b ("mario"1960"math");

        
std::cout << a.to_string() << std::endl;

        
Studente dynamic_cast <Studente *> (a.clone());

        
std::cout << c->to_string() << std::endl;

        if(
a.equals(*c))
            
std::cout << "equals" << std::endl;



    
std::cin.get();



Object header
PHP Code:
#ifndef OBJECT_H
#define OBJECT_H

#include <string>


class Object
{
public:
    
Object(void);
    
virtual ~Object(void);
    
    
virtual const std::string to_string ();
    
virtual bool equals (Objectin_obj);

    
virtual void print();

protected:
    
virtual Object* clone() ;

};

#endif 
Object cpp
PHP Code:
#include "Object.h"
#include <typeinfo>
#include <iostream>


const std::string Object::to_string (){
        return ( 
typeid(*this).name());
        
}

bool Object::equals (Objectin_obj){

    try{
        if(
typeid(*this) == typeid(in_obj)){
            return 
true;
        }else{
            return 
false;
        }
    }
    catch (
std::bad_typeid ){
        return 
false;
    }

}

ObjectObject::clone(){

    return (new 
Object());
}


void Object::print(){
    
std::cout << typeid(*this).name();
}



Object::Object(void)
{
}


Object::~Object(void)
{

Persona header
PHP Code:
#ifndef PERSONA_H
#define PERSONA_H

#include "object.h"
#include <string>

class Persona :    public Object
{

public:
    
Persona(std::string in_nomeint in_anno);
    
virtual ~Persona(void);

    
virtual const std::string to_string ();
    
virtual bool equals (Objectin_obj);
    
virtual Object* clone();

    
virtual std::string getnome();
    
virtual int getanno();

private:

    
std::string nome;
    
int anno;

};


#endif 
Persona cpp
PHP Code:
#include "Persona.h"
#include <string>


Persona::Persona(std::string in_nomeint in_anno)
{
    
nome=in_nome;
    
anno in_anno;
}


const 
std::string Persona::to_string (){

    return (
Object::to_string() + " Nome " nome " - Anno " std::to_string(anno));

}
bool Persona::equals (Objectin_obj){
    if( 
Object::equals(in_obj) && this->nome == ((Persona&)in_obj).getnome() && this->anno == ((Persona&)in_obj).getanno()){
        return 
true;
    }else{
        return 
false;    
    }

}

ObjectPersona::clone(){

            
Persona = new Persona(nome,anno);

            return 
t;

}


std::string Persona::getnome(){
        return 
nome;        
}

int Persona::getanno(){
    return 
anno;
}






Persona::~Persona(void)
{

Studente header
PHP Code:
#ifndef STUDENTE_H
#define STUDENTE_H
#include "persona.h"

class Studente : public Persona
{
public:
    
Studente(std::string in_nomeint in_annostd::string corso);
    
virtual ~Studente(void);

    
virtual std::string getcorso();

    
virtual const std::string to_string ();
    
virtual bool equals (Objectin_obj);
    
virtual Object* clone();

private:

    
std::string corso;

};


#endif 
Studente cpp
PHP Code:
#include "Studente.h"


Studente::Studente(std::string in_nomeint in_annostd::string in_corso)
    :
Persona(in_nome,in_anno)
{
    
corso in_corso;
}


std::string Studente::getcorso(){
    return 
corso;
}



Studente::~Studente(void)
{
}


const 
std::string Studente::to_string (){
    
    return (
Persona::to_string() + " - Corso " corso);

}


bool Studente::equals (Objectin_obj){
    if(
Persona::equals(in_obj) && ((Studente&)in_obj).getcorso() == corso)
        return 
true;
        return 
false;
}

ObjectStudente::clone(){

    return (new 
Studente(getnome(), getanno(),  corso));