中国高校课件下载中心 》 教学资源 》 大学文库

《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 7 Constructors and Other Tools

文档信息
资源类别:文库
文档格式:PPT
文档页数:44
文件大小:1.14MB
团购合买:点击进入团购
内容简介
《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 7 Constructors and Other Tools
刷新页面文档预览

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; }

Constructor Notes Notice name of constructor:DayOfYear Same name as class itself! Constructor declaration has no return-type ◆Not even void! Constructor in public section It's called when objects are declared ◆If private,could never declare objects! Copyright6 Pearson Addison-Wesley.All rights reserved. 7-6

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-6 Constructor Notes ¨ Notice name of constructor: DayOfYear ¨ Same name as class itself! ¨ Constructor declaration has no return-type ¨ Not even void! ¨ Constructor in public section ¨ It’s called when objects are declared ¨ If private, could never declare objects!

Calling Constructors ◆Declare objects: DayOfYear date1(7,4), date2(5,5); Objects are created here Constructor is called Values in parents passed as arguments to constructor Member variables month,day initialized: date1.month→7date2.month→5 date1.dat→4date2.day→5 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 7-7

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-7 Calling Constructors ¨ Declare objects: DayOfYear date1(7, 4), date2(5, 5); ¨ Objects are created here ¨ Constructor is called ¨ Values in parents passed as arguments to constructor ¨ Member variables month, day initialized: date1.month  7 date2.month  5 date1.dat  4 date2.day  5

Constructor Equivalency ◆Consider: DayOfYear date1,date2 date1.DayOfYear(7,4);/ILLEGAL! date2.DayOfYear(5,5);/ILLEGAL! ◆Seemingly OK. CANNOT call constructors like other nember functions! Copyright006 Pearson Addison-Wesley.All rights reserved. 7-8

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-8 Constructor Equivalency ¨ Consider: ¨DayOfYear date1, date2 date1.DayOfYear(7, 4); // ILLEGAL! date2.DayOfYear(5, 5); // ILLEGAL! ¨ Seemingly OK. ¨CANNOT call constructors like other member functions!

Constructor Code Constructor definition is like all other member functions: DayOfYear:DayOfYear(int monthValue,int dayValue) month monthValue; day dayValue; Note same name around Clearly identifies a constructor ◆Note no return type Just as in class definition Copyright 2006 Pearson Addison-Wesley.All rights reserved. 7-9

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-9 Constructor Code ¨ Constructor definition is like all other member functions: DayOfYear::DayOfYear(int monthValue, int dayValue) { month = monthValue; day = dayValue; } ¨ Note same name around :: ¨ Clearly identifies a constructor ¨ Note no return type ¨ Just as in class definition

Alternative Definition Previous definition equivalent to: DayOfYear:DayOfYear( int monthValue, int dayValue) month(monthValue),day(dayValue)< {.} Third line called "Initialization Section" ◆Body left empty Preferable definition version Copyright 2006 Pearson Addison-Wesley.All rights reserved. 7-10

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-10 Alternative Definition ¨ Previous definition equivalent to: DayOfYear::DayOfYear( int monthValue, int dayValue) : month(monthValue), day(dayValue)  {.} ¨ Third line called "Initialization Section" ¨ Body left empty ¨ Preferable definition version

刷新页面下载完整文档
VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
相关文档