Introduction to OOP Chapter 5: Messages Instances and Initialization: next previous audio real text

Overloaded Constructors

Constructors are often overloaded, meaning there are a number of functions with the same name. They are differentiated by the type signature, and the arguments used in the function call or declaration:

class PlayingCard {
public:
	PlayingCard ( )  // default constructor, 
			// used when no arguments are given
		{ suit = Diamond; rank = 1; faceUp = true; }

	PlayingCard (Suit is) // constructor with one argument
		{ suit = is; rank = 1; faceUp = true; }

	PlayingCard (Suit is, int ir) // constructor with two arguments
		{ suit = is; rank = ir; faceUp = true; }
};

PlayingCard *cardOne = new PlayingCard();
PlayingCard *cardTwo = new PlayingCard(Heart);
PlayingCard *cardThree = new PlayingCard(Diamond, 7);

Intro OOP, Chapter 5, Slide 11