CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.

Slides:



Advertisements
Similar presentations
Class and Objects.
Advertisements

1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
1 Operator Overloading. 2 Syntax The general syntax is: [friend] returntype operator ( ) { ; }
Chapter 13: Overloading.
Chapter 15: Operator Overloading
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.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator overloading Object Oriented Programming.
Operator Overloading in C++
Review of C++ Programming Part II Sheng-Fang Huang.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Operator Overloading and Type Conversions
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
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.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
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.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Operator overloading and type convesions BCAS,Bapatla B.mohini devi.
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.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
1 CSC241: Object Oriented Programming Lecture No 08.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Operator Overloading.
Operator Overloading Introduction
Yan Shi CS/SE 2630 Lecture Notes
CS212: Object Oriented Analysis and Design
Learning Objectives Pointers as dada members
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Class: Special Topics Copy Constructors Static members Friends this
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Operator Overloading; String and Array Objects
CISC/CMPE320 - Prof. McLeod
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Operator Overloading; String and Array Objects
Class: Special Topics 2 For classes using memory allocation
SPL – PS3 C++ Classes.
Presentation transcript:

CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor

Recap of Lecture 9 Function Overloading Name Mangling Resolution of name and type Copy constructor

Outline of lecture 10 Copy constructor Operator overloading Unary operator Operator chaining

Pointers in C++ Pointers in C++ is a more strongly typed language C doesn’t let you casually assign a pointer of one type to another It does allow you to accomplish this through a void* bird* b; rock* r; void* v; v = r; b = v;

Recap of reference A reference must be initialized when it is created. Once a reference is initialized to an object, it cannot be changed to refer to another object. You cannot have NULL references. Must be careful while returning reference from a function !! Should not refer to unknown memory.

Constant Reference The function does modify the outside argument. Making the argument a const reference will allow the function to be used in all situations. For built-in types, the function will not modify the argument For user-defined types, the function will call only const member functions, won’t modify any public data members. Temporary objects are always constant

Argument-passing guidelines Normal habit when passing an argument to a function should be to pass by const reference. There are concerns other than efficiency To pass an argument by value requires a constructor and destructor call If the arguments are not going to be modified : pass by const reference

Passing and returning by value Declare a function and make a function call, how does the compiler know how to pass and return those variables? Equivalent assembly code int f(int x, char c); int g = f(a, b); push b push a call f() add sp,4 mov g, register a

Passing and returning large objects The entire contents of B is pushed on the stack The address of B2 is pushed before making the call Constraints on the compiler when it’s making a function call. struct Big { char buf[100]; int i; long d; } B, B2; Big bigfun(Big b) { b.i = 100; // Do something return b; } int main() { B2 = bigfun(B); }

Function-call stack frame 1.Pushes all the arguments on the stack 2.Makes the call 3.Provide storage for the function’s local variables Function arguments Return address Local variables Re-entrancy

Functions in C and C++ support interrupts: re-entrant They also support recursive calls Return values on the stack?? Allocating the extra storage on the stack for the return values before calling the function. Push the address of the return value’s destination on the stack as one of the function arguments, The function copy the return information directly into the destination.

Bitwise copy and its issue Class: Student NameRollMarks Sumit925 Class: Student NameRollMarks Sumit925 A B= A

Bitwise copy and its issue Class: Student NameRollM1M2M3M4 Sumit A B= A Class: Student NameRollM1M2M3M4 Sumit

Bitwise copy and its issue Class: Student NameRoll*Marks Sumit90xABC Class: Student NameRoll*Marks Sumit90xABC A B= A

Create object from an existing object One of the more important forms of an overloaded constructor is the copy constructor. Defining a copy constructor can help you prevent problems that might occur when one object is used to initialize another. When we copy? Initialize Pass as argument Return as parameter

Deep vs Shallow copy Copy constructor is called only for initializations For assignment ? Operator overload ??

Operator overloading Compiler sees an expression consisting of an argument followed by an operator followed by an argument, it simply calls a function. An operator is simply a function call with a different syntax. There must be a previously declared function to match that operator Operator overloading is just “syntactic sugar,” which means it is simply another way for you to make a function call.

Is the knowledge about operator wrong ? Maybe everything they know about operators in C is suddenly wrong Operators for built-in types won’t suddenly start working differently Overloaded operators can be created only where new data types are involved 1 << 4; << 1; won’t suddenly change its meaning won’t suddenly start working.

Operator overloading Specify more than one definition for an operator in the same scope Declared (or defined) previously in the same scope The compiler determines the most appropriate definition to use Overload resolution Redefine or overload most of the built-in operators available

We are already overloading Inserters ( >) A stream is an object that formats and holds bytes. int i; cin >> i; float f; cin >> f; char c; cin >> c; char buf[100]; cin >> buf;

Operator overloading Overloaded operators are functions with special names The overloaded function will perform relative to the class upon which it will work The keyword operator followed by the symbol for the operator being defined Operator functions can be member or non-member of a class Non-member operator function : friend functions

Creating a Member operator function The general form of such a function is Ret-type classname::operator#(arg-list){ //operations } // Overload + for loc. loc loc::operator+(loc op2) Demonstration

Can we overload all operators +-*/%^ &|~!,= <><=>=++-- <<>>==!=&&|| +=-=/=%=^=&= |=*=<<=>>=[]() ->->*newnew []deletedelete [] ::.*.?:

Parameter passing to operator function Operator function Global Unary (1) Binary (2) Member Unary (0) Binary (1) The number of arguments in the overloaded operator’s argument list depends on two factors

Restrictions Cannot combine operators that currently have no meaning Cannot change the evaluation precedence of operators Cannot change the number of arguments required by an operator

Overloading Unary Operator Takes just one operand Takes no argument Example: Increment (++) and decrement(--) operator Unary minus (-) operator The logical not (!) operator The unary operators operate on the object for which they were called

Operator chaining Assignment is right-associative In order to support operator chaining, the assignment operator must return some value The value that should be returned is a reference to the left- hand side of the assignment. But should this reference be a const or nonconst? int a, b, c, d, e; a = b = c = d = e = 42;

Self assignment MUST CHECK FOR SELF-ASSIGNMENT !! Especially important when your class does its own memory allocation MyClass& MyClass::operator=(const MyClass &rhs) { // 1. Deallocate any memory that MyClass is using internally // 2. Allocate some memory to hold the contents of rhs // 3. Copy the values from rhs into this instance // 4. Return *this } MyClass mc;... mc = mc;

How to recover self assignment There are many ways to answer the question Are these two instances the same? Just compare the two objects' addresses MyClass& MyClass::operator=(const MyClass &rhs) { if (this != &rhs) {... // Deallocate, allocate new space, copy values... } return *this; }

Assignment operator The guidelines for the assignment operator are: Take a const-reference for the argument (the right-hand side of the assignment). Return a reference to the left-hand side, to support safe and reasonable operator chaining. (Do this by returning *this.) Check for self-assignment, by comparing the pointers (this to &rhs).

Thank you Next Lecture: Operator Overloading-II