Operator overloading and type convesions BCAS,Bapatla B.mohini devi.

Slides:



Advertisements
Similar presentations
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Advertisements

Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
1 Operator Overloading. 2 Syntax The general syntax is: [friend] returntype operator ( ) { ; }
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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 Object Oriented Programming.
Operator Overloading in C++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Operator Overloading and Type Conversions
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
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.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
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.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
 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.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Operator Overloading. Introduction Computer is calculating machine. It calculates the data provided to it. It performs the various operations on data.
CONSTRUCTOR AND DESTRUCTORS
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
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.
CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
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.
Constructor It is a special member of a class that has the following characteristic 1)It has the same name as of its class. 2)It don’t have an explicit.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
 Binary operators  Unary operators  Conversion Operators  Proxy Classes (simulating a reference) ▪ bitset example  Special operators  Indexing 
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Programming with ANSI C ++
Constructors and Destructors
Operator Overloading Ritika Sharma.
Chapter 13: Overloading and Templates
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Programming with ANSI C ++
Developed By : Ms. K. S. Kotecha
Constructor & Destructor
Chapter 15: Overloading and Templates
Operator Overloading BCA Sem III K.I.R.A.S.
This pointer, Dynamic memory allocation, Constructors and Destructor
Conversions of the type of the value of an expression
Operator Overloading
Operators Lecture 10 Fri, Feb 8, 2008.
Contents Introduction to Constructor Characteristics of Constructor
Constructors and destructors
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
INTERNATIONAL INSTITUTE OF IFORMATION TECHNOLOGY, (I²IT)
Operator Overloading I
Chapter 6 Polymorphism.
SPL – PS3 C++ Classes.
Presentation transcript:

operator overloading and type convesions BCAS,Bapatla B.mohini devi

 pointer,vitual functions&Polymorphism

Intnroduction  C++ has ability to provide the operators with a special meaning for a data types.  We can overload (give additional meaning to) all the C++ operators except:  It is one of the many exciting features of C++. –Class member access operators (. &.*) –Scope resolution operators ( : : ) –Size operator (sizeof) –Conditional operators (? : )  When an operator is overloaded, its original meaning is not lost.

Defining Operator Overloading  To define an additional task to an operator, we must specify what it means in relation to the class to which the operator is applied.  This is done with the help of a special function called operator function. return type class-name : :operator op (arg-list) { Function body // task defined Function body // task defined}

return type class-name : :operator op (arg-list) { Function body // task defined Function body // task defined}  return type is the type of value returned by the specified operation.  op is the operator being overloaded.  op is preceded by the keyword operator.  operator op is the function name.

Operator Function must be either –member function (non-static) Or –friend function. The basic difference :  A friend function will have only one argument for unary operators and two for binary operators.  A member function has no arguments for unary operators and one argument for binary operators.  This is because the object used to invoke the member function is passed implicitly and therefore is available for the member function.  Arguments may be passed either by value or by reference.

The process of overloading involves the following steps: –Create a class that defines the data type that is to be used in the overloading operation. –Declare the operator function operator op( ) in the public part of the class. It may be either a member function or a friend function. –Define the operator function to implement the required operations.

Overloading Unary Operators Consider a unary minus operator: –It takes just one operand. –It changes the sign of an operand when applied to a basic data item. –The unary minus when applied to an object should change the sign of each of its data items.

Overloading Binary Operators As a rule, in overloading binary operators, –the left-hand operand is used to invoke the operator function and –the right-hand operand is passed as an argument

return complex((x+c.x), (y+c.y)); The compiler invokes an appropriate constructor, initializes an object with no name and returns the contents for copying into an object. Such an object is called a temporary object and goes out of space as soon as the contents are assigned to another object.

Overloading Binary Operators Using Friends –Friend function requires two arguments to be explicitly passes to it. –Member function requires only one. friend complex operator+(complex, complex); complex operator+(complex a, complex b) { return complex((a.x + b.x),(a.y + b.y)); return complex((a.x + b.x),(a.y + b.y));} –We can use a friend function with built-in type data as the left-hand operand and an object as the right-hand operand.