Chapter 7 ABSOLUTE C++ Constructors and Other Tools WALTER SAVITCH SECOND EDITION PEARSON Copyright2006 Pearson Addison-Wesley All rights reserved
Chapter 7 Constructors and Other Tools
Learning Objectives ◆Constructors ◆Definitions ◆Calling ◆More Tools const parameter modifier ◆Inline functions ◆Static member data ◆Vectors Introduction to vector class Copyright006 Pearson Addison-Wesley.All rights reserved. 7-2
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives ¨ Constructors ¨ Definitions ¨ Calling ¨ More Tools ¨ const parameter modifier ¨ Inline functions ¨ Static member data ¨ Vectors ¨ Introduction to vector class
Constructors Initialization of objects Initialize some or all member variables Other actions possible as well .A special kind of member function Automatically called when object declared ◆Very useful tool ◆Key principle of OOP Copyright 2006 Pearson Addison-Wesley.All rights reserved. 7-3
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-3 Constructors ¨ Initialization of objects ¨Initialize some or all member variables ¨Other actions possible as well ¨ A special kind of member function ¨Automatically called when object declared ¨ Very useful tool ¨Key principle of OOP
Constructor Definitions Constructors defined like any member function ◆Except: 1.Must have same name as class 2.Cannot return a value;not even void! Copyright 2006 Pearson Addison-Wesley.All rights reserved. 7-4
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-4 Constructor Definitions ¨ Constructors defined like any member function ¨ Except: 1. Must have same name as class 2. Cannot return a value; not even void!
Constructor Definition Example Class definition with constructor: ◆class DayOfYear public: DayOfYear(int monthValue,int dayValue); //Constructor initializes month day void input(); void output(); private: int month; int day; } Copyright 2006 Pearson Addison-Wesley.All rights reserved. 7-5
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-5 Constructor Definition Example ¨ Class definition with constructor: ¨ class DayOfYear { public: DayOfYear(int monthValue, int dayValue); //Constructor initializes month & day void input(); void output(); . private: int month; int day; }