W3101: Programming Languages C++ Ramana Isukapalli

Similar documents
Dr. Md. Humayun Kabir CSE Department, BUET

OBJECT ORIENTED PROGRAMMING USING C++

Where do we stand on inheritance?

Module Operator Overloading and Type Conversion. Table of Contents

Advanced C++ Programming Workshop (With C++11, C++14, C++17) & Design Patterns

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

G52CPP C++ Programming Lecture 15

Variables. Data Types.

Chapter 15 - C++ As A "Better C"

Overloading Operators in C++

Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini

Laboratorio di Tecnologie dell'informazione

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

Introduction to C++ Systems Programming

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

CS3157: Advanced Programming. Outline

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

C++ Casts and Run-Time Type Identification

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

CMSC 202 Midterm Exam 1 Fall 2015

C++ Scope Resolution Operator ::

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Tokens, Expressions and Control Structures

CMPS 221 Sample Final

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

Lab Instructor : Jean Lai

CS11 Advanced C++ Fall Lecture 7

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Lecture 23: Pointer Arithmetic

Friends and Overloaded Operators

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

AN OVERVIEW OF C++ 1

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

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

CS2141 Software Development using C/C++ C++ Basics

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Friends and Overloaded Operators

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

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

Introduction to Programming

C++ Programming: From Problem Analysis to Program Design, Third Edition

OBJ. ORI.& MULT. PROG., M.C.Q. BANK, FOR UNIT -2, SECOND YEAR COMP. ENGG. SEM-4, 2012 PATTERN, U.O.P. UNIT-2

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

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

Array Elements as Function Parameters

EEE145 Computer Programming

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

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

G52CPP C++ Programming Lecture 18

CS201 Some Important Definitions

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Programming in C++: Assignment Week 8

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

CS201- Introduction to Programming Current Quizzes

PIC 10A Objects/Classes

Object-Oriented Design (OOD) and C++

C++ Programming: Polymorphism

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

C++ Programming for Non-C Programmers. Supplement

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

Program template-smart-pointers-again.cc

Fast Introduction to Object Oriented Programming and C++

Object Reference and Memory Allocation. Questions:

CPSC 427: Object-Oriented Programming

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

CE221 Programming in C++ Part 1 Introduction

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

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Time(int h, int m, int s) { hrs = h; mins = m; secs = s; } void set(int h, int m, int s) { hrs = h; mins = m; secs = s; }

Interview Questions of C++

LECTURE 02 INTRODUCTION TO C++

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Objectives. In this chapter, you will:

Ch 8. Operator Overloading, Friends, and References

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Increases Program Structure which results in greater reliability. Polymorphism

C++ Coding Standards and Practices. Tim Beaudet March 23rd 2015

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Ch. 12: Operator Overloading

Introduction to Programming using C++

C++#Overview#(1)# Topics# COS320# Heejin#Ahn#

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Advanced Systems Programming

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

C++ For Science and Engineering Lecture 15

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

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

PHY4321 Summary Notes

pointers + memory double x; string a; int x; main overhead int y; main overhead

CSCE 110 PROGRAMMING FUNDAMENTALS

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Transcription:

Lecture-6 Operator overloading Namespaces Standard template library vector List Map Set Casting in C++

Operator Overloading

Operator overloading On two objects of the same class, can we perform typical operations like Assignment (=), increment (++), decrement(--) Write to a stream ( << ) Reading from a stream ( >> ) Can be defined for user defined classes. Operator overloading Most of the common operators can be overloaded. Operators can be member/non-member functions

Operator overloading cont. Arity of operator Number of parameters required. Unary operators take one argument E.g., ++, --,!, ~, etc. C unary operators remain unary in C++ Binary operators take two arguments. E.g., =, >, <, +, -, etc. C binary operators remain binary. Typical overloaded operators +, -, >, <, ++, --, +=, ==,!=, <=, >=, <<, >>, [ ]

Operator functions rules Member function operators Leftmost operand must be an object (or reference to an object) of the class. If left operand is of a different type, operator function must NOT be a member function Built-in operators with built-in data types CANNOT be changed. Non-member operator function must be a friend if private or protected members of that class are accessed directly

Syntax Member function return_type classname :: operator symbol (args) { // code } Non-Member function return_type operator symbol (args) { // code }

Example class Integer { private: int value; public: Integer (int val) : value (val) { } void operator++( ) { value++; } // Member op void operator-( ) { value = 0 - value; } // Member op // Non-member friend function operator friend Integer operator + (const Integer& i, const Integer& j); void showvalue( ) { cout << "Value is: " << this->value << endl; } }; Integer operator + (const Integer&i, const Integer& j) { return Integer (i.value + j.value); }

Namespaces

Namespaces A way to give scope to different variables, as opposed to a global scope. E.g., namespace mynamespace { int a, b; } a and b are visible ONLY in mynamespace Can be accessed using the keywork using using namespace mynamespace a = 3; b = 4; mynamespace::a = 3; mynamespace::b = 4; using mynamespace::a cout << value of a is: << a << endl;

Namespace example namespace mynamespace { int a = 3; double b = 4.3; void printhello ( ) { cout << Hello << endl; } } int main ( ) { using namespace mynamespace; printhello( ); // Note: No declaration for a and b cout << a: << a << << b: << b << endl; }

Standard Template Libraries (STL)

Standard template library Defines many useful classes. Popular among them vector, list, map, set, iterators, etc. Each of these is a template class. Has many useful functions. References: http://www.cplusplus.com/reference/stl/ They list all the functions, coding examples and many nice features for vectors, lists and iterators.

C++ type casting Casting: converting a variable/object of one type/class to another. C type casting can create run time errors in C++. C++ needs to cast between related classes C++ provides additional casting methods dynamic_cast static_cast reinterpret_cast const_cast

C++ type casting contd. E.g., Base b, *bp; Derived d, *dp dynamic_cast: Casting from derived to base class, NOT from base to derived to base class. bp = dynamic_cast <base*> (dp); // Allowed dp = dynamic_cast <derived *>(b); // NOT allowed static_cast: Casting from base to derived and vice-versa. bp = static_cast <base*> (d); // Allowed dp = static_cast <derived *>(b); // allowed

Casting contd. reinterpret_cast: Binary copy of values from one pointer to another Can cast any type to any other type, even for unrelated class objects. Class A {.. }; Class B {..}; // not related to A A *pa = new A; B *pb = reinterpret_cast<b*> pa; Suggestion: Don t use it unless you know what you are doing. const_cast: Set or remove the const ness of an object Const object can be passed as an non-const argument to a function.

const_cast - example #include <iostream> using namespace std; void myprint (char * str) { cout << str << endl; } int main ( ) { const char * c = "sample text"; myprint ( const_cast<char *> (c) ); return 0; }