html5-img
1 / 66

COS220 Concepts of PLs AUBG, COS dept

COS220 Concepts of PLs AUBG, COS dept. Lecture 24 OOP Data Conversion Reference: R.Sebesta, Chapter 12 Lafore, Chapter 8. Lecture Contents:. Assignment operator and data conversions: Assignment with same data types Conversions between basic types

lanza
Télécharger la présentation

COS220 Concepts of PLs AUBG, COS dept

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COS220 Concepts of PLs AUBG, COS dept Lecture 24 OOP Data Conversion Reference: R.Sebesta, Chapter 12 Lafore, Chapter 8 Assoc. Prof. Stoyan Bonev

  2. Lecture Contents: • Assignment operator and data conversions: • Assignment with same data types • Conversions between basic types • Conversions between objects and basic types • Conversions between objects of different classes • Constructors: • Default Constructor and User defined Constructor • The Copy constructor • The Assignment constructor Assoc. Prof. Stoyan Bonev

  3. From Basic type to User Defined type Float to Distance Meters to feet/inches From User Defined Type to Basic type Distance to float feet/inches to meters Assignment Operator and Data Conversion: • Assignment between the same basic data types • Assignment btw the same user defined data types; • Assignment (data conversion) between basic data types; • Assignment (data conversion) between objects (user defined types) and basic types; Assoc. Prof. Stoyan Bonev

  4. Assignment operator(Assignment statement)

  5. Assignment with same data types • = assigns one variable value to another same data type variable or in general assigns an expression evaluated to a variable int intvar1, intvar2=50, intvar3; intvar1=intvar2; intvar3 = 5 + 6 * 8; • = assigns the value (all data members) of one user-defined object to another provided. Both objects are of the same type. Distance d1, d2(5,10.0f), d3, d4, d5; d1 = d2; d3 = d4 + d5; Assoc. Prof. Stoyan Bonev

  6. Assignment with same data types • Normally, when the value of one object is assigned to another of the same type, the values of all the member data items are simply copied into the new object. No need of special instructions to the compiler. • Thus assignments btw same data types, whether they are basic types, or user-defined types are handled by compiler, provided that the same data type is used on both sides of the equals sign. Assoc. Prof. Stoyan Bonev

  7. Assignment with same data types • basic (fundamental) data types: int v1,v2=35; float v3,v4=8.5f; v1 = v2; v3 = v4; • user defined (abstract) data types: Distance dist1, dist2(10,11.6f); dist1 = dist2; Assoc. Prof. Stoyan Bonev

  8. ConversionsbetweenBasic Data Types

  9. Conversions btw basic types When developers write statements like intvar = floatvar; they assume that the compiler will call a special routine to convert floating point value to fixed point value, so that it can be assigned to intvar. There are many such conversions: int to double, char to float, float to double, char to int. Each such conversion has its own routine, built into compiler and called up when data types of different sides of the = sign so dictate. Assoc. Prof. Stoyan Bonev

  10. Conversions btw basic types • Conversions like that on previous slide are implicit, as they aren’t apparent in listing. • Sometimes the compiler may be forced explicitly to convert one type to another. To do this the cast operator is to be used in two notations • Operator notation (supported in C and C++) • Functional notation (supported in C++ only) • However, explicit conversions use the same built-in routines as the implicit conversions. Assoc. Prof. Stoyan Bonev

  11. Conversions btw basic types Example: int vari; float varf=2.78f; We intend to assign varf to vari. Such conversions may be called in four ways: 1/ implicitly vari = varf; 2a/ explicitly using casting (type) operator vari = (int)varf; 2b/ explicitly using functional notation (C++) vari = int(varf); 2c/ explicitly using static casting operator (C++) vari = static_cast<int>(varf); Assoc. Prof. Stoyan Bonev

  12. 2c example explanations 2c/ explicitly using static casting operator (C++) vari = static_cast<int>(varf); Here the variable to be cast (varf) is placed as argument in parentheses and the type it’s to be changed to (int) is placed in angle brackets. The result is that varf is changed to int before it is assigned to vari. Such a conversion is sometimes called a coercion. The data is coerced into becoming another type. Assoc. Prof. Stoyan Bonev

  13. 2c example explanations Before Standard C++, casts were handled using format like this: vari = (int)varf; vari = int(varf); One problem with these approaches is that they are hard to see and hard to search using text editor vari = static_cast<int>(varf); The new format is easy to see and search. The old casts still work, but their use is discouraged. Assoc. Prof. Stoyan Bonev

  14. ConversionsbetweenObjects and Basic Types

  15. Conversions btw Objects and Basic Types • When we want to convert between user-defined data types and basic types, we can’t rely on built-in routines, since the compiler doesn’t know anything about the user-defined data type besides what we tell it. • Instead, developer must write routines himself. Assoc. Prof. Stoyan Bonev

  16. ConversionfromBasic TypetoUser Defined Type

  17. Conversions btw Objects and Basic Types From Basic type > to > User defined Type float > to > Distance meters > to > feet + inches Distance d1(2.35f), d2 = 4.15f; Solution: ??? (see next slide!) Assoc. Prof. Stoyan Bonev

  18. Conversions btw Objects and Basic Types From Basic type > to > User defined Type float > to > Distance meters > to > feet + inches Distance d1(2.35f), d2 = 4.15f; Solution: 1-arg constructor Assoc. Prof. Stoyan Bonev

  19. Conversions btw Objects and Basic Types From Basic type > to > User defined Type float > to > Distance meters > to > feet + inches Distance d3, d4, d5, d6; float m1 = 10.05f; -------------------------------------- d3 = 8.16f; d5.operator=(8.16f); -------------------------------------- d4 = m1; d6.operator=(m1); -------------------------------------- Solution: ??? (read carefully or open next slide!) Assoc. Prof. Stoyan Bonev

  20. Conversions btw Objects and Basic Types From Basic type > to > User defined Type float > to > Distance meters > to > feet + inches Distance d3, d4, d5, d6; float m1 = 10.05f; -------------------------------------- d3 = 8.16f; d5.operator=(8.16f); -------------------------------------- d4 = m1; d6.operator=(m1); -------------------------------------- Solution: overloaded = operator Assoc. Prof. Stoyan Bonev

  21. Conversions btw Objects and Basic Types From Basic type > to > User defined Type float > to > Distance meters > to > feet + inches Distance d1(2.35f), d2 = 4.15f, d3, d4, d5, d6; float m1 = 10.05f; d3 = 8.16f; d4 = m1; d5.operator=(8.16f); d6.operator=(m1); Solution: 1-arg constructor and/or overloaded = operator Assoc. Prof. Stoyan Bonev

  22. From Basic type to User defined Type Solution: 1-arg constructor Distance (float meters) { float fltfeet = meters * MTF; feet = int(fltfeet); inches = 12 *(fltfeet - feet); } This constructor is called when a Distance object is created with a single argument, assumed to present meters. It converts the argument to feet/inches and assigns the resulting values to the object. Conversion is carried out along with the creation of an object, like in the following statements Distance dist1(3.45f), dist2=2.35f; Assoc. Prof. Stoyan Bonev

  23. From Basic type to User defined Type Solution: overloaded = operator Here is one more way to perform same conversion dist1 = 1.0f; dist2.operator=(1.0f); We are converting float to Distance The compiler looks for a tool that might do the job. It finds an overloaded = operator that converts float to Distance. void operator=(float meters) { float fltfeet = meters * MTF; feet = int(fltfeet); inches = 12 *(fltfeet - feet); } Assoc. Prof. Stoyan Bonev

  24. From Basic type to User defined Type Solution: 1-arg constructor Here is one more way to perform same conversion dist1 = 1.0f; We are converting float to Distance without explicitly creating a new Distance object. The compiler looks for a tool that might do the job. If it doesn’t find an overloaded = operator, the compiler looks for and finds a constructor that converts float to Distance, so it applies it to the assignment situation, creating an anonymous unnamed temporary Distance object with feet/inches value corresponding to 1.0f meters. These feet/inches values are copied to dist1 feet/inches. Thus the compiler blurs the distinction between definition and assignment. If no overloaded = operator is available, the compiler looks for a constructor to do the same job. Assoc. Prof. Stoyan Bonev

  25. Conversionfrom User Defined Type to Basic Type

  26. Conversions btw Objects and Basic Types From User defined Type > to > Basic type Distance > to > float feet + inches > to > meters Distance d1(6,8.3f); float m1, m2, m3, m4; m1 = d1; // implicit conversion m2 = (float)d1; // explicit conversion m3 = float(d1); // explicit conversion m4 = static_cast<float>(d1); Solution: ???(read carefully or open next slide) Assoc. Prof. Stoyan Bonev

  27. Conversions btw Objects and Basic Types From User defined Type > to > Basic type Distance > to > float feet + inches > to > meters Distance d1(6, 8.3f); float m1, m2, m3, m4; m1 = d1; // implicit conversion m2 = (float)d1; // explicit conversion m3 = float(d1); // explicit conversion m4 = static_cast<float>(d1); Solution: Conversion function Assoc. Prof. Stoyan Bonev

  28. Static casts • Static casting is sometimes called coercion. The data is coerced into becoming another type. • Example: An integer variable intvar can cast to double type before processing static_cast<double>intvar Assoc. Prof. Stoyan Bonev

  29. Static casts Distance d1(6, 8.3); float m1, m2, m3, m4; m1 = d1; // implicit conversion m2 = (float)d1; // explicit conversion m3 = float(d1); // explicit conversion m4 = static_cast<float>(d1); One problem with first three approaches is they are hard to see and hard to search using Find cmd of Text Editor The 4th format solves this problem: static_cast is easy to see and easy to search for. The old casts still work, but their use is discouraged (or deprecated, in technical term) Assoc. Prof. Stoyan Bonev

  30. From User defined Type to Basic type Solution: Conversion function operator float() // conversion function { float fracfeet = inches/12; fracfeet += float(feet); return fracfeet / MTF; } The operator overloads unary cast (type) operator, so it has no parameter. (The rule from Operator overloading lecture) The operator takes the value (feet/inches) of the Distance object of which it is a member, converts this value to a float value representing meters, and returns it. Assoc. Prof. Stoyan Bonev

  31. From User defined Type to Basic type Solution: Conversion function This operator can be called explicitly in two ways: mtrs = float(dist2); mtrs = (float)dist2; This converts the Distance object dist2 to its equivalent float value in meters. Again the compiler uses the same conversion in assignment stmts mtrs = dist2; This has exactly the same effect. When the compiler finds that you want to convert a user-defined type to a basic type, the compiler starts by looking for an overloaded = operator. But when it doesn’t find one, it finds the conversion function and uses it instead. Assoc. Prof. Stoyan Bonev

  32. Example based on the Distance class • Demo program oop4aDistance.cpp Assoc. Prof. Stoyan Bonev

  33. ConversionsbetweenObjects of Different Classes

  34. Conversionsbetween Objects of Different Classes • How to convert data btw objects of different user-defined classes? • The same two methods just shown for conversion between basic types and user-defined types also apply to conversions between two user-defined types • One-argument constructor • Conversion function • The choice depends on whether you want to put the conversion routine in the class definition of the source object (sender or r-value) or in the class definition of the destination object (receiver or l-value) Assoc. Prof. Stoyan Bonev

  35. Conversionsbetween Objects of Different Classes class A {...}; class B {...}; A obja; B objb; <destination> = <source>; obja = objb; Assoc. Prof. Stoyan Bonev

  36. Alternate Presentations of 2-D Coordinate System:RECTANGLE & POLAR

  37. Conversionsbetween Objects of Different Classes class Rect { . . . }; Rect rec1, rec2(3.0, 4.0); class Polar { . . }; Polar pol1(10.0, 0.7858), pol2; // assignment and data conversion rec1 = pol1; pol2 = rec2; Assoc. Prof. Stoyan Bonev

  38. Practical example • From Polar to Rectangle coordinates • class Polar { . . . }; // source class(object) • class Rect { . . . }; // destination class(object) Polar pol(10.0, 0.7858); Rect rec; <dest> = <source>; rec = pol; Assoc. Prof. Stoyan Bonev

  39. Conversion btw Objects of Different Classes: Routine in Source class(conversion function) Rect rec; Polar pol1(10.0, 0.7858); // angle 45 degrees = 3.14/4 rec = pol1; // Polar to Rect implicit conversion Polar pol2(10.0, 3.141592/6.); // angle 30 degrees rec = Rect(pol2); // Polar to Rect explicit conversion Polar pol3(10.0, 3.141592/3.); // angle 60 degrees rec = (Rect)pol3; // Polar to Rect explicit conversion Assoc. Prof. Stoyan Bonev

  40. Conversion btw Objects of Different Classes: Routine in Source class(conversion function) class Rect { private: double xco, yco;// destination class public: Rect() {xco = yco = 0.0; } Rect(double x, double y) {xco = x; yco = y; } void ShowData() {. . .} }; //==================================- class Polar{private: double radius, angle; // source class public: Polar() {radius = angle = 0.0; } Polar(double r, double a) {radius = r; angle = a;} void ShowData(){ . . .} operator Rect(){ double x = radius * cos(angle); double y = radius * sin(angle); return Rect(x,y); } }; Assoc. Prof. Stoyan Bonev

  41. Conversion btw Objects of Different Classes: Routine in Source class(conversion function) Demo: Source text: OOP4cPolarToRect1.cpp Assoc. Prof. Stoyan Bonev

  42. Conversion btw Objects of Different Classes: Routine in Dest class(1-arg constructor or overloaded = operator) Rect rec; Polar pol1(10.0, 0.785398); // angle 45 degrees = 3.14/4 rec = pol1; // Polar to Rect conversion in assignment Polar pol2(10.0, 3.141592/6.); // angle 30 degrees Rect rec2(pol2); // Polar>Rect conversion when create object Polar pol3(10.0, 3.141592/3.); // angle 60 degrees Rect rec3=pol3; // Polar>Rect conversion when create object Assoc. Prof. Stoyan Bonev

  43. Conversion btw Objects of Different Classes: Routine in Dest class(1-arg constructor or overloaded = operator) class Rect{private: double xco, yco; // destination class public: Rect() { xco = yco = 0.0; } Rect(double x, double y) {xco = x; yco = y; } Rect(Polar p) { double rad = p.getrad(); double ang = p.getang(); xco = rad * cos(ang); yco = rad * sin(ang); } void ShowData() { . . .} }; //========================================= class Polar{private: double radius, angle; // source class public: Polar() { radius = angle = 0.0; } Polar(double r, double a) { radius = r; angle = a; } void ShowData(){ . . .} double getrad() { return radius; } double getang() { return angle; } }; Assoc. Prof. Stoyan Bonev

  44. Conversion btw Objects of Different Classes: Routine in Dest class(1-arg constructor or overloaded = operator) Demo: Source text: OOP4dPolarToRect2.cpp Assoc. Prof. Stoyan Bonev

  45. Data Types Conversions Routine in Destination Routine in Source from Basic type to Basic type Built-in conversion functions from Basic to Class Constructor NA from Class to Basic NA Conversion function from Class to Class Constructor Conversion function Assignment (data conversion) btw objects of different classes Assoc. Prof. Stoyan Bonev

  46. Two Kinds of Time12-Hour Time and 24-Hour Time

  47. Illustration 12-Hour Time 24-Hour Time 12:00 a.m. (midnight) 00:00 12:01 a.m. 00:01 6:00 a.m. 06:00 11:59 a.m. 11:59 12:00 p.m. (noon) 12:00 12:01 p.m. 12:01 6:00 p.m. 18.00 11:59 p.m. 23:59 Assoc. Prof. Stoyan Bonev

  48. Conversions: when to use what • In case you have an access to both classes, the choice depends on you • Sometimes the choice is made for you • If you purchase a library of classes, perhaps you have no access to the source code Assoc. Prof. Stoyan Bonev

  49. Conclusion on Conversion • Avoid ambiguity • Suppose you use both a one-argument constructor and a conversion function to perform the same transformation (for example, Polar to Rect) • How will the compiler know which conversion to use? It won’t know and will signal error. • So, avoid doing same conversion in more than one way Assoc. Prof. Stoyan Bonev

  50. More on OOP Data Conversion Data Conversion in VBasic Data Conversion in C# Data Conversion in Java 50

More Related