use of subclasses

 

Hey Guys

I have two subclasses with lots of the same functions and I would like to call a series of same functions from both classes, the simplest example of what I would like to do is below (btw I use "virtual" to ensure the sub-base function is prioritized over superclass function!)

int OnStart()

{

    BigClass bigClass;

    SubClass1 sub1;

    SubClass2 sub2;

    for(int i=0;i<=1;i)

    {

         if(i==0) bigClass=sub1;

         else bigClass=sub2;

         bigClass.print();

    }

    return(0);

}




class BigClass

{

      public:

      double a,b;

      virtual void print(){Print("hello");}

};



class SubClass1 :   BigClass

{

      public:

      void print(){Print("hello1");}

};


class SubClass2 :   BigClass

{

      public:

      void print(){Print("hello2");}

};


 

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


 
wilson_wilson:

Hey Guys

I have two subclasses with lots of the same functions and I would like to call a series of same functions from both classes, the simplest example of what I would like to do is below (btw I use "virtual" to ensure the sub-base function is prioritized over superclass function!)



Not sure what you wanted to ask, as you did not place any question. But in case you want to copy the object to another type, the inheritance must be public, and a copy constructor for that type would be necessary.
 
Ex Ovo Omnia:

Not sure what you wanted to ask, as you did not place any question. But in case you want to copy the object to another type, the inheritance must be public, and a copy constructor for that type would be necessary.

is there a way to use a "generic" pointer or generic subclass that gets assigned a different subclass on each iteration ? I prefer not to explicitly do copy constructor as the two subclass objects already exist and I prefer not to write many more lines of code.
 
Use pointers - then you'll have no need to copy objects and implement copy constructors. Pointer of base type can hold actual pointers to objects of derived classes.
 
Stanislav Korotky:
Use pointers - then you'll have no need to copy objects and implement copy constructors. Pointer of base type can hold actual pointers to objects of derived classes.

yep, base class pointer did the trick :)