STATIC DATA MEMBER & MEMBER 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

PHP functions What are Functions? A function structure:
CPS 235 Object Oriented Programming Paradigm
Starting Out with C++, 3 rd Edition 1 Chapter 14 – More About Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.

Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
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.
Win32 Programming Lesson 4: Classes and Structures.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
C++ Lecture 6 Object Life-times Creating objects using new Using pointers to objects Aggregation (Containment –UML speak) Other C++ class features.
Classes and Objects. Class  A class is a way to bind the data describing an entity and its associated functions together.  In C++ class makes a data.
Classes and Objects Presented by: Gunjan Chhabra.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Operator overloading Object Oriented Programming.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Learners Support Publications Classes and Objects.
1 CSC241: Object Oriented Programming Lecture No 06.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
Object Oriented Programming (OOP) Lecture No. 11.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function in C 
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
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.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Friends.
Static Variables. Function Scope  “Normal” local variables go out of scope and are deallocated when a function terminates.
Friend functions.
Name Spaces: ALL versus OOL
2 Chapter Classes & Objects.
Class and Object Cont’d
Examples of Classes & Objects
C Functions -Continue…-.
CSC241: 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.
Encapsulation, Data Hiding and Static Data Members
Classes & Objects.
Concepts and Basics of C++ Programming
This technique is Called “Divide and Conquer”.
Object-Oriented Programming Using C++
Static Data Member and Functions
Lecture 4-7 Classes and Objects
HYBRID INHERITANCE : AMBIGUITY REMOVAL
Structs and Classes Static Class Members Recall the following
Classes & Objects: Examples
6 Chapter Functions.
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Static in Classes CSCE 121 J. Michael Moore.
Object Oriented Programming Using C++
Operator Overloading.
Namespaces How Shall I Name Thee?.
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Chapter 7: User-Defined Functions II
Scope of variables class scopeofvars {
Object-Oriented Programming Using C++
Dynamic Memory.
Submitted By : Veenu Saini Lecturer (IT)
Object Oriented Programming (OOP) Lecture No. 11
Constructors and Deconstructor
Object Oriented Programming (OOP) Lecture No. 12
Scope Rules.
Presentation transcript:

STATIC DATA MEMBER & MEMBER FUNCTIONS Ritika sharma

STATIC DATA MEMBERS It is normally used to maintain values common to the entire class. For ex: It can be used as counter that records the occurrences of all the objects. Static data member is associated with class not with object. But it is shared by all the objects of a class.(it may exist if no object of a class it created. Initialized to Zero , when first object of class is created. Ritika sharma

class abc { private: static int count; int number; public: void getdata(int a) { cout <<"enter no"<<endl; number=a; count++; } void getcount() cout<<"count:=="; cout<<count; }; int abc :: count ; Ritika sharma

int abc :: count ; int main() { abc a,b; a.getcount(); b.getcount(); a.getdata(100); b.getdata(200); return 0; } Ritika sharma

PURPOSE OF MAKING DATA MEMBER STATIC It is used to maintain Data which is specific for class not for object. Only one copy of that member is created for the entire class and is shared by all the objects of the class. Virtually eliminates need of GLOBAL Variable. Static data member behave exactly like global variable, difference is what its scope is within the class, global variable scope is within the program. Its lifetime is entire program , and is visible only within the class. Ritika sharma

How static data member can be accessed With the class name followed by the scope resolution operator. Definition is done outside the class scope(in addition to the declaration within the class scope to avoid linker error. Ritika sharma

Static Member function Its is also not associated with object. It cannot access non-static data members Can be ACCESSED with the class name followed by resolution operator. It is used to read /write static data members ,as for encapsulation data members should be kept private. Ritika sharma

class abc { private: static int count; int number; public: void getdata(int a) { cout <<"enter no"<<endl; number=a; count++; } static void getcount() cout<<"count:=="; cout<<count; }; int abc :: count ; Ritika sharma

int abc :: count ; int main() { abc a,b; abc::getcount(); a.getcount(); b.getcount(); a.getdata(100); b.getdata(200); return 0; } Ritika sharma