Search code examples
c++downcast

Overriding CAST operator (i think it is called downcasting)


I ma quite new to c++ and i got a small problem that is probably easy for others. I have a class A and a class B that extends class A. I have an object A, lets call it a1. I want to downcast a1 to a type B class using the syntax: "B b1=(B)a1;"

 class IAmortizabil
 {
 public:
     virtual double getAmortizare()=0;
 };



 class Utilaj : public IAmortizabil{
 protected:
     float valInventar;
     char codUtilaj[10];
     char *denUtilaj;
     int anIntrFunct;
     int durataNormata;

 public:

     Utilaj(){
         denUtilaj=NULL;
         valInventar=0;
         anIntrFunct=0;
         durataNormata=0;
     }

     Utilaj(char *codUtilaj, char *denUtilaj, float valInventar, int      anIntrFucnt, int durataNormata){
         strcpy(this->codUtilaj, codUtilaj);
         this->denUtilaj=new char[strlen(denUtilaj)+1];
         strcpy(this->denUtilaj, denUtilaj);
         this->valInventar=valInventar;
         this->anIntrFunct=anIntrFucnt;
         this->durataNormata=durataNormata;
     }

     Utilaj(Utilaj &u)
     {
         strcpy(codUtilaj, u.codUtilaj);
         denUtilaj = new char[strlen(u.denUtilaj) + 1];
         strcpy(denUtilaj, u.denUtilaj);
         valInventar = u.valInventar;
         anIntrFunct = u.anIntrFunct;
         durataNormata = u.durataNormata;
     }


     friend ostream &operator<<(ostream &iesire, Utilaj &u)
     {
         iesire << " cod utilaj " << u.codUtilaj << endl;
         iesire << "denumire utilaj " << u.denUtilaj << endl;
         iesire << "valoare inventar " << u.valInventar << endl;
         iesire << "an intrare in functiune " << u.anIntrFunct << endl;
         iesire << " durata normata " << u.durataNormata << endl;
         return iesire;
     }

     Utilaj operator=(Utilaj &u)
     {
         strcpy(codUtilaj, u.codUtilaj);
         if (denUtilaj != NULL)
             delete[]denUtilaj;
         denUtilaj = new char[strlen(u.denUtilaj) + 1];
         strcpy(denUtilaj, u.denUtilaj);
         valInventar = u.valInventar;
         anIntrFunct = u.anIntrFunct;
         durataNormata = u.durataNormata;
         return *this;
     }



     double getAmortizare()
     {
         cout << '\n';
         if (durataNormata != 0)
             return valInventar / durataNormata;
         else {
             cout << "Durata normata este 0=>eroare";
             return 0;
         }
     }


     ~Utilaj(){
         if(denUtilaj!=NULL){
             delete[]denUtilaj;
         }
     }
 };


 class Strung :public Utilaj
 {
 protected:
     int nrBucati;

 public:
     Strung() :Utilaj()
     {
         nrBucati = 0;
     }
     Strung(char *_codUtilaj, char *_denUtilaj, float _valInventar, int _an, int _durata, int _nrBucati) :Utilaj(_codUtilaj, _denUtilaj, _valInventar, _an, _durata)
     {
         nrBucati = _nrBucati;
     }

     Strung(Strung &s) :Utilaj(static_cast< Utilaj&>(s))
     {
         nrBucati = s.nrBucati;
     }

         };

EDIT: Addeted my classes maybe it helps. I know nothing here is "the way to do it" but my teacher wants them like this and it is no point in arguing with him.


Solution

  • In C++ you can cast a pointer to a base type into a pointer to a derived type using dynamic_cast, assuming it pointed to the derived type in the first place.

    class A
    {
    };
    
    class B : public A
    {
    };
    
    int main()
    {
        A * a = new B();
        B * b = dynamic_cast<B*>(a);
    
        if( b == nullptr )
        {
            // Error - "a" was not a "B" object
        }
    
        return 0;
    }
    

    When you see (sometype)Object, in C++, that is a C-style cast, and is not the way to enlightenment. This is equivalent to a C++ reinterpret cast and can be quite dangerous. It certainly should not be used where a dynamic_cast is sufficient.

    Also, in C++, when you have a polymorphic type, you must use a pointer. You would not cast an instance of an object, but rather, you would cast a pointer to the object.

    Please Google both "dynamic_cast" and "reinterpret_cast" for a better understanding of both.