上海交通大学交大密西根 联合学院·一 ◆ ■ 81T UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Class Declaration A class may contains a number of data members and some methods (functions)defined to manipulate those attributes;both of them could be defined either as private or public: Default attribute is private:only accessible inside of the class;public means you can access it outside of the class class name /data members declaration public:/following functions can be accessed outside of the class /method (function)declaration ;
Class Declaration Class Declaration • A class may contains a number of data members and some methods (functions) defined to manipulate those attributes; both of them could be defined either as private or public: • Default attribute is private: only accessible inside of the class; public means you can access it outside of the class class name { // data members declaration public:// following functions can be accessed outside of the class // method (function) declaration };
上海交通大学交大密西根 联合学院·一 ■ 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Object Usage To use functions defined in an object,use object name followed by a period and the method name as follows console.printLine ("add two integers\n"); int1 console.readInt ("Input an integer"); console.disp (int1X,int2,intSum); Note here that,the function printline
Object Usage Object Usage • To use functions defined in an object, use object name followed by a period and the method name as follows console.printLine (“add two integers\n”); int1 = console.readInt (“Input an integer”); … console.disp (int1X, int2, intSum); • Note here that,the function printLine () and readInt () are not defined in
上海交通大学交大密西根 ·联合学院一 1811 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University The Add2Integers Program int main (void) AddIntConsoleT console; int int1,int2,intSumj console.printLine (This program will compute the sum ofn'); console.printLine (two integers specified by the user.n'in'); int1 console.readInt (Please input the first integer") int2 console.readInt ("Please input the second integer"); intSum =int1+int2; console.disp (int1,int2,intSum); nl n2 total retur 0; 17 25 42 This program will computer the sum of Two integers specified by the user Please input the first integer 17 Please input the second integer 25 17+25=42
Add2Integers The Add2Integers Program The Add2Integers Program This program will computer the sum of Two integers specified by the user Please input the first integer 17 + 25 = 42 Please input the second integer n1 n2 total 17 25 42 17 25 n1
上海交通大学交大密西根 联合学院· 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Programming Idioms and Patterns Experienced programmers also often take a holistic approach to programming.Effective programmers can recognize a variety of common operations and have learned a standard solution strategy for each one.The code that implements such a solution strategy is called a programming idiom or programming pattern.Learning to use these patterns saves you from having to think about the nitty- gritty details. As an example,it is important to think of a statement like int intx console.readInt("Enter n1:") not in terms of what each part of the statement means,but rather as a holistic pattern to read an integer from the user: int variable console.readint ("prompt");
Programming Idioms and Patterns Programming Idioms and Patterns • Experienced programmers also often take a holistic approach to programming. Effective programmers can recognize a variety of common operations and have learned a standard solution strategy for each one. The code that implements such a solution strategy is called a programming idiom or programming pattern. Learning to use these patterns saves you from having to think about the nittygritty details. • As an example, it is important to think of a statement like • not in terms of what each part of the statement means, but rather as a holistic pattern to read an integer from the user: int intX = console.readInt("Enter n1: "); int variable = console.readInt("prompt");
上海交通大学交大密西根 联合学院· ■ 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Classes and Objects As described in Chapter 1,C++programs are written as collections of classes,which serve as templates for individual objects.Each object is an instance of a particular class,which can serve as a pattern for many different objects. Classes in C++form hierarchies.A class can have many subclasses,but each class has only one base class (or superclass). A class represents a specialization of its base class.If you create an object that is an instance of a class,that object is also an instance of all other classes in the hierarchy above it in the base class chain. When you define a new class in C++,that class automatically inherits the behavior of its base class
Classes and Objects Classes and Objects • As described in Chapter 1, C++ programs are written as collections of classes, which serve as templates for individual objects. Each object is an instance of a particular class, which can serve as a pattern for many different objects. • Classes in C++ form hierarchies. A class can have many subclasses, but each class has only one base class (or superclass). • A class represents a specialization of its base class. If you create an object that is an instance of a class, that object is also an instance of all other classes in the hierarchy above it in the base class chain. • When you define a new class in C++, that class automatically inherits the behavior of its base class