Introduction to Programming - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Introduction to Programming

Description:

Introduction to Programming Lecture 33 In Today s Lecture Operator overloading Assignment operator A this pointer Operator over loading using this pointer ... – PowerPoint PPT presentation

Number of Views:97
Avg rating:3.0/5.0
Slides: 33
Provided by: Saba153
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Programming


1
Introduction to Programming
  • Lecture 33

2
In Todays Lecture
  • Operator overloading
  • Assignment operator
  • A this pointer
  • Operator over loading using this pointer
  • Conversion function

3
Assignment Operator
4
a b
5
Member Wise Assignment
6
Member Wise Copy
7
Example
  • class String
  • char buf 100
  • public
  • String ( )
  • String ( )
  • ...

8
Example
  • main ( )
  • String s1 ( This is a test )
  • String s2
  • s2 s1

9
s2 s1
10
Example
  • void String operator ( String s1 )
  • delete buf
  • buf new char s1.buf 1
  • strcpy ( buf , s1.buf )

11
Assignment Operator
  • int i , j , k
  • i 5
  • j 10
  • k 15
  • i j
  • k i

12
i j k k i j
13
k i j
14
this pointer
15
this pointer
  • buf
  • this -gt buf
  • ( this ).buf

16
  • int i
  • i i // nothing happens

17
  • String s 1000 This is a Test
  • s s

18
Self Assignment
19
Example
  • main ( )
  • String s , sPtr
  • sPtr s
  • .
  • .
  • .
  • s sPtr

20
Example
  • void String operator ( String other )
  • if ( this other )
  • return
  • --------

21
Example
  • String String operator ( String other )
  • if ( this other )
  • return this
  • delete buf
  • buf new char other.length 1
  • strcpy ( buf , other.buf )
  • return this

22
s3 s2 s1
23
cout ltlt a ltlt b ltlt c
24
Example
  • Date d1 , d2
  • d2 d1
  • d2 d1 1

25
Example
  • Date date1 , date2
  • date2 date1 1
  • date2 date1

26
Example
  • main ( )
  • int i
  • float x
  • x i
  • i x

27
Example
  • main ( )
  • Date d
  • int i
  • d i

?
28
Example
  • main ( )
  • int i
  • float x
  • x ( float ) i

29
Conversion Function
30
  • Date ( )
  • // body of function

31
  • double x 1 / 3 Output 0.33333
  • x 3 Output 0.99999

32
Common Business Oriented Language
Write a Comment
User Comments (0)
About PowerShow.com