File originale in java
main
Object header
Object cpp
Persona header
Persona cpp
Studente header
Studente cpp
PHP Code:
http://www.xgiovio.com/forum/forum/programming/java/447-gerarchie-object-persona-studente
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 * c = dynamic_cast <Studente *> (a.clone());
std::cout << c->to_string() << std::endl;
if(a.equals(*c))
std::cout << "equals" << std::endl;
std::cin.get();
}
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 (Object& in_obj);
virtual void print();
protected:
virtual Object* clone() ;
};
#endif
PHP Code:
#include "Object.h"
#include <typeinfo>
#include <iostream>
const std::string Object::to_string (){
return ( typeid(*this).name());
}
bool Object::equals (Object& in_obj){
try{
if(typeid(*this) == typeid(in_obj)){
return true;
}else{
return false;
}
}
catch (std::bad_typeid & e ){
return false;
}
}
Object* Object::clone(){
return (new Object());
}
void Object::print(){
std::cout << typeid(*this).name();
}
Object::Object(void)
{
}
Object::~Object(void)
{
}
PHP Code:
#ifndef PERSONA_H
#define PERSONA_H
#include "object.h"
#include <string>
class Persona : public Object
{
public:
Persona(std::string in_nome, int in_anno);
virtual ~Persona(void);
virtual const std::string to_string ();
virtual bool equals (Object& in_obj);
virtual Object* clone();
virtual std::string getnome();
virtual int getanno();
private:
std::string nome;
int anno;
};
#endif
PHP Code:
#include "Persona.h"
#include <string>
Persona::Persona(std::string in_nome, int 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 (Object& in_obj){
if( Object::equals(in_obj) && this->nome == ((Persona&)in_obj).getnome() && this->anno == ((Persona&)in_obj).getanno()){
return true;
}else{
return false;
}
}
Object* Persona::clone(){
Persona * t = new Persona(nome,anno);
return t;
}
std::string Persona::getnome(){
return nome;
}
int Persona::getanno(){
return anno;
}
Persona::~Persona(void)
{
}
PHP Code:
#ifndef STUDENTE_H
#define STUDENTE_H
#include "persona.h"
class Studente : public Persona
{
public:
Studente(std::string in_nome, int in_anno, std::string corso);
virtual ~Studente(void);
virtual std::string getcorso();
virtual const std::string to_string ();
virtual bool equals (Object& in_obj);
virtual Object* clone();
private:
std::string corso;
};
#endif
PHP Code:
#include "Studente.h"
Studente::Studente(std::string in_nome, int in_anno, std::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 (Object& in_obj){
if(Persona::equals(in_obj) && ((Studente&)in_obj).getcorso() == corso)
return true;
return false;
}
Object* Studente::clone(){
return (new Studente(getnome(), getanno(), corso));
}