Type Conversions Lecture 13 Wed, Feb 21, 2007.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

For(int i = 1; i
12-8 Mixed Expressions and Complex Fractions Objective Students will be able to simplify complex fractions.
Operator Overloading Fundamentals
C++ Features and Constructs Ch. 3 except 3.2, 3.4, 3.9, 3.11.
Abstract Data Types Development and Implementation JPC and JWD © 2002 McGraw-Hill, Inc.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Abstract Data Types Development and Implementation.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
CS-240 Operator Overloading Dick Steflik. Operator Overloading What is it? –assigning a new meaning to a specific operator when used in the context of.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 14. User Defined Classes.
Decimals and Fractions
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R1. ADTs as Classes.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Case Study - Fractions Timothy Budd Oregon State University.
Rational numbers. Whole numbers Whole numbers Rational numbers Whole numbers Natural numbers Integers / ¾ 18% A rational number.
Introduction to Classes in C++
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CH1, C++ INTERLUDE 1.
Operator Overloading & Exception Handling TCP1201 OOPDS 1 Lecture 5 1.
12-6 Rational Expressions with Like Denominators Objective: Students will be able to add and subtract rational expressions with like denominators.
 Multiply rational expressions.  Use the same properties to multiply and divide rational expressions as you would with numerical fractions.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II.
Add & Subtract Rationals – Common Denominator To add or subtract rational expressions with common denominators: 1) Add or subtract the numerators 2) Write.
Conversion of Fractions - Mixed Number to Improper Fraction
Ch 8: Exponents D) Rational Exponents
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO , APP D, C++ BOOK.
Rational Exponents Rules Examples Practice Problems.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
To simplify a rational expression, divide the numerator and the denominator by a common factor. You are done when you can no longer divide them by a common.
Object-Oriented Programming (OOP) Lecture No. 16
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value A copy of the argument’s value.
CSCE 210 Data Structures and Algorithms
Adding and Subtracting Rational Expressions
Friend functions, operator overloading
Programming with ANSI C ++
Operator overloading Conversions friend inline
Operator overloading Conversions friend inline
Developed By : Ms. K. S. Kotecha
Object-Oriented Programming (OOP) Lecture No. 21
Lecture 16: Introduction to Data Types
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Operator overloading Conversions friend inline
Object-Oriented Programming (OOP) Lecture No. 16
Type Conversions Lecture 14 Wed, Feb 22, 2006.
Conversions of the type of the value of an expression
Unit-2 Objects and Classes
Advanced Program Design with C++
Multiplying and Dividing Rational Expressions
Simplify Complex Rational Expressions
Operator overloading Dr. Bhargavi Goswami
Simplifying Complex Rational Expressions
5.7 Rational Exponents 1 Rules 2 Examples 3 Practice Problems.
Default Arguments.
Complex Rational Expressions
The Destructor and the Assignment Operator
Type Conversions Lecture 14 Wed, Feb 18, 2004.
COMS 261 Computer Science I
7.4 Adding and Subtracting Rational Expressions
COP 3330 Object-oriented Programming in C++
Java Programming Language
Development and Implementation
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
The Constructors Lecture 7 Fri, Feb 2, 2007.
Class rational part2.
Classes Lecture 4 Secs 2.3, 4.1 – 4.3 Fri, Jan 26, 2007.
Unit-2 Objects and Classes
Presentation transcript:

Type Conversions Lecture 13 Wed, Feb 21, 2007

Conversion of Types Frequently in a program an object must be converted from one type to another. For the built-in types, this is done automatically whenever it is sensible. Convert float to int. Convert int to float. How can it be done with created types? 11/9/2018 Type Conversion

Converting to a Created Type A class uses its constructors to define rules for converting an object of another type to an object of that type. The prototype is Class-name::Class-name(other type); Rational::Rational(int); Complex::Complex(double); 11/9/2018 Type Conversion

Example: Convert int to Rational // Rational constructor Rational::Rational(int n) { numerator = n; denominator = 1; return; } // Usages Rational r(100); Rational r = 100; r = (Rational)100; r = Rational(100); 11/9/2018 Type Conversion

Example How would you convert a Point to a Vector? vectr.h VectrTest.cpp point.h point.cpp 11/9/2018 Type Conversion

Converting to a Built-in Type Sometimes we want to convert an object of a created type to an object of a built-in type. For example, we might want to convert A Rational to a float. A Complex to a double. For this we need a conversion operator. 11/9/2018 Type Conversion

Conversion Operators A conversion operator has prototype The operator converts the created-type object to the built-in type and returns the object of the built-in type. Class-name::operator built-in-type() const; Rational::operator float() const; Complex::operator double() const; 11/9/2018 Type Conversion

Example: Convert Rational to float Rational::operator float() const { return (float)numerator/denominator; } 11/9/2018 Type Conversion

Convert a Rational to a Float rational.h rational.cpp RationalTest.cpp 11/9/2018 Type Conversion