jeudi 30 janvier 2020

C++: Design suggestions for class

Lets say i have an object CAR

class CAR
{
public:
    string doorColor
    string seatColor
    string engineCC
    string engineTransmission 
    string engineTorque
    string enginePower
    string steering
    string break
    void initCarSpec();

    ..
}

At some part i have a business logic based on engine , where the logic is based only on engine properties and does not care about rest of the properties. The current design is this way loading up the car object and iterating through it :

std::map <std::string , std::shared_ptr <CAR>> carsPortfolio 
carsPortfolio["Ford"] = carspec1
carsPortfolio["Tesla"] = carspec2
carsPortfolio["BMW"] = carspec3

for(auto itr = carsPortfolio.begin(); itr != carsPortfolio.end(); ++itr)
{
    if (itr->engineCC > x) {do something}
    if (itr->enginePower > y) {do something}
}

I m thinking it would be better not to load the entire object , instead create a struct only for engine spec and iterate through it.

struct Engine
{
    string engineCC
    string engineTransmission 
    string engineTorque
    string enginePower
};

class CAR
{
public:
    string doorColor
    string seatColor
    struct Engine engineSpec;
    string steering
    string break
    void initCarSpec();

    ..
}

std::map <std::string ,std::shared_ptr <struct Engine>> engineSpecs;
engineSpecs["Ford"] = engineSpec1;
engineSpecs["Tesla"] = engineSpec2;
engineSpecs["BMW"] = engineSpec3;
for(auto itr = engineSpecs.begin(); itr != engineSpecs.end(); ++itr)
{
    if (itr->engineCC > x) {do something}
    if (itr->enginePower > y) {do something}
}

Please suggest.. I m also looking for suggestions for a good material to read , i did a lot of reads on the web which got me more confused than before.

Aucun commentaire:

Enregistrer un commentaire