Static Data Member and Functions

Slides:



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

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Lecture 14 Today: Overloading: Revision on this Revision on increment operators the assignment operator the [] operator Book: p , 215,
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
EC-241 Object-Oriented Programming
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
OBJECT ORIENTED PROGRAMMING Instructor: Rashi Garg Coordinator: Gaurav Saxena.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
1 Lab Session-4 CSIT121 Fall 2004 Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
1 Session-9 CSIT 121 Spring 2006 Lab Demo (first 20 minutes) The correct way to do the previous lab sheet Function calls Calling void functions Calling.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
Object Oriented Programming (OOP) Lecture No. 11.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
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.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
C++ Lesson 1.
Chapter 22 - C++ Templates
Two or more functions can have the same name but different parameters
Chapter 7: User-Defined Functions II
Examples of Classes & Objects
OBJECT ORIENTED PROGRAMMING
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Templates.
this Pointer Scope Resolution Operator Friend Function
Polymorphism Lec
Lecture 4-7 Classes and Objects
Dynamic Memory Allocation Reference Variables
Contents Introduction to Constructor Characteristics of Constructor
Classes & Objects: Examples
Pointers & Functions.
Classes Short Review of Topics already covered Constructor
CSC 253 Lecture 7.
Static in Classes CSCE 121 J. Michael Moore.
Object Oriented Programming Using C++
Introduction to Programming
Dr. Khizar Hayat Associate Prof. of Computer Science
Namespaces How Shall I Name Thee?.
Chapter 7: User-Defined Functions II
Submitted By : Veenu Saini Lecturer (IT)
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Pointers & Functions.
Dr. Khizar Hayat Associate Prof. of Computer Science
STATIC DATA MEMBER & MEMBER FUNCTIONS
CS 144 Advanced C++ Programming February 21 Class Meeting
Object Oriented Programming (OOP) Lecture No. 11
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
Introduction to Algorithms and Programming COMP151
Object Oriented Programming (OOP) Lecture No. 12
Object Oriented Programming
Presentation transcript:

Static Data Member and Functions Lecture 10 Static Data Member and Functions Thapar University UTA009

Static Data Member Single copy exists; shared by all the objects. Also known as class variables. Exists and initialized to zero before any object is created. Within a class, they are declared not defined. Requires a global definition outside the class. Re-declaration using scope resolution operator. Thapar University UTA009

Example #include<iostream> using namespace std; class myClass { static int a; int b; public: static int c; void set(int i, int j) { a++; b = i; c = j; } void get() { cout << "\nStatic a: " << a << ". Non-static b: " << b << ". Static c: " << c; } }; int myClass :: a = 10; int myClass :: c; myClass Common to all the objects a c set() get() b Object 1 b Object 2 b Object O … Thapar University UTA009

Example int main() { cout << "\nPublic Static c: " << myClass :: c; myClass m1, m2; m1.set(1,2); m2.set(3,4); m1.get(); m2.get(); return 0; } Output Public Static c: 0 Static a: 12. Non-static b: 1. Static c: 4 Static a: 12. Non-static b: 3. Static c: 4 Thapar University UTA009

Virtually eliminates any need for global variables. Uses Provide access control of shared resource used by all the objects of a class, e.g. writing a file. To keep track of the number of objects of a particular class type. Note: Virtually eliminates any need for global variables. Thapar University UTA009

Static Member Functions Can access only other static members of the class. Do not have a this pointer. They cannot be declared as const or volatile. They may not be virtual. There cannot be static and non-static version of the same function. Can be called using An object. Class name and scope resolution operator. Thapar University UTA009

Example Objective: Write a program to provide access control to some shared resource. Example: Create several objects, each of which wants to print a file on a specific printer. Clearly, only one object can be allowed to print a file at a time. In this case, declare a static variable that indicates when the printer is in use and when it is free. Each object then interrogates this variable to get the printer. Thapar University UTA009

Contd… #include<iostream> using namespace std; class shared { static int resource; public: static int getResource() { if(resource) return 0; else { resource = 1; return 1; } } void freeResource() { resource = 0; } }; int shared :: resource; int main() { shared o1, o2; if(o1.getResource()) cout << "\no1 has resource."; if(!shared :: getResource()) cout << "\no2 access denied."; o1.freeResource(); if(shared :: getResource()) cout << "\no2 has resource."; return 0; } Thapar University UTA009

Uses Very limited application. Pre-initialize private static data members of a class before any object is actually created. Thapar University UTA009