Chapter 10 Classes A Deeper Look Part 2

  • Slides: 33
Download presentation
Chapter 10 Classes: A Deeper Look, Part 2 © 1992 -2010 by Pearson Education,

Chapter 10 Classes: A Deeper Look, Part 2 © 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

10. 1 Introduction Constness - enforce principle of least privilege which is one of

10. 1 Introduction Constness - enforce principle of least privilege which is one of the most fundamental principles of good software engineering Achieved using const keyword const int a; Attribute not modifiable Compiler error if attempt to modify attribute Not even in constructor body Must be initialized using the data member initializer list.

10. 2 const (Constant) Objects and const Member Functions A const may be: v

10. 2 const (Constant) Objects and const Member Functions A const may be: v data member. vmember function. vobject. Constructors and destructors cannot be const ◦ Constructor initializes objects ◦ Destructor performs termination housekeeping C++ disallows member function calls for const objects unless the member functions themselves are also declared const. (True even for get member functions that do not modify the object. ) A member function is specified as const both in its prototype and in its definition.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

Constant member function © 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

Constant member function © 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

10. 2 const (Constant) Objects and const Member Functions (cont. ) Member initializer syntax

10. 2 const (Constant) Objects and const Member Functions (cont. ) Member initializer syntax Syntax ◦ Appears between a constructor’s parameter list and the left brace; separated from the parameter list with a colon (: ) ◦ Each member initializer consists of data member name followed by parentheses containing initial value ◦ Multiple member initializers are separated by commas ◦ Executes before the body of the constructor executes © 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

Constant data member © 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

Constant data member © 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

10. 3 Composition: Objects as Members of Classes Composition ◦ Sometimes referred to as

10. 3 Composition: Objects as Members of Classes Composition ◦ Sometimes referred to as a has-a relationship ◦ A class has objects of other classes as data members ◦ Form of software reusability Examples ◦ A car has a motor, 4 wheels, and a transmission You might declare a wheel class, a motor class and a transmission class, and then use them in the car class An object’s constructor can pass arguments to member-object constructors via member initializers.

10. 3 Composition: Objects as Members of Classes

10. 3 Composition: Objects as Members of Classes

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

10. 3 Composition: Objects as Members of Classes (cont. ) notice that the class

10. 3 Composition: Objects as Members of Classes (cont. ) notice that the class does not provide a constructor that receives a parameter of type Date. If a member object is not initialized through a member initializer, the member object’s default constructor will be called implicitly. Values, if any, established by the default constructor can be overridden by set functions. © 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2010 by Pearson Education, Inc. All Rights Reserved.