vendredi 13 septembre 2019

How should i write the constructor for my class

I have a container class to which i can send a Geometry object as constructor argument.

Geometry is a polymorphic class as interface for other geometric types like Sphere and Rectangle.

My question is that in Container class the constructor with arguments "Container(std::string str, Geometry* geometry)" is it the correct may to code it.

include "pch.h"
#include <iostream>
#include<fstream>
#include "Container.h"
#include "Geometry.h"
#include "Sphere.h"
#include "Rectangle.h"
#include "Container.h"

int main()
{
    const char* fileName = "saved.txt"; 
    Sphere sph;
    Rectangle rect;
    Container contSphere("ABC", &sph);
    Container contRectangle("DEF", &rect);
    Sphere* s = (Sphere*)contSphere.getGeomtry();
    s->PrintGeom();
    Rectangle* r = (Rectangle*)contRectangle.getGeomtry();
    r->PrintGeom(); 
    do
    {
        std::cout << '\n' << "Press a key to continue...";
    } while (std::cin.get() != '\n');
}

///////////////////////////////////////////////////////////////////////////////////////////////

#pragma once
#include <string>
class Geometry
{
private:
    std::string stdstringGeom;
    std::string stdstrType;

public:

    Geometry() : stdstringGeom("GeometyrString"), stdstrType("Geometry") {}

    virtual std::string getType()
    {
        return stdstrType;
    }

     virtual void  PrintGeom()
    {
        std::cout << "geometry virtual function";
    }

};

/////////////////////////////////////////////////////////////////////////////////

#pragma once

#include "Geometry.h"
class Sphere : public Geometry
{
private:
    std::string stdstrSphere;
    std::string stdstrType;


public:
    Sphere() : Geometry() ,  stdstrSphere( "DefaultSphere") , stdstrType("Sphere") {}
    Sphere( std::string str) : Geometry() ,  stdstrSphere(str) , stdstrType("Sphere"){}
    void PrintGeom()
    {
        std::cout << "Sphere Virtual Function" << std::endl;
    }

    std::string getType()
    {
        return stdstrType;
    }

};

///////////////// Defination for Constructor class////////////////////

#include "Geometry.h"
#include "Sphere.h"
#include "Rectangle.h"

class Container
{
private:
    std::string stdstrCont;
    Geometry* geom;

public:
    Container() : stdstrCont("NoName") { geom = new Geometry; }
    Container(std::string str, Geometry* geometry) : stdstrCont(str)
    {
    // I am doing this to avoid slicing and i want to do a deep copy.   
     if (geometry->getType() == "Sphere")
        {
            Sphere* sph = (Sphere*)geometry;
            geom = new Sphere(*sph);
        }
        else if (geometry->getType() == "Rectangle")
        {
            Rectangle* rec = (Rectangle*)geometry;
            geom = new Rectangle(*rec);

        }
    }

    ~Container()
    {
        if (geom != nullptr)
            delete geom;
    }

    Geometry* getGeomtry()
    {
        return geom;
    }

    void PrintContainer()
    {
        std::cout << stdstrCont;
    }
};

Aucun commentaire:

Enregistrer un commentaire