Classes Static Members

Slides:



Advertisements
Similar presentations
EC-241 Object-Oriented Programming
Advertisements

Abstract Data Type Fraction Example
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Dale Roberts Object Oriented Programming using Java - Inheritance Constructors Dale Roberts, Lecturer Computer Science, IUPUI
Fall 2005CSE 115/503 Introduction to Computer Science I1 Composition details Recall, composition involves 3 things: –Declaration of instance variable of.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
Dale Roberts 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Introduction to Java - Access Specifiers Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Dale Roberts Program Control using Java - Boolean Expressions Dale Roberts, Lecturer Computer Science, IUPUI Department of.
Dale Roberts Object Oriented Programming using Java - Enumerations Dale Roberts, Lecturer Computer Science, IUPUI Department.
Dale Roberts 1 Classes Constructors & Destructors Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 1Winter Quarter C++: I/O and Classes Lecture 25.
Dale Roberts Object Oriented Programming using Java - OOD to OOP: ATM Case Study Dale Roberts, Lecturer Computer Science, IUPUI
Object Oriented Programming (OOP) Lecture No. 11.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Information Representation: Negative Integer Representation.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Introduction to Programming Lecture 40. Class Class is a user defined data type.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Introduction to Java - Input, Program Control and Instantiation Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Object Oriented Programming using Java - Getters and Setters Dale Roberts, Lecturer Computer Science, IUPUI
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Dale Roberts 1 Operator Overloading Member Functions Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Object-Oriented Programming (OOP) Lecture No. 24.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Object Oriented Programming using Java - Composition
GUI Programming using Java - Key Events
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Functions Examples CSCI 230
Abstract Data Types Polynomials CSCI 240
Object Oriented Programming using Java - Class Instance Variables
Variable Declarations, Data types, Expressions
Variable Declarations, Data types, Expressions
Abstract Data Types Sparse Matrices CSCI 240
Scope, Parameter Passing, Storage Specifiers
Classes Access Specifiers
Program Control using Java - Theory
Advanced Programming Basics
Fraction Abstract Data Type
Negative Integer Representation
Dale Roberts, Lecturer IUPUI
Code Organization Classes
Classes Copy Constructors
Department of Computer and Information Science, School of Science, IUPUI CSCI 265 Classes Dale Roberts, Lecturer Computer Science, IUPUI
Pointers Call-by-Reference CSCI 230
Object-Oriented Programming (OOP) Lecture No. 25
Introduction to Programming
Functions Divide and Conquer
Analysis of Algorithms Growth Rates
Dale Roberts, Lecturer IUPUI
Analysis of Algorithms Big-Omega and Big-Theta
Characters and Strings Functions
Department of Computer and Information Science, School of Science, IUPUI Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI
Classes Introduction CSCI 240
Code Organization Classes
Classes Class Data Members & Initializers
Classes Member Qualifiers
Structures Declarations CSCI 230
Presentation transcript:

Classes Static Members Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Classes Static Members Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

Static Members Data and Functions Can be static Exists Independently of Any Instantiated Objects

Static Data Members COMMON to All the Objects of that Class Exist Even When NO Class Objects Exist Definition for static Data Members is a Declaration in header file Initialization ONLY by an Initializer or an Assignment Statement inside a static member function Accessing -- class_name::data_name or object_name.data_name

Static Data Members -- Example // student.h #define size 20 class student{ char *name, *ss; static char *instructor; public: student(char *, char *); ~student(); };

Static Data Members – Example (cont) // student.cpp #include “student.h” #include <iostream> student::student(char *ip_name, char *ip_ss){ name = new char[size]; ss = new char[size]; strcpy(name, ip_name); strcpy(ss, ip_ss);} void student::print(){ cout << "Name: " << name << endl; cout << "SS: " << ss << endl; cout << "Instructor: " << instructor << endl;} }; /* Definition and Initialization of Static member */ char *student::instructor = "Henry";

Static Data Members -- Example // client.cpp #include “student.h” main() { student s1("John", "012345678"); student s2("Tom", "999999999"); s1.print(); s2.print(); } Name: John SS: 012345678 Instructor: Henry Name: Tom SS: 999999999 Instructor: Henry  

Static Member Functions COMMON to All Objects of the Class Created by the Class Definition -- Exist Even when NO Class Objects Exist Can Only Access Static Data Members of the Class or other static member functions Accessing -- class_name::function_name() or object_name.function_name()

Static Member Functions -- Example // student.h #define size 20 class student{ char *name; char *ss; static char *instructor; public: student(char *ip_name, char *ip_ss); static void print_instructor(); };

Static Member Functions – Example (cont) // student.cpp #include “student.h” #include <iostream> student::student(char *ip_name, char *ip_ss){ name = new char[size]; ss = new char[size]; strcpy(name, ip_name); strcpy(ss, ip_ss);} static void student::print_instructor() { cout << "Instructor: " << instructor << endl;} /* Definition and Initialization of Static member */ char *student::instructor = "Henry";

Static Member Functions – Example (cont) // client.cpp #include “student.h” main() { student::print_instructor(); } Output will be: Instructor: Henry Notice that no instance of student is required.

Acknowledgements These slides were originally prepared by Rajeev Raje, modified by Dale Roberts.