Class 4 (L34) u Constructors u Default Constructor u Example of Default Constructors u Destructors u Constructors Are Called in Global Scope u Constructors.

Slides:



Advertisements
Similar presentations
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Advertisements

C++ Classes & Data Abstraction
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6: Classes and Data Abstraction Outline 6.1 Introduction 6.2 Structure Definitions 6.3 Accessing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction 16.2Implementing a Time Abstract Data.
Introduction to Classes and Data Abstraction
1 Review (Week2) - Control Structures C++ has only seven control structures: –Sequence structure Programs executed sequentially by default –Selection structures.
More C++ Classes Systems Programming. Systems Programming: C++ Classes 2 Systems Programming: 2 C++ Classes  Preprocessor Wrapper  Time Class Case Study.
 2003 Prentice Hall, Inc. All rights reserved Introduction Object-oriented programming (OOP) –Encapsulates data (attributes) and functions (behavior)
Classi - Esempi1 // SalesPerson class definition // Member functions defined in salesp.cpp #ifndef SALESP_H #define SALESP_H class SalesPerson { public:
1 Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
 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.
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.
1 Classes and Data Abstraction Part II Initializing Class Objects: Constructors Constructors  Initialize data members  Same name as class 
1 Chapter 4 CLASSES AND OBJECTS. 2 Outlines Procedural vs. Object ‑ Oriented Programming C++ Structures versus C Structures Classes –Accessing Class Members.
SMIE-121 Software Design II School of Mobile Information Engineering, Sun Yat-sen University Lecture.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Classes and Data Abstraction Part I Introduction Object-oriented programming (OOP)  Encapsulates data (attributes) and functions (behavior)
1 Classes and Data Abstraction 2 Objectives Understand encapsulation and data hiding Understand data abstraction and Abstract Data Types (ADTs) Create.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 6: Classes and Data Abstraction Outline 6.1Introduction 6.2Structure Definitions 6.3Accessing.
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.
Class 3 (L33) u Client of a Class u Purpose of Public Members u Private Class Members u Controlling Access u Access Functions u Predicate Functions u Utility.
1 const and this Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3: Classes May 24,
Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 4: Classes Jan 27,
1 Lecture 6 Classes and Data Abstraction: Part II.
1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.
EEL 3801 Part VI Fundamentals of C and C++ Programming Classes and Objects.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 20 November 10, 2009.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7: Classes Part II Outline 7.1 Introduction 7.2 const (Constant) Objects and const Member Functions.
 2000 Prentice Hall, Inc. All rights reserved. NHTI CP 107 Fall 2001 M. Saleem Yusuf Introduction Object-oriented programming (OOP) –Objects Encapsulates.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. Këpuska Summer 2004 from Dr. S. Kozaitis Spring 2003 slides 1 Summary of Chapter 6: Classes.
1 Example Original Array Array After 2 nd Pass Array After 1 st Pass Array After 3 rd Pass.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
Chapter 16: Classes and Data Abstraction
Lecture 5: Classes (continued) Feb 1, 2005
Chapter 16: Classes and Data Abstraction
Classes: A Deeper Look Outline
Lecture 5: Classes September 14, 2004
Chapter 6: Classes and Data Abstraction
Chapter 9 Classes: A Deeper Look, Part 1
Classes and Data Abstraction
Chapter 6: Classes and Data Abstraction
Object-oriented programming (OOP)
Chapter 6: Classes and Data Abstraction
9-10 Classes: A Deeper Look.
#include <iostream.h>
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Classes: A Deeper Look, Part 1
Chapter 6: Classes and Data Abstraction
Chapter 6: Classes and Data Abstraction
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
Presentation transcript:

Class 4 (L34) u Constructors u Default Constructor u Example of Default Constructors u Destructors u Constructors Are Called in Global Scope u Constructors Are Called for Automatic Local Objects u Constructors Are Called for Static Local Objects Dr. Ming Zhang

Constructors u A constructor is a class member function with the same name as the class. u When a class object is created, its members can be initialized by that class’s constructor function. u The programmer provides the constructor which is then invoked automatically each time an object of that class is created ( instantiated). u Constructors may be overloaded to provide a variety of means for initializing objects of a class. Dr. Ming Zhang

Default Constructor u A programmer-supplied constructor that defaults all its arguments (or explicitly requires no arguments) is called a default constructor. u There can be only one default constructor per class. u By providing default arguments to the constructor, even if no values are provided in a constructor call, the object is still guaranteed to be initialized to a consistent state due to the default arguments. Dr. Ming Zhang

Example of Default Constructors (1) class Time { public: Time( int = 0, int = 0, int = 0 ); // default constructor void setTime( int, int, int );// set hour, minute, second void printMilitary(); // print military time format void printStandard(); // print standard time format private: int hour; // int minute; // int second; // }; Dr. Ming Zhang

Example of Default Constructors (2) // Member function definitions for Time class. #include using std::cout; #include "time2.h" // Time constructor initializes each data member to zero. // Ensures all Time objects start in a consistent state. Time::Time( int hr, int min, int sec ) { setTime( hr, min, sec ); } Dr. Ming Zhang

Example of Default Constructors (3) // Set a new Time value using military time. Perform validity // checks on the data values. Set invalid values to zero. void Time::setTime( int h, int m, int s ) { hour = ( h >= 0 && h < 24 ) ? h : 0; minute = ( m >= 0 && m < 60 ) ? m : 0; second = ( s >= 0 && s < 60 ) ? s : 0; } Dr. Ming Zhang

Example of Default Constructors (4) // Print Time in military format void Time::printMilitary() { cout << ( hour < 10 ? "0" : "" ) << hour << ":" << ( minute < 10 ? "0" : "" ) << minute; } Dr. Ming Zhang

Example of Default Constructors (5) // Print Time in standard format void Time::printStandard() { cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) << ":" << ( minute < 10 ? "0" : "" ) << minute << ":" << ( second < 10 ? "0" : "" ) << second << ( hour < 12 ? " AM" : " PM" ); } Dr. Ming Zhang

Example of Default Constructors (6) // Demonstrating a default constructor function for class #include using std::cout; using std::endl; #include "time2.h" int main() { Time t1, // all arguments defaulted t2(2), // minute and second defaulted t3(21, 34), // second defaulted t4(12, 25, 42), // all values specified t5(27, 74, 99); // all bad values specified ……. Dr. Ming Zhang

Example of Default Constructors (7) cout << "Constructed with:\n" << "all arguments defaulted:\n "; t1.printMilitary(); cout << "\n "; t1.printStandard(); Dr. Ming Zhang Constructed with: all arguments defaulted: 00:00 12:00:00AM

Example of Default Constructors (8) cout << "\nhour specified; minute and second defaulted:" << "\n "; t2.printMilitary(); cout << "\n "; t2.printStandard(); Dr. Ming Zhang Hour specified; minute and second defaulted: 02:00 2:00:00 AM

Example of Default Constructors (9) cout << "\nhour and minute specified; second defaulted:" << "\n "; t3.printMilitary(); cout << "\n "; t3.printStandard(); Dr. Ming Zhang Hour and minute specifies; second defaulted: 21:34 9:34:00 PM

Example of Default Constructors (10) cout << "\nhour, minute, and second specified:" << "\n "; t4.printMilitary(); cout << "\n "; t4.printStandard(); Dr. Ming Zhang Hour, minute, and second specified: 12:25 12:25:42 PM

Example of Default Constructors (11) cout << "\nall invalid values specified:" << "\n "; t5.printMilitary(); cout << "\n "; t5.printStandard(); cout << endl; return 0; } Dr. Ming Zhang All invalid values specified: 00:00 12:00:00 AM

Destructors u A destructor is a special member function of a class u The name of the destructor for a class is the tilde (~) character followed by the class name. u A class’s destructor is called when an object is destroyed - e.g., when program execution leaves the scope in which an object of that class was instantiated. u The destructor performs termination housekeeping before the system reclaims the object’s memory so that memory may be reused to hold new objects. Dr. Ming Zhang

When Constructors & Destructors are Called u Constructors and destructors are called automatically. u The order in which these function calls are made depends on the order in which execution enters and leaves the scope in which objects are instantiated. Dr. Ming Zhang

Constructors Are Called in Global Scope u Constructor are called for objects defined in global scope before any other function (including main) in that file begins execution. u The corresponding destructors are called when main terminates or the exit function is called. Dr. Ming Zhang

Constructors Are Called for Automatic Local Objects u Constructors are called for automatic local objects when execution reaches the point where the objects are defined. u The corresponding destructors are called when the objects leave scope (i.e., the block in which they are defined is exited) u Constructors and destructors for automatic objects are called each time the objects enter and leave scope. Dr. Ming Zhang

Constructors Are Called for Static Local Objects u Constructors are called for static local objects only once when execution first reaches the point where the objects are defined. u u Corresponding destructors are called when main terminates or the exit function is called. Dr. Ming Zhang

Example of When Constructors & Destructors Are Called (1) // Definition of class CreateAndDestroy. // Member functions defined in create.cpp. #ifndef CREATE_H #define CREATE_H class CreateAndDestroy { public: CreateAndDestroy( int ); // constructor ~CreateAndDestroy(); // destructor private: int data; }; Dr. Ming Zhang

Example of When Constructors & Destructors Are Called (2) //Member function definitions for classCreateAndDestroy #include using std::cout; using std::endl; #include "create.h" CreateAndDestroy::CreateAndDestroy( int value ) { data = value; cout << "Object " << data << " constructor"; } CreateAndDestroy::~CreateAndDestroy() { cout << "Object " << data << " destructor " << endl; } Dr. Ming Zhang

Example of When Constructors & Destructors Are Called (3) #include using std::cout; using std::endl; #include "create.h" void create( void ); // prototype CreateAndDestroy first( 1 ); // global object Dr. Ming Zhang

Example of When Constructors & Destructors Are Called (4) // Function to create objects void create( void ) { CreateAndDestroy fifth( 5 ); cout << " (local automatic in create)" << endl; static CreateAndDestroy sixth( 6 ); cout << " (local static in create)" << endl; CreateAndDestroy seventh( 7 ); cout << " (local automatic in create)" << endl; } Dr. Ming Zhang

Example of When Constructors & Destructors Are Called (5) int main() { cout << " (global created before main)" << endl; CreateAndDestroy second( 2 ); // local object cout << " (local automatic in main)" << endl; static CreateAndDestroy third( 3 ); // local object cout << " (local static in main)" << endl; create(); // call function to create objects CreateAndDestroy fourth( 4 ); // local object cout << " (local automatic in main)" << endl; return 0; } Dr. Ming Zhang

Example of When Constructors & Destructors Are Called (6) Object 1 constructor (global created before main) Object 2 constructor (local automatic in main) Object 3 constructor (local static in main) Object 5 constructor (local automatic in create) Object 6 constructor (local static in create) Object 7 constructor (local automatic in create) Object 7 destructor Object 5 destructor Object 4 constructor (local automatic in main) Object 4 destructor Object 2 destructor Object 6 destructor Object 3 destructor Object 1 destructor Dr. Ming Zhang