1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 1 Chapter 10 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

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Road Map Introduction to object oriented programming. Classes
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.
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.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented Design.
 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.
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 10 Classes and Objects: A Deeper Look Visual C# 2010 for Programmers © by Pearson Education, Inc. All Rights Reserved.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
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.
More C++ Features True object initialisation
Chapter 4 Introduction to Classes, Objects, Methods and strings
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)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
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 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
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.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
 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.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Chapter 9 More on Objects and Classes
Chapter 9 Classes: A Deeper Look, Part 1
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 9 Objects and Classes
Constructors and Other Tools
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Andy Wang Object Oriented Programming in C++ COP 3330
Creating and Using Classes
Presentation transcript:

1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes

2 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 2 Objectives  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 enable friend functions and friend classes to access a class’s private members (§10.8).  To use the C++ vector class as a resizable array (§10.12).

3 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 3 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. TestPerson1Date1

4 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X

5 5 Instance and Static Members Run TestCircle5.cppCircle5.hCircle5.cpp

6 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 6 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.

7 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 7 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.

8 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X

9 9 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 shows a Circle class with a destructor defined. Run TestCircle6.cpp Circle6.hCircle6.cpp

10 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 10 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 shows a Circle class with a destructor defined. Run TestCircle6.cpp Circle6.hCircle6.cpp

11 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 11 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 demonstrates this. Run CopyConstructorDemo

12 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X

13 13 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 demonstrates this. Run ShallowCopyDemo

14 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 14 Shallow Copy Before person2 is copied to person1, the birthDate field of person1 and person2 point to two different Date objects.

15 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 15 Deep Copy Before person2 is copied to person1, the birthDate field of person1 and person2 point to two different Date objects.

16 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X

17 17 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.

18 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 18 Friend Classes Run TestFriendClass Date2.h

19 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 19 Friend Functions Run TestFriendFunction

20 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X

21 21 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.

22 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 22 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:

23 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 23 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.

24 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 24 The Course Class Run TestCourseCourse.hCourse.cpp

25 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X

26 26 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.

27 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 27 The C++ vector Class Run TestVector