IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Friends.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

CPS 235 Object Oriented Programming Paradigm
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
4/14/2015Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23m2 OOP Friend Functions Ref: Sebesta, Chapter 12; Lafore, Chapter 11.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
EC-241 Object-Oriented Programming
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Function and Class Templates.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator overloading Object Oriented Programming.
Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
CS212: Object Oriented Analysis and Design Lecture 5: Classes and Objects - II.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 3/2/05CS250 Introduction to Computer Science II Composition and friend Functions.
Chapter 10 Introduction to Classes
 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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Introduction to Programming Lecture 40. Class Class is a user defined data type.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
Friend Function. 2 Any data which is declared private inside a class is not accessible from outside the class. A non-member function cannot have an access.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
1 Introduction to Object Oriented Programming Chapter 10.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
1 C++ Classes & Object Oriented Programming Overview & Terminology.
COMP 3000 Object-Oriented Programming for Engineers and Scientists Operator Overloading Dr. Xiao Qin Auburn University
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Operator.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
More on Classes Programming in C++ Fall 2008
Classes & Objects: Examples
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Static in Classes CSCE 121 J. Michael Moore.
Introduction to Programming
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
STATIC DATA MEMBER & MEMBER FUNCTIONS
Lab – 2018/04/12 implement getTotalCount to return how many ‘nMatrix’s are allocated(exist) (modify constructor and destructor, use static variable and.
CS 144 Advanced C++ Programming February 21 Class Meeting
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Object Oriented Programming (OOP) Lecture No. 12
Classes Member Qualifiers
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Presentation transcript:

IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Friends and Static Members 1Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

IIT Bombay Object-oriented programming with structures and classes Accessing data members and member functions Constructors and destructors Function calls with structures/classes Operator overloading 2Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay Quick Recap of Relevant Topics

IIT Bombay Friend classes and functions Static data members and static member functions 3Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay Overview of This Lecture

IIT Bombay Acknowledgment Much of this lecture is motivated by the treatment in An Introduction to Programming Through C++ by Abhiram G. Ranade McGraw Hill Education 2014 Examples taken from this book are indicated in slides by the citation AGRBook Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay4

IIT Bombay Friend Functions Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay5 Normally, “private” members of a class are accessible only to member functions of the class Data encapsulation or hiding Occasionally it may be desirable to bypass this access restriction for a few specific non-member functions Should these functions be made members of the class? Should we make all members of the class public? C++ provides a better solution: A “friend” declaration allows a class to explicitly allow specific non-member functions to access its private members

IIT Bombay Friend Functions Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay6 class Point { private: double x, y; public: … Member functions … }; bool collinear(Point &p1, Point &p2, Point &p3) { // Not a member of class Point double temp; temp = p1.x*(p2.y – p3.y) + p2.x*(p3.y – p1.y) + p3.x* (p1.y – p2.y); return (temp == 0); }

IIT Bombay Friend Functions Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay7 class Point { private: double x, y; public: friend bool collinear(Point &p1, Point &p2, Point &p3); … Member functions … }; bool collinear(Point &p1, Point &p2, Point &p3) { // Not a member of class Point double temp; temp = p1.x*(p2.y – p3.y) + p2.x*(p3.y – p1.y) + p3.x* (p1.y – p2.y); return (temp == 0); } Can be in public or private section of class Point

IIT Bombay Friend Functions Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay8 class Point { private: double x, y; friend bool collinear(Point &p1, Point &p2, Point &p3); public: … Member functions … }; bool collinear(Point &p1, Point &p2, Point &p3) { // Not a member of class Point double temp; temp = p1.x*(p2.y – p3.y) + p2.x*(p3.y – p1.y) + p3.x* (p1.y – p2.y); return (temp == 0); }

IIT Bombay Friend Functions In general, A function func can be “friend” of several classes C1, C2, … func can access private members of classes C1, C2, … A class C can have several “friend” functions func1, func2,... Each of func1, func2, … can access private members of C Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay9

IIT Bombay Friend Classes Various members of class C1 may need access to private members of class C2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay10 class Point { private: double x, y; public: … Member functions … }; class PointsInPlane { private: int numPoints; Point pointArray[100]; public: bool collinear(Point &p1, Point &p2, Point &p3) { … } bool isEquiLateral(Point &p1, Point &p2, Point &p3) { … } … Other member functions … };

IIT Bombay Friend Classes Entire class C1 can be declared “friend” of class C2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay11 class Point { private: double x, y; public: friend class PointsInPlane; … Member functions … }; class PointsInPlane { private: int numPoints; Point pointArray[100]; public: bool collinear(Point &p1, Point &p2, Point &p3) { … } bool isEquiLateral(Point &p1, Point &p2, Point &p3) { … } … Other member functions … };

IIT Bombay Static Data Members [Ref. AGRBook] Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay12 class Point { private: double x, y; public: static int count; Point() { count++; return; } Point(double a, double b) { x = a; y = b; count++; return; } }; int Point::count = 0; C++ keyword

IIT Bombay Static Data Members [Ref. AGRBook] Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay13 class Point { private: double x, y; public: static int count; Point() { count++; return; } Point(double a, double b) { x = a; y = b; count++; return; } }; int Point::count = 0; Declaration of static public data member Single copy of static data member “count” shared across all objects of class Point Inside class Point, referred to as simply “count”

IIT Bombay Static Data Members [Ref. AGRBook] Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay14 class Point { private: double x, y; public: static int count; Point() { count++; return; } Point(double a, double b) { x = a; y = b; count++; return; } }; int Point::count = 0; Referring to member count of class Point Note use of scope resolution operator :: Necessary when referring to a member outside the class definition

IIT Bombay Static Data Members [Ref. AGRBook] Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay15 class Point { private: double x, y; public: static int count; Point() { count++; return; } Point(double a, double b) { x = a; y = b; count++; return; } }; int Point::count = 0; Creation and initialization of static public data member Note this is not tied to creation of objects of class Point

IIT Bombay Static Data Members [Ref. AGRBook] Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay16 class Point { private: double x, y; public: static int count; Point() { count++; return; } Point(double a, double b) { x = a; y = b; count++; return; } }; int Point::count = 0; int main () { Point a, b, c(0.0, 0.0); cout << “Count of points “; cout << Point::count << endl; return 0; } All constructor calls update the same static data member. So this counts the number of points created.

IIT Bombay Static Data Members [Ref. AGRBook] Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay17 class Point { private: double x, y; public: static int count; Point() { count++; return; } Point(double a, double b) { x = a; y = b; count++; return; } }; int Point::count = 0; int main () { Point a, b, c(0.0, 0.0); cout << “Count of points “; cout << Point::count << endl; return 0; } Accessing count outside the class Point requires scope resolution operator

IIT Bombay Static Member Functions [Ref. AGRBook] Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay18 class Point { private: double x, y; static int count; public: Point() { count++; return; } Point(double a, double b) {x = a; y = b; count++; return;} static void resetCount() { count = 0; return; } void printCount() {cout << count << endl; return;} }; int Point::count; Declaration of static private data member Creation of static private data member

IIT Bombay Static Member Functions [Ref. AGRBook] Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay19 class Point { private: double x, y; static int count; public: Point() { count++; return; } Point(double a, double b) {x = a; y = b; count++; return;} static void resetCount() { count = 0; return; } void printCount() {cout << count << endl; return;} }; int Point::count; Declaration of static public member function Declaration of non-static public member function

IIT Bombay Use of Static Member Functions Static member function not invoked on object of class Point Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay20 int main () { Point::resetCount(); Point a, b, c(0.0, 0.0); cout << “Count of points “; cout << Point::count << endl; a.printCount(); return 0; } Invocation of static public member function in “main” Requires scope resolution operator

IIT Bombay Summary Friend functions and friend classes and their usage Static data members, static member functions and their usage Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay21