Skip to content

Instantly share code, notes, and snippets.

@lidio601
Last active August 29, 2015 14:01
Show Gist options
  • Save lidio601/afa5f5401fe6205d0ac5 to your computer and use it in GitHub Desktop.
Save lidio601/afa5f5401fe6205d0ac5 to your computer and use it in GitHub Desktop.
C++ Esempio ereditarietà
#include<iostream.h>
#include<conio.h>
class persona
{
protected:
char* nome;
public:
persona();
~persona();
void setnome(char*);
char* getnome();
void visual();
};
class studente:public persona
{
private:
int classe;
public:
studente();
void visual();
};
class docente:public persona
{
private:
char* materia;
public:
docente();
~docente();
void visual();
};
void main()
{
persona *ogg;
int tipo;
cout<<"\n\n CLASSE BASE CON UNA FUNZIONE VIRTUAL\n\n";
cout<<"inserisci 1 se scegli tipËo studente, 2 se docente: ";
cin>>tipo;
if(tipo)
ogg = new studente();
else
ogg = new docente();
ogg -> visual();
}
void persona::visual()
{
cout<<"\n\n le propriet‡ sono: "<<nome;
}
void studente::visual()
{persona::visual();
cout<<" "<<classe;
}
void docente::visual()
{persona::visual();
cout<<" "<<materia;
}
persona::persona()
{
this->nome="nome di default";
}
char* persona::getnome()
{
return this->nome;
}
void persona::setnome(char* nome)
{
if(nome!=NULL)
this->nome=nome;
}
studente::studente()
{
this->classe=1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment