 2006 Pearson Education, Inc. All rights reserved. 1 9 9 Classes: A Deeper Look, Part 1.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Classes: A Deeper Look Systems Programming.  constconst  const objects and const member functions   Composition   Friendship  this  this pointer.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Templates.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
 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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction.
Classes: A Deeper Look Systems Programming.
Introduction to Classes and Data Abstraction
A Deeper Look at Classes CS-2303, C-Term A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
More C++ Classes Systems Programming. Systems Programming: C++ Classes 2 Systems Programming: 2 C++ Classes  Preprocessor Wrapper  Time Class Case Study.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Review of C++ Programming Part II Sheng-Fang Huang.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
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 9 Classes: A Deeper Look, Part I Part II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
 2008 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Class 4 (L34) u Constructors u Default Constructor u Example of Default Constructors u Destructors u Constructors Are Called in Global Scope u Constructors.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Learners Support Publications Constructors and Destructors.
 2006 Pearson Education, Inc. All rights reserved Templates.
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Constructors and Destructors
Chapter 16: Classes and Data Abstraction
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Chapter 9 Classes: A Deeper Look, Part 1
Constructors and Destructors
Classes: A Deeper Look, Part 1
9-10 Classes: A Deeper Look.
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Operator Overloading; String and Array Objects
Classes: A Deeper Look, Part 1
A Deeper Look at Classes
Constructors & Destructors
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
SPL – PS3 C++ Classes.
Presentation transcript:

 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1

 2006 Pearson Education, Inc. All rights reserved. 2 Performance Tip Objects contain only data, so objects are much smaller than if they also contained member functions. Applying operator sizeof to a class name or to an object of that class will report only the size of the class’s data members. The compiler creates one copy (only) of the member functions separate from all objects of the class. All objects of the class share this one copy. Each object, of course, needs its own copy of the class’s data, because the data can vary among the objects. The function code is nonmodifiable and, hence, can be shared among all objects of one class.

 2006 Pearson Education, Inc. All rights reserved Class Scope and Accessing Class Members Dot member selection operator (. ) – Accesses the object’s members – Used with an object’s name or with a reference to an object Arrow member selection operator ( -> ) – Accesses the object’s members – Used with a pointer to an object

 2006 Pearson Education, Inc. All rights reserved. 4

5 9.6 Constructors with Default Arguments Constructors can specify default arguments – Can initialize data members to a consistent state Even if no values are provided in a constructor call – Constructor that defaults all its arguments is also a default constructor Can be invoked with no arguments Maximum of one default constructor per class

 2006 Pearson Education, Inc. All rights reserved. 6

7

8

9 9.7 Destructors Destructor – A special member function – Name is the tilde character ( ~ ) followed by the class name, e.g., ~Time – Called implicitly when an object is destroyed For example, this occurs as an automatic object is destroyed when program execution leaves the scope in which that object was instantiated – Does not actually release the object’s memory It performs termination housekeeping Then the system reclaims the object’s memory – So the memory may be reused to hold new objects

 2006 Pearson Education, Inc. All rights reserved Destructors (Cont.) Destructor (Cont.) – Receives no parameters and returns no value May not specify a return type—not even void – A class may have only one destructor Destructor overloading is not allowed – If the programmer does not explicitly provide a destructor, the compiler creates an “empty” destructor

 2006 Pearson Education, Inc. All rights reserved When Constructors and Destructors Are Called Constructors and destructors – Called implicitly by the compiler Order of these function calls 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 – However, Storage classes of objects can alter the order in which destructors are called

 2006 Pearson Education, Inc. All rights reserved When Constructors and Destructors Are Called (Cont.) For objects defined in global scope – Constructors are called before any other function (including main ) in that file begins execution – The corresponding destructors are called when main terminates Function exit – Forces a program to terminate immediately – Does not execute the destructors of automatic objects

 2006 Pearson Education, Inc. All rights reserved When Constructors and Destructors Are Called (Cont.) For an automatic local object – Constructor is called when that object is defined – Corresponding destructor is called when execution leaves the object’s scope – Constructors and destructors are called each time execution enters and leaves the scope of the object

 2006 Pearson Education, Inc. All rights reserved When Constructors and Destructors Are Called (Cont.) For a static local object – Constructor is called only once When execution first reaches where the object is defined – 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

 2006 Pearson Education, Inc. All rights reserved. 15

 2006 Pearson Education, Inc. All rights reserved. 16

 2006 Pearson Education, Inc. All rights reserved. 17

 2006 Pearson Education, Inc. All rights reserved Default Memberwise Assignment Default memberwise assignment – Assignment operator ( = ) Can be used to assign an object to another object of the same type – Each data member of the right object is assigned to the same data member in the left object Can cause serious problems when data members contain pointers to dynamically allocated memory

 2006 Pearson Education, Inc. All rights reserved. 19 Outline Date.h (1 of 1)

 2006 Pearson Education, Inc. All rights reserved. 20 Outline Date.cpp (1 of 1)

 2006 Pearson Education, Inc. All rights reserved. 21 Outline fig09_19.cpp (1 of 1)

 2006 Pearson Education, Inc. All rights reserved Default Memberwise Assignment (Cont.) Copy constructor – Enables pass-by-value for objects Used to copy original object’s values into new object to be passed to a function or returned from a function – Compiler provides a default copy constructor Copies each member of the original object into the corresponding member of the new object (i.e., memberwise assignment) – Also can cause serious problems when data members contain pointers to dynamically allocated memory