Operator Overloading and Assignment Operators in C++: A Deep Dive, Slides of Computer Programming

An in-depth exploration of operator overloading and assignment operators in c++. Topics covered include the assignment operator, 'this' pointer, operator overloading using 'this' pointer, conversion functions, and self-assignment. Various examples to illustrate these concepts.

Typology: Slides

2011/2012

Uploaded on 11/06/2012

somo
somo 🇮🇳

4.8

(4)

70 documents

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to Programming
Lecture 33
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download Operator Overloading and Assignment Operators in C++: A Deep Dive and more Slides Computer Programming in PDF only on Docsity!

Introduction to Programming

Lecture 33

In Today’s Lecture

 Operator overloading

 Assignment operator

 A “this” pointer

 Operator over loading using this pointer

 Conversion function

a = b ;

Member Wise Assignment

Example class String { char buf [ 100 ] ; public: String ( ) ; ~ String ( ) ; ... } ; Docsity.com

main ( ) { String s1 ( “This is a test” ) ; String s2 ; s2 = s1 ; … … } Example

Example void String :: operator = ( String & s1 ) { delete buf ; buf = new char [ s1.buf + 1 ] ; strcpy ( buf , s1.buf ) ; }

Assignment Operator int i , j , k ; i = 5 ; j = 10 ; k = 15 ; i = j ; k = i ;

k = i = ++ j ;

this pointer

int i ; i = i ; // nothing happens

String s [ 1000 ] = { “This is a Test” } ;

s = s ;

main ( )

String s , * sPtr ;

sPtr = & s ;

s = * sPtr ;

Example

void String :: operator= ( String & other )
if ( this == & other )
return ;

Example