mercredi 21 août 2019

Inheritance and polymorphism in my Class structure

I do understand class inheritance and polymorphism in c++ but this is the first time i am doing something big and complex.

I do agree that my solution may not be a good soulution as initialy i was not aware of the scope and recquirments completely.

The Project

The software renders geometric shapes like sphere , cube , triangle.

 1) I created a tree model having nodes.

This is the header file for the node.

class TreeItem
{
public:
    explicit TreeItem( Container *data , TreeItem *parent = 0 );
    ~TreeItem();
          Container* GetContainer();
    /* all the functions for the tree item*/

private:
    QList<TreeItem*> childItems;
    Container* itemData;
    TreeItem* parentItem;
};

2) Each node has Container class object as data item.Container class contains all the data for the object like color transformation etc.

class Container
{ 
    private:
    std::string stdstrContainerName;
    std::string stdstrPluginType;
    Geometry* Geom;

public: 
    Container();
    Container(std::string, std::string, const Sum_Material &material , Sum_Texture_2D tex ,           Sum_Texture_2D tex2 ,  Sum_Texture_CubeMap cubemap , WavefrontRenderer*           wavefrontrenderer , Geometry* geometry = nullptr  );
    Container(const  Container& obj);
    Container& operator = (const Container& c);
    ~Container();
          Geometry* GetGeometry();
    /* All the container class functions Here*/
};

3) i loop through all the treeitems get their container object.

4) From container i get the geometry object and than call the Render function of the geometry object which is a virtual function.

class Geometry
{

public: 
    std::string stdstrType;
    bool bValid;

public: 
     Geometry()
    {
        stdstrType = "NO_TYPE";
        bValid = false;
    }

     Geometry( std::string strType , bool bValue )
    {
        stdstrType = strType;
        bValid = bValue;        
    }

     Geometry(const Geometry &g)
    {
        stdstrType = g.stdstrType;
        bValid = g.bValid;
    }

     Geometry( Geometry *g)
     {
         stdstrType = g->stdstrType;
         bValid = g->bValid;
     }

     ~Geometry()
     {

     } 

     Geometry & Geometry::operator = (const Geometry &g)
     {
         if (this != &g)
         {
             stdstrType = g.stdstrType;
             bValid = g.bValid;
         }
         return *this;
     }    
     virtual void draw();
     virtual void CleanUp();
     bool isValid();
     void setValidState(bool bState);
     virtual void Init();
     std::string GetName();
};

5) The reason why Geometric object is a pointer in container is to avoid slicing as this object is inherited by the geometric objects.

the Header file for Sphere object.

class Sph : public Geometry
{
public:
    Sph( float radius , float segments , const Shader& shader );
    Sph(const Sph& geometry);
    Sph& operator = (const Sph& geometry);
    ~Sph();
    void init();
    void CleanUp();
    void draw();

private:
    float fRadius, fSegments;
    bool isInited;
    unsigned int m_VAO, m_VBO;
    int iNumsToDraw;
    bool isChanged;
    Shader shader;

};

6) This is how i create sphere

Sph* sphere = new Sph( 100.0 , 60 ,sh);
sphere->setValidState(true);
sphere->stdstrType = "Sphere";
Container cont("Sphere" , "SPHERE" , Mat , tex , tex2 , cubeMapTex , this , sphere );

Container needs a geometric object pointer so i pass a Sphere object pointer which has Geometry class as base object.

this whole workflow is working but if i could make this structure better.

Aucun commentaire:

Enregistrer un commentaire