C++ Lecture 7 Function/operator overloading

Slides:



Advertisements
Similar presentations
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Advertisements

C++ Lecture 6 Object Life-times Creating objects using new Using pointers to objects Aggregation (Containment –UML speak) Other C++ class features.
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.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
C++ data types. Structs vs. Classes C++ Classes.
IEG 3080 Tutorial 1 Wilson Ip. Outline Lecture reviews: Some basics of Software Engineering principle Some basics of OOP How to use Visual Studio.NET.
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 in C++
1 CSC241: Object Oriented Programming Lecture No 07.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Review of C++ Programming Part II Sheng-Fang Huang.
1 Friends and Namespace COSC 1567 C++ Programming Lecture 6.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Operator Overloading and Type Conversions
Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Unified Modeling Language (UML) Case Study.
Programming in Java Unit 3. Learning outcome:  LO2:Be able to design Java solutions  LO3:Be able to implement Java solutions Assessment criteria: 
Case Study - Fractions Timothy Budd Oregon State University.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
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 in C++. Operator Overloading It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it.
Operator overloading and type convesions BCAS,Bapatla B.mohini devi.
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
Drawing System Sequence Diagrams
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.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
CS Object Oriented Programming Using C++
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
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.
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
Lecture 14 22/10/15. The Object-Oriented Analysis and Design  Process of progressively developing representation of a system component (or object) through.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
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.
Rational Rose For System Design What is Rational Rose? Rational Rose is the visual modeling software solution that lets you create, analyze, design,
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Operator Overloading.
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading Introduction
Eine By: Avinash Reddy 09/29/2016.
Introduction to Classes
Object-Orientated Programming
Operator Overloading
Corresponds with Chapter 7
Lecture 22 Inheritance Richard Gesick.
Operator Overloading.
Function Overloading C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define similar.
Operator overloading Dr. Bhargavi Goswami
Operator Overloading.
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Operator Overloading I
C++ data types.
Object Oriented Programming
Presentation transcript:

C++ Lecture 7 Function/operator overloading Friend functions – C++ example UML UML diagrams Use-case diagram Class diagram sequence diagram Unit Assessment

Function/operator overloading Function overloading – constructor functions actual function called determined by the number and type of the arguments Operators Unary e.g. -, !, ~, ++, -- Binary e.g. +,-,*,/,<<,>> etc. Operators are treated as functions by C++ Therefore the operators can be overloaded

Operator overloading It is therefore possible to write operator functions for the operators. The functions will be called operator@, where @ is the actual operator There will possibly be many operator functions all with the same name. They must differ in the number and type of their arguments. This is operator overloading.

Operators as functions Consider an object obj of class CAny CAny obj; The normal way of invoking a member function of the class CAny is :- obj.func() Now consider a binary operator e.g. objx @ objy where objx and objy are instances of some class and @ is a binary operator such as +,-,*,/, << or >>

Operators as functions objx @ objy is interpreted by the C++ compiler in on of two ways objx.operator@(objy) i.e. the compiler looks for a a member function called operator@ in the class of which objx is an instance. if such a member function cannot be found in the class then the following is tried operator@(objx, objy) i.e. the compiler looks for an ordinary function called operator@ that takes two arguments with the appropriate data types.

Operator overloading We have a choice over which method to implement : either as a member function of a class or as an ordinary function objx @ objy is treated as objx.operator(objy) If the operator is a member function then this implies that the object to the left of the operator MUST be an instance of the class containing the operator function. What if the object on the left is not an instance of the class containing the operator function?

Friend Functions We might therefore implement the operator as an ordinary function:- operator@(objx, objy) Since this is NOT a member function how can it access the private data of the objects the objx, objy class could have appropriate accessor and mutator functions. This would be less efficient than if the operator function could access the private data directly. C++ added Friends!

Friend Functions Friends are functions or whole classes which are granted access to the private data of a class.This breaks the encapsulation A friend is declared within the class which is allowing friendship. A function cannot make itself a friend of a class. Friendship should be used thoughtfully and sparingly A common use of friend functions is to allow the use of << and >> with cout and cin for output and input of user defined types.

Unified Modelling Language - UML UML is a modelling tool to capture and represent in the form of diagrams and text the various stages of the software development process There are 8 different diagram types. We shall only consider 3 Use-case : capture user interaction with the system Class : show relationship between objects and classes Sequence : show the interaction of objects. Rational Rose – UML development tool