Yes and no. It really depends on what you are trying to accomplish. If you look at the STL string it has both.
NathanOliver
Posting Virtuoso
1,524 posts since Apr 2009
Reputation Points: 281
Solved Threads: 281
Skill Endorsements: 3
What I don't understand is, would a real world program have a default constructor AND a constructor with parameters?
Certainly, if there's a case where the class could initialize itself to a predictable state without help from the caller. One example might be an empty string, so std::string has a default constructor that initializes itself to the equivalent of "".
deceptikon
Challenge Accepted
3,502 posts since Jan 2012
Reputation Points: 822
Solved Threads: 481
Skill Endorsements: 58
Brevity? A default constructor, copy constructor, and assignment operator are created by the compiler by default, even if not specified by the developer of the class. However, the member variables and such are not necessarily constructed as you may want (caveate programmer). As for the constructor with arguments, consider this:
class 2dpoint {
private:
int x_pos;
int y_pos;
public:
2dpoint() : x_pos(0), y_pos(0) {} // Default ctor
2dpoint(int x, int y) : x_pos(x), y_pos(y) {}
.
.
.
};
So, you have a dynamic game, and you want to position something at a specific position when constructed, some other element may look at it in the wrong place (0,0). So, you create it where you want it to be viewed/positioned
rubberman
Posting Maven
2,696 posts since Mar 2010
Reputation Points: 378
Solved Threads: 316
Skill Endorsements: 53