I wanted to ask whether you would consider it a good class design?
No, because you don't need linked list to implement stack, because you don't throw exceptions, because you aren't using existing solutions (such as std::stack
, std::vector
or std::deque
), and because your class doesn't obey rule of three.
template<typename Val> class Stack{
protected:
typedef std::vector<Val> Data;
Data data;
public:
const Val& top() const{
if (!data.size())
throw std::underflow_error();
return data[data.size()-1];
}
void push(const Val& arg){
data.push_back(arg);
}
Val pop(){
Val result = top();
data.pop_back();
return result;
}
};
this problem of implementing nested classes.
For "nested classes exercise" linked list is more suitable.
How should I go about it? What should be my approach towards it?
Write as little as possible, reuse existing solutions, and keep it simple.
Are there any good books on it?
There should be books about design patterns, but it might be too early for them. I'd suggest to just keep practicing for a while, preferably using real problems you're interested in.