5.constructors

5.constructors - Constructors Role of constructors...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Constructors [Bono] 1 Constructors • Role of constructors • Constructors for contained objects • Member initializer list • Rationale for initializing • Order of initialization • Code in body of constructor • Constructing objects contained in a vector Product and Time examples are from Big C++, by Cay Horsmann and Timothy Budd; Wiley 2005
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Constructors [Bono] 2 Role of constructors • Make sure all fields are initialized. • Put the object into a valid state. • Is called automatically when you declare a variable of that type. E.g. Time t; Time t(0, 32, 24);
Background image of page 2
Constructors [Bono] 3 Product example revisited • This version has two constructors for illustration purposes class Product { public: Product(); Product(string name, double price, int score); void read(); bool is_better_than(Product b) const; void print() const; private: string name; double price; int score; };
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Constructors for contained objects •T h e Product class contains an string object (object within an object). • How does
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 05/19/2010.

Page1 / 10

5.constructors - Constructors Role of constructors...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online