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.

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

 2005 Pearson Education, Inc. All rights reserved Introduction.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
Classes and Objects Systems Programming.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 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 Classes: A Deeper Look, Part 1.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
 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.
More C++ Classes Systems Programming. Systems Programming: C++ Classes 2 Systems Programming: 2 C++ Classes  Preprocessor Wrapper  Time Class Case Study.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
 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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Introduction to C++ Part II.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
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.
CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I.
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
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.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
計算機程式語言 Lecture 03-1 國立台灣大學生物機電系 林達德 3 3 Introduction to Classes and Objects.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
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.
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.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
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.
Classes in C++ By Ms Nashandi. Placing a class in a separate file for Reusability When building an object C++ Program, it is customary to define reusable.
 2007 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects
Chapter 16: Classes and Data Abstraction
Chapter 3 Introduction to Classes, Objects and Strings
Chapter 3 Introduction to Classes and Objects
Introduction to Classes and Objects
3-4-5 Introduction.
Introduction to Classes and Objects
Object-Oriented Programming
Object-Oriented Programming
3-4-5 Introduction.
Introduction to Classes and Objects
More C++ Classes Systems Programming.
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

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 class definitions are placed.  Allow compiler to recognize the classes when used elsewhere.  Generally have.h filename extensions. Driver files ◦ Program used to test software (such as classes) ◦ Contains a main function so it can be executed

Class definition is in a header file

Including the header file causes the class definition to be copied into the file

3.8 Placing a Class in a Separate File for Reusability #include preprocessor directive ◦ Used to include header files  Instructs C++ preprocessor to replace directive with a copy of the contents of the specified file ◦ Quotes indicate user-defined header files  Preprocessor first looks in current directory  If the file is not found, looks in C++ Standard Library directory ◦ Angle brackets indicate C++ Standard Library  Preprocessor looks only in C++ Standard Library directory

Error-Prevention Tip 3.3 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., ).

3.8 Placing a Class in a Separate File for Reusability Creating objects ◦ Compiler must know size of object  C++ objects typically contain only data members  Compiler creates one copy of class ’ s member functions  This copy is shared among all the class ’ s objects

3.9 Separating Interface from Implementation Goal ◦ Client code should not break if implementation changes, as long as interface stays the same Interface ◦ Describes what services a class ’ s clients can use and how to request those services  But does not reveal how the class carries out the services  A class definition that lists only member function names, return types and parameter types  Function prototypes ◦ A class ’ s interface consists of the class ’ s public member functions (services)

3.9 Separating Interface from Implementation Separating interface from implementation ◦ Define member functions outside the class definition, in a separate source-code file  In source-code file for a class  Use binary scope resolution operator ( :: ) to tie each member function to the class definition  Implementation details are hidden  Client code does not need to know the implementation ◦ In header file for a class  Function prototypes describe the class ’ s public interface

Interface contains data members and member function prototypes

Common Programming Error 3.8 Forgetting the semicolon at the end of a function prototype is a syntax error.

Good Programming Practice 3.7 Although parameter names in function prototypes are optional (they are ignored by the compiler), many programmers use these names for documentation purposes.

Common Programming Error 3.9 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. Note that each member function should be implemented.

Binary scope resolution operator ties a function to its class GradeBook implementation is placed in a separate source-code file Include the header file to access the class name GradeBook

Separating Interface from Implementation The Compilation and Linking Process ◦ Source-code file is compiled to create the class ’ s object code (source-code file must #include header file)  Class implementation programmer only needs to provide header file and object code to client ◦ Client must #include header file in their own code  So compiler can ensure that the main function creates and manipulates objects of the class correctly ◦ To create executable application  Object code for client code must be linked with the object code for the class and the object code for any C++ Standard Library object code used in the application

Validating Data with set Functions set functions can validate data ◦ Keeps object in a consistent state  The data member contains a valid value ◦ Can return values indicating that attempts were made to assign invalid data

Validating Data with set Functions string member functions ◦ length returns the number of characters in the string ◦ Substr returns specified substring within the string

21

set functions perform validity checking to keep courseName in a consistent state Constructor calls set function to perform validity checking

23

Constructor will call set function to perform validity checking

25 Call set function to perform validity checking

26 Software Engineering Observation 3.6 Making data members private and controlling access, especially write access, to those data members through public member functions helps ensure data integrity.

27 Software Engineering Observation 3.7 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.