Can a constructor take more than one argument? If so what's the syntax? Is it something like
Staff(std::string s, double x)
Yes, that's perfectly acceptable. there's no arbitrary limit to the number of arguments you can take - although it becomes somewhat unweildy if you provide too many IMHO. (if you've got 6 or more, your code is probably going to look quite ugly - there's almost certainly a better way)
Also, as with member functions, you may define the body of the constructor outside the class declaration, eg,
class Staff
{
vector<Employee> members;
public:
Staff(std::string, double);
Staff(Employee);
};
Staff::Staff(std::string s, double x)
{
// do something here
}
Staff::Staff(Employee e)
{
members.push_back(e);
}
Is there a situation where that would be useful or is it unconventional?
Many situations - you only need to look through the STL to find dozens of examples. The std::vector class has 8 different constructors, some of which take 2, 3 or 4 parameters.
A good example with vectors would be to create a new container based on some elements of an existing container:
#include <vector>
#include <iterator>
#include <algorithm>
int main()
{
std::vector<int> numberList;
numberList.push_back(5);
numberList.push_back(7);
numberList.push_back(19);
std::vector<int>::iterator myIter;
myIter = std::find(numberList.begin(), numberList.end(), 7);
std::vector<int> newNumberList(numberList.begin(), myIter);
} The snippet above creates a vector of int's holding these numbers - 5, 7, and 19
then the find() algorithm searches for the number 7 in the vector.
when it finds 7, it records the position into
myIter
newNumberList is then created with all the elements from numberList(begin) - that is, the first element, up until the position stored by
myIter.
This is done by a vector constructor which accepts 2 STL iterators as parameters.