上海交通大学交大密西根 ·联合学院一 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