Type conversion RITIKA SHARMA.

Slides:



Advertisements
Similar presentations
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Function Overloading Overloading  Using same name for more than one function  Example: ovldmean.cpp.
Type Conversions. 2 When different types of variables are mixed in an expression, C applies automatic type conversion to the operands The type of data.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Operator Overloading and Type Conversions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
ECE122 Feb. 22, Any question on Vehicle sample code?
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II.
Chapter 7 Functions CS185/09 - Introduction to Programming Caldwell College.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
1 CSC241: Object Oriented Programming Lecture No 11.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 8 Operator Overloading.  Operator overloading is considered one of the more useful techniques for improving readability and ease of programming.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Overloading operators C++ incorporates the option to use standard operators to perform operations with.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Constructors, Copy Constructors, constructor overloading, function overloading Lecture 04.
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Operator Overloading Ritika Sharma.
Programming with ANSI C ++
Chapter 5 Functions for All Subtasks 1
Templates.
Developed By : Ms. K. S. Kotecha
Object-Oriented Programming (OOP) Lecture No. 21
Objectives Define polymorphism Overload functions
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
The dirty secrets of objects
group work #hifiTeam
Polymorphism Lec
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
Conversions of the type of the value of an expression
CSC1201: Programming Language 2
Increment and Decrement
Operator Overloading
Contents Introduction to Constructor Characteristics of Constructor
Andy Wang Object Oriented Programming in C++ COP 3330
Learning Objectives Classes Constructors Principles of OOP
Operator overloading Dr. Bhargavi Goswami
JAVA Constructors.
More About Objects and Methods
CS360 Client/Server Programming Using Java
Class.
The Lifecycle of an Object
Java Programming Language
CSC1201: Programming Language 2
Andy Wang Object Oriented Programming in C++ COP 3330
Constructors & Destructors
Presentation transcript:

Type conversion RITIKA SHARMA

Data Conversion int_var1 = int_ver2; obj3 = obj1+obj2; The assignment between types whether they are basic type or user defined types are handled by the compiler with no effort on our part, provided same data type is used on both sides of the assignment operator. RITIKA SHARMA

Data Conversion But what will happen if the variables on different sides of the = sign are different ???? Compiler doesn’t handle things automatically. We need to tell it what to do…… RITIKA SHARMA

Automatic Type Conversion Student Book Automatic Type Conversion Automatic type conversion by the C++ compiler from the type that doesn’t fit, to the type it wants 4 types of situation might arise in the data conversion: conversion from basic type to basic type Conversion from basic type to class type Conversion from class type to basic type Conversion from class type to class type This is a powerful feature of the C++ language. It requires care, otherwise it can create ambiguity as to which type to convert to. RITIKA SHARMA

basic type to basic type Intvar = floatvar; For ex: int x; float y=2.50; x=y; RITIKA SHARMA

basic type to class type Constructors perform type conversions from the argument’s type to the constructor’s class type RITIKA SHARMA

basic type to class type #include<iostream> using namespace std; class time { int hrs; int mins; public: time(int t) hrs=t/60; mins=t%60; } void display() cout<<hrs<<" hours"; cout<<mins<<" minutes"; }; int main() { int duration=85; time t=duration; t.display(); return 0; } RITIKA SHARMA

class type to basic type constructor function does not support this operation So c++ allow us to define an overloaded casting operator that could be used to convert a class type data to basic type. It is also known as conversion function The casting operator function should satisfy following conditions It must be a class member It must not specify return type It must not have any arguments RITIKA SHARMA

USER DEFINED/CLASS TYPE TO BASIC TYPE It can be accomplished by overloading the typecasr operator. The cast operator function must be defined in that class whose object needs to be converted. The overloded cast operator function is defined by specifying the keyword operator followed by the datatype to which conversion is desired. The syntax is: operator typename(){…} RITIKA SHARMA

Here typename is any of the basic data type. This overloded typecast operator function doestnot have any return type(not even void) Because the returntype is the typename to whih the object is being converted. Moreover it does not take any parameter RITIKA SHARMA

class type to basic type Syntax:- operator typename() { …………//function statements } operator double() { …….. } operator int() ……….. RITIKA SHARMA

class type to basic type #include<iostream> using namespace std; class time { int hrs; int mins; public: time(int h,int m) {hrs=h; mins=m; } void display() { cout<<hrs<<" hours"; cout<<mins<<" minutes"; operator int() return hrs*60+mins; }; int main() { int duration; time t(3,20); t.display(); duration=t; cout<<duration; return 0; } RITIKA SHARMA

class type to class type Using conversion function source class Using constructor function in destination class RITIKA SHARMA

class type to class type In case of conversion between objects constructor function is applied to destination class. objX=objY Destination class Source class RITIKA SHARMA

cout<<"money in rupees"<<rs; class rupee { int rs; public: rupee(dollar d) rs=d.doll*50; } void show() cout<<"money in rupees"<<rs; }; int main() dollar d1(5); d1.show(); rupee r1=d1; r1.show(); return 0; class dollar { public: int doll; dollar(int x) doll=x; } void show() cout<<"Money dollars="<<doll<<endl; }; RITIKA SHARMA

A conversion function is applied to source file RITIKA SHARMA

int main() { dollar d1(5); d1.show(); rupee r1=d1; r1.show(); return 0; } class dollar { public: int doll; dollar(int x) doll=x; } operator rupee() rupee temp; temp.rs=doll*50; return temp; void show() cout<<"Money in dollars="<<doll<<endl; }; #include<iostream> using namespace std; class rupee { public: int rs; void show() cout<<"money in rupees"<<rs; } }; RITIKA SHARMA

Summary Conversion required Conversion take place in Source class Destination class Basic->class NA constructor Class->basic Casting operator Class->class RITIKA SHARMA