I defined two class, one it's call Movil and the other it's call Camion which it's a derived class of Movil. The code that I am talking is:

#include <cstdlib>
#include <iostream>

//class definition
class Movil 
{  
                int ruedas; 
                float velocidadMax; 

                public: 
                        Movil (int ruedas=0, float velocidad=0); 
                        void setRuedas (int dato); 
                        void setVelocidad (float dato); 
                        int getRuedas (void); 
                        float getVelocidad (void); 
                       ~Movil (); 
      }; 

class Camion:public Movil 
{ 
                     float cargaMax; 
                     public: 
                     Camion (int ruedas=0, float velocidad=0, float cargaMax=0);
                     void setCargaMax (float dato); 
                     float getCargaMax (void); 
};
//end of class definition

using namespace std;

//main funtion
int main(int argc, char *argv[])
{   

    Camion *ptrCamion, c(4,100,1500); 
    Movil *ptrMovil, m(4,220); 

   ** ptrCamion=&m;**
    cout<<"ruedas:"<<ptrCamion->getRuedas()<<" velocidadMax: "<<ptrCamion->getVelocidad()<<" CargaMax: "<<ptrCamion->getCargaMax ()<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
} 
//end of main funtion

//defining methods

Movil::Movil (int ruedas, float velocidad) 
{ 
             this->ruedas=ruedas; 
             this->velocidadMax=velocidad;
             } 

void Movil::setRuedas (int dato) 
{ 
     this->ruedas=dato; 
     return; 
     } 

void Movil::setVelocidad(float dato) 
{ 
     this->velocidadMax=dato; 
     return; 
     }

int Movil ::getRuedas () 
{ 
    return ruedas; 
} 

float Movil::getVelocidad () 
{ 
      return velocidadMax; 
      } 

Movil::~Movil () 
{ 
              } 

void Camion::setCargaMax (float dato) 
{ 
     cargaMax=dato; 
     return; 
     } 

float Camion::getCargaMax () 
{ 
      return cargaMax; 
      }
Camion::Camion (int ruedas, float velocidad, float cargaMax):Movil (ruedas, velocidad) 
{ 
               this->cargaMax=cargaMax;
               }

The problem is when I assign a Camion pointer to Movil object ( ptrCamion=&m;). I do this because I want to see how inheritance is made, if I would change ruedas (which it's private) using a Camion class. But the compiler gives me an error on that line. It was my understanding that a derived class can be a base class and not in the other way.

Create public getter/setter methods for your class member variables and use those to get/set the member values. If you do that, you won't have to cast the child to parent class. Just call the methods on the Camion object. Also, you have your assignment reversed. It should be ptrMovil=&camionObject. Also, you should use the appropriate cast operation. In trivial cases like this, your approach (reversed) will work, but it won't in cases where you have multiple inheritence. Avoid casting unless ABSOLUTELY necessary. Using the appropriate methods is a better approach, and more reliable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.