DATA CONVERSION

Views:
 
Category: Entertainment
     
 

Presentation Description

No description available.

Comments

By: ashutoshgadgil1983 (113 month(s) ago)

its good

By: gopal.nanda1981 (121 month(s) ago)

its good

By: viv_dimri (124 month(s) ago)

actually i m nt able to download above slides its says do request for downloading above slides....

By: viv_dimri (124 month(s) ago)

let me download the slide.. its seems quit intresting

Presentation Transcript

DATA CONVERSION : 

Conversions between Basic Data TypesThese conversion routines are built into the compiler & called up whenever required. We called these type of conversions as implicit, because they aren’t appears in listing. DATA CONVERSION 4/12/2010 1 RAJNI BHALLA

Automatic Type Conversion : 

Automatic Type Conversion 4/12/2010 RAJNI BHALLA 2 An assignment operation also causes the automatic type conversion The type of data to the right of an assignment operator is automatically converted to the type of variable on the left. For eg: int m; float x=3.14159; m=x; Convert x to an integer before its value assigned to m. Fractional part is truncated. Some of these conversions may imply a loss of precision, which the compiler can signal with a warning. This can be avoided with an explicit conversion.

Type casting otherwise called as Explicit Conversion : 

Type casting otherwise called as Explicit Conversion 4/12/2010 RAJNI BHALLA 3 type (expression) This approach was adopted since it provided more clarity to the C++ programmers rather than the C-style casting.Say for instance the as per C-style casting (type) firstVariable * secondVariable is not clear but when a programmer uses the C++ style casting it is much more clearer as below type (firstVariable) * secondVariable

Slide 4: 

4/12/2010 RAJNI BHALLA 4 #include <iostream.h>void main(){int a;float b,c;cout<< “Enter the value of a:”;cin>>a;cout<< “n Enter the value of b:”;cin>>b;c = float(a)+b;cout<<”n The value of c is:”<<c;} The output of the above program is Enter the value of a: 10Enter the value of b: 12.5The value of c is: 22.5

What happens when they are user-defined types? : 

What happens when they are user-defined types? 4/12/2010 RAJNI BHALLA 5 v3=v1+v2; // v1,v2 and v3 are class type objects When the objects are of same type the operations of assignment and addition are carried out smoothly. Q . What if one of the operands is an object and other is a built-in type variable? Ans: Compiler does not support automatic type conversion for such data types.We must,therefore,design the conversion routines by ourselves,if such operations are required.

Three types of situations might arise in the data conversion between uncompatible types: : 

Three types of situations might arise in the data conversion between uncompatible types: 4/12/2010 RAJNI BHALLA 6 Conversion from basic to class type. Conversion from class type to basic type. Conversion from one class type to another class type.

Basic To Class Type : 

Basic To Class Type 4/12/2010 RAJNI BHALLA 7 It may be recalled that the use of constructors was illustraed in a number of examples to initialize objects. For eg: class time { int hrs; int mins; public: ………. time (int t) { hrs=t/60; mins=t%60; } }; Converting an int type to class type. time t1; //object t1 created int duration=85; t1=duration; //int to class type Output: 1 25

Slide 8: 

4/12/2010 RAJNI BHALLA 8 Note: constructors used for type conversion always use single argument

Class to Basic Type : 

Class to Basic Type 4/12/2010 RAJNI BHALLA 9 Constructors function do not support this operation. C++ allows us to overload casting operator that could be used to convert a class type to basic type The general form of an overloaded casting operator function usually referred to as conversion function: The operator double() converts a class object to type double operator typename() { ------------ --------------- }

The casting operator should satisfy the following conditions: : 

The casting operator should satisfy the following conditions: 4/12/2010 RAJNI BHALLA 10 It must be a class member. It must not specify a return type. It must not have any arguments. Since it is a member function, it is invoked by the object and therefore, the values used for conversion inside the function belong to the object that invoked the function. This means function does not need an argument.

One Class To Another Class Type : 

One Class To Another Class Type 4/12/2010 RAJNI BHALLA 11 Convert one class type to another class type Example: Objx=Objy; // objects of different types Objx is an object of class x Objy is an object of class y The class y type data is converted to the class z type data and the converted value is assigned to the objx. Since the conversion takes place from class y to class x,y is known as source class and x is known as destination class.

authorStream Live Help