上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_objects and classes

上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Chapter 9 Objects and Classes
Chapter 9 Objects and Classes Chapter 9 Objects and Classes

上海交通大学交大密西根 ·联合学院一 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Objectives To understand objects and classes,and use classes to model objects (§9.2) To understand the role of constructors when creating objects($9.3). To learn how to declare a class and how to create an object of a class (§9.4). To know how to separate a class declaration from a class implementation (9.5). To access object members using pointers($9.6). To create objects using the new operator on the heap (9.7)
Objectives Objectives • To understand objects and classes, and use classes to model objects (§9.2). • To understand the role of constructors when creating objects (§9.3). • To learn how to declare a class and how to create an object of a class (§9.4). • To know how to separate a class declaration from a class implementation (§9.5). • To access object members using pointers (§9.6). • To create objects using the new operator on the heap (§9.7)

上海交通大学交大密西根 ·联合学院一 ■ 81T UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Objectives To declare private data fields with appropriate get and set functions for data field encapsulation to make classes easy to maintain (9.9). To understand the scope of data fields (9.10). To reference hidden data field using the this pointer (9.11). To develop functions with object arguments($9.12). To store and process objects in arrays($9.13). ● To apply class abstraction to develop software (9.14- 9.15). To initialize data fields with a constructor initializer (9.16)
Objectives Objectives • To declare private data fields with appropriate get and set functions for data field encapsulation to make classes easy to maintain (§9.9). • To understand the scope of data fields (§9.10). • To reference hidden data field using the this pointer (§9.11). • To develop functions with object arguments (§9.12). • To store and process objects in arrays (§9.13). • To apply class abstraction to develop software (§§9.14- 9.15). • To initialize data fields with a constructor initializer (§9.16)

上海交通大学交大密西根 联合学院 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University O0 Programming Concepts Object-oriented programming (OOP)involves programming using objects.An object represents an entity in the real world that can be distinctly identified.For example,a student,a desk,a circle,a button,and even a loan can all be viewed as objects.An object has a unique identity,state,and behaviors.The state of an object consists of a set of data fields (also known as properties)with their current values.The behavior of an object is defined by a set of functions
OO Programming Concepts OO Programming Concepts • Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly identified. For example, a student, a desk, a circle, a button, and even a loan can all be viewed as objects. An object has a unique identity, state, and behaviors. The state of an object consists of a set of data fields (also known as properties) with their current values. The behavior of an object is defined by a set of functions

上海交通大学交大密西根 联合学院·一 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Introduction To Classes The basic unit of abstraction in C++is the class. -A class is used to encapsulate some piece of user- defined data and the operations that access and manipulate that data. A class is the blueprint from which an object is created-put another way,an object is an instance of a class. In fact,starting from our very first example,we are experiencing defining class ourselves like MessageT, AddIntegersT
Introduction To Classes Introduction To Classes • The basic unit of abstraction in C++ is the class. – A class is used to encapsulate some piece of userdefined data and the operations that access and manipulate that data. – A class is the blueprint from which an object is created — put another way, an object is an instance of a class. – In fact, starting from our very first example, we are experiencing defining class ourselves like MessageT, AddIntegersT, …

上海交通大学交大密西根 8 联合学院·一 ◆ 1811 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Redefine class AddIntegersT Add a private data member intSum; Add a function to initialize and/or reset the value of intSum class AddIntegersT:public ConsoleT int intSumj public: void setSum (int sum)(intSum sumj) void disp 0 (printLine (the sum is",intSum,endl))
Redefine Redefine class AddIntegersT AddIntegersT • Add a private data member intSum; • Add a function to initialize and/or reset the value of intSum

Rewrite Add2Intergers #include #include using namespace std; f∥add two integers int main (void) AddIntegersT addIntegersj int int1,int2,intSumj addIntegers.printLine (This program will compute the sum ofn); addIntegers.printLine (two integers specified by the user.nin"); int1 addIntegers.readInt (Please input the first integer"); int2 addIntegers.readInt (Please input the second integer); intSum int1 int2j return 0j
Rewrite Add2Intergers Rewrite Add2Intergers

上海交通大学交大密西根 联合学院· UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Defining a Class Let's look at an another example of a very simple class to represent a random number generator. Class RandomT Data Members; Function_Members; ∥Also called Methods } We define the class by using the class keyword,and supply both data and function members for the class together.A member is just a fancy name for one data or function element of a class,and a method is an even fancier name for a function member
Defining a Class Defining a Class • Let’s look at an another example of a very simple class to represent a random number generator. Class RandomT { Data_Members; Function_Members; // Also called Methods } • We define the class by using the class keyword, and supply both data and function members for the class together. A member is just a fancy name for one data or function element of a class, and a method is an even fancier name for a function member

上海交通大学交大密西根 联合学院·一 ◆ ■ 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Defining a Class In general,the definition of a class appears as its own .h file. The practice of separation interface from implementation is still in place;the interface is defined in a random.h file, whereas the implementation of class methods are typed up in the corresponding random.cpp file
Defining a Class Defining a Class • In general, the definition of a class appears as its own .h file. • The practice of separation interface from implementation is still in place; the interface is defined in a random.h file, whereas the implementation of class methods are typed up in the corresponding random.cpp file

上海交通大学交大密西根 ·联合学院·一 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Defining a Class class RandomT nameT { start the definition int seed; Data member definition static const int m=Ox7fffffffL; area default as private static const int a 16807L; Static means stay in int nextO; ∥private function memory all the time public: /could be accessed outside of the class specifier RandomT O; Construct functions RandomT (int initSeed); int nextInteger(int lower,int upper); Get next random int int nextInteger (int limit); double nextDouble(double lower,double upper); Get next random dbl double nextDouble O; bool nextBoolean (double p); Get next random prob ;”end of the def
Defining a Class Defining a Class class RandomT { int seed; static const int m = 0x7fffffffL; static const int a = 16807L; int next(); // private function public: // could be accessed outside of the class RandomT (); RandomT (int initSeed); int nextInteger (int lower, int upper); int nextInteger (int limit); double nextDouble (double lower, double upper); double nextDouble (); bool nextBoolean (double p); }; • nameT • ‘{‘ start the definition • Data member definition area default as private • Static means stay in memory all the time • specifier • Construct functions • Get next random int • Get next random dbl • Get next random prob • “};” end of the def
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Vg101.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Computer and Programming.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Function.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_files_DataBase Design.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Expressions and Statements.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_examples on class design.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Array and its Applications.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_20 Looking Ahead.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_19 Recursion 1.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_16 MATLAB environment short.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_15 Introduction to matlab.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_Chapter 1 Introduction.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_第二章习题与答案(第三版).doc
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_第三章习题与答案(第三版).doc
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter8 Views, Indexes.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter7 Constraints and Triggers.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter6 The database Language SQL –as a tutorial.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter5 Algebraic and Logic Query languages.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter4 High-level Database Models.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter3 Design Theory for Relational Databases.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_programming style guide for C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Random Number_Graphics.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Start with C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_vector_string.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation 1.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_recitation 13.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation IX.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation V.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VIII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation X.ppt
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十四章 MCS-51单片机(1/2).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十四章 MCS-51单片机(2/2).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第一章 微机原理与接口技术绪论(朱兰娟).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第二章 8086系统结构.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter9 SQL in a server environment.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Pointer Review Solution.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Pointer Review.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Practice Final 1.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_practice Final 2.pdf