OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV

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

Classes: A Deeper Look. Systems Programming

W8.1 Continuing Classes friend Functions and friend Classes Using the this Pointer Cascading Function Calls

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

A Deeper Look at Classes

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES III

Lecture 5. Function Pointers

Friends and Overloaded Operators

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

Friends and Overloaded Operators

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

Introduction Of Classes ( OOPS )

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

IS 0020 Program Design and Software Tools

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

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

C How to Program, 6/e, 7/e

Friend Functions. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Object-Oriented Programming

Object oriented programming with C++

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

The Compositional C++ Language. Denition. Abstract. This document gives a concise denition of the syntax and semantics

Abstraction and Encapsulation. Benefits of Abstraction & Encapsulation. Concrete Types. Using Typedefs to streamline classes.

Lecture 18 Tao Wang 1

Example : class Student { int rollno; float marks; public: student( ) //Constructor { rollno=0; marks=0.0; } //other public members };

Object Oriented Design

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

G52CPP C++ Programming Lecture 13

(7 2) Classes: A Deeper Look D & D Chapter 9. Instructor - Andrew S. O Fallon CptS 122 (February 22, 2019) Washington State University

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

Computer Programming Class Members 9 th Lecture

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS201- Introduction to Programming Current Quizzes

CS304 Object Oriented Programming Final Term

Chapter 10 Introduction to Classes

CS313D: ADVANCED PROGRAMMING LANGUAGE

Java Primer 1: Types, Classes and Operators

IS 0020 Program Design and Software Tools

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.

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

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

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

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

CS201 Latest Solved MCQs

Program construction in C++ for Scientific Computing


Chapter 9 Classes : A Deeper Look, Part 1

Tokens, Expressions and Control Structures

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

Abstraction in Software Development

CS111: PROGRAMMING LANGUAGE II

IS0020 Program Design and Software Tools Midterm, Fall, 2004

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

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Chapter 17 - C++ Classes: Part II

Chapter 11: More About Classes and Object-Oriented Programming

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

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

CLASSES AND OBJECTS IN JAVA

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

CS313D: ADVANCED PROGRAMMING LANGUAGE

Short Notes of CS201

CMSC202 Computer Science II for Majors

Ch02. True/False Indicate whether the statement is true or false.

CS201 - Introduction to Programming Glossary By

RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING QUESTION BANK UNIT I 2 MARKS

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

C++ 8. Constructors and Destructors

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Programming 2. Object Oriented Programming. Daniel POP

Recharge (int, int, int); //constructor declared void disply();

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

Computer Programming with C++ (21)

6.096 Introduction to C++ January (IAP) 2009

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Constructors for classes

Encapsulation in C++

Special Member Functions

Pointers review. int a = 5; int *ptr = &a; cout << *ptr;

Object-Oriented Principles and Practice / C++

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

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

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

Preview 11/1/2017. Constant Objects and Member Functions. Constant Objects and Member Functions. Constant Objects and Member Functions

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

OBJECT ORIENTED PROGRAMMING USING C++

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Classes Ch

CS111: PROGRAMMING LANGUAGE II

SFU CMPT Topic: Classes

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Computer Science II CSci 1200 Lecture 18 Operators and Friends

Chapter 13: Copy Control. Overview. Overview. Overview

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

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

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Classes Nov 3, Ch

Transcription:

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV KOM3191 Object-Oriented Programming

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 Date.h

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 3 Date.cpp

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 4 Date.cpp

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 5 Employee.h

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 6 Employee.cpp Class Date does not provide a constructor that receives a parameter of type Date. The compiler provides each class with a default copy constructor. Each data member is copied. birthdate=dateofbirth; hiredate=dateofhire; If birthdate and hiredate are not const parameters, then default memberwise assignment can be used. Again no definition of such a constructor by the user

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 7 Employee.cpp

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 8 Default Member-wise Assignment Many classes must provide their own assignment operator To intelligently assign one object to another More about assignment of objects to each other when we get to Operator Overloading The '=' operator can be overloaded, just like any other Copy Constructor A constructor that copies another object of the same type Compiler provides a default copy constructor Copies each member of the original object into the corresponding member of the new object i.e., member-wise assignment Enables pass-by-value for objects Used to copy original object s values into new object to be passed to a function or returned from a function Many classes must provide an explicit Copy Constructor To intelligently copy one object to another A Deeper Look at Classes

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 9

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 10 test_employee.cpp

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 11 friend Function of a Class Ordinary Member Functions: 1. Function can access the private members of the class 2. Function is in the scope of the class 3. Function must be invoked on a specific object of the class e.g., ptr -> func() obj.func()

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 12 friend Function of a Class Defined outside that class s scope. Not a member function of that class. Has right to access non-public and public members of that class. Often appropriate when a member function cannot be used for certain operations. Can enhance performance.

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 13 friend Functions and friend Classes To declare a function as a friend of a class: Provide the function prototype in the class definition preceded by keyword friend To declare a class as a friend of another class: Place a declaration of the form friend class ClassTwo; in the definition of class ClassOne All member functions of class ClassTwo are friends of class ClassOne Friendship is granted, not taken. For class B to be a friend of class A, class A must explicitly declare that class B is its friend. Friendship relation is neither symmetric nor transitive If class A is a friend of class B, and class B is a friend of class C, cannot infer that class B is a friend of class A, class C is a friend of class B, class A is a friend of class C.

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 14 friend Functions and friend Classes friend function declaration (can appear anywhere in the class) friend function can modify Count s private data Calling a friend function; note that we pass the Count object to the function

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 15 this Pointer Member functions know which object s data members to manipulate Every object has access to its own address through a pointer called this (a C++ keyword) An object s this pointer is not part of the object itself The this pointer is passed (by the compiler) as an implicit argument to each of the object s non-static member functions Objects may use the this pointer implicitly or explicitly. this is used implicitly when accessing members directly. It is used explicitly when using keyword this. Type of the this pointer depends on type of the object, and whether member function is declared const.

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 16 this Pointer Implicitly using the this pointer to access member x Explicitly using the this pointer to access member x Using the dereferenced this pointer and the dot operator

KOM3191 Object Oriented Programming Dr Muharrem Mercimek 17 Cascaded member-function calls Multiple functions are invoked in the same statement. Enabled by member functions returning the dereferenced this pointer. Time t; t.sethour(15).setminute( 30 ).setsecond( 22 ); Time& Time::setHour(int hr) { hour=hr; return *this; }