《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 6 Structures and Classes

Chapter 6 ABSOLUTE C++ Structures and Classes WALTER SAVITCH SECOND EDITION PEARSON Copyright2006 Pearson Addison-Wesley All rights reserved
Chapter 6 Structures and Classes

Learning Objectives ◆Structures ◆Structure types Structures as function arguments Initializing structures ◆Classes Defining,member functions Public and private members Accessor and mutator functions Structures vs.classes Copyright006 Pearson Addison-Wesley.All rights reserved. 6-2
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives ¨ Structures ¨ Structure types ¨ Structures as function arguments ¨ Initializing structures ¨ Classes ¨ Defining, member functions ¨ Public and private members ¨ Accessor and mutator functions ¨ Structures vs. classes

Structures 2nd aggregate data type:struct Recall:aggregate meaning "grouping" Recall array:collection of values of same type Structure:collection of values of different types Treated as a single item,like arrays Major difference:Must first "define"struct Prior to declaring any variables Copyright 2006 Pearson Addison-Wesley.All rights reserved. 6-3
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-3 Structures ¨ 2nd aggregate data type: struct ¨ Recall: aggregate meaning "grouping" ¨ Recall array: collection of values of same type ¨ Structure: collection of values of different types ¨ Treated as a single item, like arrays ¨ Major difference: Must first "define" struct ¨ Prior to declaring any variables

Structure Types ◆Define struct globally(typically) No memory is allocated Just a "placeholder"for what our struct will "look like" ◆Definition: struct CDAccountV1 Name of new struct "type" double balance;member names double interestRate; int term; }; Copyright 2006 Pearson Addison-Wesley.All rights reserved. 6-4
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-4 Structure Types ¨ Define struct globally (typically) ¨ No memory is allocated ¨ Just a "placeholder" for what our struct will "look like" ¨ Definition: struct CDAccountV1 Name of new struct "type" { double balance; member names double interestRate; int term; };

Declare Structure Variable With structure type defined,now declare variables of this new type: CDAccountV1 account; Just like declaring simple types Variable account now of type CDAccount 1 It contains "member values" Each of the struct "parts" Copyright 2006 Pearson Addison-Wesley.All rights reserved. 6-5
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-5 Declare Structure Variable ¨ With structure type defined, now declare variables of this new type: CDAccountV1 account; ¨Just like declaring simple types ¨Variable account now of type CDAccountV1 ¨It contains "member values" ¨Each of the struct "parts

Accessing Structure Members Dot Operator to access members ◆account.balance account.interestRate ◆account.term Called "member variables" The "parts"of the structure variable Different structs can have same name member variables ◆No conflicts Copyright 2006 Pearson Addison-Wesley.All rights reserved. 6-6
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-6 Accessing Structure Members ¨ Dot Operator to access members ¨ account.balance ¨ account.interestRate ¨ account.term ¨ Called "member variables" ¨ The "parts" of the structure variable ¨ Different structs can have same name member variables ¨ No conflicts

Structure Example: Display 6.1 A Structure Definition (1 of 3) Display 6.1 A Structure Definition 1 //Program to demonstrate the CDAccountV1 structure type. 2 #include 3 using namespace std; 4 //Structure for a bank certificate of deposit: An improved version of this struct CDAccountV1 6 structure will be given later in this > double balance; chapter. e double interestRate; 9 int term;//months until maturity 10 11 void getData(CDAccountV1&theAccount); 1 //Postcondition:theAccount.balance,theAccount.interestRate,and 3 //theAccount.term have been given values that the user entered at the keyboar Copyright 2006 Pearson Addison-Wesley.All rights reserved. 6-7
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-7 Structure Example: Display 6.1 A Structure Definition (1 of 3)

Structure Example: Display 6.1 A Structure Definition(2 of 3) 14 int main() 15 16 CDAccountV1 account; 17 getData(account); 18 double rateFraction,interest; 19 rateFraction account.interestRate/100.0; 20 interest account.balance*(rateFraction*(account.term/12.0)); 21 account.balance account.balance interest; 22 cout.setf(ios:fixed); 23 cout.setf(ios:showpoint); 24 cout.precision(2); 25 cout <<"When your CD matures in 26 <account.term <<months,\n" 7 <<"it will have a balance of $ 28 <account.balance <endl; 29 return 0; 30} (continued) Copyright006 Pearson Addison-Wesley.All rights reserved. 6-8
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-8 Structure Example: Display 6.1 A Structure Definition (2 of 3)

Structure Example: Display 6.1 A Structure Definition(3 of 3) Display 6.1 A Structure Definition 31 //Uses iostream: 32 void getData(CDAccountV1&theAccount) 33 34 cout theAccount.balance; 36 cout theAccount.interestRate; 38 cout theAccount.term; 40 3 SAMPLE DIALOGUE Enter account balance:$100.00 Enter account interest rate:10.0 Enter the number of months until maturity:6 When your CD matures in 6 months, it will have a balance of $105.00 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 6-9
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-9 Structure Example: Display 6.1 A Structure Definition (3 of 3)

Structure Pitfall Semicolon after structure definition ◆;MUST exist: struct WeatherData double temperature; double windVelocity; };←REQUIRED semicolon! Required since you "can"declare structure variables in this location Copyright 2006 Pearson Addison-Wesley.All rights reserved. 6-10
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-10 Structure Pitfall ¨ Semicolon after structure definition ¨; MUST exist: struct WeatherData { double temperature; double windVelocity; }; REQUIRED semicolon! ¨Required since you "can" declare structure variables in this location
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 2 Flow of Control.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 3 Function Basics.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 1 C++ Basics.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 4 Parameters and Overloading.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第三章.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第二章 信源与信息熵.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第一章 绪论(主讲:王军选).ppt
- 《信息论与编码》课程教学资源(作业习题)第三章 信道与信道容量(含解答).pdf
- 《信息论与编码》课程教学资源(作业习题)自测题无答案.pdf
- 《信息论与编码》课程教学实验指导书.pdf
- 《信息论与编码》课程教学大纲 Element of Information Theory and Coding B.pdf
- 《信息论与编码》课程教学大纲 Element of Information Theory and Coding A.pdf
- 《信息安全概论》课程教学资源(PPT课件)第9章 信息安全标准与法律法规.ppt
- 《信息安全概论》课程教学资源(PPT课件)第2章 信息保密技术.ppt
- 《信息安全概论》课程教学资源(PPT课件)第7章 网络安全技术.ppt
- 《信息安全概论》课程教学资源(PPT课件)第8章 信息安全管理.ppt
- 《信息安全概论》课程教学资源(PPT课件)第6章 访问控制技术.ppt
- 《信息安全概论》课程教学资源(PPT课件)第5章 操作系统与数据库安全.ppt
- 《信息安全概论》课程教学资源(PPT课件)第1章 绪论.ppt
- 《信息安全概论》课程教学资源(PPT课件)第3章 信息认证技术.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 7 Constructors and Other Tools.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 8 Operator Overloading, Friends, and References.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 5 Arrays.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 11 Separate Compilation and Namespaces.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 12 Streams and File IO.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 10 Pointers and Dynamic Arrays.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 9 Strings.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 13 Inheritance.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 14 Polymorphism and Virtual Functions.ppt
- 《微机技术及应用》课程教学大纲 Microcmputer Technology and aplications.doc
- 《微型计算机技术及应用》课程电子教案(PPT教学课件,共十五章,完整版).pptx
- 《计算机导论》课程教学大纲 Computer Concepts.pdf
- 《计算机导论》课程教学课件(英文讲稿)1-a-Computer History+ Di Devices.pdf
- 《计算机导论》课程教学课件(英文讲稿)1-b-Digital Data Representation.pdf
- 《计算机导论》课程教学课件(英文讲稿)2-a-Computer Hardware.pdf
- 《计算机导论》课程教学课件(英文讲稿)2-b-Computer Hardware.pdf
- 《计算机导论》课程教学课件(英文讲稿)3-a-b-Computer Software.pdf
- 《计算机导论》课程教学课件(英文讲稿)4- operating system.pdf
- 《计算机导论》课程教学课件(英文讲稿)4-a- File mangement.pdf
- 《计算机导论》课程教学课件(英文讲稿)5-a- LANS_WANS.pdf