KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Similar documents
OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV

Operator Overloading in C++ Systems Programming

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES III

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

11.2. Overloading Operators

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

Ch. 12: Operator Overloading

CS304 Object Oriented Programming Final Term

Intermediate Programming & Design (C++) Classes in C++

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

CS201 Latest Solved MCQs

CS201 Some Important Definitions

Chapter 18 - C++ Operator Overloading

Object-Oriented Design (OOD) and C++

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

CS201- Introduction to Programming Current Quizzes

Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects

Object Oriented Design

Object-Oriented Principles and Practice / C++


Midterm Review. PIC 10B Spring 2018

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

More Advanced Class Concepts

Launchpad Lecture -10

Lecture 5. Function Pointers

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Constructors and Destructors. Chapter 6

Introduction to Programming Using Java (98-388)

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

IS0020 Program Design and Software Tools Midterm, Fall, 2004

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions.

Program construction in C++ for Scientific Computing

Module Operator Overloading and Type Conversion. Table of Contents

C++ (Non for C Programmer) (BT307) 40 Hours

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Short Notes of CS201

ADTs & Classes. An introduction

7.1 Optional Parameters

CS201 - Introduction to Programming Glossary By

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

Absolute C++ Walter Savitch

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

UNIT- 3 Introduction to C++

2 ADT Programming User-defined abstract data types

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

6.096 Introduction to C++

CS304 Object Oriented Programming

CS 247: Software Engineering Principles. ADT Design

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

CS 162, Lecture 25: Exam II Review. 30 May 2018

More class design with C++ Starting Savitch Chap. 11

For Teacher's Use Only Q No Total Q No Q No

Problem Solving with C++

Chapter 8. Operator Overloading, Friends, and References. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Programming C++ Lecture 6. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

Cpt S 122 Data Structures. Introduction to C++ Part II

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

Review. What is const member data? By what mechanism is const enforced? How do we initialize it? How do we initialize it?

(7-2) Operator Overloading D & D Chapter 10. Instructor - Andrew S. O Fallon CptS 122 (February 23, 2018) Washington State University

CS105 C++ Lecture 7. More on Classes, Inheritance

A <Basic> C++ Course

Overloading Operators in C++

A <Basic> C++ Course

6.096 Introduction to C++ January (IAP) 2009

Chapter 11: More About Classes and Object-Oriented Programming

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Interview Questions of C++

Intermediate Programming, Spring 2017*

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

CS3157: Advanced Programming. Outline

Review of the C Programming Language for Principles of Operating Systems

Collected By Anonymous

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Object Oriented Programming

W3101: Programming Languages C++ Ramana Isukapalli

Operator overloading

C++ 8. Constructors and Destructors

Java Primer 1: Types, Classes and Operators

Pointers, Dynamic Data, and Reference Types

CPSC 427: Object-Oriented Programming



Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:


Evolution of Programming Languages

CSC 330 Object Oriented Programming. Operator Overloading Friend Functions & Forms

And Even More and More C++ Fundamentals of Computer Science

+2 Volume II OBJECT TECHNOLOGY OBJECTIVE QUESTIONS R.Sreenivasan SanThome HSS, Chennai-4. Chapter -1

Lecture 18 Tao Wang 1

Use the dot operator to access a member of a specific object.

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

Where do we go from here?

Transcription:

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OPERATOR OVERLOADING KOM3191 Object-Oriented Programming

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 Dynamic Memory Management Allocation and deallocation of memory in a program is dynamic memory management and is performed with operators new and delete. Consider the following declaration and statement: Time *timeptr = new Time; The new operator allocates storage of the proper size for an object of type Time, calls the default constructor to initialize the object and returns a pointer of the type specified to the right of the new operator (i.e., a Time *). Note that new can be used to dynamically allocate any fundamental type (such as int or double) or class type. If new is unable to find sufficient space in memory for the object, it indicates that an error occurred by "throwing an exception." The new operator returns a 0 pointer in versions of C++ prior to the ANSI/ISO standard. To destroy a dynamically allocated object and free the space for the object, use the delete operator as follows: delete timeptr;

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 3 Dynamic Memory Management C++ allows you to provide an initializer for a newly created fundamental-type variable, as in double *ptr = new double( 3.14159 ); which initializes a newly created double to 3.14159 and assigns the resulting pointer to ptr. The same syntax can be used to specify a comma-separated list of arguments to the constructor of an object. For example, Time *timeptr = new Time( 12, 45, 0 ); initializes a newly created Time object to 12:45 PM and assigns the resulting pointer to timeptr. 10-element integer array can be allocated and assigned to gradesarray as follows: int *gradesarray = new int[ 10 ];

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 4 Dynamic Memory Management size of an array created at compile time must be specified using a constant integral expression. const int size=10; int gradesarray[size]; However, the size of a dynamically allocated array can be specified using any integral expression that can be evaluated at execution time. When allocating an array of objects dynamically, the programmer cannot pass arguments to each object's constructor. Instead, each object in the array is initialized by its default constructor. To delete the dynamically allocated array to which gradesarray points, use the statement delete [ ] gradesarray; The preceding statement deallocates the array to which gradesarray points. If the preceding statement did not include the square brackets ([ ]) and gradesarray pointed to an array of objects, only the first object in the array would receive a destructor call.

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 5 static Class Members There is an important exception to the rule that each object of a class has its own copy of all the data members of the class. In certain cases, only one copy of a variable should be shared by all objects of a class. A static data member is used for these and other reasons. Such a variable represents "class-wide" information (i.e., a property of the class shared by all instances, not a property of a specific object of the class). The declaration of a static member begins with keyword static. A class's static data members have class scope. Also, static members can be declared public, private or protected. A fundamental-type static data member is initialized by default to 0. If you want a different initial value, a static data member can be initialized once (and only once). A const static data member of int or enum type can be initialized in its declaration in the class definition. However, all other static data members must be defined at file scope (i.e., outside the body of the class definition) and can be initialized only in those definitions. Note that static data members of class types (i.e., static member objects) that have default constructors need not be initialized because their default constructors will be called.

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 6 static Class Members A class's private and protected static members are normally accessed through public member functions of the class or through friends of the class. 1. A class's public class members can be accessed through any object of that class using the object's name, the dot operator and the name of the member e.g: mygradebook.studentnumber 2. A class's public static class members can also be accessed through any object of that class using the object's name, the dot operator and the name of the member e.g: mygradebook.studentnumber 3. To access a private or protected static class member when no objects of the class exist, provide a public static member function and call the function by prefixing its name with the class name and binary scope resolution operator

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 7 static Class Members

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 8 static Class Members

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 9 static Class Members

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 10 //Example 2 //Test.h

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 11 //Example 2 //Test.cpp

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 12 //Example 2 //mymain.cpp

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 13 //Example 2 //mymain.cpp

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 14

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 15 Operator overloading C++ can give special meanings to operators, when they are used with user-defined classes. This is called operator overloading. You can implement C++ operator overloads by providing special member-functions To overload the + operator for your class, you would provide a member-function named operator + on your class. To overload the = for your class you would provide a member function named operator = on your class Point point1(1,2); Point point2(1,2); Point point3 = point1 + point2; Here two operators are used The following set of operators overloaded mostly for user-defined classes: = (assignment operator) + - * (binary arithmetic operators) += -= *= (compound assignment operators) ==!= (comparison operators)

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 16 Operator overloading Operators That Must Be Overloaded as Member Functions When overloading (), [], -> or any of the assignment operators, the operator overloading function must be declared as a class member. For the other operators, the operator overloading functions can be class members or global functions. Operators as Member Functions and Global Functions Whether an operator function is implemented as a member function or as a global function, the operator is still used the same way in expressions. So which implementation is best? When an operator function is implemented as a member function, the leftmost (or only) operand must be an object (or a reference to an object) of the operator's class. If the left operand must be an object of a different class or a fundamental type, this operator function must be implemented as a global function A global operator function can be made a friend of a class if that function must access private or protected members of that class directly. Operator member functions of a specific class are called (implicitly by the compiler) only when the left operand of a binary operator is specifically an object of that class, or when the single operand of a unary operator is an object of that class.

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 17 Point Class Example

Similarly, the overloaded stream extraction operator (>>) is used In an expression in which the left operand has type istream &, as in cin >> classobject, and the right operand is an object of a user-defined class, so it, too, must be a global function. Also, each of these overloaded operator functions may require access to the private data members of the class object being output or input, so these overloaded operator functions can be made friend functions of the class for performance reasons. KOM3191 Object Oriented Programming Dr Muharrem Mercimek 18 Point Class Example The overloaded stream insertion operator (<<) is used in an expression in which the left operand has type ostream &, as in cout << classobject. To use the operator in this manner where the right operand is an object of a userdefined class, it must be overloaded as a global function.

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 19 Point Class Example A binary operator can be overloaded as a non-static member function with one argument or as a global function with two arguments (one of those arguments must be either a class object or a reference to a class object).

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 20 Point Class Example

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 21 Point Class Example