C for Engineers and Scientists Third Edition

上传人:e****s 文档编号:243360063 上传时间:2024-09-21 格式:PPT 页数:58 大小:539.50KB
返回 下载 相关 举报
C for Engineers and Scientists Third Edition_第1页
第1页 / 共58页
C for Engineers and Scientists Third Edition_第2页
第2页 / 共58页
C for Engineers and Scientists Third Edition_第3页
第3页 / 共58页
点击查看更多>>
资源描述
Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,C+ for Engineers and Scientists, Third Edition,*,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,C+ for Engineers and Scientists, Third Edition,*,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,C+ for Engineers and Scientists, Third,*,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,C+ for Engineers and Scientists, Third,*,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,C+ for Engineers and Scientists, Third,*,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,C+ for Engineers and Scientists, Third,*,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,C+ for Engineers and Scientists, Third Edition,*,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,C+ for Engineers and Scientists, Third Edition,*,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,C+ for Engineers and Scientists, Third Edition,*,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,C+ for Engineers and Scientists, Third Edition,*,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,C+ for Engineers and Scientists, Third Edition,*,Chapter 11,Class Functions and Conversions,C+ for Engineers and Scientists,Third Edition,1,Objectives,In this chapter youll learn about:,Assignment,Additional class features,Operator functions,Data type conversions,Random numbers and simulations,Class inheritance,Polymorphism,Common programming errors,C+ for Engineers and Scientists, Third Edition,2,Assignment,In this section, you will be introduced to:,How assignment works when it is applied to objects,How to define your own assignment operator to override the default one for user defined types,C+ for Engineers and Scientists, Third Edition,3,Assignment (continued),Program 11.1 demonstrates memberwise assignment,C+ compiler builds this type of default assignment operator for each class,For classes without pointer data members, this is adequate,C+ for Engineers and Scientists, Third Edition,4,Assignment (continued),Assignment operators are:,Declared in the class declaration section,Defined in the class implementation section,Operator declarations must include the operator keyword in the declaration in the form,void operator=(,ClassName,&),C+ for Engineers and Scientists, Third Edition,5,Assignment (continued),Sample declaration for the,Date,object assignment operator:,void operator=(Date,Implementation of an assignment operator for the Date object as declared above,C+ for Engineers and Scientists, Third Edition,6,C+ for Engineers and Scientists, Third Edition,7,Assignment (continued),C+ for Engineers and Scientists, Third Edition,8,(continued),Assignment (continued),Copy Constructors,Assignment is totally different than initialization,Initialization occurs every time object is created,No new object is created on assignment; value of existing object is changed,C+ for Engineers and Scientists, Third Edition,9,Copy Constructors (continued),C+ for Engineers and Scientists, Third Edition,10,Figure 11.1:,Initialization and assignment,Base/Member Initialization,True initialization has no reliance on assignment,C+ makes initialization possible with base/member initialization list syntax,Initialization list syntax is only available in constructor functions,ClassName(argument list) : list of data members( initializing values) ,C+ for Engineers and Scientists, Third Edition,11,Base/Member Initialization (continued),Example: Initialization list in declaration section,C+ for Engineers and Scientists, Third Edition,12,Example: Initialization list in implementation section,Base/Member Initialization (continued),In both forms, the body of the constructor is empty, demonstrating that no assignment is required,Code can still be placed in the constructor,This type of initialization is required to create a,const,class instance,C+ for Engineers and Scientists, Third Edition,13,Additional Class Features,Additional features include:,Class scope,Static class members,Granting access privileges to nonmember functions,C+ for Engineers and Scientists, Third Edition,14,Class Scope,The scope of a variable defines the portion of a program where the variable can be assessed,Local variable scope is defined by any block inside a brace pair,Each class defines an associated class scope,Names of data and function members are local to the scope of their class,C+ for Engineers and Scientists, Third Edition,15,Class Scope (continued),Global variables can be assessed from their point of declaration throughout the remaining portion of the file containing them with three exceptions,If a local variable has the same name, the global variable must be referenced with the scope resolution operator,:,A global variables scope can be extended into another file via the,extern,keyword,Same global name can be used in another file to define a separate variable via,static,keyword,C+ for Engineers and Scientists, Third Edition,16,Class Scope (continued),If global variable name is reused in a class, the global variable is hidden by the class member,Local variables hide the names of class data members having the same name,Member function names can only be used by objects declared for the class,C+ for Engineers and Scientists, Third Edition,17,Class Scope (continued),Example: scope of variables and functions,C+ for Engineers and Scientists, Third Edition,18,C+ for Engineers and Scientists, Third Edition,19,Example of scopes,Class Scope (continued),Static Class Members,As each class object is created, it gets its own block of memory for its data members,In some cases, it is convenient for every instantiation of a class to share the same memory location for a specific variable,C+ for Engineers and Scientists, Third Edition,20,C+ for Engineers and Scientists, Third Edition,21,Figure 11.3,Sharing the static data member,taxRate,Static Class Members (continued),Friend Functions,Private variables can be accessed and manipulated through a classs member functions,C+ for Engineers and Scientists, Third Edition,22,Direct access provided to member functions,Friend Functions (continued),External access to private functions can be granted through the friends list mechanism,The friends list members are granted the same privileges as a classs member functions,Nonmember functions in the list are called friend functions,Friends list: Series of function prototype declarations preceded with the,friend,keyword,C+ for Engineers and Scientists, Third Edition,23,Operator Functions,Only the symbols in Table 11.1 can be used for user-defined purposes,New operator symbols cannot be created,Neither the precedence nor the associativity of the C+ operators can be modified,C+ for Engineers and Scientists, Third Edition,24,Operator Functions (continued),A user-defined operation is created as a function that redefines C+ built-in operator symbols for class use,Functions that define operations on class objects and use C+ built-in operator symbols are operator functions,Function name connects operator symbol to operation defined by function,Operator function name has format,operator,C+ for Engineers and Scientists, Third Edition,25,Operator Functions as Friends,Most operator functions can be written as friend functions,Exceptions include =, (), , and -,In all cases, the friend version of a member operator function must contain an additional class reference that the member function doesnt require,C+ for Engineers and Scientists, Third Edition,26,Operator Function Argument Requirements,Data Type Conversions,Possibilities for conversion between data types include:,Conversion from built-in type to built-in type,Conversion from built-in type to class type,Conversion from class type to built-in type,Conversion from class type to class type,C+ for Engineers and Scientists, Third Edition,27,Built-in to Built-in Conversion,Can be implicit or explicit,Implicit conversion occurs in C+s operations,Explicit conversion occurs when a cast is used,Two cast notations:,C notation:,(dataType)expression,C+ notation:,dataType(expression),C+ for Engineers and Scientists, Third Edition,28,Built-in to Class Conversion,User-defined casts for converting a built-in type to a class type are created by using constructor functions,Type conversion constructor:,A constructor whose first argument is not a member of its class and whose remaining arguments, if any, have default values,If the first argument is a built-in type, then constructor can be used to cast the built-in to the class type,C+ for Engineers and Scientists, Third Edition,29,Built-in to Class Conversion (continued),Constructor function,Is called implicitly to initialize an object,Can be called explicitly after all objects have been declared,Sample conversion construction:,C+ for Engineers and Scientists, Third Edition,30,Class to Built-in Conversion,Conversion operator function,Converts from user-defined data type to built-in data type,Is a member function having the same name as the built-in data type or class,When name is the same as built-in type, used to convert from class to built-in data type,Conversion operator for class to long conversion would be named operator,long(),Has no explicit argument or return type,C+ for Engineers and Scientists, Third Edition,31,Class to Class Conversion,Class to class conversion same as class to built-in,Implemented as conversion operator function,Uses class name rather than built in data type name,Requires,forward declaration,of class when class not otherwise already known,C+ for Engineers and Scientists, Third Edition,32,A Case Study: Random Numbers and Simulations,Random numbers: Series of numbers whose order cant be predicted,Pseudorandom numbers: random enough for the type of applications being programmed,All C+ compilers provide a,rand(),function for creating random numbers,C+ for Engineers and Scientists, Third Edition,33,C+ for Engineers and Scientists, Third Edition,34,A Case Study: Random Numbers and Simulations (continued),Scaling,In most applications random numbers must be integers in a specified range,Scaling is the procedure for adjusting random numbers produced by a random number generator to fall in a specified range,C+ for Engineers and Scientists, Third Edition,35,Elevator Simulation,Simulate an elevators operation,Output the current floor on which elevator is stationed or passing by,Provide an internal elevator button to request move to another floor,Elevator can travel between first and fifteenth floor of building,C+ for Engineers and Scientists, Third Edition,36,Class Inheritance,Ability to create new classes from existing ones is the underlying motivation and power behind class- and object-oriented programming techniques,Inheritance:,Deriving one class from another class,Polymorphism:,Redefining how member functions of related classes operate based on the class object being referenced,C+ for Engineers and Scientists, Third Edition,37,Class Inheritance (continued),Base class: Initial class used as a basis for a derived class,Also called parent or superclass,Derived class: New class incorporating all the data members and member functions of its base class,Also called child class or subclass,Can, and usually does, add its own data members and member functions,Can override any base class function,C+ for Engineers and Scientists, Third Edition,38,Class Inheritance (continued),Simple inheritance: Derived type has only one base type,Multiple inheritance: Derived type has two or more base types,Class hierarchies: Illustrate the hierarchy or order in which one class is derived from another,Derived class has same form as any other class except: Includes access specifier and base class name,class derivedClassName : classAccess baseClassName,C+ for Engineers and Scientists, Third Edition,39,C+ for Engineers and Scientists, Third Edition,40,Relating object types,Class Inheritance (continued),C+ for Engineers and Scientists, Third Edition,41,An example of multiple inheritance,Class Inheritance (continued),Class Inheritance (continued),Circle,is the name of an existing class,Cylinder,can be derived as shown below:,C+ for Engineers and Scientists, Third Edition,42,Access Specifications,Private status ensures that data members can only be accessed by class member functions or friends,No access to nonclass functions except friends,Protected status same as private status except derived classes can access the base class data member,C+ for Engineers and Scientists, Third Edition,43,Access Specifications (continued),C+ for Engineers and Scientists, Third Edition,44,Inherited Access Restrictions,Access Specifications (continued),Definition of class,Circle,C+ for Engineers and Scientists, Third Edition,45,Access Specifications (continued),C+ for Engineers and Scientists, Third Edition,46,Relationship between,Circle,and,Cylinder,data members,Access Specifications (continued),C+ for Engineers and Scientists, Third Edition,47,An assignment from derived to base class,Polymorphism,Polymorphism permits the using the same function name to invoke:,One response in a base classs objects,Another response in a derived classs objects,C+ for Engineers and Scientists, Third Edition,48,Polymorphism (continued),Two types of function binding,Static binding: Determination of function to call is made at compile time,Dynamic binding: Determination of function to call is made at runtime based on object type making call,Depends on virtual functions,Virtual function:,Compiler creates a pointer to a function; assigns value to pointer upon function call,C+ for Engineers and Scientists, Third Edition,49,C+ for Engineers and Scientists, Third Edition,50,An inheritance diagram,Polymorphism (continued),Common Programming Errors,Using user-defined operator in a multiple assignment expression when operator does not return object,Using keyword,static,when defining static data member or member function,Should only be used in declaration,Using,friend,keyword with a function,Should only be used in declaration,Failing to instantiate,static,data members before creating class objects that must access these data members,C+ for Engineers and Scientists, Third Edition,51,Common Programming Errors (continued),Attempting to redefine operators meaning as applied to built-in data type,Redefining overloaded operator to perform function not indicated by conventional meaning,Works but considered bad programming practice,Attempting to make a conversion operator function a friend rather than member function,Attempting to return type for conversion operator function,C+ for Engineers and Scientists, Third Edition,52,Common Programming Errors (continued),Attempting to override virtual function without using same type and number of arguments as original,Using keyword,virtual,in class implementation section,Functions declared as virtual only in declaration section,C+ for Engineers and Scientists, Third Edition,53,Chapter Summary,Assignment operator can be declared for class as:,void operator=(className,Copy constructor initializes one object by using another object of the same class,className(const className,Each class has associated class scope defined by brace pair,containing class definition,Each class object has separate memory for members except those declared static,Static function members apply to class as whole rather than to separate objects,C+ for Engineers and Scientists, Third Edition,54,Chapter Summary (continued),Non-member function can access classs private data members if granted friend status,User-defined operators can be constructed for classes by using operator functions,User-defined operators can be called as a conventional function with arguments or as an operator function,Operator functions can also be written as friend functions,C+ for Engineers and Scientists, Third Edition,55,Chapter Summary (continued),Four categories of data type conversions,Built-in types to built-in types,Built-in types to class types,Class types to built-in types,Class types to class types,Type conversion constructor: First argument is not a member of its class; any remaining arguments have default values,Conversion operator function: Member function having the name of a class,No explicit arguments or return type,C+ for Engineers and Scientists, Third Edition,56,Chapter Summary (continued),Inheritance: Capability of deriving one class from another class,Initial class used as basis for derived class: base, parent, or superclass,Derived class: child or subclass,Base class functions can be overridden by derived class functions with same name,Polymorphism: Capability of having the same function name invoke different responses based on the object making the call,C+ for Engineers and Scientists, Third Edition,57,Chapter Summary (continued),Override functions and virtual functions can be used to implement polymorphism,Static binding: Determination of which function is called is made at compile time,Virtual binding: Dynamic binding should take place,Specification is made in functions prototype by placing the keyword,virtual,before the functions return type,After a function is declared,virtual,it remains virtual for all derived classes,C+ for Engineers and Scientists, Third Edition,58,
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业管理 > 商业计划


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!