SlideShare a Scribd company logo
S202 4- 1
Implicit conversions occur when mixed type
expressions are evaluated or when the actual
arguments in a function call do not match the formal
arguments of the function prototype.
First, the compiler tries to apply a trivial conversion.
If there is no match, then the compiler attempts to
apply a promotion.
If there is no match, then the compiler attempts to
apply a built-in type conversion.
If there is no match, then the compiler attempts to
apply a user defined type conversion.
If there is no match, the compiler generates an error.
Implicit Conversions
S202 4- 2
To determine if a user defined type conversion is used,
the compiler checks if a conversion is defined for the
type needed.
If there is, then the compiler checks that the type
matches the type supplied or that the type supplied can
be converted by applying one of the built-in type
promotions or conversions.
Only one such built-in type promotion or conversion
will be applied before applying the user defined type
conversion itself.
Thus, at most one built-in type conversion and at most
one user defined type conversion will ever be implicitly
applied when converting from one type to another.
Implicit Conversions
S202 4- 3
Explicit conversions occur when the client explicitly
invokes a cast operation.
Both implicit and explicit type conversions result in a
temporary object of the type being converted to.
This temporary object is used in place of the original.
The value of the original object is not affected.
When considering execution efficiency, it is important
to reduce or minimize the creation of such temporaries.
We recommend minimizing user defined conversions
as much as possible by avoiding mixed type
expressions and by supplying arguments to functions
that exactly match the type required by the function
prototypes.
Explicit Conversions
S202 4- 4
Constructors taking a single argument define a
conversion from the type of its argument to the type of
its class.
Such conversion functions can be used either implicitly
or explicitly.
When used implicitly, at most one implicit built-in
promotion or conversion will be applied to the
argument of the constructor. name(char* = "");
//implicitly convert char* to name
name obj; obj = "sue smith";
User Defined Type Convers.
S202 4- 5
Remember, the lifetime of a temporary object is from
the time it is created until the end of the statement in
which it was created.
In the assignment statement, the temporary object is
destroyed after the temporary is assigned.
As a formal argument (passed by value), the temporary
object is destroyed after the function is called.
Therefore, not only are temporary objects formed (and
the constructor implicitly invoked) but the objects are
also destroyed (and the destructors are implicitly
invoked).
User Defined Type Convers.
S202 4- 6
If we do not want a constructor taking a single
argument to also define an implicit conversion
function, we can prevent that by preceding the
constructor declaration with the keyword explicit.
The application is now required to provide explicit type
casts in order to convert a char* to a name object.
Using a constructor as a conversion function allows us
to convert an object of some other type to an object of a
class. They do not allow us to define a conversion from
a class to some other built-in type or class.
To do so, we must define a type conversion function.
User Defined Type Convers.
S202 4- 7
A conversion function allows us to define a conversion
from an object of a class to another built-in type.
Conversion functions are also useful when we need to
convert from an object of our class to an object of a class
that we do not have access to or do not want to modify.
When we do have access to the class and are willing to
modify it, we can always define a constructor taking a
single argument of our class type to perform the
conversion.
operator other_type();
User Defined Type Convers.
S202 4- 8
Notice that this conversion function has no return type.
That is because the return type is implicitly specified
(other_type), since that is the type we are converting to.
The conversion function converts the current object into
the new type and returns the value.
The conversion function can be called either implicitly
or explicitly.
The name of the conversion function must be a single
identifier; therefore, for types that are not a single
identifier, a typedef must first be used to create a single
identifier.
User Defined Type Convers.
S202 4- 9
Notice that this conversion function has no return type.
That is because the return type is implicitly specified
(other_type), since that is the type we are converting to.
The conversion function converts the current object into
the new type and returns the value.
The name of the conversion function must be a single
identifier; therefore, for types that are not a single
identifier, a typedef must first be used. In the class:
typedef const char* pchar; //make single identifier
operator pchar(); //conversion (name to char*)
Implementing:
name::operator pchar() { //conversion function
...
return array; //of type char * being returned
}
User Defined Type Convers.
S202 4- 10
In order for the conversion function to be called
explicitly when it is a typedef name, that name must be
in the global namespace and cannot be hidden within
the class.
name obj("sue smith");
const char* p;
//Three examples of explicit conversions:
p = (pchar)obj;
p = pchar(obj);
p = static_cast<pchar>(obj);
User Defined Type Convers.
S202 4- 11
With constructors as conversion functions and also
conversion functions, avoid mutual conversions.
Many times type conversions are specified to minimize
the number of operators overloaded, especially for
symmetric operators where there can be multiple
combinations of types for the two operands. It can
reduce the number of versions of an operator to a
single, non-member.
However, this can produce subtle changes in the way a
program works. If we were to add a string to char *
user defined conversion function to our string class, all
uses of operator+ containing both char * and string
types would become ambiguous.
User Defined Type Convers.
S202 4- 12
Type conversions will not help when operators are
members -- because a member is required to have the
first operand as an object of the class; if it converted to
some other type, the member function will not be
found as a possible candidate function and a compile
error will result or the wrong operator will be used
(such as the built-in operators).
Also, when user defined conversions to built-in types
have been specified, the predefined operators may end
up being used even when we use an operator with a
user defined type for which an overloaded operator
exists! Overloaded operators are only invoked if at least
one of those operands is a user defined type.
User Defined Type Convers.

More Related Content

What's hot

Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
Jasleen Kaur (Chandigarh University)
 
Code generation
Code generationCode generation
Code generation
Aparna Nayak
 
Packages in java
Packages in javaPackages in java
Packages in java
Elizabeth alexander
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
Mohammad Ilyas Malik
 
Semantics analysis
Semantics analysisSemantics analysis
Semantics analysis
Bilalzafar22
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
Mir Majid
 
Types of Parser
Types of ParserTypes of Parser
Types of Parser
SomnathMore3
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
kunal kishore
 
Passes of compilers
Passes of compilersPasses of compilers
Passes of compilers
Vairavel C
 
C fundamental
C fundamentalC fundamental
C fundamental
Selvam Edwin
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
Richa Sharma
 
Ports & sockets
Ports  & sockets Ports  & sockets
Ports & sockets
myrajendra
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
Dattatray Gandhmal
 
IPv4 Addressing
 IPv4 Addressing   IPv4 Addressing
IPv4 Addressing
TheGodfather HA
 
Block coding, error detection (Parity checking, Cyclic redundancy checking (C...
Block coding, error detection (Parity checking, Cyclic redundancy checking (C...Block coding, error detection (Parity checking, Cyclic redundancy checking (C...
Block coding, error detection (Parity checking, Cyclic redundancy checking (C...
Paulo_Vangui
 
Translation of expression(copmiler construction)
Translation of expression(copmiler construction)Translation of expression(copmiler construction)
Translation of expression(copmiler construction)
IrtazaAfzal3
 
Classical encryption techniques
Classical encryption techniquesClassical encryption techniques
Classical encryption techniques
Dr.Florence Dayana
 
Automata theory
Automata theoryAutomata theory
Automata theory
Pardeep Vats
 
COMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed TranslationCOMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed Translation
Jyothishmathi Institute of Technology and Science Karimnagar
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
Akshaya Arunan
 

What's hot (20)

Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Code generation
Code generationCode generation
Code generation
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
Semantics analysis
Semantics analysisSemantics analysis
Semantics analysis
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Types of Parser
Types of ParserTypes of Parser
Types of Parser
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
 
Passes of compilers
Passes of compilersPasses of compilers
Passes of compilers
 
C fundamental
C fundamentalC fundamental
C fundamental
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 
Ports & sockets
Ports  & sockets Ports  & sockets
Ports & sockets
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
IPv4 Addressing
 IPv4 Addressing   IPv4 Addressing
IPv4 Addressing
 
Block coding, error detection (Parity checking, Cyclic redundancy checking (C...
Block coding, error detection (Parity checking, Cyclic redundancy checking (C...Block coding, error detection (Parity checking, Cyclic redundancy checking (C...
Block coding, error detection (Parity checking, Cyclic redundancy checking (C...
 
Translation of expression(copmiler construction)
Translation of expression(copmiler construction)Translation of expression(copmiler construction)
Translation of expression(copmiler construction)
 
Classical encryption techniques
Classical encryption techniquesClassical encryption techniques
Classical encryption techniques
 
Automata theory
Automata theoryAutomata theory
Automata theory
 
COMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed TranslationCOMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed Translation
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 

Viewers also liked

Type Casting in C++
Type Casting in C++Type Casting in C++
Type Casting in C++
Sachin Sharma
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
Sudip Singh
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
United International University
 
Type checking
Type checkingType checking
Type checking
rawan_z
 
Type conversion
Type conversionType conversion
Type conversion
Frijo Francis
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversions
Amogh Kalyanshetti
 
operator overloading & type conversion in cpp
operator overloading & type conversion in cppoperator overloading & type conversion in cpp
operator overloading & type conversion in cpp
gourav kottawar
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
Aabha Tiwari
 
Syntaxdirected (1)
Syntaxdirected (1)Syntaxdirected (1)
Syntaxdirected (1)
Royalzig Luxury Furniture
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
Shine Raj
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
gourav kottawar
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
Huawei Technologies
 
Run time storage
Run time storageRun time storage
Run time storage
Rasineni Madhan Mohan Naidu
 
BCD,GRAY and EXCESS 3 codes
BCD,GRAY and EXCESS 3 codesBCD,GRAY and EXCESS 3 codes
BCD,GRAY and EXCESS 3 codes
student
 
Efficient Viterbi algorithms for lexical tree based models
Efficient Viterbi algorithms for lexical tree based modelsEfficient Viterbi algorithms for lexical tree based models
Efficient Viterbi algorithms for lexical tree based models
Francisco Zamora-Martinez
 
Compiler1
Compiler1Compiler1
Compiler1
Natish Kumar
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and Associativity
Aakash Singh
 
Jscript part1
Jscript part1Jscript part1
Jscript part1
Girish Srivastava
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
nTier Custom Solutions
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01
Iffat Anjum
 

Viewers also liked (20)

Type Casting in C++
Type Casting in C++Type Casting in C++
Type Casting in C++
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Type checking
Type checkingType checking
Type checking
 
Type conversion
Type conversionType conversion
Type conversion
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversions
 
operator overloading & type conversion in cpp
operator overloading & type conversion in cppoperator overloading & type conversion in cpp
operator overloading & type conversion in cpp
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
Syntaxdirected (1)
Syntaxdirected (1)Syntaxdirected (1)
Syntaxdirected (1)
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Run time storage
Run time storageRun time storage
Run time storage
 
BCD,GRAY and EXCESS 3 codes
BCD,GRAY and EXCESS 3 codesBCD,GRAY and EXCESS 3 codes
BCD,GRAY and EXCESS 3 codes
 
Efficient Viterbi algorithms for lexical tree based models
Efficient Viterbi algorithms for lexical tree based modelsEfficient Viterbi algorithms for lexical tree based models
Efficient Viterbi algorithms for lexical tree based models
 
Compiler1
Compiler1Compiler1
Compiler1
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and Associativity
 
Jscript part1
Jscript part1Jscript part1
Jscript part1
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01
 

Similar to Type conversions

C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
nirajmandaliya
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
nirajmandaliya
 
Handout # 4 functions + scopes
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes
NUST Stuff
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
zueZ3
 
Function in C++
Function in C++Function in C++
Function in C++
Prof Ansari
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
Krushal Kakadia
 
Javascripts hidden treasures BY - https://geekyants.com/
Javascripts hidden treasures            BY  -  https://geekyants.com/Javascripts hidden treasures            BY  -  https://geekyants.com/
Javascripts hidden treasures BY - https://geekyants.com/
Geekyants
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
miki304759
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
Rhishav Poudyal
 
Functions and Arguments in Python
Functions and Arguments in PythonFunctions and Arguments in Python
Functions and Arguments in Python
Mars Devs
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
AmanuelZewdie4
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Pranali Chaudhari
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANI
Nikul4470
 
Ch-5.pdf
Ch-5.pdfCh-5.pdf
Ch-5.pdf
Ch-5.pdfCh-5.pdf
BASIC CONCEPTS OF C++ CLASS 12
BASIC CONCEPTS OF C++ CLASS 12BASIC CONCEPTS OF C++ CLASS 12
BASIC CONCEPTS OF C++ CLASS 12
Dev Chauhan
 
C functions
C functionsC functions
Javascript functions
Javascript functionsJavascript functions
Javascript functions
Alaref Abushaala
 
Functions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjwFunctions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjw
MayankSinghRawat6
 
C questions
C questionsC questions
C questions
parm112
 

Similar to Type conversions (20)

C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
 
Handout # 4 functions + scopes
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
 
Function in C++
Function in C++Function in C++
Function in C++
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
 
Javascripts hidden treasures BY - https://geekyants.com/
Javascripts hidden treasures            BY  -  https://geekyants.com/Javascripts hidden treasures            BY  -  https://geekyants.com/
Javascripts hidden treasures BY - https://geekyants.com/
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
 
Functions and Arguments in Python
Functions and Arguments in PythonFunctions and Arguments in Python
Functions and Arguments in Python
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANI
 
Ch-5.pdf
Ch-5.pdfCh-5.pdf
Ch-5.pdf
 
Ch-5.pdf
Ch-5.pdfCh-5.pdf
Ch-5.pdf
 
BASIC CONCEPTS OF C++ CLASS 12
BASIC CONCEPTS OF C++ CLASS 12BASIC CONCEPTS OF C++ CLASS 12
BASIC CONCEPTS OF C++ CLASS 12
 
C functions
C functionsC functions
C functions
 
Javascript functions
Javascript functionsJavascript functions
Javascript functions
 
Functions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjwFunctions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjw
 
C questions
C questionsC questions
C questions
 

More from sanya6900

.Net framework
.Net framework.Net framework
.Net framework
sanya6900
 
INTRODUCTION TO IIS
INTRODUCTION TO IISINTRODUCTION TO IIS
INTRODUCTION TO IIS
sanya6900
 
INTRODUCTION TO IIS
INTRODUCTION TO IISINTRODUCTION TO IIS
INTRODUCTION TO IIS
sanya6900
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
sanya6900
 
operators and expressions in c++
 operators and expressions in c++ operators and expressions in c++
operators and expressions in c++
sanya6900
 
Memory allocation
Memory allocationMemory allocation
Memory allocation
sanya6900
 
File handling in_c
File handling in_cFile handling in_c
File handling in_c
sanya6900
 
Ch7
Ch7Ch7
Cpphtp4 ppt 03
Cpphtp4 ppt 03Cpphtp4 ppt 03
Cpphtp4 ppt 03
sanya6900
 
Inheritance (1)
Inheritance (1)Inheritance (1)
Inheritance (1)
sanya6900
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
sanya6900
 
Pointers
PointersPointers
Pointers
sanya6900
 
C cpluplus 2
C cpluplus 2C cpluplus 2
C cpluplus 2
sanya6900
 

More from sanya6900 (13)

.Net framework
.Net framework.Net framework
.Net framework
 
INTRODUCTION TO IIS
INTRODUCTION TO IISINTRODUCTION TO IIS
INTRODUCTION TO IIS
 
INTRODUCTION TO IIS
INTRODUCTION TO IISINTRODUCTION TO IIS
INTRODUCTION TO IIS
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
 
operators and expressions in c++
 operators and expressions in c++ operators and expressions in c++
operators and expressions in c++
 
Memory allocation
Memory allocationMemory allocation
Memory allocation
 
File handling in_c
File handling in_cFile handling in_c
File handling in_c
 
Ch7
Ch7Ch7
Ch7
 
Cpphtp4 ppt 03
Cpphtp4 ppt 03Cpphtp4 ppt 03
Cpphtp4 ppt 03
 
Inheritance (1)
Inheritance (1)Inheritance (1)
Inheritance (1)
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
Pointers
PointersPointers
Pointers
 
C cpluplus 2
C cpluplus 2C cpluplus 2
C cpluplus 2
 

Recently uploaded

Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
An improved modulation technique suitable for a three level flying capacitor ...
An improved modulation technique suitable for a three level flying capacitor ...An improved modulation technique suitable for a three level flying capacitor ...
An improved modulation technique suitable for a three level flying capacitor ...
IJECEIAES
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
LAXMAREDDY22
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
Software Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.pptSoftware Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.ppt
TaghreedAltamimi
 
integral complex analysis chapter 06 .pdf
integral complex analysis chapter 06 .pdfintegral complex analysis chapter 06 .pdf
integral complex analysis chapter 06 .pdf
gaafergoudaay7aga
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
Atif Razi
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
21UME003TUSHARDEB
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
Divyanshu
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
ElakkiaU
 
People as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimalaPeople as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimala
riddhimaagrawal986
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
Prakhyath Rai
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
gowrishankartb2005
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
cnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classicationcnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classication
SakkaravarthiShanmug
 

Recently uploaded (20)

Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
An improved modulation technique suitable for a three level flying capacitor ...
An improved modulation technique suitable for a three level flying capacitor ...An improved modulation technique suitable for a three level flying capacitor ...
An improved modulation technique suitable for a three level flying capacitor ...
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
Software Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.pptSoftware Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.ppt
 
integral complex analysis chapter 06 .pdf
integral complex analysis chapter 06 .pdfintegral complex analysis chapter 06 .pdf
integral complex analysis chapter 06 .pdf
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
 
People as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimalaPeople as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimala
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
cnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classicationcnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classication
 

Type conversions

  • 1. S202 4- 1 Implicit conversions occur when mixed type expressions are evaluated or when the actual arguments in a function call do not match the formal arguments of the function prototype. First, the compiler tries to apply a trivial conversion. If there is no match, then the compiler attempts to apply a promotion. If there is no match, then the compiler attempts to apply a built-in type conversion. If there is no match, then the compiler attempts to apply a user defined type conversion. If there is no match, the compiler generates an error. Implicit Conversions
  • 2. S202 4- 2 To determine if a user defined type conversion is used, the compiler checks if a conversion is defined for the type needed. If there is, then the compiler checks that the type matches the type supplied or that the type supplied can be converted by applying one of the built-in type promotions or conversions. Only one such built-in type promotion or conversion will be applied before applying the user defined type conversion itself. Thus, at most one built-in type conversion and at most one user defined type conversion will ever be implicitly applied when converting from one type to another. Implicit Conversions
  • 3. S202 4- 3 Explicit conversions occur when the client explicitly invokes a cast operation. Both implicit and explicit type conversions result in a temporary object of the type being converted to. This temporary object is used in place of the original. The value of the original object is not affected. When considering execution efficiency, it is important to reduce or minimize the creation of such temporaries. We recommend minimizing user defined conversions as much as possible by avoiding mixed type expressions and by supplying arguments to functions that exactly match the type required by the function prototypes. Explicit Conversions
  • 4. S202 4- 4 Constructors taking a single argument define a conversion from the type of its argument to the type of its class. Such conversion functions can be used either implicitly or explicitly. When used implicitly, at most one implicit built-in promotion or conversion will be applied to the argument of the constructor. name(char* = ""); //implicitly convert char* to name name obj; obj = "sue smith"; User Defined Type Convers.
  • 5. S202 4- 5 Remember, the lifetime of a temporary object is from the time it is created until the end of the statement in which it was created. In the assignment statement, the temporary object is destroyed after the temporary is assigned. As a formal argument (passed by value), the temporary object is destroyed after the function is called. Therefore, not only are temporary objects formed (and the constructor implicitly invoked) but the objects are also destroyed (and the destructors are implicitly invoked). User Defined Type Convers.
  • 6. S202 4- 6 If we do not want a constructor taking a single argument to also define an implicit conversion function, we can prevent that by preceding the constructor declaration with the keyword explicit. The application is now required to provide explicit type casts in order to convert a char* to a name object. Using a constructor as a conversion function allows us to convert an object of some other type to an object of a class. They do not allow us to define a conversion from a class to some other built-in type or class. To do so, we must define a type conversion function. User Defined Type Convers.
  • 7. S202 4- 7 A conversion function allows us to define a conversion from an object of a class to another built-in type. Conversion functions are also useful when we need to convert from an object of our class to an object of a class that we do not have access to or do not want to modify. When we do have access to the class and are willing to modify it, we can always define a constructor taking a single argument of our class type to perform the conversion. operator other_type(); User Defined Type Convers.
  • 8. S202 4- 8 Notice that this conversion function has no return type. That is because the return type is implicitly specified (other_type), since that is the type we are converting to. The conversion function converts the current object into the new type and returns the value. The conversion function can be called either implicitly or explicitly. The name of the conversion function must be a single identifier; therefore, for types that are not a single identifier, a typedef must first be used to create a single identifier. User Defined Type Convers.
  • 9. S202 4- 9 Notice that this conversion function has no return type. That is because the return type is implicitly specified (other_type), since that is the type we are converting to. The conversion function converts the current object into the new type and returns the value. The name of the conversion function must be a single identifier; therefore, for types that are not a single identifier, a typedef must first be used. In the class: typedef const char* pchar; //make single identifier operator pchar(); //conversion (name to char*) Implementing: name::operator pchar() { //conversion function ... return array; //of type char * being returned } User Defined Type Convers.
  • 10. S202 4- 10 In order for the conversion function to be called explicitly when it is a typedef name, that name must be in the global namespace and cannot be hidden within the class. name obj("sue smith"); const char* p; //Three examples of explicit conversions: p = (pchar)obj; p = pchar(obj); p = static_cast<pchar>(obj); User Defined Type Convers.
  • 11. S202 4- 11 With constructors as conversion functions and also conversion functions, avoid mutual conversions. Many times type conversions are specified to minimize the number of operators overloaded, especially for symmetric operators where there can be multiple combinations of types for the two operands. It can reduce the number of versions of an operator to a single, non-member. However, this can produce subtle changes in the way a program works. If we were to add a string to char * user defined conversion function to our string class, all uses of operator+ containing both char * and string types would become ambiguous. User Defined Type Convers.
  • 12. S202 4- 12 Type conversions will not help when operators are members -- because a member is required to have the first operand as an object of the class; if it converted to some other type, the member function will not be found as a possible candidate function and a compile error will result or the wrong operator will be used (such as the built-in operators). Also, when user defined conversions to built-in types have been specified, the predefined operators may end up being used even when we use an operator with a user defined type for which an overloaded operator exists! Overloaded operators are only invoked if at least one of those operands is a user defined type. User Defined Type Convers.