Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 31. Operator Overloading.
Advertisements

Operator Overloading Fundamentals
Operator Overloading. Basics Define operations on user-defined data types –Perform same operation as it is supposed to but, with operands of the user-defined.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
Chapter 14: Overloading and Templates
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
Chapter 13: Overloading.
Chapter 15: Operator Overloading
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator Overloading 1. Introduction Let’s define a class for Complex numbers: class Complex { private: double real, image; public: Complex () : real(0.0),
Operator overloading Object Oriented Programming.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
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.
Operator Overloading. 2 C++ has the ability to assign special meaning to an operator, called operator overloading one operator can be used to carry out.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Operator Overloading and Type Conversions
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler.
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Operator overloading and type convesions BCAS,Bapatla B.mohini devi.
 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.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Dynamic object creation
OPERATOR OVERLOADING Understand Operator Overloading and how it is implemented in C++ ? | Website for students | VTU NOTES.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
Operator Overloading. Introduction Computer is calculating machine. It calculates the data provided to it. It performs the various operations on data.
Operator Overloading. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
CONSTRUCTOR AND DESTRUCTORS
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter -6 Polymorphism
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
Friend Function. 2 Any data which is declared private inside a class is not accessible from outside the class. A non-member function cannot have an access.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
CSCI-383 Object-Oriented Programming & Design Lecture 11.
1 CSC241: Object Oriented Programming Lecture No 08.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Learners Support Publications Constructors and Destructors.
 Binary operators  Unary operators  Conversion Operators  Proxy Classes (simulating a reference) ▪ bitset example  Special operators  Indexing 
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Operator Overloading CS 3370 – C++ Chapter 14.
Constructors and Destructors
Type conversion RITIKA SHARMA.
Operator Overloading Ritika Sharma.
Chapter 13: Overloading and Templates
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Operator Overloading.
Developed By : Ms. K. S. Kotecha
Object-Oriented Programming (OOP) Lecture No. 21
Constructor & Destructor
Chapter 15: Overloading and Templates
Operator Overloading BCA Sem III K.I.R.A.S.
Conversions of the type of the value of an expression
Operator Overloading
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
Operator Overloading.
POLYMORPHISM ( in C++) POLYMORPHISM ( in C++). Presentation Outline Polymorphism Definition Types – Compile time and Run time polymorphism Function overloading.
COP 3330 Object-oriented Programming in C++
INTERNATIONAL INSTITUTE OF IFORMATION TECHNOLOGY, (I²IT)
Presentation transcript:

Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

Defining Operator Overloading Defining Additional task to an operator. General form return_type class_name::operator op(arglist) { function body } Op is the operator being overloaded. Op is preceded by the keyword operator.

Operator functions must be either member functions or friend functions. Differences between using friend function and member function. –Friend function will have only one argument for unary operator and two argument for binary operators. –Member function will have no arguments for unary operators and only one argument for binary operators. The object used to invoke the member function is passed implicitly to the function and this is not the case with friend function.

Overloading unary operator A minus operator when used as a unary, takes just one operator. Operation is discussed with the help of example.

Overloading unary minus #include class space { int x; int y; int z; public: void get_data(int a, int b,int c); void display(void); void operator-();//overloading unary minus };

Defining all member functions void space::get_data(int a,int b,int c) { x=a; y=b; z=c; } void space::display(void) { cout<<x<<“ ”<<y<<“ ”<<z<<“\n”; } void space:: operator-() { x=-x; y=-y; z=-z;}

Defining main() function int main() { space s; s.getdata(10,-20,30); cout<<“S:=”; s.display(); -s;//activates operator-() function cout<<“S:=“; s.display(); return 0; }

Output S: = S: =

Using friend function same operation can be done using friend function even as shown friend void operator-(space &s); // declaration void operator-(space &s)//definition { s.x=-s.x; s.y=-s.y; s.z=-s.z; }

Overloading binary operators Binary operator can be overloaded in similar fashion as the unary operator. illustrated with an example.

Class Definition #include class complex { float x; float y; public: complex(){} complex(float real, float imag) { x=real; y= imag;} complex operator+(complex); void display(void); };

Member functions complex complex::operator +(complex c) { complex temp; temp.x=x+c.x; temp.y=y+c.y; return (temp); } void complex:: display(void) { cout<<x<<“+j”<<y<<“\n”; }

Main() function int main() { complex c1,c2,c3; c1=complex(2.5,3.5); //invokes constructor 1 c2=complex(1.6,2.7); // invokes constructor 2 c3=c1+c2; cout<<“c1 =“; c1.display(); cout<<“c2=“; c2.display(); cout<<“c3=“;c3.display(); return 0; }

Output of the program c1=2.5+j3.5 c2=1.6+j2.7 c2=4.1+j6.2

Using friend function Declaration: friend complex operator+(complex c1, complex c2); Definition: complex operator +(complex c1, complex c2) { complex temp; temp.x=c1.x+c2.x; temp.y=c1.y+c2.y; return (temp); }

Rules for overloading operators Only existing operator can be overloaded New operators cannot be created. Overloaded operator should have atleast one operand of user defined data type. We cannot change the basic meaning of the operator. overloaded operators follow the syntax rules of the original operators.

Operators that cannot be overloaded Sizeof size of operator. Membership operator.*pointer to member operator ::scope resolution operator ?:conditional operator

Type Conversions Three types of situations might arise in the data conversion between incompatible types: 1.Conversion from basic type to class type. 2.Conversion from class type to basic type 3.Conversion from one class type to another class type.

Basic to Class Type class time { int hrs,mins; public: time(int t) {hrs=t/60; mins=t%60; } };

If there are statements like as shown below Time t1; //object t1 is created int duration=85; t1=duration; // converting int to class type –Constructor converts duration from int to class type and then assigns the time type to the values of the object time –After conversion, the hrs member of t1 will contain a value of 1 and mins member a value of 25, denoting 1 hrs & 25 mins.

Class to Basic Type C++ allow us to define overloaded casting operator that is used to convert a class type data to a basic data type. General form: operator typename() { function statements }

Eg for overloaded casting operator vector :: operator double() { double sum=0; for(int i=0; i<size; i++) sum=sum + v[i] * v[i]; return sqrt(sum); } The function converts a vector to the corresponding scalar magnitude. The magnitude of a vector is given by square root of the sum of the squares of its components. double length=double(v1); ----or----- double length=v1; // V1 is an object of type vector

Casting operator function Casting operator function should satisfy the following conditions: –It must be a class member. –It must not specify a return type. –It must not have any arguments.

One class to another class Objects of different classes can be carried out by either: –Constructor –Conversion function Eg: X obj_x; Y obj_y; Obj_x= obj_y; Casting operator function converts the class object of which it is a member to typename. (Typename may be builtin type or any user defined one(class)). Conversion takes place in the source class and the result is given to the destination class object.

class Y { public: operator X(); }; Y::operator X() { }

Using constructor function Constructor function to be placed in destination class. class X { public: X(Y obj_y) { } };

End_Of_unit_7