Classes and Objects: Chapter 8, Slide 1 Object and its encapsulation.

Slides:



Advertisements
Similar presentations
1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs.
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.
Chapter 6 Data Types
Operator Overloading Fundamentals
Dynamic allocation and deallocation of memory: Chapter 4, Slide 1.
CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Informática II Prof. Dr. Gustavo Patiño MJ
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers Applications
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
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.
OOP Languages: Java vs C++
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 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.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
Pointer Data Type and Pointer Variables
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.
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.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
1 Data Structures - CSCI 102 CS102 C++ Pointers & Dynamic Objects Prof Tejada.
C++ Memory Overview 4 major memory segments Key differences from Java
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
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.
More C++ Features True object initialisation
Chapter 9 Classes: A Deeper Look, Part I Part II.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
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.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
C++ 程序语言设计 Chapter 12: Dynamic Object Creation. Outline  Object creation process  Overloading new & delete.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Design issues for Object-Oriented Languages
Memory Management.
Chapter 2 Objects and Classes
Constructors and Destructors
Pointers and Dynamic Arrays
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Constructor & Destructor
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamic Memory Allocation
Constructors and Destructors
Destruction and Copying
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.
Dynamic Memory.
Destruction and Copying
Object and its encapsulation
SPL – PS3 C++ Classes.
Presentation transcript:

Classes and Objects: Chapter 8, Slide 1 Object and its encapsulation

Chapter 8, Slide 2 Inheritance is facilitated by subclasses (derived classes) Polymorphism is characterized by overloading of names of methods and operators. In C “data containers” can be either in static memory, on the heap, or on the stack. C++ objects can span all three: Consider chapter8_1 program:chapter8_1

Chapter 8, Slide 3 Create some global objects: Sample sample ("hey"); Sample sample1("good bye");

Chapter 8, Slide 4 Analyze program chapter8_2 program: chapter8_2 Destructor is missing, memory is leaking!

Chapter 8, Slide 5 Constructors and destructors are not inherited from the base class. The base constructor may be invoked implicitly or explicitly. The base destructor will be invoked implicitly. Analyze chapter8_3 program:chapter8_3 The program will display hey,Joe and then (the main purpose of this illustration) ~CC() invoked followed by ~C() invoked. Notice the reverse order of destructors called. All C allocators/deallocator can be used in C++. On top of it, there are two new operators -- allocator new (and new[]) and deallocator delete (and delete[]).

Chapter 8, Slide 6 When new fails, it either uses new_handler or throws an exception of type bad_alloc. The operator new is involved in object creation: class X { public: X(..) {.... } // constructor.. };//end class X.. X* ptr;.. ptr = new X(..);

Chapter 8, Slide 7 Placement syntax of the operator new : #include.. class X { public: void* operator new(size_t s,int a1,int a2){... }.. };//end class X int main () { X* ptr = new(1,2) X;.. return 0; }

Chapter 8, Slide 8 Even though destructors cannot be called explicitly, a placement new should have a corresponding “placement” delete (used only when construction fails midway): #include.. class X { public: void* operator new(size_t s,int a1,int a2){... } void operator delete(void* p,int a1,int a2){... }.. };//end class X

Chapter 8, Slide 9 Arrays of objects must be created by new[] using default constructors only! Analyze chapter8_5 program: They have to be deallocated by delete[] Analyze chapter 8_4 program: A few guidelines to make sure that the process memory manager does not get corrupted: Never pass a pointer to free() that has not been returned previously by malloc(), calloc(), or realloc(). Deallocate segments allocated by malloc(), calloc(), and realloc() }} using exclusively free().

Chapter 8, Slide 10 Never pass a pointer to delete that has not been returned previously by new. Deallocate segments allocated by new using exclusively delete. Never pass a pointer to delete[] that has not been returned previously by new[]. Deallocate segments allocated by new[] using exclusively delete[]. If your program uses placement- new, it should have a corresponding placement- delete, even though it will be only called implicitly by the compiler when an exception is thrown during the construction of an object.

Chapter 8, Slide 11 Copy constructor: Analyze chapter8_6 program:chapter8_6 The will work fine displaying hey Joe on the screen. The object c created in main() using constructor C(char*) will be copied to the activation frame of doit() using the copy constructor C(const C&). When doit() terminates, the object d --- as built in the activation frame of doit() --- is destructed using ~C(). Without the copy constructor (the memberwise copy constructor would have been used), the program would have crashed, as no “deep” copy of the salutation would have been provided, yet ~C() would still try to deallocate it.

Chapter 8, Slide 12 Assignment operator --- operator= This method specifies how to perform assignments of type o1=o2 between two objects (which is in a sense again a form of copying, from o1 to o2 ). In the absence of an explicit assignment, the memberwise assignment is performed. The same problems may ensue as discussed for the missing copy constructor. However, a missing assignment is even more dangerous as it can lead to memory leaks: class C {... };//end class C int doit() { C c1("hey Joe"), c2; c1=c2; return 0; } //end doit

Chapter 8, Slide 13 c1 is created and c1.salutation points to a dynamically created string "hey Joe". c2 is created using the default constructor and hence the value of c2.salutation is set to NULL. When c1=c2 assignment is performed, due to the absence of an explicit assignment method in the definition of the class C, the memberwise copy is performed and thus c1.salutation is set to NULL. When c1 and c2 are destructed, none of them is “linked” to the string "hey Joe" and so the string is never deallocated and “leaks”. The difference between copy and assignment results from the fact that the copy is concerned with “forming the raw memory into an object” while the assignment must deal with a “well-constructed object’”, thus in essence the assignment must de-construct the object before it can do the copying: Analyze chapter8_7 program:chapter8_7

Chapter 8, Slide 14 End of slides for chapter 8