dimanche 21 mars 2021

How to create list that contains multiple types of objects in C++

Scenario:

I am on design phase in OOP with c++. I just want to design a move action with four types of action. These actions has same common variables and function. Also these classes has some discrete variables and functions. Same functions and variables located in Class_Action(base class).

Other four types of action given in below inherits Class_Action.

class Class_Action_Action_Type_A : public Class_Action 

class Class_Action_Action_Type_B : public Class_Action 

class Class_Action_Action_Type_C : public Class_Action 

class Class_Action_Action_Type_D : public Class_Action 

I want to hold all move action in a list with generic type as Class_Move. Class_Move inherits all Action types. It errors as "no instance of overloaded function mathes the argument list."

I have there following question:

1-) How can I apply the above status?

2-) As you know if I inherit all four action class from Class_Move every initialization of Class_Move hold other Classes members(funtion and variable). For example, I want to add Action_Type_A to Class_Move when I reach the element as Class_Move it holds set_action_type_B_time variable located in Action_Type_B. How can I avoid redundancy of parameters?

3-) What is the optimum way to design these problem with OOP.

4-) Which Design Pattern can be used in this case? (I am try to apply Builder Pattern)

Thanks,

I have following classes:

-------------------------------------------------------------------------
Class_Action.h :

class Class_Action
{
protected:
    float action_time_f32;               
    float battery_consumption_result_f32;    
    float battery_consumption_value_f32;                                             
public:

    Class_Action                             ();

    void     update_vehicle_current_altitude ();
    void     set_battery_consumption_value   (float p_battery_consumption_value); 
    void     set_battery_consumption_result  (float p_battery_consumption_result);
    void     set_Action_time                 (float p_action_time);

    float    calc_battery_consumption        ();
    float    get_battery_consumption_value   ();
    float    get_battery_consumption_result  ();
    float    get_action_time                 ();

};
-------------------------------------------------------------------------
-------------------------------------------------------------------------
Class_Action_Action_Type_A.h :

class Class_Action_Action_Type_A : public Class_Action 
{
private:
    float  end_point_f32;       
    float  start_point_f32;                                
    float  action_type_A_rate_f32;         
public:
    Class_Action_Action_Type_A                   ();
    Class_Action_Action_Type_A                   (float battery_consumption_value_f32,
                                                  float action_type_A_rate_f32,
                                                  float start_point_f32,
                                                  float end_point_f32);
    void     update_vehicle_current_altitude     ();
    void     set_end_point                       (float p_end_point);
    void     set_start_point                     (float p_start_point);
    void     set_action_type_A_rate              (float p_action_type_A_rate);

    float  calc_battery_consumption              ();  
    float  calc_action_type_A_time               ();  
    float  get_end_point                         ();
    float  get_start_point                       ();
    float  get_action_type_A_rate                ();
};
-------------------------------------------------------------------------
-------------------------------------------------------------------------
Class_Action_Action_Type_B.h :

class Class_Action_Action_Type_B : public Class_Action
{
private:
    uint16_t  action_type_B_time_u16;                                                   
public:
    Class_Action_Action_Type_B                  ();
    Class_Action_Action_Type_B                  (float         p_battery_consumption_value,
                                                 uint16_t      p_action_type_B_time_u16);
    void      update_vehicle_current_altitude();
    void      set_action_type_B_time                (uint16_t  p_action_type_B_time);
    float     get_action_time                       ();
    uint16_t  get_action_type_B_time                ();
    float     calc_battery_consumption              ();   



};
-------------------------------------------------------------------------
-------------------------------------------------------------------------
Class_Action_Action_Type_C.h :

class Class_Action_Action_Type_C : public Class_Action
{

private:
    float destination_distance_f32;
    float vehicle_speed_f32;    
public:
    Class_Action_Action_Type_C                  ();
    Class_Action_Action_Type_C                  (float battery_consumption_value_f32,
                                                 float destination_distance_f32,
                                                 float vehicle_speed_f32);
    void    update_vehicle_current_altitude     ();
    void    set_destination_distance            (float p_destination_distance);
    void    set_destination_vehicle_speed       (float p_vehicle_speed);

    float   calc_battery_consumption            ();
    float   calculate_flight_time               ();
    float   get_destination_distance            ();
    float   get_destination_vehicle_speed       ();

    
};
-------------------------------------------------------------------------
-------------------------------------------------------------------------
Class_Action_Action_Type_D.h :

class Class_Action_Action_Type_D : public Class_Action
{
private:
    float  end_point_f32;       
    float  start_point_f32;                                
    float  action_type_D_rate_f32;         
public:
    Class_Action_Action_Type_D                   ();
    Class_Action_Action_Type_D                   (float battery_consumption_value_f32,
                                                  float action_type_D_rate_f32,
                                                  float start_point_f32,
                                                  float end_point_f32);
    void     update_vehicle_current_altitude     ();
    void     set_end_point                       (float p_end_point);
    void     set_start_point                     (float p_start_point);
    void     set_action_type_D_rate              (float p_action_type_D_rate);

    float  calc_battery_consumption            ();   
    float  calc_action_type_D_time             ();  
    float  get_end_point                       ();
    float  get_start_point                     ();
    float  get_action_type_D_rate              ();

};
-------------------------------------------------------------------------

-------------------------------------------------------------------------
Class_Move.h:

class Class_Move: public Class_Action_Action_Type_A, public Class_Action_Action_Type_B, public Class_Action_Action_Type_C, public Class_Action_Action_Type_D
{                                    
public:

    Class_Move                              ();

};
-------------------------------------------------------------------------


-------------------------------------------------------------------------
main.c:

#include <list.h>
#include "Class_Move.h"
int main()
{
    
    list<Class_Move*>           list_Moves;
    list<Class_Move*>::iterator iter_Moves;
    
    list_Moves.push_back(new Class_Action_Action_Type_A());
    list_Moves.push_back(new Class_Action_Action_Type_B());
    list_Moves.push_back(new Class_Action_Action_Type_C());
    list_Moves.push_back(new Class_Action_Action_Type_D());
}

Aucun commentaire:

Enregistrer un commentaire