Non capisco perchč nel main di questo programma non va bene la creazione dell' oggetto, il compilatore dice che non riesce a convertire.
codice:#ifndef ANIMALE_HPP #define ANIMALE_HPP #include <string> using namespace std; class Animale { protected: string specie; string nome; int anni; public: // Animale(string nome); Animale(string nome, int anni); Animale(string nome); ~Animale(); void setanni(int anni); void setnome(string n); void setspecie(string s); int getanni(); string getnome(); string getspecie(); }; #endif /* ANIMALE_HPP */
codice:#include "Animale.hpp" Animale:: Animale(string n) { nome=n; } Animale::Animale(string n, int a) { nome=n; anni=a; } Animale::~Animale() { specie=""; nome=""; anni=-1; } void Animale::setanni(int a){anni=a;} void Animale::setnome(string n){nome=n;} void Animale:: setspecie(string s){specie=s;} int Animale::getanni(){return anni;} string Animale::getnome(){return nome;} string Animale::getspecie(){return specie;}
codice:#ifndef CANE_HPP #define CANE_HPP #include "Animale.hpp" class Cane :public Animale { protected: string specie; string nome; int anni; public: Cane(string nome, int anni); Cane(string nome); ~Cane(); void setanni(int a); void setnome(string n); void setspecie(string s); int getanni(); string getnome(); string getspecie(); }; #endif /* CANE_HPP */
codice:#include "Cane.hpp" Cane::Cane(string name, int age):Animale(name, age) { anni=age; nome=name; } Cane::Cane(string nome):Animale(nome) { nome=nome; } Cane::~Cane() { specie=""; nome=""; anni=-1; } void Cane::setanni(int a){anni=a;} void Cane::setnome(string n){nome=n;} void Cane:: setspecie(string s){specie=s;} int Cane::getanni(){return anni;} string Cane::getnome(){return nome;} string Cane::getspecie(){return specie;}
E il main:
codice:#include <cstdlib> #include "Animale.hpp" #include <stdio.h> #include <iostream> using namespace std; /* * */ int main() { string nome="Scheggia"; int anni=7; Animale * a=new Cane(nome, anni); //errore cannot convert int* to Animale. cout << "Il cane si chiama " <<a->getnome()<<"." <<endl; cout <<"Il cane ha " <<a->getanni()<<" anni." <<endl; return 0; }

Rispondi quotando
