1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.

Slides:



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

Chapter 18 Vectors and Arrays
Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
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.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming Lecture 39. Copy Constructor.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Dynamically Allocated Arrays May 2, Quiz 5 Today.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
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.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Copy Constructors Fall 2008 Dr. David A. Gaitros
1 Inside the Vector Class: with additional needed methods CPS212CPS212 Gordon College.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 13: Pointers You are not responsible for virtual functions (starting on.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
More C++ Features True object initialisation
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
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:
Chapter 9 Classes: A Deeper Look, Part I Part II.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
1 Structural Types, Data Abstractions and Classes.
1 Final Exam Tues 3/16/10 (2-3:50) (Same classroom) Old Textbook - Chapters 11-16, 18 Focus is on 15, 16 and 18 Final Exam Tues 3/16/10 (2-3:50) (Same.
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++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
1 Review: C++ class represents an ADT 2 kinds of class members: data members and function members Class members are private by default Data members are.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.
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.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
Learners Support Publications Constructors and Destructors.
Pointers and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Constructors and Destructors
Learning Objectives Pointers as dada members
CISC181 Introduction to Computer Science Dr
Chapter Structured Types, Data Abstraction and Classes
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Introduction to Structured Data Types and Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Constructors and Destructors
Chapter 15-3 Pointers, Dynamic Data, and Reference Types
CS148 Introduction to Programming II
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
Review: C++ class represents an ADT
CS148 Introduction to Programming II
SPL – PS3 C++ Classes.
Presentation transcript:

1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor is always the name of the class, and there is no return type for the constructor a class may have several constructors with different parameter lists. A constructor with no parameters is the default constructor a constructor is implicitly invoked when a class object is declared--if there are parameters, their values are listed in parentheses in the declaration

2 Specification of Time Class Constructors class Time// Time.h { public : // function members void set ( int h, int m, int s) ; int getHour(); int getMins(); int getSecs(); void increment ( ) ; void write ( ) const ; bool Equal ( Time otherTime ) const ; bool LessThan ( Time otherTime ) const ; Time ( int initH, int initM, int initS ) ; // constructor Time ( ) ; // default constructor private :// 3 data members int hour ; int mins ; int secs ; } ; 2

3 Implementation of Time Default Constructor Time :: Time ( ) { hour = 0 ; mins = 0 ; secs = 0 ; } 3

4 Implementation of Another Time Class Constructor Time :: Time ( /* in */ int initH, /* in */ int initM, /* in */ int initS ) { hour = initH ; mins = initM ; secs = initS ; } 4

5 Automatic invocation of constructors occurs Time departureTime ; // default constructor invoked Time movieTime (19, 30, 0 ) ; // parameterized constructor departureTime movieTime Private data: hrs mins secs Set Increment Write LessThan Equal Private data: hrs mins secs Set Increment Write LessThan Equal

6 Using a Pointer Variable Arrow operation Time* t1; Time t2(13,5,2); t1 = new Time(12,3,9); t1->printTime(); delete t1; t2.printTime(); Output: 12:03:09 13:05:02

7 Destructor ~Time(); The function will be invoked when the class object is destroyed. Exit the scope of object delete the object

8 Why is a destructor needed? When a DynArray class variable goes out of scope, the memory space for data members size and pointer arr is deallocated. But the dynamic array that arr points to is not automatically deallocated. A class destructor is used to deallocate the dynamic memory pointed to by the data member.

9 What happens... When a function is called that uses pass by value for a class object of DynArray type? ? 75 ? ? Private: size 5 arr DynArray Store ValueAt DynArray ~DynArray CopyFrom

10 // FUNCTION CODE void SomeFunc( DynArray someArr ) // Uses pass by value {. } Passing a Class Object by Value

11 Initialization of Class Objects C++ defines initialization to mean  initialization in a variable declaration  passing an object argument by value  returning an object as the return value of a function by default, C++ uses shallow copies for these initializations