Static Members. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

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

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

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

Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Object Oriented Programming. C++ 6 th Sem, A Div Ms. Mouna M. Naravani

Simple Java Programs. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Chapter 3 Function Overloading

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

Introduction To C#.NET

CS 2530 INTERMEDIATE COMPUTING

Implementing Abstract Data Types (ADT) using Classes

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Object Oriented Programming(OOP).

Object-Oriented Programming. Chapter 4

STUDENT LESSON A5 Designing and Using Classes

Introduction to Programming Using Java (98-388)

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

b. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a.

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Chapter-8 DATA TYPES. Introduction. Variable:

CS201- Introduction to Programming Current Quizzes

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


JAVA GUI PROGRAMMING REVISION TOUR III


IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

BASIC ELEMENTS OF A COMPUTER PROGRAM

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

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

Chapter 10 Introduction to Classes

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

Module Operator Overloading and Type Conversion. Table of Contents

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

Data Structures using OOP C++ Lecture 3

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

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

[0569] p 0318 garbage

Object Oriented Design

Chapter 10 Defining Classes

Technical Questions. Q 1) What are the key features in C programming language?

DECLARAING AND INITIALIZING POINTERS

Introduction to C++ Friends, Nesting, Static Members, and Templates Topic #7

Lecture 18 Tao Wang 1

C++ 8. Constructors and Destructors

Darshan Institute of Engineering & Technology for Diploma Studies

Lecture 3 Tao Wang 1

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

CS242 COMPUTER PROGRAMMING

M.CS201 Programming language

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

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

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Array. Prepared By - Rifat Shahriyar

OBJECT ORİENTATİON ENCAPSULATİON

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

CSE 230 Intermediate Programming in C and C++ Functions

Padasalai.Net s Model Question Paper

Pointers, Dynamic Data, and Reference Types

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

15 FUNCTIONS IN C 15.1 INTRODUCTION

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

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

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Fundamentals of Programming Session 12

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Introduction to C++ with content from

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Object oriented programming C++

Unit 7. Functions. Need of User Defined Functions


More About Objects and Methods

More About Objects and Methods. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Object-Oriented Programming

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

CEN 414 Java Programming

Object Oriented Design

Chapter 7 - Notes User-Defined Functions II

C Programming for Engineers Functions

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

Chapter 6 Structures and Classes. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

C++ : Object Oriented Features. What makes C++ Object Oriented

C++ (classes) Hwansoo Han

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

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

CSCI 171 Chapter Outlines

Advanced Computer Programming

Computational Expression

Functions. A function is a subprogram that performs a specific task. Functions you know: cout << Hi ; cin >> number;

Protection Levels and Constructors The 'const' Keyword

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

IV Unit Second Part STRUCTURES

More on Functions. Lecture 7 COP 3014 Spring February 11, 2018

Transcription:

Static Members OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani

Static Data Members Static data members hold global data that is common to all objects of the class. Ex: count of objects currently present, common data accessed by all objects, etc. Ex: We want all objects of a class to calculate interest at the rate of 4.5%. This data should be globally available to all objects of a class. 2 problems: i] This data cannot be a member of any object, because same value would have to be maintained for this data in all objects, which takes unnecessary space. ii] This data should not be stored in a global variable. Even non-member functions will be able to access this data.

Storing the data in a static variable of the class resolves this conflict. Static data members are members of the class(also called as class variables) and not of any object of the class, that is they are not contained inside any object. Declaration of static data member: by using the keyword static static data_type variable_name; Ex: class Account }; static float interest_rate; - - - - - - - - - //a static data member

When a static data member is declared inside a class, memory will not be allocated for it. Moreover, memory for a static data member will not get allocated when objects of the class are declared. This is because a static data member is not a member of any object. In order to allocate memory for static data member, it must be explicitly defined outside the class. Syntax: data_type class_name : : variable_name; Ex: float Account : : interest_rate; //memory is allocated, defined outside the class There is only one copy of static data member in the memory.(for all the objects of a class) Static data members do not increase the size of objects of the class.

class Account }; static float interest_rate; - - - - - - - - - //a static data member float Account : : interest_rate; //memory is allocated, defined outside the class The above statement initializes interest_rate to zero(by default). You can also assign different value to static data member(if needed).

class Account }; static float interest_rate; - - - - - - - - - //a static data member float Account : : interest_rate = 4.5; //memory is allocated, defined outside the class, //initialized with 4.5 Making static data members private prevents any change from non-member functions as only member functions can change the value of static data members.

Static data members can be of any type. Ex: class Account static float interest_rate; static char name[30]; - - - - - - - }; float Account : : interest_rate = 4.5; char Account : : name = The Rich and Poor Bank ;

Static data members of integer type can be initialized within the class itself if the need arises. Ex: class Account static int namelength = 30; //static int type, initialized within class static char name[namelength]; - - - - - - - }; int Account : : namelength; char Account : : name = The Rich and Poor Bank ;

The static data member that has been initialized inside the class must be still defined outside the class to allocate memory for it. Once the initial value has been supplied within the class, the static data member must not be re-initialized when it is defined. Non-integer static data members cannot be initialized within the class. Ex: class Account }; static char name[30] = The Rich and Poor Bank ; //ERROR

One static data member can directly refer to another without using the scope resolution operator. Ex: class Account static int namelength = 30; static char name[namelength]; - - - - - - - };

Member functions can refer to static data members directly. Ex: class Account static float interest_rate; public: void updatebalance(); - - - - - - }; float Account : : interest_rate = 4.5; void Acount : : updatebalance() if(end_of_year) balance + = balanace * interest_rate /100; }

There are some things static data members can do but non-static data members cannot. 1. A static data member can be of the same type as the class of which it is a member.

2. Static data members can appear as the default value for the formal arguments of member functions of its class. StaticDataMember.cpp

Static Member Functions A member function that is declared static has the following properties: A static function can have access to only other static members (functions or variables) declared in the same class. Prefixing the function prototype with the keyword static specifies it as a static member function. Ex: static return_type function_name(arguments if any); The keyword static should not reappear in the definition of the function. A static member function can be called using the class name(instead of its objects) as follows: class_name : : static_function_name();

Ex: class Account static float interest_rate; public: static void set_interest_rate(float); - - - - - - -- }; float Account : : interest_rate = 4.5; void Account : : set_interest_rate(float p) interest_rate = p; } static void set_interest_rate(float) - - - - - - }

The function can be called directly without an object. Account : : set_interest_rate(5); Accessing non-static data members through a static member function results in compile time errors. Static functions can access only static data members of the class. Static member functions can still be called with respect to objects. Account a1; a1. set_interest_rate(5); StaticMemberFunction.cpp

References Sourav Sahay, Objected Oriented Programming with C++ E Balagurusamy, Objected Oriented Programming with C++