Chapter 9 Type Conversions

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Operator overloading redefine the operations of operators
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - Operator Overloading Outline 8.1 Introduction 8.2 Fundamentals of Operator Overloading 8.3.
1 Chapter 11 Introducing the Class Pages ( )
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
EC-241 Object-Oriented Programming
Operator Overloading Fundamentals
C++ Classes & Data Abstraction
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
Chapter Objectives You should be able to describe: Assignment Pointers as Class Members Additional Class Features Common Programming Errors.
Classes & Objects classes member data and member function access control and data hiding instantiation of objects class constructor and destructor objects.
Classes. COMP104 Lecture 25 / Slide 2 Motivation  Types such as int, double, and char are simple objects. * They can only answer one question: “What.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS 1400 Chapter 5, section 2. Loops! while (rel-expression)while (rel-expression) {statements statement } if the relational-expression is true, execute.
CS Jan 2007 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example:
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Definition Class In C++, the class is the construct primarily used to create objects.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
Chapter 12: Adding Functionality to Your Classes.
Data Structures and Algorithms -- Chapter 3 Abstract Data Types Mohamed Mustaq.
March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 1 Structure Pointer Operator For accessing members in structures and classes we have.
Chapter 10 Introduction to Classes
FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message **** ** Welcome.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
1 Chapter 8 Operator Overloading. 2 User-defined Meaning for “built-in” Operators zNatural, suggestive usage yComplex number addition, subtraction, etc.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
11 Introduction to Object Oriented Programming (Continued) Cats.
CSC241 Object-Oriented Programming (OOP) Lecture No. 7.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
1 2/21/05CS250 Introduction to Computer Science II Destructors, Get and Set, and Default Memberwise Assignment.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Individual Testing, Big-O, C++ Bryce Boe 2013/07/16 CS24, Summer 2013 C.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
CS410 – Software Engineering Lecture #12: C++ Basics V
Access Functions and Friend Functions
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Polymorphism.
Operator Overloading; String and Array Objects
Chapter 5 Function Basics
Starting Out with C++: From Control Structures through Objects
Learning Objectives Classes Constructors Principles of OOP
Constructors Member Functions Inheritance
Operators.
Implementing Classes Chapter 3.
How Classes Work with Memory Diagram
Object Oriented Programming (OOP) Lecture No. 13
Classes.
Lecture 16 Oct 30, 02.
CS148 Introduction to Programming II
Java Programming Language
CS 201(Introduction To Programming)
How Dynamic Memory Works with Memory Diagram
How Classes Work with Memory Diagram
Objects as Function Arguments
Chapter 11 Classes.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Chapter 9 Type Conversions Examples adapted from section 13.3 of A First Book of C++, 2nd Edition, by Gary J Bronson, Brooks/Cole 2000.

Type Conversion Need the ability to convert: Already can create user-defined types Already have default convert capabilities among built-in types. Need the ability to convert: built-in type to user-defined type user-defined type to built-in type user-defined type to user-defined type

Built-in to User-defined The conversion of a built-in type to a user-defined type can be accomplished by the use of an appropriate constructor for the targeted user-defined type. This makes the conversion as simple as an explicit cast of one built-in type to another built-in type.

A Date Class class Date { private: int Month, Day, Year; public: Date(int M, int D, int Y); Date(int yyyymmdd); // conversion constructor void ShowDate(); // display function }; Converts an int value into a Date object.

Date Class Implementation Date::Date() { Month = 7; Day = 4; Year = 2001; } Date::Date(int M, int D, int Y) { Month = M; Day = D; Year = Y; void Date::ShowDate() { cout << setfill('0') << setw(2) << Month << '/' << setw(2) << Day << '/' << setw(2) << Year;

int to Date conversion Date::Date(int yyyymmdd) { Year = yyyymmdd / 10000; Month = (yyyymmdd - Year * 10000) / 100; Day = yyyymmdd - Year * 10000 - Month * 100; }

Using the conversion void main() { Date a; cout << "Date a is:" << endl; a.ShowDate(); cout << endl; a = Date(20020101); cout << "Date a is now: " << endl; cout << endl << endl; } Conversion of int value into a Date object. Looks like standard explicit cast. Date a is: 07/04/2001 Date a is now: 01/01/2002 Output

User-defined to Built-in The conversion of a user-defined type to a built-in type can be accomplished by the use of an appropriate conversion operator function as a member of the user-defined type. This also makes the conversion as simple as an explicit cast of one built-in type to another built-in type.

Revised Date Class class Date { private: int Month, Day, Year; public: Date(int M, int D, int Y); operator int(); void ShowDate(); }; Converts a Date object into an int.

Date to int conversion Date::operator int() { int yyyymmdd; yyyymmdd = Year * 10000 + Month * 100 + Day; return yyyymmdd; }

Using the conversion void main() { Date a(4, 1, 1999); int b; b = (Date) a; cout << "a's date is: "; a.ShowDate(); cout << endl << "This date, as an int, is: " << b << endl;; } Conversion of Date object into an int value. Looks like standard explicit cast. Output a's date is: 04/01/1999 This date, as an int, is: 19990401

User-defined to User-defined The conversion of a user-defined type to a user-defined type is also accomplished by the use of a member conversion operator function. This makes the conversion as simple as an explicit cast of one built-in type to another built-in type.

Add an IntDate Class // Dates.h class IntDate; // forward declaration private: int Month, Day, Year; public: Date(int M = 7, int D = 4, int Y = 2001); operator IntDate(); // conversion operator void ShowDate(); }; // continues. . . Converts a Date object into an IntDate object.

Add an IntDate Class // . . . class IntDate { private: int yyyymmdd; public: IntDate(int ymd = 0); operator Date(); // conversion operator void ShowIntDate(); }; Converts an IntDate object into a Date object.

Date to IntDate conversion Date::operator IntDate() { int Temp; Temp = 10000 * Year + 100*Month + Day; return IntDate(Temp); } Assumes IntDate has an appropriate constructor.

Date to IntDate conversion Date::operator IntDate() { int Temp; Temp = 10000 * Year + 100*Month + Day; return IntDate(Temp); } Assumes IntDate has an appropriate constructor.

IntDate Class Implementation IntDate::IntDate(int ymd) { yyyymmdd = ymd; } void IntDate::ShowIntDate() { cout << yyyymmdd;

IntDate to Date conversion IntDate::operator Date() { int M, D, Y; Y = yyyymmdd / 10000; M = (yyyymmdd - Y*10000) / 100; D = yyyymmdd - Y*10000 - M*100; return Date(M, D, Y); } Assumes IntDate has an appropriate constructor.

Using the conversion void main() { Date a(4, 1, 1999), b; IntDate c(20011215), d; b = Date(c); d = IntDate(a); cout << "a's date is: "; a.ShowDate(); cout << endl << "as an IntDate object this date is: "; d.ShowIntDate(); // continues . . . Conversions of IntDate object into a Date object and Date object into an IntDate object. Looks like standard explicit cast.

Using the conversion // . . . continued cout << endl << "c's date is: "; c.ShowIntDate(); cout << endl << "as a Date object this date is: "; b.ShowDate(); cout << endl << endl; } a's date is: 04/01/1999 as an IntDate object this date is: 19990401 c's date is: 20011215 as a Date object this date is: 12/15/2001 Output