Understanding Constructors in C++: Role, Member Initialization, and Contained Objects - Pr, Study notes of Data Structures and Algorithms

An in-depth explanation of constructors in c++ programming, including their role, member initialization lists, and the rationale behind initializing objects instead of assigning values. The text also covers the order of initialization and the use of constructors for contained objects. Examples using the product and time classes are provided.

Typology: Study notes

Pre 2010

Uploaded on 11/08/2009

koofers-user-53q
koofers-user-53q 🇺🇸

10 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Understanding Constructors in C++: Role, Member Initialization, and Contained Objects - Pr and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Constructors [Bono]

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

Constructors [Bono]

Role of constructors

Make sure all fields are initialized.

Put the object into a valid state.

Is called automatically when you declare avariable of that type. E.g.

Time t; Time t(0,

Constructors [Bono]

Constructors for contained objects

• The

Product

class contains an

string

object (object within an object).

• How does

name

field get initialized?

Product::Product() {

price = 1; score = 0;

Constructors [Bono]

Creating contained objects

What if we wanted to init to non-empty string.

How to call the constructor for the containedobject?

NOT in the class definition itself:^ class

Product

private: string name(“Dell”);

does not compile double price; int score; };

Constructors [Bono]

Rationale for initializing, not assigning

Use mbr. init. list to init.

all

fields in

all

objects

(text uses this style)

Why?

  • Sometimes we have to / want to: easier to do it always

When have/want to?

  • more efficient – no default constructor for contained type – contained object is reference or const

Constructors [Bono]

Code inside body of constructor

can only put

expressions

as member init list values

Sometimes will need code in body of constructor too:

E.g.,

assert

statements.

Or, suppose we want to initialize numbers to be a vectorcontaining the values 1 – 10

class Foo

private: vector numbers; }; Foo::Foo()

numbers(10)

what does this line do? { for (int i=0; i

numbers.size(); i++)

numbers[i]

i+1; } }