611 18200 計算機程式語言 Lecture 03-1 國立台灣大學生物機電系 林達德 3 3 Introduction to Classes and Objects.

Slides:



Advertisements
Similar presentations
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
Advertisements

計算機程式語言 Lecture 10-1 國立台灣大學生物機電系 林達德 10 Classes: A Deeper Look, Part 2.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
 2006 Pearson Education, Inc. All rights reserved Introduction.
 2006 Pearson Education, Inc. All rights reserved Templates.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
 2009 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 01] Introduction to.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
計算機程式語言 Lecture 09-1 國立台灣大學生物機電系 林達德 9 9 Classes: A Deeper Look, Part 1.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
計算機程式語言 Lecture 05-1 國立台灣大學生物機電系 林達德 5 5 Control Statements: Part 2.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
計算機程式語言 Lecture 02-1 國立台灣大學生物機電系 林達德 2 2 Introduction to C++ Programming.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 3 Part II. 3.8 Placing a Class in a Separate File for Reusability.cpp file is known as a source-code file. Header files ◦ Separate files in which.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 16 is a copy of C++ How to Program Chapter.
Chapter 3 Introduction to Classes, Objects and Strings C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
 2007 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects
Introduction to Classes and Objects
Chapter 3 Introduction to Classes, Objects and Strings
Introduction to Classes and Objects
Chapter 3 Introduction to Classes, Objects Methods and Strings
Lecture 22 Inheritance Richard Gesick.
3-4-5 Introduction.
Introduction to Classes and Objects
Introduction to Classes and Objects
Object Oriented Programming in java
Introduction to Classes and Objects
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

計算機程式語言 Lecture 03-1 國立台灣大學生物機電系 林達德 3 3 Introduction to Classes and Objects

計算機程式語言 Lecture 03-2 國立台灣大學生物機電系 林達德 3.1 Introduction 3.2 Classes, Objects, Member Functions and Data Members 3.3 Overview of the Chapter Examples 3.4 Defining a Class with a Member Function 3.5 Defining a Member Function with a Parameter 3.6 Data Members, set Functions and get Functions 3.7 Initializing Objects with Constructors 3.8 Placing a Class in a Separate File for Reusability 3.9 Separating Interface from Implementation 3.10 Validating Data with set Functions

計算機程式語言 Lecture 03-3 國立台灣大學生物機電系 林達德 OBJECTIVES In this chapter you will learn: What classes, objects, member functions and data members are. How to define a class and use it to create an object. How to define member functions in a class to implement the class's behaviors. How to declare data members in a class to implement the class's attributes. How to call a member function of an object to make that member function perform its task. The differences between data members of a class and local variables of a function. How to use a constructor to ensure that an object's data is initialized when the object is created. How to engineer a class to separate its interface from its implementation and encourage reuse.

計算機程式語言 Lecture 03-4 國立台灣大學生物機電系 林達德 3.1 Introduction Object Oriented Programming Concept, OOP Structured Programming vs. OOP Class –Data members –Member functions Grade-book class as an example for OOP.

計算機程式語言 Lecture 03-5 國立台灣大學生物機電系 林達德 3.2 Classes, Objects, Member Functions and Data Members Performing a task in a program requires a function. The function describes the mechanisms that actually perform its task. In C++, we begin by creating a program unit called a class to house a function. A function belonging to a class is called a member function. An object of a class need to be created in order to get a program to perform the tasks the class describes.

計算機程式語言 Lecture 03-6 國立台灣大學生物機電系 林達德 3.2 Classes, Objects, Member Functions and Data Members A member-function call tells a member function of the object to perform its task. Performing a task in a program requires a function. An object has attributes that are carried with the object as it is used in a program. Attributes are specified by the class’s data member.

計算機程式語言 Lecture 03-7 國立台灣大學生物機電系 林達德 3.3 Overview of the Chapter Examples GradeBook class examples to demonstrate the OOP concept –Defining a class with a member function –Defining a member function with a parameter –Data members, set functions and get functions –Initializing objects with constructors –Placing a class in a separate file for reusability –Separating interface from implementation –Validating data with set functions

計算機程式語言 Lecture 03-8 國立台灣大學生物機電系 林達德 3.4 Defining a Class with a Member Function Defining a class. Every class’s body is enclosed in a pair of left and right braces ( { and } ). Capitalization style – Camel case. The keyword public is called an access specifier. When defining a function, a return type must be specified to indicate the type of the value return by the function. The return type void indicates that the function will not return any data to its calling function.

計算機程式語言 Lecture 03-9 國立台灣大學生物機電系 林達德 Outline fig03_01.cpp (1 of 1) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.4 Defining a Class with a Member Function Forgetting the semicolon at the end of a class definition is a syntax error. Common Programming Error 3.1

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.4 Defining a Class with a Member Function Returning a value from a function whose return type has been declared void is a compilation error. Common Programming Error 3.2

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.4 Defining a Class with a Member Function Defining a function inside another function is a syntax error. Common Programming Error 3.3

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.4 Defining a Class with a Member Function UML class diagram for class GradeBook –UML is a graphical language used by programmers to represent their object-oriented systems in a standard manner. –UML diagram UML class diagram indicating that class GradeBook has a public displayMessage operation.

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.5 Defining a Member Function with a Parameter A function call supplies values – called arguments – for each of the function’s parameter. A function can specify multiple parameters by separating each parameter from the next with a comma. The argument types in the function call must match the types of the corresponding parameters in the function header.

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_03.cpp (1 of 2) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_03.cpp (2 of 2) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.5 Defining a Member Function with a Parameter Placing a semicolon after the right parenthesis enclosing the parameter list of a function definition is a syntax error. Common Programming Error 3.4

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.5 Defining a Member Function with a Parameter Defining a function parameter again as a local variable in the function is a compilation error. Common Programming Error 3.5

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.5 Defining a Member Function with a Parameter To avoid ambiguity, do not use the same names for the arguments passed to a function and the corresponding parameters in the function definition. Good Programming Practice 3.1

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.5 Defining a Member Function with a Parameter Choosing meaningful function names and meaningful parameter names makes programs more readable and helps avoid excessive use of comments. Good Programming Practice 3.2

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.5 Defining a Member Function with a Parameter UML class diagram indicating that class GradeBook has a displayMessage operation with a courseName parameter of UML type String.

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.6 Data Members, set Functions and get Functions Variables declared in a function’s body are known as local variables. Attributes represented as variables in a class definition are called data members. Set functions –Perform validity checks before modifying private data –Notify if invalid values –Indicate with return values Get functions –“Query” functions –Control format of data returned

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.6 Data Members, set Functions and get Functions Variables or functions declared after access specifier private is are accessible only to member functions of the class for which they are declared. Declaring data members with access specifier private is known as data hiding.

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_05.cpp (1 of 3) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_05.cpp (2 of 3) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_05.cpp (3 of 3) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.6 Data Members, set Functions and get Functions Place a blank line between member-function definitions to enhance program readability. Good Programming Practice 3.3

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.6 Data Members, set Functions and get Functions As a rule of thumb, data members should be declared private and member functions should be declared public. (We will see that it is appropriate to declare certain member functions private, if they are to be accessed only by other member functions of the class.) Software Engineering Observation 3.1

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.6 Data Members, set Functions and get Functions An attempt by a function, which is not a member of a particular class (or a friend of that class, as we will see in Chapter 10), to access a private member of that class is a compilation error. Common Programming Error 3.6

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.6 Data Members, set Functions and get Functions Despite the fact that the public and private access specifiers may be repeated and intermixed, list all the public members of a class first in one group and then list all the private members in another group. This focuses the client ’ s attention on the class ’ s public interface, rather than on the class ’ s implementation. Good Programming Practice 3.4

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.6 Data Members, set Functions and get Functions If you choose to list the private members first in a class definition, explicitly use the private access specifier despite the fact that private is assumed by default. This improves program clarity. Good Programming Practice 3.5

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.6 Data Members, set Functions and get Functions We will learn in Chapter 10, Classes: Part 2, that functions and classes declared by a class to be friends can access the private members of the class. Software Engineering Observation 3.2

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.6 Data Members, set Functions and get Functions Making the data members of a class private and the member functions of the class public facilitates debugging because problems with data manipulations are localized to either the class ’ s member functions or the friends of the class. Error-Prevention Tip 3.1

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.6 Data Members, set Functions and get Functions Forgetting to return a value from a function that is supposed to return a value is a compilation error. Common Programming Error 3.7

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.6 Data Members, set Functions and get Functions Always try to localize the effects of changes to a class ’ s data members by accessing and manipulating the data members through their get and set functions. Changes to the name of a data member or the data type used to store a data member then affect only the corresponding get and set functions, but not the callers of those functions. Good Programming Practice 3.6

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.6 Data Members, set Functions and get Functions It is important to write programs that are understandable and easy to maintain. Change is the rule rather than the exception. Programmers should anticipate that their code will be modified. Software Engineering Observation 3.3

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.6 Data Members, set Functions and get Functions The class designer need not provide set or get functions for each private data item; these capabilities should be provided only when appropriate. If a service is useful to the client code, that service should typically be provided in the class ’ s public interface. Software Engineering Observation 3.4

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.6 Data Members, set Functions and get Functions UML class diagram for class GradeBook with a private courseName attribute and public operations setCourseName, getCourseName and displayMessage.

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.7 Initializing Objects with Constructors Constructors –Initialize data members Or can set later –Same name as class –No return type Initializers –Passed as arguments to constructor –In parentheses to right of class name before semicolon Class-type ObjectName( value1,value2,…);

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.7 Initializing Objects with Constructors Constructors –Can specify default arguments –Default constructors Defaults all arguments OR Explicitly requires no arguments Can be invoked with no arguments Only one per class

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_07.cpp (1 of 3) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_07.cpp (2 of 3) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_07.cpp (3 of 3) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.7 Initializing Objects with Constructors Unless no initialization of your class ’ s data members is necessary (almost never), provide a constructor to ensure that your class ’ s data members are initialized with meaningful values when each new object of your class is created. Error-Prevention Tip 3.2

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.7 Initializing Objects with Constructors Data members can be initialized in a constructor of the class or their values may be set later after the object is created. However, it is a good software engineering practice to ensure that an object is fully initialized before the client code invokes the object ’ s member functions. In general, you should not rely on the client code to ensure that an object gets initialized properly. Software Engineering Observation 3.5

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.7 Initializing Objects with Constructors UML class diagram indicating that class GradeBook has a constructor with a name parameter of UML type String.

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.8 Placing a Class in a Separate File for Reusability Header files Including a header file that contains a user-defined class How header files are located Additional software engineering issues –The class is reusable –The clients of the class know what member functions the class provides, how to call them and what return types to expect –The clients do not know how the class’s member functions are implemented

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_09.cpp (1 of 2) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_09.cpp (2 of 2) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_10.cpp (1 of 1) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.8 Placing a Class in a Separate File for Reusability To ensure that the preprocessor can locate header files correctly, #include preprocessor directives should place the names of user-defined header files in quotes (e.g., "GradeBook.h" ) and place the names of C++ Standard Library header files in angle brackets (e.g., ). Error-Prevention Tip 3.3

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.9 Separating Interface from Implementation Interface of a class Separating the interface from the implementation Defining a class’s interface with function prototypes Defining member functions in a separate source- code file Testing class GradeBook The compilation and linking process

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_11.cpp (1 of 1) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.9 Separating Interface from Implementation Forgetting to return a value from a function that is supposed to return a value is a compilation error. Common Programming Error 3.8

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.9 Separating Interface from Implementation Although parameter names in function prototypes are optional (they are ignored by the compiler), many programmers use these names for documentation purposes. Good Programming Practice 3.7

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.9 Separating Interface from Implementation Parameter names in a function prototype (which, again, are ignored by the compiler) can be misleading if wrong or confusing names are used. For this reason, many programmers create function prototypes by copying the first line of the corresponding function definitions (when the source code for the functions is available), then appending a semicolon to the end of each prototype. Error-Prevention Tip 3.4

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.9 Separating Interface from Implementation When defining a class ’ s member functions outside that class, omitting the class name and binary scope resolution operator ( :: ) preceding the function names causes compilation errors. Common Programming Error 3.9

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_12.cpp (1 of 2) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_12.cpp (2 of 2) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_13.cpp (1 of 1) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.9 Separating Interface from Implementation Compilation and linking process that produces an executable application.

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.10 Validating Data with set Functions Enhance class GradeBook’s member function setCourseName to perform validity checking The C++ standard library’s string class –length –substr

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_15.cpp (1 of 1) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_16.cpp (1 of 2) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_16.cpp (2 of 2) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_17.cpp (1 of 2) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 Outline fig03_17.cpp (2 of 2) Outline

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.10 Validating Data with set Functions Making data members private and controlling access, especially write access, to those data members through public member functions helps ensure data integrity. Software Engineering Observation 3.6

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.10 Validating Data with set Functions The benefits of data integrity are not automatic simply because data members are made private — the programmer must provide appropriate validity checking and report the errors. Error-Prevention Tip 3.5

計算機程式語言 Lecture 國立台灣大學生物機電系 林達德 3.10 Validating Data with set Functions Member functions that set the values of private data should verify that the intended new values are proper; if they are not, the set functions should place the private data members into an appropriate state. Software Engineering Observation 3.7