Chapter 9 More on Objects and Classes

Slides:



Advertisements
Similar presentations
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.
Advertisements

Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 10 Class Design.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 9 Thinking in Objects.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 10 Thinking in Objects.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
Programming in C++ 1. Learning Outcome  At the end of this slide, student able to:  Understand the usage of classes and functions.  Understand static.
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.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
More C++ Features True object initialisation
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Object Oriented Analysis and Design Class and Object Diagrams.
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.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
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.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
1 Introduction to Object Oriented Programming Chapter 10.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Memory Management.
Constructors and Destructors
Chapter 10 Thinking in Objects
Lecture 3: Introduction to Object and Classes
Learning Objectives Pointers as dada members
Chapter 10 Thinking in Objects
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 10 Thinking in Objects
Pointers, Polymorphism, and Memory Allocation
A First C++ Class – a Circle
Introduction to Classes
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to Classes
Chapter 9 Thinking in Objects
Corresponds with Chapter 7
Chapter 10 Thinking in Objects
Chapter 9 Classes: A Deeper Look, Part 1
Chapter 9 Objects and Classes
Constructors and Destructors
Chapter 9 Thinking in Objects
9-10 Classes: A Deeper Look.
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Chapter 9 Objects and Classes
Classes: A Deeper Look, Part 1
OO Programming Concepts
Class: Special Topics 2 For classes using memory allocation
Lecture 8 Object Oriented Programming (OOP)
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Creating and Using Classes
SPL – PS3 C++ Classes.
Object Oriented Programming
Presentation transcript:

Chapter 9 More on Objects and Classes DDC 2133 Programming II

Objectives To create immutable objects from immutable classes (§10.2). To prevent multiple declarations using the #ifndef include guard directive (§10.3). To understand the difference between instance and static variables and functions (§10.4). To implement destructors to perform customized operations (§10.5). To create objects using copy constructors with initial data copied from another object of the same type (§10.6). To customize copy constructors to perform deep copy (§10.7). To enable friend functions and friend classes to access a class’s private members (§10.8). To develop classes for modeling composition relationships (§10.9). To use the C++ vector class as a resizable array (§10.12). DDC 2133 Programming II

Immutable Objects and Classes If the contents of an object cannot be changed once the object is created, the object is called an immutable object and its class is called an immutable class. If you delete the set function in the Circle class in Listing 9.5, the class would be immutable because radius is private and cannot be changed without a set function. DDC 2133 Programming II

Example A class with all private data fields and no mutators is not necessarily immutable. To demonstrate this, let us define two classes: Person and Date. Figures 10.1 and 10.2 give the UML class diagrams of these two classes. DDC 2133 Programming II

Example DDC 2133 Programming II

Example Code Person.h Person .cpp Date.h Date.cpp TestPerson Run DDC 2133 Programming II

Preventing Multiple Declarations It is a common mistake to include the same header file in a program multiple times inadvertently. For example, you may also add the header file for the Date class, as shown in Listing 10.6, because Date is used in this program. If so, you would get a compile error to indicate that there are multiple declarations for Date. TestPerson1 Date1 DDC 2133 Programming II

Instance and Static Members Circle5.h TestCircle5.cpp Run Circle5.cpp DDC 2133 Programming II

Use Class Name Use ClassName::functionName(arguments) to invoke a static function and ClassName::staticVariable. This improves readability because the user can easily recognize the static function and data in the class. DDC 2133 Programming II

Instance or Static? How do you decide whether a variable or function should be instance or static? A variable or function that is dependent on a specific instance of the class should be an instance variable or function. A variable or function that is not dependent on a specific instance of the class should be a static variable or function. For example, every circle has its own radius. Radius is dependent on a specific circle. Therefore, radius is an instance variable of the Circle class. Since the getArea function is dependent on a specific circle, it is an instance function. Since numberOfObjects is not dependent on any specific instance, it should be declared static. DDC 2133 Programming II

Destructors Destructors are the opposite of constructors. A constructor is invoked when an object is created and a destructor is invoked when the object is destroyed. Every class has a default destructor if the destructor is not explicitly defined. Sometimes, it is desirable to implement destructors to perform customized operations. Destructors are named the same as constructors, but you must put a tilde character (~) in front of it. Listing 10.11 shows a Circle class with a destructor defined. Circle6.h Run TestCircle6.cpp Circle6.cpp DDC 2133 Programming II

Copy Constructors Destructors are the opposite of constructors. A constructor is invoked when an object is created and a destructor is invoked when the object is destroyed. Every class has a default destructor if the destructor is not explicitly defined. Sometimes, it is desirable to implement destructors to perform customized operations. Destructors are named the same as constructors, but you must put a tilde character (~) in front of it. Listing 10.11 shows a Circle class with a destructor defined. Circle6.h Run TestCircle6.cpp Circle6.cpp DDC 2133 Programming II

Copy Constructors Each class may define several overloaded constructors and one destructor. Additionally, every class has a copy constructor. The signature of the copy constructor is: ClassName(ClassName &) For example, the copy constructor for the Circle class is Circle(Circle &) The copy constructor can be used to create an object initialized with another object’s data. By default, the copy constructor simply copies each data field in one object to its counterpart in the other object. Listing 10.14 demonstrates this. Run CopyConstructorDemo DDC 2133 Programming II

Shallow Copy vs. Deep Copy The default copy constructor or assignment operator for copying objects performs a shallow copy, rather than a deep copy, meaning that if the field is a pointer to some object, the address of the pointer is copied rather than its contents. Listing 10.15 demonstrates this. Run ShallowCopyDemo DDC 2133 Programming II

Shallow Copy Before person2 is copied to person1, the birthDate field of person1 and person2 point to two different Date objects. DDC 2133 Programming II

Deep Copy Before person2 is copied to person1, the birthDate field of person1 and person2 point to two different Date objects. DDC 2133 Programming II

Customizing Copy Constructor As discussed in the preceding section, the default copy constructor or assignment operator = performs a shallow copy. To perform a deep copy, you can implement the copy constructor. Listing 10.16 revises the declaration for the Person class to declare a copy constructor in line 12. Person1 .h Person1.cpp Run CustomCopyConstructor DDC 2133 Programming II

Friend Functions and Classes Private members of a class cannot be accessed from outside of the class. Occasionally, it is convenient to allow some trusted functions and classes to access a class’s private members. C++ enables you to use the friend keyword to declare friend functions and friend classes for a class so these functions and classes can access the class’s private members. DDC 2133 Programming II

Friend Classes Date2 .h Run TestFriendClass DDC 2133 Programming II

Friend Functions Run TestFriendFunction DDC 2133 Programming II

Object Composition Composition is actually a special case of the aggregation relationship. Aggregation models has-a relationships and represents an ownership relationship between two objects. The owner object is called an aggregating object and its class an aggregating class. The subject object is called an aggregated object and its class an aggregated class. DDC 2133 Programming II

Naming Objects and Classes An aggregation relationship is usually represented as a data field in the aggregating class. For example, the relationship in Figure 10.6 can be represented as follows: DDC 2133 Programming II

Aggregation or Composition Since aggregation and composition relationships are represented using classes in similar ways, many texts don’t differentiate them and call both compositions. DDC 2133 Programming II

The Course Class Course.h Course.cpp Run TestCourse DDC 2133 Programming II

The StackOfInteger Class A stack is a data structure that holds objects in a last-in first-out fashion, as illustrated in Figure 10.10. It has many applications. For example, the compiler uses a stack to process function invocations. When a function is invoked, the parameters and local variables of the function are pushed into a stack. When a function calls another function, the new function’s parameters and local variables are pushed into the stack. When a function finishes its work and returns to its caller, its associated space is released from the stack. DDC 2133 Programming II

The StackOfInteger Class DDC 2133 Programming II

Class UML The StackOfIntegers class encapsulates the stack storage and provides the operations for manipulating the stack. DDC 2133 Programming II

Class UML StackOfIntegers.h StackOfIntegers.cpp Run TestStackOfIntegers DDC 2133 Programming II

The C++ vector Class The preceding two examples use arrays to store students and int values. There is a serious limitation: the array size is fixed in the class declaration. C++ provides the vector class, which is more flexible than arrays. You can use a vector object just like an array, but a vector’s size can grow automatically if needed. DDC 2133 Programming II

The C++ vector Class TestVector Run DDC 2133 Programming II