Structs and Classes Static Class Members Recall the following

Slides:



Advertisements
Similar presentations
EC-241 Object-Oriented Programming
Advertisements

C/c++ 4 Yeting Ge.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Learners Support Publications Classes and Objects.
1 CSC241: Object Oriented Programming Lecture No 06.
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
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.
Functions, Scope, and The Free Store Functions Functions must be declared by a function prototype before they are invoked, return_type Function_name(type,
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Structs and Classes Structs A struct can be used to define a data structure type as follows: struct Complex { double real, imag;} // specifying a Complex.
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 Classes struct Public and Private Parts of a struct Class Scope of a Class Overloading Member Functions Class in a Class Static Members of Classes this.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Memory Management.
Procedural and Object-Oriented Programming
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.
CISC181 Introduction to Computer Science Dr
Introduction to 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?
Chapter 15: Overloading and Templates
Pointers Revisited What is variable address, name, value?
Object-Oriented Programming Using C++
CSCE 210 Data Structures and Algorithms
Templates.
This pointer, Dynamic memory allocation, Constructors and Destructor
Using local variable without initialization is an error.
Static Data Member and Functions
Dynamic Memory Allocation
Scope, Parameter Passing, Storage Specifiers
Introduction to Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
7 Arrays.
Constant pointers and pointers to constants
CONSTRUCTORS AND DESRUCTORS
Chapter 15 Pointers, Dynamic Data, and Reference Types
Classes Short Review of Topics already covered Constructor
Classes and Objects.
PZ09A - Activation records
Object Oriented Programming Using C++
Linked Lists Chapter 4.
Today’s Topic Const Ref:
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
7 Arrays.
9-10 Classes: A Deeper Look.
Dynamic Memory.
Submitted By : Veenu Saini Lecturer (IT)
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
CS410 – Software Engineering Lecture #5: C++ Basics III
Classes: A Deeper Look, Part 1
STATIC DATA MEMBER & MEMBER FUNCTIONS
Pointers and References
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Object Oriented Programming (OOP) Lecture No. 12
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
(4 – 2) Introduction to Classes in C++
Presentation transcript:

Structs and Classes Static Class Members Recall the following A static global object (i.e., an object with a file scope) has no external linkage, i.e., it cannot be in any other file A static local object (i.e., an object with a function or a block scope) is not destroyed upon function or block exit A static data member of a class is shared by all instantiated objects of the class (all objects of the class share a single instance of the static data member) Class X { int j; // every object of class X have have its own instance of j static int I; // A static data member is declared, only one instance of I // will be defined and shared by all objects of class X. } // the static member I is only declared above and must be defined in a .cpp // file before it can be used (in X.cpp or in main_program.cpp) as follows

Structs and Classes Static Class Members(cont.) int X::I = 10;//the instance of I is defined and initialized, if the member is not // initialized, it will be initialized to zero by default main(){ X a,b,c; // instantiate three objects of type X a.j = 100; b.j = 10; c.j = 5; // assume j is in the public section of X a.I = 10; // also assumes s I is public in X, //the above is the same as b.I=10; or c.I=10 since I is shared by all objects X::I=100;// the class name can also be used to reference I } X::I j = 100 j = 10 j = 5 a b c

Structs and Classes Static Class Members(cont.) can be used for 1. Storing constant values or structures used by objects of the class 2. Passing data from any object of the class to other objects of the class 3. Storing results computer by collaboration among objects of the class The above considerably reduce the need for global variables Example: Fixed lenght vectors Class Vector{……. static const int lenght; // the declaratoin of length …..} const int Vector::length = 100;// the definition of length Vector::Vector(int n = length) {…….} The above shows that a static data member can appear as a default argument of a member function

Structs and Classes Static Class Members(cont.) Example: Counting the number of objects class X{… static int count; public: X(){…..count++;…..} static int get_count(){return count;}// a static member function is a // function which may only access static data members } main(){ X a,b,c; cout << “no. of objects created =\t” << X::get_count(); // Note a static member function can be invoked using the class name

Structs and Classes Static Class Members(cont.) A static data member can be an object of the class in which it is a member Class X{…. X* ptr; // OK X& ref; // OK X obj; // Error, a data member of a class can not have the class type static X obj; // OK since obj is a static data member } Example:The human race class class Human{…. static Human Adam, Eve; // these static objects are shared by all objects // of this class A pointer to a static data member is defined as follows Human * ptr_to_Adam = & Human::Adam;

Structs and Classes The this pointer A special pointer used in member functions of a class as a pointer to the object instance for which the member function was invoked. Class X { …. int I; public: int get_I(){return I;}// I can be referenced by this->I X increment_I(){ I++; return (*this);}// the pointer is implicitly declared // in every member of the class by X* const this } void f(X a, X b){… int I = a.get_I(); int j = b.get_I(); cout << a.increment_I().get_I(); }// a.increment returns back the instance of object a // Member functions utilizing “this” must be non-static

Structs and Classes The this pointer (cont.) A doubly linked list head prev next Class Dlink { void* item; Dlink* prev; Dlink* next; public: void insert_before_me(Dlink* p){ p->next = this; p->prev = prev; prev->next = p; prev = p; } void append_to_me(Dlink* p){p->next = next; p->prev = this; next->prev = p; next = p;} }; Dlink* head = new Dlink; viod fun(Dlink* x, Dlink* y){ head->insert_before_me(x); // will this work? // what changes needed to be done in the member function insert_before_me()? head->append_to_me(y); //will this work? // what changes needed in member function append_to_me()