上海交通大学交大密西根 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 <vg101class.h> #include <AddIntegersT.h> 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 <class>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 <public>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); }; • <class> nameT • ‘{‘ start the definition • Data member definition area default as private • Static means stay in memory all the time • <public> specifier • Construct functions • Get next random int • Get next random dbl • Get next random prob • “};” end of the def