C++ OOP (virtual) and multiple inheritance

Discussion on data structures & algorithms, programming languages, computer science and more
doctorlai
Site Admin
Posts:44
Joined:Tue Jan 15, 2013 3:16 pm
C++ OOP (virtual) and multiple inheritance

Post by doctorlai » Thu Jul 30, 2015 11:38 am

Code: Select all


class WIFI
{
public :
   WIFI()
   {
      
   }

   void foo()
   {
      std::cout << "WIFI";
   }
};

class LTE
{
public :
   LTE()
   {

   }

   void foo()
   {
      std::cout << "LTE";
   }
};

class XSystem : public LTE, public WIFI
{
public :
   XSystem()
   {

   }

   void foo()
   {
      std::cout << "XSystem";
   }
};


int _tmain(int argc, _TCHAR* argv[])
{
   LTE obj;   
   ((XSystem*)(&obj))->foo();

   int i;
   std::cin >> i;
   return 0;
}



The above prints XSystem, however, if you add a virtual keyword at parent class LTE.foo(), then it will print LTE.

Post Reply