Basic question about class

To add comments, please log in or register
Kittamet Sirinyamas
671
Kittamet Sirinyamas  

Hi experts,

I have a little bit confuse about class.Would someone please clarify this?  Please see following code and its result,

The results show different despite both function are called from base class.Why not the same?

I understand that, If there is prefix "virtual" in front of the function , They must be used by child only?

Thank you so much

class CBase
  {
public:
   virtual string Function()
     {
      return("2 = Base");
     }
     string Function2()
     {
      return("2 = Base");
     }
  };
//+------------------------------------------------------------------+
//|   Child class 1                                                  |
//+------------------------------------------------------------------+
class Class1: public CBase
  {
public:
   string Function()
     {
      return("1 = Child");
     }
  };
//+------------------------------------------------------------------+


Class1 c1; // Load class 1

//+------------------------------------------------------------------+
//|   Function to process objects                                    |
//+------------------------------------------------------------------+
void Function(CBase  &c)
  {
   Alert(c.Function());
  }
  
  void Function2(CBase  &c)
  {
   Alert(c.Function2());
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
// Process objects using one function.
   Function(c1);
   Function2(c1);

  }


Laszlo Tormasi
5120
Laszlo Tormasi  
Kittamet Sirinyamas:

Hi experts,

I have a little bit confuse about class.Would someone please clarify this?  Please see following code and its result,

The results show different despite both function are called from base class.Why not the same?

I understand that, If there is prefix "virtual" in front of the function , They must be used by child only?

Thank you so much


Why would they be called from base class once you declared c1 as Class1?
Kittamet Sirinyamas
671
Kittamet Sirinyamas  
Laszlo Tormasi:
Why would they be called from base class once you declared c1 as Class1?

Because Class1 is a child of base. It can be used.

But This's not point, I doubt that how "virtual" prefix make different result ? .

William Roeder
18878
William Roeder  
Kittamet Sirinyamas: I doubt that how "virtual" prefix make different result ? .

All virtual does is allow a sub-class to override the base-class method. No override and you get the base-class version. Class1 overrides Function() with

return("1 = Child");
So any call to a class1 instance does that.
Kittamet Sirinyamas
671
Kittamet Sirinyamas  
William Roeder:

All virtual does is allow a sub-class to override the base-class method. No override and you get the base-class version. Class1 overrides Function() with

So any call to a class1 instance does that.

Thank you

One more thing, plz see following code and its result.

Why the result still return sub-class even though no virtual function?'

class CBase
  {
public:
    string Function()
     {
      return("Base");
     }
  };
//+------------------------------------------------------------------+
//|   Child class 1                                                  |
//+------------------------------------------------------------------+
class Class1: public CBase
  {
public:
   string Function()
     {
      return("child");
     }
  };

Class1 c1;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
  Alert(c1.Function());
  }


Seng Joo Thio
556
Seng Joo Thio  
Kittamet Sirinyamas:

One more thing, plz see following code and its result.

Why the result still return sub-class even though no virtual function?'

Virtual or not, class inheritance will allow similar named functions in sub-classes to replace those in the root classes.

However, virtual declaration (done correctly) in root class will REQUIRE sub-classes to implement those functions. To see what I mean, change your CBase to the following:

class CBase
  {
public:
    virtual string Function() = 0;
  };

This way, if you don't implement Function() in your Class1, the compiler will not let you proceed.

One use case will be for a container class - you want all inherited sub-classes to implement a suitable 'print' function that caters to peculiar data structures within those sub-classes.

nicholi shen
2380
nicholi shen  
Seng Joo Thio:

Virtual or not, class inheritance will allow similar named functions in sub-classes to replace those in the root classes.

However, virtual declaration (done correctly) in root class will REQUIRE sub-classes to implement those functions. To see what I mean, change your CBase to the following:

This way, if you don't implement Function() in your Class1, the compiler will not let you proceed.

One use case will be for a container class - you want all inherited sub-classes to implement a suitable 'print' function that caters to peculiar data structures within those sub-classes.

Technically what you are referring to is called a "pure virtual method" which requires subclasses to implement it, but you can also define virtual methods that do not require subclass implementation. The virtual keyword just modifies a method to use polymorphism. 

To add comments, please log in or register