Let's take cnode as example. It currently does two things.
It holds the information of a character (name, class, gender) and it holds the information about the next character.
This can be split up. Let one class focus on the information needed for a character. And let another class take care of holding all the characters.
At the bottom of this post is a little program that uses a std::list to hold the characters.
Before you make it more efficient make it work. I have seen too much people (including me!) who start optimisation and refactoring without having anything working. If you wait until you have a working piece of software, you will notice if you break anything while refactoring / optimising.
In my optinion there will be some repetitive code, because you must perform similar actions on different objects.
Another thing: do not put std::cin in a constructor. There are situations where default objects are created (e.g. std::vector::resize), and then the user is forced to enter all the data. If you need cin to get data needed for constructing of an object, put it in a seperate function.
Ok, now here is the little peace of code, that allows you to create characters and displaying them.
Checking whether a character already exists with a given name is not implemented yet. Neither is editing.
Adding another type to bank, e.g. weapons is basically done like the characters. Add a class / struct, add a list in bank, add the functions. After that check what code is duplicated and try to merge it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include <list>
#include <iostream>
#include <string>
using namespace std;
//Its a struct, because it just holds data and I don't think that getters and setters are needed in this snippet.
struct character
{
string name;
string gender;
string type;
};
class bank
{
public:
bank()
: characters()
{
}
void
add_character()
{
characters.push_back(create_new_character());
}
void
display()
{
for (list<character>::iterator it = characters.begin();
it != characters.end();
++it)
{
cout << it->name << ", " << it->gender << ", " << it->type << endl;
}
}
private:
list<character> characters;
character
create_new_character()
{
character new_character;
cout<<"Please Enter Name"<< endl;
getline(cin, new_character.name);
//Check whether a character already has this name [e.g. find_if]
cout<<"please Enter Gender"<< endl;
getline(cin, new_character.gender);
cout<<"Enter Character Class"<< endl;
getline(cin, new_character.type);
return new_character;
}
};
int
main()
{
bank players_bank;
char option;
do
{
cout << "\n\n\n";
cout << "Main Menu\n\n";
cout << "1. Add Character\n";
cout << "5. Display All\n";
cout << "Quit" << endl;
cin >> option;
cin.ignore();
switch (option)
{
case '1':
players_bank.add_character();
break;
case '5':
players_bank.display();
break;
case 'q':
break;
default:
cout << "You have entered an invalid option" << endl;
break;
}
}
while (option != 'q');
}
|
If you are not familiar with list check out
http://www.cplusplus.com/reference/list/
And note that another container (e.g. vector) is also possible. But since you will add (and delete) characters most of the time list seems to be a good choice.
Regards,
mathes