Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.

Slides:



Advertisements
Similar presentations
Storage class in C Topics Automatic variables External variables
Advertisements

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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Memory Management Object Life Cycle:  Construction  Allocation  Preinitialization  Initialization  Use  Destruction  Cleanup  Post cleanup  Deallocation.
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];
Road Map Introduction to object oriented programming. Classes
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z z Room: D104.
Objects Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
February 11, 2005 More Pointers Dynamic Memory Allocation.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Object-Oriented Programming in C++
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
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.
 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.
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Object-Oriented Programming in C++ More examples of Association.
More C++ Features True object initialisation
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
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.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
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.
Chapter -6 Polymorphism
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
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 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Eine By: Avinash Reddy 09/29/2016.
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.
Storage class in C Topics Automatic variables External variables
Object Lifetime and Dynamic Objects
Constructor & Destructor
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
CS212: Object Oriented Analysis and Design
9-10 Classes: A Deeper Look.
Dynamic Memory.
CS410 – Software Engineering Lecture #5: C++ Basics III
C++ Class Members Class Definition class Name { public: constructor(s)
C Programming Lecture-17 Storage Classes
9-10 Classes: A Deeper Look.
Storage classes in C In C language, each variable has a storage class which decides the following things: scope i.e where the value of the variable would.
Object Oriented Programming (OOP) Lecture No. 12
SPL – PS3 C++ Classes.
Presentation transcript:

Object Lifetimes and Dynamic Objects Lecture-7

Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the lifetime of a program. Automatic (local) Objects Persistent and visible only throughout the local scope (where they were created) Static Objects Persistent throughout a program but only visible within local scope Dynamic Objects Lifetime may be controlled within a particular scope

What was that all about ? So far we have only used objects with specific names these are called 'automatic' objects These can be determined at compile time so we give them set names These may be defined as external, static, or automatic depending upon their use However dynamic objects can't easily be defined at compile time so we have to use a different mechanism

External (global) objects External Objects are persistent and visible throughout a program module i.e it's scope is an entire module (source file) It may also be visible to other source files These have identities and attribute which remain constant throughout the program External objects are always global to the module they are declared in and may become global to other objects by using the extern keyword

Automatic Objects Automatic Objects exist in a predictable manner for a particular period of time However Automatic objects are instantiated within the scope of part of a program that is, declared within the braces {} of either main or a function Automatic Objects are automatically destroyed once they fall out of the scope in which they were declared Therefore automatic objects are persistent and visible within the scope they are declared in.

Static Objects C++ allows for a variable or Objects to have the scope of an automatic object but the lifetime of an external Object This means that an object could be created once and only once, but with it's visibility delimited by the scope in which it is declared. This means that the object's state will persist even when it is not in scope (and therefore not visible) Therefore the Object can be said to 'remember' it's state when it comes

Dynamic Objects Dynamic objects are used when the object to be created is not predictable enough This is usually when we cannot determine at compile time: Object Identities Object Quantities Object Lifetimes Therefore when creating Dynamic Objects they cannot be given unique names, so we have to give them some other identities In this case we use pointers

Creating Dynamic Objects Dynamic Objects use dynamic memory allocation In C++ a pointer can be directed to an area of dynamically allocated memory at runtime This can then be made to point to a newly created object. To do this we use the new operator in the following way Point2D *point1 point1 = new Point2D(); First we create a pointer to the Point2D Class using the * Then we use the new operator to construct a new instance to the class

Using Dynamic Objects To call a user defined constructor we use the following syntax Point2D *point1 = new Point2D(100.0,100.0); To send a message (i.e. use a method) we use the -> 'pointed to' delimiter as follows cout GetY(); Apart from these distinctions dynamic Objects behave the same as static

Destroying a Dynamic Object To create an Object we use a constructor, conversely to destroy an object we use a destructor. As there is a default constructor which the programmer does not define, there is also a default destructor which the programmer does not define. However it is also possible to define a default destructor This will either be called by the programmer or by the program when the object falls out of scope

Cleanup and Garbage Collection The default constructor is used to allocate memory for the creation of the Object The default destructor is use to de-allocate this memory With a static object this is done implicitly however for dynamic objects this must be done by the programmer This now allows us to determine exactly the lifecycle of the object by being able to control both the creation and destruction of the object

Defining a destructor method The destructor method may be defined to add extra functionality to the destruction of an object It has certain characteristic which mark it out from the other methods as follows It takes the same name as the class, preceded by the 'tilde' character ~ It cannot take arguments It cannot return a value class Point2D { public : Point2D(); //constructor ~Point2D(); //destructor }; Point2D::~Point2D() { cout <<"destructor code"<<endl; }

Destroying a Dynamic Object To destroy a dynamic Object the destructor must be called. This is done by using the delete operator as follows Point2D *point = new Point2D(100.0,12.0); // now do some thing with the object delete point; // now we destroy the object

A Scope Example The following example shows how several Objects are created and go in and out of scope #include class Object { private : std::string name; static int ObjectCount; public : Object(std::string namein); ~Object(); intgetObjCount(void){return ObjectCount;} };

A Scope Example Object::Object(char * namein) { ObjectCount ++; name=namein; cout << "constructor called for "<< name << " "<< ObjectCount << " Object Instances" <<endl; } Object::~Object() { delete [] name; ObjectCount --; cout << "destructor called for " << name << " "<< ObjectCount<< " Object Instances" <<endl; } int Object::ObjectCount=0;

A Scope Example #include #include "scope.h" Object externalObject("external Object"); int main(void) { cout << "beginning of Main" << endl; { cout <<"now within the { " <<endl<<endl; Object AutoObject("Auto Object"); static Object StaticObject("Static Object"); } cout <<"now out of the } " << endl << endl; Object * Objectptr = new Object("Dynamic Object"); delete Objectptr; cout <<"end of main"<<endl; }

Program Output constructor called for external Object 1 Object beginning of Main now within the { constructor called for Auto Object 2 Object constructor called for Static Object 3 Object destructor called for Auto Object 2 Object now out of the } constructor called for Dynamic Object 3 Object destructor called for Dynamic Object 2 Object end of main destructor called for Static Object 1 Object destructor called for external Object 0 Object }