Chapter 10 Classes A Deeper Look Part 2
- Slides: 33
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.
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 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.
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.
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.
© 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 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
© 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 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.
- Look up look down look all around
- Into the heart of jesus deeper and deeper i go
- Classe e subclasse de palavras exercícios 9 ano
- Pre ap classes vs regular classes
- Student portal rio
- Activity 1.picture identification
- Activity 1 picture
- Activity 1 a) look at the picture
- Go deeper in christ
- Deeper life netherlands
- Dclm netherlands
- Deeper life bible church netherlands
- Deeper life netherlands
- What is deeper exam
- Deeper life sunday message
- Dclm netherlands
- Christian deeper learning conference
- Deeper life bible church
- Search the scriptures netherlands
- Growing deeper in god
- Deeper life netherlands
- Deeper life netherlands
- Dclm netherlands
- Deeper life bible church netherlands
- Deeper life netherlands
- Civil war digging deeper webquest answers
- Deeper life canada
- Jesus lord of heaven
- Piled higher and deeper
- To dive deeper synonym
- Kelly services netherlands
- Deeper life netherlands
- Deeper life netherlands
- Deeper life netherlands