[ create a new paste ] login | about

Link: http://codepad.org/mPP6OlQT    [ raw code | output | fork ]

C++, pasted on Dec 14:
 #include <iostream>
using namespace std;

class Army                         // Class declaration
{
private:
    int numberSuffix_;     
    int armyCode_;                
   
public:
    Army(int armycode, int suffix) {
         //cout << "Parent Class:: " << __func__ << endl;     
    }
   
};

class ArmyHospital : virtual public Army {
private:
    string hospitalName_;
public:
    ArmyHospital(int armycode, int suffix, string name) : Army(armycode, suffix) {
       hospitalName_ = name;
       //cout << "Derived Class: " << __func__ << endl;
    }
};


int main() 
{

    Army* off = new Army(2,1);
  
    ArmyHospital* hos = new ArmyHospital(2, 110, "Rosy");


    /* This is valid as instance of derived class
     * can always be substituted to an instance of 
     * base class. It might lose some information (Slicing)
     */
    off = hos;
    // This is not legal.
    hos = off;
    return 0;
}


Output:
1
2
3
In function 'int main()':
Line 42: error: invalid conversion from 'Army*' to 'ArmyHospital*'
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: