1 CSC241: Object Oriented Programming Lecture No 22.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 CSC241: Object Oriented Programming Lecture No 21.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Introduction to Programming Lecture 39. Copy Constructor.
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Copy Constructors Shallow Copy: –The data members of one object are copied into the data members of another object without taking any dynamic memory pointed.
OOP Spring 2007 – Recitation 21 Object Oriented Programming Spring 2007 Recitation 2.
Operator Overloading CS 308 – Data Structures What is operator overloading? Changing the definition of an operator so it can be applied on the objects.
1 CSC241: Object Oriented Programming Lecture No 07.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
1 CSC241: Object Oriented Programming Lecture No 13.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
C++ Review CS 302 – Data Structures Review Topics Calling functions by value or reference Pointers and reference variables Static and dynamic arrays.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Learners Support Publications edited by Taranjit singh Aulakh, BGIET sangrur,CSE deptt Constructors and Destructors.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Pointers and Dynamic Memory Allocation. Declaring a pointer.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
More C++ Features True object initialisation
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
1 CSC241: Object Oriented Programming Lecture No 02.
1 CSC241: Object Oriented Programming Lecture No 11.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
1 CS161 Introduction to Computer Science Topic #16.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
1 CSC241: Object Oriented Programming Lecture No 05.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 CSC241: Object Oriented Programming Lecture No 03.
1 CSC241: Object Oriented Programming Lecture No 17.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
1 CSC241: Object Oriented Programming Lecture No 08.
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Learners Support Publications Constructors and Destructors.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Object Oriented Programming Lecture 2: BallWorld.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
1 Ugly Realities The Dark Side of C++ Chapter 12.
CSC241: Object Oriented Programming
CSC241: Object Oriented Programming
Object-Oriented Programming (OOP) Lecture No. 17
LinkedList Class.
More Object Oriented Programming
Chapter 15 Pointers, Dynamic Data, and Reference Types
Constructors and Destructors
Introduction to Programming
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
Presentation transcript:

1 CSC241: Object Oriented Programming Lecture No 22

2 Previous Lecture Friend function – example program: Distance class – Friend function for functional notation Friend classes static functions Assignment operator and copy initialization

3 Today’s Lecture Example program – Copy initialization – Assignment operator overloaded Memory efficient string class Dynamic Type Information

4 The Copy Constructor Distance d2 = d1; It is similar to what assignment operator (d2 = d1) does The difference is that copy initialization also creates a new object feet inches d1 feet inches d

5 Example program – assignment and copy constructor class alpha { private: int data; public: alpha() { } alpha(int d) { data = d; } alpha(alpha& a) { data = a.data; cout << “\nCopy constructor invoked”; } void display() { cout << data; } void operator = (alpha& a) { data = a.data; cout << “\nAssignment operator invoked”; } }; a2 = a1; alpha a3(a1); Program Output Assignment operator invoked Copy constructor invoked

6 When Copy constructor is invoked 1.Copy constructor may be invoked when an object is defined e.g. alpha a3 = a2; or alpha a3(a2); 2.when arguments are passed by value to functions. void func(alpha a) {... } called by the statement func(a1); 3.when values are returned from functions alpha func(); a2 = func(); Alpha a = a1;

7 Why Not alpha(alpha a) Constructor? Do we need to use a reference in the argument to the copy constructor? alpha(alpha &a) Could we pass by value instead? alpha(alpha a) No, the compiler complains that it is out of memory if we try to compile Why? Because when an argument is passed by value, a copy of it is constructed What makes the copy? The copy constructor. But this is the copy constructor, so it calls itself It calls itself over and over until the compiler runs out of memory.

8 A Memory-Efficient String Class Defects with the String Class – String object contains an array of char – s2 = s1; – problem with this is that the same string now exists in two (or more) places in memory An efficient approach String object contain its own char* s2 = s1; This is efficient, since only a single copy of the string itself needs to be stored in memory S1 This is a long string in memory S2 S1S2 This is a long string in memory

9 Problem with efficient approach If we use this system we need to be careful when we destroy a String object Consider a situation in which several objects are point to same string S2S3 This is a long string in memory S1 If one string object is deleted, it will also delete the string it is pointing Other object point to memory that does not contain the string

10 Cont. To use pointers to strings in String objects, – it is required to keep track of how many String objects point to a particular string – so that array of character will be delete when the last string object that points to it is deleted S2S3 This is a long string in memory S1

11 Solution: A String-Counter Class Problem statement: – Several String objects pointing to the same string and we want to keep a count of how many Strings object point to the string. Where will we store this count? Cumbersome for each object to maintain a count of how many pointer objects are pointing to the same string static variable ?? – No this requires overhead

12 Cont. It’s more efficient to create a new class to store the count Each object of this class, which we call strCount, contains a count and also a pointer to the string

13 String and strCount objects String s1 = “This is long string in memory”; s2 = s1; s3 = s1; delete s1; String s4 = “This is different string ”; 1 2 s5 = s4; delete s5; delete s4; 0

14 Example program class strCount { private: int count; char* str; friend class String; strCount(char* s) { int length = strlen(s); str = new char[length+1]; strcpy(str, s); count=1; } ~strCount() { delete[] str; } }; class String { private: strCount* psc; public: String() { psc = new strCount(“\0”); } String(char* s) { psc = new strCount(s); } String(String& S) { psc = S.psc; (psc->count)++; } ~String() { if(psc->count==1) delete psc; else (psc->count)--; }

15 Cont. void display() { cout str; cout <<“addr=” << psc ; } void operator = (String& S) { if(psc->count==1) delete psc; else, (psc->count)--; psc = S.psc; (psc->count)++; } };

16 Cont. main() { } S3 psc: hello world :strcount count: str: String s3 = “hello world.”; String(char* s) { psc = new strCount(s); } String(char* s) { psc = new strCount(s); } strCount(char* s) { int length = strlen(s); str = new char[length+1]; strcpy(str, s); count=1; } strCount(char* s) { int length = strlen(s); str = new char[length+1]; strcpy(str, s); count=1; } 1 cout << “\ns3=”; s3.display(); Program Output s3= hello world. addr= String s1; String() { psc = new strCount(“\0”); } String() { psc = new strCount(“\0”); } S1 psc: :strcount count: str: \0 s1 = s3; void operator = (String& S) { if(psc->count==1) delete psc; else, (psc->count)--; psc = S.psc; (psc->count)++; } void operator = (String& S) { if(psc->count==1) delete psc; else, (psc->count)--; psc = S.psc; (psc->count)++; } 1 ~strCount() { delete[] str; } ~strCount() { delete[] str; } 2 cout << “\ns1=”; s1.display(); s1= hello world. addr=7550 String s2(s3); S2 psc: String(String& S) { psc = S.psc; (psc->count)++; } String(String& S) { psc = S.psc; (psc->count)++; } cout << “\ns2=”; s2.display(); s2= hello world. addr= Go to program

17