C for Engineers and Scientists Second Edition - PowerPoint PPT Presentation

1 / 49
About This Presentation
Title:

C for Engineers and Scientists Second Edition

Description:

C for Engineers and Scientists, Second Edition. 4. Assignment. Memberwise assignment: copying one object to ... Operator functions may be friend functions ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 50
Provided by: tmskUi
Category:

less

Transcript and Presenter's Notes

Title: C for Engineers and Scientists Second Edition


1
C for Engineers and ScientistsSecond Edition
  • Chapter 10
  • Class Functions and Conversions

2
Objectives
  • Assignment
  • Additional Class Features
  • Operator Functions
  • Data Type Conversions
  • Applications

3
Objectives (continued)
  • Class Inheritance
  • Polymorphism
  • Common Programming Errors

4
Assignment
  • Memberwise assignment copying one object to
    another, a member at a time, as if assigning each
    member individually

5
Assignment (continued)
  • Operator overloading constructing your own
    operator in a class
  • Use the keyword operator in the function
    declaration
  • Example assignment operator

6
Assignment (continued)
  • To avoid inadvertent alteration to the object of
    the assignment, use a constant reference
    argument
  • void Dateoperator(const Date newdate)
  • Assignment does not create a new object, but can
    be part of initialization of a new object
  • Examples
  • Date b a
  • Date b(a)

7
Assignment (continued)
  • Copy constructor special constructor whose
    argument is a reference to an object of the same
    class it creates a new object that is a copy of
    the object referenced
  • Syntax
  • ClassName(const ClassName)
  • Compiler provides a default copy constructor that
    performs a memberwise copy can encounter
    problems if the class contains pointer data
    members

8
Assignment (continued)
9
Assignment (continued)
  • Base/Member Initialization List a list of values
    applied to constructor functions to initialize
    data members
  • List must be specified in the classs declaration
    section
  • ClassName(argument list) list of data members
    (initializing values)
  • This type of constructor is required when there
    is a const class instance variable

10
Additional Class Features
  • Class scope data and function members are local
    to the scope of their class
  • Static class member a variable that can be
    shared between all instantiations of a class
    acts as a global variable for the class
  • Static variables must be declared within the
    classs declaration section

11
Additional Class Features (continued)
12
Additional Class Features (continued)
  • Static member function applies to a class as a
    whole, and can only access static data members
    and other static member functions of the class
  • Friend function a non-member function that has
    been granted the same access as a classs own
    functions
  • Friends list the list of non-member friend
    functions a list of function prototype
    declarations using the keyword friend

13
Additional Class Features (continued)
  • Friend functions
  • May be declared anywhere within the declaration
    section usually placed after class header by
    convention
  • Use the keyword friend only in the class
    declaration, not in the function definition
  • Have at least one argument that is a reference to
    a class object of the class granting it friend
    status
  • Can only be given friend status by an external
    class they cannot bestow it on themselves

14
Operator Functions
  • Most operators can be overloaded for specific
    purposes within a class, with these limitations
  • Cannot create new operator symbols
  • Cannot modify precedence or associativity
  • Cannot redefine operators for built-in data types
  • Unary operators cannot be made binary, and vice
    versa
  • Operator must either be a class member or use a
    member as an operand

15
Operator Functions (continued)
  • Operator functions functions that define
    operations on class objects and use built-in
    operator symbols
  • Operator functions are called by using their
    associated symbols or by using their names
  • Examples
  • ab
  • a.operator(b)

16
Operator Functions (continued)
17
Operator Functions (continued)
  • Operator function may be a friend function
  • Requires an additional class reference
  • Example
  • friend Date operator(Date, int)
  • Operator Function Argument Requirements

18
Operator Functions (continued)
19
Operator Functions (continued)
  • Operator Function Guidelines
  • Friend functions are more appropriate for binary
    functions that do not modify any of their
    operands
  • Member functions are more appropriate for binary
    functions that do modify one of their operands

20
Data Type Conversions
  • Types of possible conversions
  • From built-in type to built-in type
  • From built-in type to class type
  • From class type to built-in type
  • From class type to class type
  • Built-in to built-in conversion can be implicit,
    or explicit using a cast
  • Syntax for explicit conversion
  • dataType(expression)

21
Data Type Conversions (continued)
  • Built-in to class conversion
  • can be done with a type conversion constructor
  • Type conversion constructor a constructor whose
    first argument is not a member of its class its
    remaining arguments (if any) all have default
    values
  • Type conversion constructor can be explicitly
    invoked after all objects have been declared

22
Data Type Conversions (continued)
23
Data Type Conversions (continued)
  • Class to built-in conversion
  • can be done with a conversion operator function
  • Conversion operator function a member operator
    function with the name of a built-in data type or
    class
  • When the name is that of a built-in data type, it
    converts from a class to a built-in data type
  • Conversion operators have no explicit arguments
    and no explicit return type

24
Data Type Conversions (continued)
25
Data Type Conversions (continued)
  • Class to class conversion
  • also uses a conversion operator function, but the
    operator function uses the name of a class being
    converted to
  • The operator function name determines the result
    of the conversion
  • If two classes each have conversion operators for
    converting to the other class, a forward
    declaration to the second class is needed in the
    first class

26
Data Type Conversions (continued)
27
Applications
  • Multi-Object Gas Pump Simulation
  • Add a Customer class
  • Create a multi-class simulation program using the
    Pump and Customer classes

28
Class Inheritance
  • Inheritance the ability to derive one class from
    another class
  • Base class the initial class used for
    derivation also called parent or superclass
  • Derived class the class created from a base
    class also called child or subclass
  • Derived class incorporates all data and member
    functions of its base class, but can add new or
    override existing data and member functions

29
Class Inheritance (continued)
  • Simple inheritance each derived type has only
    one immediate base type

30
Class Inheritance (continued)
  • Multiple inheritance a derived type has two or
    more base types

31
Class Inheritance (continued)
  • Class hierarchies illustrate the order in which
    one class is derived from another
  • Syntax for declaring a derived class
  • class derivedClassName classAccess
    baseClassName

32
Access Specifications
  • Access specifier private in base class precludes
    access by a derived class
  • protected access in base class permits only
    member or friend functions, but allows derived
    class to specify its access

33
Access Specifications (continued)
  • Derived class defines the inheritance subject to
    the base classs restriction, using the
    class-access specifier
  • Inherited Access Restrictions

34
Class Inheritance (continued)
35
Class Inheritance (continued)
36
Class Inheritance (continued)
  • A derived class object can be assigned to a base
    class object only the base class data members
    are actually assigned

37
Polymorphism
  • Polymorphism the manipulation or overloading of
    preexisting objects to suit the current need
  • Polymorphism permits the same function name to be
    used in both base and derived classes
  • Static binding the decision of which function to
    use is made at compile time
  • Dynamic binding the decision of which function
    to use is made at run time

38
Polymorphism (continued)
  • Dynamic binding requires the use of virtual
    functions
  • Virtual function creates a pointer to a
    function, but the pointer value is not assigned
    until the function is actually called
  • At run time, the proper function will be selected
    based on the object making the call
  • Use the virtual keyword to make a function virtual

39
Polymorphism (continued)
40
Polymorphism (continued)
41
Polymorphism (continued)
42
Polymorphism (continued)
  • A virtual function remains virtual in the next
    derived class regardless of the use of the
    keyword virtual in the derived class
  • All subsequent derived class override versions of
    a virtual function must have the same return type
    and parameter list

43
Polymorphism (continued)
  • An inheritance diagram

44
Common Programming Errors
  • Using a user-defined assignment operator in a
    multiple assignment expression when the operator
    was not defined to return an object
  • Using the keyword friend when defining a friend
    function. The friend keyword should be used only
    within the class declaration section
  • Using the keyword static when defining either a
    static data or function member (should be used in
    the class declaration section only)

45
Common Programming Errors (continued)
  • Failing to instantiate static data members before
    creating class objects that access them
  • Attempting to redefine an operators meaning for
    built-in data types
  • Redefining an overloaded operator to perform a
    function not indicated by its conventional
    meaning (bad programming practice)

46
Common Programming Errors (continued)
  • Attempting to make a conversion operator function
    a friend instead of a member function
  • Attempting to specify a return type for a member
    conversion operator function
  • Attempting to override a virtual function without
    using the same type and number of arguments
  • Using the keyword virtual in the class
    implementation section (only used in the class
    declaration section)

47
Summary
  • Assignment operator may be declared for a class
  • Copy constructor initializes an object by using
    another object of the same class
  • Class scope is defined by the pair of braces
  • Each object has its own set of data members,
    except for static members which are shared
  • Static functions apply to the whole class, and
    can only access static data members and other
    static function members

48
Summary (continued)
  • Friend status allows a nonmember function to
    access a classs private data members
  • User-defined operators can be constructed using
    member operator functions
  • Operator functions may be friend functions
  • Type conversion constructor its first argument
    is not a class member and its other arguments
    have default values
  • Conversion operator function a member operator
    function with the name of a built-in data type or
    class

49
Summary (continued)
  • Inheritance ability to derive one class from
    another
  • Polymorphism ability to have the same function
    name invoke different functions based on the
    object making the call
  • Static binding function to be used is determined
    at compile time
  • Dynamic binding function to be used is
    determined at run time
  • Virtual function designates that dynamic binding
    should be used
Write a Comment
User Comments (0)
About PowerShow.com