Static Members Sharing is caring….

Slides:



Advertisements
Similar presentations
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Advertisements

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.
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
©2004 Brooks/Cole Chapter 10 More on Classes. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Object assignment With primitive types, setting.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Unsigned and Signed Numbers. Hexadecimal Number 217A 16 Position Digits A Value = 2x x x16 + Ax1 = 2x x x16.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
 2006 Pearson Education, Inc. All rights reserved Arrays.
GAME 1024: Advanced Game Programming Lesson 1: The Beginning By Kain Shin.
Programming Languages and Paradigms Programming C++ Classes.
1 CSC241: Object Oriented Programming Lecture No 06.
Object Oriented Programming (OOP) Lecture No. 11.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
#include guards Practical session #2 Software Engineering
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
Object and Classes อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 3.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Variable Scope. When you declare a variable, that name and value is only “alive” for some parts of the program  We must declare variables before we use.
Introduction to Object-oriented Programming
Copyright © Jim Fawcett Spring 2017
Topic: Classes and Objects
Abstract classes and interfaces
Hank Childs, University of Oregon
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?
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.
Programming with ANSI C ++
C Programming Tutorial – Part I
More About Objects and Methods
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?
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
This pointer, Dynamic memory allocation, Constructors and Destructor
Class Variables We’ve seen how to add extra subroutines into our programs Can be accessed from within one another We can also add variables that are “global”
Advanced Programming in Java
Static Data Member and Functions
Abstract Data Types and Encapsulation Concepts
Const in Classes CSCE 121 J. Michael Moore.
Abstract classes and interfaces
7 Arrays.
Classes & Objects: Examples
Interfaces.
Chapter 9 Objects and Classes
Operator Overloading, Friends, and References
Writing Functions.
Programs and Classes A program is made up from classes
Tonga Institute of Higher Education
Documentation and Style
The Destructor and the Assignment Operator
Method of Classes Chapter 7, page 155 Lecture /4/6.
Abstract classes and interfaces
Submitted By : Veenu Saini Lecturer (IT)
CPS120: Introduction to Computer Science
Class rational part2.
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
Class Circle { Float xc, yc, radius;
Object Oriented Programming (OOP) Lecture No. 12
Classes Member Qualifiers
Object Oriented Programming
Presentation transcript:

Static Members Sharing is caring…

Single Copy Some things only need/want one of: Constants NextID

Static Static : Shared Member for which very object of class shares value Student mascot: Storm student1 name: Bob mascot: student2 name: Jane mascot:

Static Changing static variable in one changes for all: Student mascot: Storm student1 name: Bob mascot: student2 name: Jane mascot:

Static Changing static variable in one changes for all: student1.setMascot("Bruin") Student mascot: Bruin student1 name: Bob mascot: student2 name: Jane mascot:

Special Static Rules Non-const & non-literal member variables not initialized in constructor or declaration: .h file: Get own initialization outside of class .cpp file:

Static Functions Static Functions May only access static variables Every object guaranteed to give the same answer Student mascot: Bruin student1 name: Bob mascot: student2 name: Jane mascot:

Static Functions Static Functions Every object guaranteed to give the same answer Allowed to call static functions directly on class: CLASSNAME::function()

Book Example : Counter Circle with static int : number of Circles .cpp:

Book Example : Counter Constructors add to counter

Constant Constants are good things to share: In .cpp

Constant Inside class, access via variable name: From outside the class, need fully qualified name:

Static Rules of Thumb OK to make static Think long and hard first… Constants Think long and hard first… Something you think should only be one of Functions you want to access through class Factory Functions Function Libraries (java style)