Review of C++ Programming Part II Sheng-Fang Huang.

Slides:



Advertisements
Similar presentations
Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Advertisements

 2008 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Classes: A Deeper Look Systems Programming.  constconst  const objects and const member functions   Composition   Friendship  this  this pointer.
Class and Objects.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
More on Operator Overloading CS-2303, C-Term More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The.
 2006 Pearson Education, Inc. All rights reserved Pointers.
Classes: A Deeper Look Systems Programming.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
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.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator Overloading in C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
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.
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
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.
Classes: A Deeper Look, Part But what, to serve our private ends, Forbids the cheating of our friends? ◦ Charles Churchill Instead of this absurd.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
 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.
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++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
More C++ Features True object initialisation
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
Chapter 9 Classes: A Deeper Look, Part I Part II.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Programming Techniques Classes II Important Class Features Spring 2009.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Operator Overloading; String and Array Objects
Introduction to Classes
Operator Overloading; String and Array Objects
Operator Overloading.
Operator Overloading; String and Array Objects
10.2 const (Constant) Objects and const Member Functions
Classes: A Deeper Look, Part 2
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
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 2
CS410 – Software Engineering Lecture #5: C++ Basics III
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
Presentation transcript:

Review of C++ Programming Part II Sheng-Fang Huang

Composition: Objects as Members of Classes Composition –A class can have objects of other classes as members Example –AlarmClock object with a Time object as a member –A common form of software reusability is composition, in which a class has objects of other classes as members.

Composition: Objects as Members of Classes Initializing member objects –Member initializers pass arguments from the object’s constructor to member-object constructors –If a member initializer is not provided A compilation error occurs if the member object’s class does not provide any proper constructor.

Parameters to be passed via member initializers to the constructor for class Date const objects of class Date as members

Member initializers that pass arguments to Date ’s implicit default copy constructor

Passing objects to a host object constructor

friend To declare a function as a friend of a class: –Provide the function prototype in the class definition preceded by keyword friend. To declare a class as a friend of a class: –Place a declaration of the form friend class ClassTwo; in the definition of class ClassOne All member functions of class ClassTwo are friend s of class ClassOne

friend Classes Friendship is granted, not taken –For class B to be a friend of class A, class A must explicitly declare that class B is its friend Friendship relation is neither symmetric nor transitive –If class A is a friend of class B, and class B is a friend of class C, you cannot infer that class B is a friend of class A, that class C is a friend of class B, or that class A is a friend of class C Place all friendship declarations first inside the class definition’s body and do not precede them with any access specifier.

friend function declaration (can appear anywhere in the class)

friend function can modify Count ’s private data Calling a friend function; note that we pass the Count object to the function

Dynamic Memory Management with Operators new and delete Dynamic memory management –Enables programmers to allocate and deallocate memory for any built-in or user- defined type –Performed by operators new and delete –For example, dynamically allocating memory for an array instead of using a fixed-size array

Dynamic Memory Management with Operators new and delete Operator new –Allocates (i.e., reserves) storage of the proper size for an object at execution time –Calls a constructor to initialize the object –Returns a pointer of the type specified to the right of new –Can be used to dynamically allocate any fundamental type (such as int or double ) or any class type Heap –Region of memory assigned to each program for storing objects created at execution time

Dynamic Memory Management with Operators new and delete Operator delete –Destroys a dynamically allocated object –Calls the destructor for the object –Deallocates (i.e., releases) memory from the heap –The memory can then be reused by the system to allocate other objects Not releasing dynamically allocated memory when it is no longer needed can cause the system to run out of memory prematurely. This is sometimes called a “memory leak.”

Dynamic Memory Management with Operators new and delete new operator can be used to allocate arrays dynamically –Dynamically allocate a 10-element integer array: int *gradesArray = new int[ 10 ]; –Size of a dynamically allocated array Specified using any integral expression that can be evaluated at execution time

Dynamic Memory Management with Operators new and delete Delete a dynamically allocated array: delete [] gradesArray; –If the pointer points to an array of objects First calls the destructor for every object in the array Then deallocates the memory –If the statement did not include the square brackets ( [] ) and gradesArray pointed to an array of objects Only the first object in the array would have a destructor call

static Class Members static data member –Only one copy of a variable shared by all objects of a class “Class-wide” information A property of the class shared by all instances, not a property of a specific object of the class –Declaration begins with keyword static

static Class Members static member function –Is a service of the class, not of a specific object of the class static applied to an item at file scope –That item becomes known only in that file –The static members of the class need to be available from any client code that accesses the file So we cannot declare them static in the.cpp file—we declare them static only in the.h file.

static Class Members Use static data members to save storage when a single copy of the data for all objects of a class will suffice. A class’s static data members and static member functions exist and can be used even if no objects of that class have been instantiated.

Function prototype for static member function static data member keeps track of number of Employee objects that currently exist

static data member is defined and initialized at file scope in the.cpp file static member function can access only static data, because the function might be called when no objects exist

Non- static member function (i.e., constructor) can modify the class’s static data members

Calling static member function using class name and binary scope resolution operator Dynamically creating Employee s with new Calling a static member function through a pointer to an object of the class

Fundamentals of Operator Overloading Types for operator overloading –Built in ( int, char ) or user-defined (classes) –Can use existing operators with user-defined types Cannot create new operators Overloading operators –Create a function for the class –Operator overloading contributes to C++’s extensibility—one of the language’s most appealing attributes

Overloading Stream Insertion and Stream Extraction Operators > operators –Already overloaded to process each built-in type –Can also process a user-defined class Overload using global, friend functions Example program –Class PhoneNumber Holds a telephone number –Print out formatted number automatically (123)

Notice function prototypes for overloaded operators >> and << (must be global, friend functions)

Display formatted phone number Allows cout << phone; to be interpreted as: operator<<(cout, phone);

Input each portion of phone number separately ignore skips specified number of characters from input (1 by default)

Testing overloaded >> and << operators to input and output a PhoneNumber object

Overloading Unary Operators Overloading unary operators –Can overload as non- static member function with no arguments –Can overload as global function with one argument Argument must be class object or reference to class object –Remember, static functions only access static data

Overloading Unary Operators Overload ! to test for empty string –If non- static member function, needs no arguments class String { public: bool operator!() const; … }; !s becomes s.operator!() –If global function, needs one argument bool operator!( const String & ) s! becomes operator!(s)

Overloading Binary Operators Overloading binary operators –Non- static member function, one argument –Global function, two arguments One argument must be class object or reference

Overloading Binary Operators Overloading += –If non- static member function, needs one argument class String { public: const String & operator+=( const String & ); … }; y += z becomes y.operator+=( z ) –If global function, needs two arguments const String &operator+=( String &, const String & ); y += z becomes operator+=( y, z )

Most operators overloaded as member functions (except >, which must be global functions) Prototype for copy constructor != operator simply returns opposite of == operator – only need to define the == operator

Operators for accessing specific elements of Array object

We must declare a new integer array so the objects do not point to the same memory

Want to avoid self assignment This would be dangerous if this is the same Array as right

integers1[ 5 ] calls integers1.operator[]( 5 )

Retrieve number of elements in Array Use overloaded >> operator to input

Use overloaded << operator to output Use overloaded != operator to test for inequality Use copy constructor Use overloaded = operator to assign

Use overloaded == operator to test for equality Use overloaded [] operator to access individual integers, with range-checking