This pointer, Dynamic memory allocation, Constructors and Destructor

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
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.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
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];
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
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++ 程序语言设计 Chapter 12: Dynamic Object Creation. Outline  Object creation process  Overloading new & delete.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
 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.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Learners Support Publications Constructors and Destructors.
Memory Management.
Constructors and Destructors
Eine By: Avinash Reddy 09/29/2016.
Pointer to an Object Can define a pointer to an object:
Pointers and Dynamic Arrays
Chapter 13: Overloading and Templates
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
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.
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Programming with ANSI C ++
CONSTRUCTORS & DESTRUCTORS
Constructors & Destructors.
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
Constructor & Destructor
Class Operations Pointer and References with class types
Class: Special Topics Copy Constructors Static members Friends this
Pointers Revisited What is variable address, name, value?
group work #hifiTeam
Dynamically Allocated Memory
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Array Lists Chapter 6 Section 6.1 to 6.3
Introduction to Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 9 Classes: A Deeper Look, Part 1
Contents Introduction to Constructor Characteristics of Constructor
Overview of Memory Layout in C++
Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Constructors and destructors
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
Classes and Objects.
Classes, Constructors, etc., in C++
9-10 Classes: A Deeper Look.
Submitted By : Veenu Saini Lecturer (IT)
Classes: A Deeper Look, Part 1
COP 3330 Object-oriented Programming in C++
Class: Special Topics 2 For classes using memory allocation
The Constructors Lecture 7 Fri, Feb 2, 2007.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Destructors, Copy Constructors & Copy Assignment Operators
Pointers, Dynamic Data, and Reference Types
Destructors, Copy Constructors & Copy Assignment Operators
Constructors & Destructors
More C++ Classes Systems Programming.
CS 144 Advanced C++ Programming April 30 Class Meeting
9-10 Classes: A Deeper Look.
Copy Constructors and Overloaded Assignment
SPL – PS3 C++ Classes.
SPL – PS2 C++ Memory Handling.
Introduction to Classes and Objects
Presentation transcript:

This pointer, Dynamic memory allocation, Constructors and Destructor

Scope Resolution Operator New operator in C++ Has many roles To define member functions outside classes Initialization of static data members To access a global variable with same name as that of local variable

This pointer ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions  constant pointer that holds the memory address of the current object Not available in static member functions as static member functions can be called without any object (with class name).

When local variable’s name is same as member’s name ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions  constant pointer that holds the memory address of the current object Not available in static member functions as static member functions can be called without any object (with class name).

New and delete in C++ Similar to malloc and free in C But there is an option to initialize memory Can be used allocate memory for single or array of elements

New and delete in C++ If memory is available, the new operator allocates memory space for the requested object/array, and returns a pointer to (address of) the memory allocated. If sufficient memory is not available, the new operator returns NULL. Dynamically allocated object/array exists until the delete operator destroys it

Constructors Special type of member functions Have same name as class Automatically called by system during object creation Should be with public access specifier Should allocate memory if required Should initialize data members

Constructors Three types Default – No Parameters Parameterized Copy If no constructor is provided then compiler provides one in C++  Compiler created default constructor has empty body and doesn’t initialize Compiler doesn’t create a default constructor if we write any constructor 

Parameterized Constructors Parameters are passed to initialize data members Default arguments may be present Parameterized constructors may be overloaded

Copy Constructors Important when class has pointer data members and dynamic memory allocation is done in default constructor Compiler creates a copy constructor if we don’t write our own copy constructor. But does something called shallow copying Address is copied instead of allocating memory and copying content

Problem without Copy Constructors Two objects refer to same memory location Unsafe At any point of time the other object’s life may end Leads to dangling pointers – Pointer with address which is not valid

Copy Constructors Compiler creates a copy constructor if we don’t write our own copy constructor. So we need to write copy constructor only when we have pointers or run time allocation of resource like file handle, a network connection, etc

When is copy constructor called? In C++, a Copy Constructor may be called in following cases:  1. When an object of the class is returned by value. 2. When an object of the class is passed (to a function) by value as an argument. 3. When an object is constructed based on another object of the same class. 4. When compiler generates a temporary object.

When one constructor (either parameterized or copy) is provided, it is mandatory to provide default constructor

Argument to Copy constructor must be constant When objects are returned the argument of copy constructor should be const Because compiler will create temporary object and invoke copy constructor to copy values from temporary object compiler created temporary objects cannot be bound to non-const references

Why argument to a copy constructor must be passed as a reference? A copy constructor is called when an object is passed by value. Copy constructor itself is a function. So if we pass argument by value in a copy constructor, a call to copy constructor would be made to call copy constructor which becomes a non-terminating chain of calls. Therefore compiler doesn’t allow parameters to be pass by value.

In Lab Practice Develop a class complex numbers Develop a class vector, dynamically allocate memory