I will try to add some things to already posted answers.
There are two ways about making this OOP.
- one way is class holding the actual string, responsible for manipulation with it.
- other way is helper utility class providing only functions, not holding any string at all.
Your code is unfortunately weird mix of both. I will try to show headers for both ways here (just by hand, will very likely not compile without fixing, plus it's missing implementation): (and I'm afraid I'm at the moment tainted by Java too much (coding in Java in other window, so I may mess up some C++ syntax badly, this answer is more about OOP-like ideas, how to design your API and classes).
class SmartString { private: // I don't mind explicit private, it's easier to read for me std::string str; // my string, my responsibility public: SmartString(const std::string & initstr) : str(initstr) { } const std::string & get() const { return str; } const std::string & reverseAndGet(); int length() const { return str.length(); } void display(std::ostream & out) const; }
Ideas behind this one:
- display can be redirected to any
ostream
, binding it with std::cout
in method body would limit usage of such class. This is important especially with unit testing, where you want to fake and have under control any external systems the tested code does use, so providing the code with external system instance during construction, or per-call helps a lot (when done during construction, it's called "dependency injection"). - whenever string is returned, it's
const
reference to the string belonging to the class instance, so if outer code want to manipulate it, it must use only method available in the SmartString
API, and SmartString
instance has full control over inner representation of string (it can use instead of std::string
an char []
, and the public API would not need to be changed at all, as that part of implementation is completely hidden. - the API feels weird BTW... the
reverseAndGet
... I was thinking about making it void reverse();
only, so to get reversed SmartString test("tset");
you would have to do test.reverse(); std::string reversedString = test.get();
.
To make it less weird, and more OOP like, it would make more sense to extend std::string
, as that's already well known defined API, and just adding one more reverse
method to it makes much more sense in OOP world, having then extendedString
class which can be used in place of std::string where needed.
Helper utility class (or just a namespace with functions would do probably even better) would tried to provide pure functions, taking everything needed on input, and returning output, not having any instance at all, so they are accessible from anywhere, when you have the input ready.
namespace stringAlgo { std::string reverse(const std::string & str); //there's little point to do "length", as it's part of original std::string //but just as an example int length(const std::string & str) { return str.length(); } void displayQuoted(std::ostream & out, const std::string & str) { out << "„" << str << "“"; } }
Ideas behind this one:
- there's no instance anywhere, the functions are ready to be called straight away when you have some std::string to process, and some std::ostream to output to. (in Java world these would be
static
methods of class/interface, in C++ it's OK to create them as global public functions (but put them into some namespace to avoid naming clashes)) - namespace ... actually
stringAlgo
is like asking for collision with some generic library, when working on library code, use very specific namespace names, like raajStrLib
. When the code will be used only inside the single project (will it, really? What about the code re-use), you can get away with more generic namespace names, but still make sure they are not too generic (in case you will later have to add some 3rd party lib, which may collide), and it's actually easy to understand what is the purpose and to recall the name (now I like stringAlgo
even less... :D).
Overall in OOP, when designing classes/interfaces, try:
- use problem-domain language and abstraction, so when providing bank account library, don't talk about strings in upper layers of abstractions, talk about account numbers (even when represented by std::string, but wrapped in
bank_account_number
type). - try to visualise all the abstractions, and consider their level of abstraction toward problem-domain, decide on reasonable amount of levels, and group the things together. Adjust then everything in particular group to have the same level of abstraction. So you don't start suddenly to work with back_account_number.charAt() in the top most layer, when you have lower layer responsible for single char manipulating related methods.
- usually the
main
of the problem solution should be able to solve the problem with the top-most abstraction layer completely, without touching the lower levels. The lower levels should be accessible mostly for extending/building new top-layer blocks, not for regular usage. - try to decide, what/who is responsible for what. An bank_account class can have utility function to decide whether some string forms valid bank account number (or when bank_account_number type/class exists, it belongs there), but will not provide sum of amounts over list of accounts, only provides amounts read from the account, which it is instance of. For sums over list you should have another utility method, or eventually class holding the list itself, and providing API for manipulating the list. But such class should not do transfer(account acFrom, account acTo, amount), that either belongs to single account or to transfersManager. The list of accounts may be responsible for suggestAccountsForSavingTransfer, which may return some general account and some saving account from it's list.
- try to hide inner implementation of classes, so no by-products of calculations are gettable trough API, no algorithms usage is explicitly stated/visible, try to keep the API minimal (but powerful enough), exposing only results on the desired level of abstraction. So anytime later you may completely change inner implementation, build it from other structures/data, but as long as you fulfil the API, nobody outside should notice.
Hmmm... anyway, I really like what you wrote here:
"I'm noticing that reading doesn't really help much as much as writing programs and running into issues and solving them."
This is absolutely spot on. All this general babbling around usually makes little sense when hit by real world problem.
Also refactor a lot. Not just the code, and implementations, but as learning is your goal, dive deep into finished project and go trough all the API, all the abstraction, and try to imagine something better, easier to use, easier to understand it's responsibilities, easier to read and get the idea and eventually easier to implement (although sometimes to get better API and abstraction the implementation can be more bloated, but the cost of bloat has to be clearly justified by the cleanliness of resulting API/abstraction).
return;
the same object. And a member function could display its instance's data without passing it its own instance, by reference and then return it, just by calling it and adding thecout
inside of it.\$\endgroup\$