Would any one mind showing me a clear example of a class with multiple constructors, not including the copy constructor? I keep trying to use more than one but I get an error saying that the second has no "type".
class SomeData
{
public:
// a constructor to load data from the given file name
SomeData(constchar* filename)
{
// .. open filename here, read data from file
}
// a constructor to load data from a given stream
SomeData(istream& stream)
{
// .. read data from stream here
}
};
You can have many examples depending on what your class want you to do:
1 2 3 4 5 6 7 8 9 10 11 12
class Employee
{
public:
//must give name not salary assigning this to 0
Employee(std::string, int salary = 0);
//the same but also takes char * as name
Employee(char *, int salary = 0);
//Name and salary not set OR set to a default value
Employee();
};
On the approach before author intended to create an Employee object even when no information was given. Maybe you don't want that. Skip the last constructor (or to be more accurate) make it private to prevent constructing of default object.
You could have more combinations, like a constructor with 3 arguments including its address plus the other 2 etc