• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

How would you refactore this code, on your ways, for best performance and simplicity.

Stellow

C++/C#/PHP/LUA
Joined
Oct 23, 2008
Messages
1,112
Reaction score
221
Location
Germany
GitHub
eubrunomiguel
Just a topic to discuss the best c++ new approaches for performance and simplicity.
- Note you need a base class animal, and subclasses.


Code:
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <cassert>
using namespace std;

enum class animals{ fish, insects, reptiles, mammals, amphibians, birds, last};
ostream& operator<<(ostream& os, animals obj)
{
    switch (static_cast<int>(obj))
    {
    case 0: os << "Fishes"; break;
    case 1: os << "Insects"; break;
    case 2: os << "Reptiles"; break;
    case 3: os << "Mammals"; break;
    case 4: os << "Amphibians"; break;
    case 5: os << "Birds"; break;
    default: os << "Unknow";
    }
    return os;
}
class animal
{
public:
    void printAllAnimals() const;
    int returnAnimalCount() const { return animalC; };
    virtual void push_animal(string&&){};
    virtual void print() const{};
protected:
    static int animalC;
    void insert(const animals&, string&&);
    void printAnimalTable(const animals&) const;
private:
    vector<vector<string>> container{static_cast<int>(animals::last)};
};
int animal::animalC = 0;
void animal::insert(const animals& obj, string&& s)
{
    container[static_cast<int>(obj)].push_back(move(s));
    animalC++;
}
void animal::printAnimalTable(const animals& obj) const
{
    cout << obj << ": ";
    for (int i = 0; i < container[static_cast<int>(obj)].size(); i++)
    {
        cout << container[static_cast<int>(obj)][i] << " ";
    }
}
void animal::printAllAnimals() const{
    for (int i = 0; i < container.size(); i++)
    {
        cout << animals(i) << ": ";
        for (int j = 0; j < container[i].size(); j++)
        {
            cout << container[static_cast<int>(i)][j] << " ";
        }
        cout << endl;
    }
}
class fish : public animal
{
public:
    fish() :numFishes(0){};
    void push_animal(string&& s)
    {
        insert(animals::fish, forward<string>(s));
        numFishes++;
    };
    void print() const { printAnimalTable(animals::fish); };
private:
    int numFishes;
};


/* 
other similar classes to fish, but with other animals name
*/

void menu()
{
    cout << "MENU - Please enter a number to select an option" << endl;
    int i = 0;
    for (i; i < static_cast<int>(animals::last); i++)
    {
        cout << i + 1 << " - Insert an " << animals(i) << endl;
    }
}
animal* selectMenu()
{
    animal* ptr = nullptr;
    int option = 0;
    cout << "Enter an option: ";
    cin >> option;
    cin.clear();
    switch (--option)
    {
    case static_cast<int>(animals::fish) : ptr = new fish(); break;
    case static_cast<int>(animals::insects) : ptr = new insects(); break;
    case static_cast<int>(animals::reptiles) : ptr = new reptiles(); break;
    case static_cast<int>(animals::mammals) : ptr = new mammals(); break;
    case static_cast<int>(animals::amphibians) : ptr = new amphibians(); break;
    case static_cast<int>(animals::birds) : ptr = new birds(); break;
    default: ptr = nullptr;
    }
    return ptr;
}
int main() {
    menu();
    unique_ptr<animal> a{ selectMenu() };
    assert(a != nullptr);
    a->push_animal("animal");
    a->printAllAnimals();
    /*a->printAllAnimals();
    a->push_animal("fish");
    cout << a->returnAnimalCount();
    a->printAllAnimals();
    a->print();*/
    system("pause");
    return 0;
}
 
Back
Top