Object Oriented Design

Similar documents
Chapter 9 Classes : A Deeper Look, Part 1

More C++ Classes. Systems Programming

Lecture 18 Tao Wang 1

Classes: A Deeper Look

Chapter 10 Introduction to Classes

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Computer Programming with C++ (21)

IS 0020 Program Design and Software Tools

Introduction to Programming session 24

Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

Lecture 5. Function Pointers

Classes and Objects. Class scope: - private members are only accessible by the class methods.


Constructors for classes

Cpt S 122 Data Structures. Introduction to C++ Part II

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

CSCI 123 Introduction to Programming Concepts in C++

Function Overloading

Object-Oriented Design (OOD) and C++

Fundamentals of Programming Session 24

9.1 Introduction. Integrated Time class case study Preprocessor wrapper Three types of handles on an object. Class functions

Introduction to Programming Using Java (98-388)

Data Structures using OOP C++ Lecture 3

A Deeper Look at Classes

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

Introduction Of Classes ( OOPS )

... IntArray B (100); // IntArray with 100 elements, each initialized to 0

Scope. Scope is such an important thing that we ll review what we know about scope now:

IS0020 Program Design and Software Tools Midterm, Fall, 2004

CS201 Some Important Definitions

Ch02. True/False Indicate whether the statement is true or false.

Implementing an ADT with a Class

Pointer Assignments. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 18 October 29, The new Operator

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

clarity. In the first form, the access specifier public : is needed to {

INHERITANCE: CONSTRUCTORS,

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES III

Classes and Data Abstraction

Introduction to Classes

Constructor - example

Java Primer 1: Types, Classes and Operators

An Introduction to C++

Object Oriented Design

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

57:017, Computers in Engineering C++ Classes

Interview Questions of C++

CS201- Introduction to Programming Current Quizzes

Abstract Data Types and Encapsulation Concepts

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

Recharge (int, int, int); //constructor declared void disply();

W8.1 Continuing Classes friend Functions and friend Classes Using the this Pointer Cascading Function Calls

AN OVERVIEW OF C++ 1

Chapter 11. Abstract Data Types and Encapsulation Concepts

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

CS304 Object Oriented Programming Final Term

Abstraction in Software Development

ADTs & Classes. An introduction

Iterator: A way to sequentially access ( traverse ) each object in a container or collection. - Access is done as needed, not necessarily at once.

by Pearson Education, Inc. All Rights Reserved.

Lab 2: ADT Design & Implementation

2. It is possible for a structure variable to be a member of another structure variable.

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

CSCE 110 PROGRAMMING FUNDAMENTALS

OBJECT ORIENTED PROGRAMMING USING C++

CMSC 4023 Chapter 11

Padasalai.Net s Model Question Paper

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

Praktikum: Entwicklung interaktiver eingebetteter Systeme

Chapter 11. Abstract Data Types and Encapsulation Concepts 抽象数据类型 与封装结构. 孟小亮 Xiaoliang MENG, 答疑 ISBN

Ch. 12: Operator Overloading

CS313D: ADVANCED PROGRAMMING LANGUAGE

GEA 2017, Week 4. February 21, 2017

And Even More and More C++ Fundamentals of Computer Science

Preview 9/20/2017. Object Oriented Programing with C++ Object Oriented Programing with C++ Object Oriented Programing with C++

C++ Object-Oriented Programming

III. Classes (Chap. 3)

Functions and Recursion

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

Come and join us at WebLyceum

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Constructors & Destructors

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

CS313D: ADVANCED PROGRAMMING LANGUAGE

Motivation was to facilitate development of systems software, especially OS development.

Chapter 6: Classes and Data Abstraction

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Intermediate Programming & Design (C++) Classes in C++

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Object-Oriented Principles and Practice / C++

C++ Classes, Constructor & Object Oriented Programming

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

CS201 Latest Solved MCQs

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

Object Oriented Programming(OOP).

CSE 12, Week Six, Lecture Two Discussion: Getting started on hw7 & hw8

Transcription:

Object Oriented Design Chapter 9 Initializing a non-static data member in the class definition is a syntax error 1

9.2 Time Class Case Study In Fig. 9.1, the class definition is enclosed in the following preprocessor wrapper: // prevent multiple inclusions of header file #ifndef TIME_H #define TIME_H... #endif Prevents the code between #ifndef and #endif from being included if the name TIME_H has been defined. If the header has not been included previously in a file, the name TIME_H is defined by the #define directive and the header file statements are included. If the header has been included previously, TIME_H is defined already and the header file is not included again. 2

3

9.2 Time Class Case Study (cont.) setfill specifies the fill character that is displayed when an integer is output in a field wider than the number of digits in the value. By default, the fill characters appear to the left of the digits in the number. If the number being output fills the specified field, the fill character will not be displayed. 4

5

9.3 Class Scope and Accessing Class Members A class s data members and member functions belong to that class s scope. Within a class s scope, class members are immediately accessible by all of that class s member functions and can be referenced by name. Outside a class s scope, public class members are referenced through one of the handles on an object an object name, a reference to an object or a pointer to an object. 6

9.3 Class Scope and Accessing Class Members (cont.) Overload: Member functions of a class can be overloaded only by other member functions of that class. :: to clarify the scope If a variable in a member function has the same name as a data member, how to get access to either one? Use :: The dot member selection operator (.) is preceded by an object s name or with a reference to an object to access the object s members. The arrow member selection operator (->) is preceded by a pointer to an object to access the object s members. 9.3 Class Scope and Accessing Class Members (cont.) Accessing public Class Members Through Objects, References and Pointers Consider an Account class that has a public setbalance member function. Given the following declarations: Account account; // an Account object // accountref refers to an Account object Account &accountref = account; // accountptr points to an Account object Account *accountptr = &account; 7

9.3 Class Scope and Accessing Class Members (cont.) Using the dot (.) and arrow (->) member selection operators: // call setbalance via the Account object account.setbalance( 123.45 ); // call setbalance via a reference to the Account object accountref.setbalance( 123.45 ); // call setbalance via a pointer to the Account object accountptr->setbalance( 123.45 ); 9.4 Access Functions and Utility Functions Access Functions Access functions can read or display data. A common use for access functions is to test the truth or falsity of conditions such functions are often called predicate functions. Utility Functions A utility function (also called a helper function) is a private member function that supports the operation of the class s other member functions. 8

Cannot do type casting: Time a=10; 9

10

11

12

C++11 C++11: Using List Initializers to Call Constructors C++11 now provides a uniform initialization syntax called list initializers that can be used to initialize any variable. Lines 11 13 of Fig. 9.6 can be written using list initializers as follows: Time t2{ 2 }; // hour specified; minute and second defaulted Time t3{ 21, 34 }; // hour and minute specified; second defaulted Time t4{ 12, 25, 42 }; // hour, minute and second specified or Time t2 = { 2 }; // hour specified; minute and second defaulted Time t3 = { 21, 34 }; // hour and minute specified; second defaulted Time t4 = { 12, 25, 42 }; // hour, minute and second specified The form without the = is preferred. Overload Constructor A class s constructors and member functions can also be overloaded. Overloaded constructors typically allow objects to be initialized with different types and/or numbers of arguments. 13

In Figs. 9.4 9.6, the Time constructor with three parameters had a default argument for each parameter. We could have defined that constructor instead as four overloaded constructors with the following prototypes: Time(); // default hour, minute and second to 0 Time( int ); // initialize hour; default minute and second to 0 Time( int, int); // initialize hour and minute; default second to 0 Time( int, int, int ); // initialize hour, minute and second C++11 now allows constructors to call other constructors in the same class. The calling constructor is known as a delegating constructor it delegates its work to another constructor. The name of the destructor for a class is the tilde character (~) followed by the class name. Called implicitly when an object is destroyed. The destructor itself does not actually release the object s memory it performs termination housekeeping before the object s memory is reclaimed Receives no parameters and returns no value May not specify a return type not even void A class may have only one destructor A destructor must be public If you do not explicitly provide a destructor, the compiler creates an empty destructor 14

9.8 When Constructors and Destructors Are Called Constructors and destructors are called implicitly. The order in which these function calls occur depends on the order in which execution enters and leaves the scopes where the objects are instantiated. Generally, destructor calls are made in the reverse order of the corresponding constructor calls The storage classes of objects can alter the order in which destructors are called. Constructors are called for objects defined in global scope before any other function (including main) in that file begins execution (although the order of execution of global object constructors between files is not guaranteed). The corresponding destructors are called when main terminates. Function exit forces a program to terminate immediately and does not execute the destructors of automatic objects. Function abort performs similarly to function exit but forces the program to terminate immediately, without allowing the destructors of any objects to be called. 15

Constructors and destructors for automatic objects are called each time execution enters and leaves the scope of the object. The constructor for a static local object is called only once, when execution first reaches the point where the object is defined the corresponding destructor is called when main terminates or the program calls function exit. Global and static objects are destroyed in the reverse order of their creation. Destructors are not called for static objects if the program terminates with a call to function abort. 16

17

18

End 19