C++ programming program design including data structure 7th ch10

46 106 0
C++  programming program design including data structure 7th ch10

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter 10: Classes and Data Abstraction Objectives In this chapter, you will: – Learn about classes – Learn about private, protected, and public members of a class – Explore how classes are implemented – Become aware of accessor and mutator functions – Examine constructors and destructors C++ Programming: Program Design Including Data Structures, Seventh Edition Objectives (cont’d.) – Learn about the abstract data type (ADT) – Explore how classes are used to implement ADTs – Become aware of the differences between a struct and a class – Learn about information hiding – Explore how information hiding is implemented in C++ – Learn about the static members of a class C++ Programming: Program Design Including Data Structures, Seventh Edition Classes • Object-oriented design (OOD): a problem solving methodology • Objects: components of a solution • Class: a collection of a fixed number of components • Member: a component of a class C++ Programming: Program Design Including Data Structures, Seventh Edition Classes (cont’d.) • Class definition: – Defines a data type; no memory is allocated – Don’t forget the semicolon after the closing brace • Syntax: C++ Programming: Program Design Including Data Structures, Seventh Edition Classes (cont’d.) • Class member can be a variable or a function • If a member of a class is a variable – It is declared like any other variable – You cannot initialize a variable when you declare it • If a member of a class is a function – Function prototype is listed – Function members can (directly) access any member of the class C++ Programming: Program Design Including Data Structures, Seventh Edition Classes (cont’d.) • Three categories of class members: – private (default) • Member cannot be accessed outside the class – public • Member is accessible outside the class – protected C++ Programming: Program Design Including Data Structures, Seventh Edition Unified Modeling Language Class Diagrams • Unified Modeling Language (UML) notation: used to graphically describe a class and its members – +: member is public – -: member is private – #: member is protected C++ Programming: Program Design Including Data Structures, Seventh Edition Unified Modeling Language Class Diagrams (cont’d.) C++ Programming: Program Design Including Data Structures, Seventh Edition Variable (Object) Declaration • Once defined, you can declare variables of that class type clockType myClock; • A class variable is called a class object or class instance C++ Programming: Program Design Including Data Structures, Seventh Edition 10 In-line initialization of Data Members and the Default Constructor • C++11 standard allows member initialization in class declarations • Called in-line initialization • Can eliminate the need for a default constructor • Not all compilers recognize this feature C++ Programming: Program Design Including Data Structures, Seventh Edition 32 Arrays of Class Objects (Variables) and Constructors • If you declare an array of class objects, the class should have the default constructor C++ Programming: Program Design Including Data Structures, Seventh Edition 33 Destructors • Destructors are functions without any type • The name of a destructor is the character '~' followed by class name – For example: ~clockType(); • A class can have only one destructor – The destructor has no parameters • Destructor automatically executes when the class object goes out of scope C++ Programming: Program Design Including Data Structures, Seventh Edition 34 Data Abstract, Classes, and Abstract Data Types • Abstraction – Separating design details from usage – Separating the logical properties from the implementation details • Abstraction can also be applied to data • Abstract data type (ADT): data type that separates the logical properties from the implementation details C++ Programming: Program Design Including Data Structures, Seventh Edition 35 A struct Versus a class • By default, members of a struct are public – private specifier can be used in a struct to make a member private • By default, the members of a class are private • classes and structs have the same capabilities C++ Programming: Program Design Including Data Structures, Seventh Edition 36 A struct Versus a class (cont’d.) • In C++, the definition of a struct was expanded to include member functions, constructors, and destructors • If all member variables of a class are public and there are no member functions – Use a struct C++ Programming: Program Design Including Data Structures, Seventh Edition 37 Information Hiding • Information hiding: hiding the details of the operations on the data • Interface (header) file: contains the specification details • File extension is h • Implementation file: contains the implementation details • File extension is cpp • In header file, include function prototypes and comments that briefly describe the functions – Specify preconditions and/or postconditions C++ Programming: Program Design Including Data Structures, Seventh Edition 38 Information Hiding (cont’d.) • Implementation file must include header file via include statement • In include statement: – User-defined header files are enclosed in double quotes – System-provided header files are enclosed between angular brackets C++ Programming: Program Design Including Data Structures, Seventh Edition 39 Information Hiding (cont'd.) • Precondition: A statement specifying the condition(s) that must be true before the function is called • Postcondition: A statement specifying what is true after the function call is completed C++ Programming: Program Design Including Data Structures, Seventh Edition 40 Executable Code • To use an object in a program – The program must be able to access the implementation • Visual C++ 2015 Express, Visual Studio 2015, and C++ Builder put the editor, compiler, and linker into a package – One command (build, rebuild, or make) compiles program and links it with the other necessary files – These systems also manage multiple file programs in the form of a project C++ Programming: Program Design Including Data Structures, Seventh Edition 41 Static Members of a Class • Use the keyword static to declare a function or variable of a class as static • A public static function or member of a class can be accessed using the class name and the scope resolution operator • static member variables of a class exist even if no object of that class type exists C++ Programming: Program Design Including Data Structures, Seventh Edition 42 Static Members of a Class (cont’d.) • Multiple objects of a class each have their own copy of non-static member variables • All objects of a class share any static member of the class C++ Programming: Program Design Including Data Structures, Seventh Edition 43 Summary • Class: collection of a fixed number of components • Members: components of a class – Accessed by name – Classified into one of three categories: • private, protected, and public • Class variables are called class objects or, simply, objects C++ Programming: Program Design Including Data Structures, Seventh Edition 44 Summary (cont’d.) • The only built-in operations on classes are assignment and member selection • Constructors guarantee that data members are initialized when an object is declared – Default constructor has no parameters • Destructor automatically executes when a class object goes out of scope – A class can have only one destructor – The destructor has no parameters C++ Programming: Program Design Including Data Structures, Seventh Edition 45 Summary (cont’d.) • Abstract data type (ADT): data type that separates the logical properties from the implementation details • A public static member, function or data, of a class can be accessed using the class name and the scope resolution operator • Static data members of a class exist even when no object of the class type exists • Instance variables: non-static data members C++ Programming: Program Design Including Data Structures, Seventh Edition 46 ... protected C++ Programming: Program Design Including Data Structures, Seventh Edition Unified Modeling Language Class Diagrams (cont’d.) C++ Programming: Program Design Including Data Structures,... – Assignment (=) C++ Programming: Program Design Including Data Structures, Seventh Edition 12 Assignment Operator and Classes C++ Programming: Program Design Including Data Structures, Seventh... operator :: C++ Programming: Program Design Including Data Structures, Seventh Edition 19 Implementation of Member Functions (cont’d.) C++ Programming: Program Design Including Data Structures,

Ngày đăng: 06/02/2018, 09:15

Từ khóa liên quan

Mục lục

  • Chapter 10: Classes and Data Abstraction

  • Objectives

  • Objectives (cont’d.)

  • Classes

  • Classes (cont’d.)

  • Slide 6

  • Slide 7

  • Unified Modeling Language Class Diagrams

  • Unified Modeling Language Class Diagrams (cont’d.)

  • Variable (Object) Declaration

  • Accessing Class Members

  • Built-in Operations on Classes

  • Assignment Operator and Classes

  • Class Scope

  • Class Scope (cont’d.)

  • Functions and Classes

  • Reference Parameters and Class Objects (Variables)

  • Reference Parameters and Class Objects (Variables) (cont’d.)

  • Implementation of Member Functions

  • Implementation of Member Functions (cont’d.)

Tài liệu cùng người dùng

Tài liệu liên quan