Shadow2531
[H]ard|Gawd
- Joined
- Jun 13, 2003
- Messages
- 1,670
I've never really needed to make my own class for anything. I've always just made functions for something I needed to do, but I am curious. I made an example (just an example) function (listed first below) and then made a class to do the same thing. The class code works, but is it correct? Could I simplify it more than that? It's my first try at making a class.Thanks
Code:
#include <iostream>
#include <string>
#include <map>
using namespace std;
string getkw(const string& name) {
map<string,string> keywords;
keywords["test"] = "bla bla";
return keywords[name];
}
int main() {
cout << getkw("test") << endl;
}
Code:
#include <iostream>
#include <string>
#include <map>
using namespace std;
class kw {
public:
explicit kw();
string operator[](const string& name);
private:
map<string,string> keywords;
};
kw::kw() {
keywords["test"] = "bla bla";
}
string kw::operator[](const string& name) {
return keywords[name];
}
int main() {
kw getkw;
cout << getkw["test"] << endl;
}