Object Oriented Programming (OOP) Lecture No. 12

Slides:



Advertisements
Similar presentations
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Advertisements

 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Object Oriented Programming (OOP) Lecture No. 11.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Classes: user-defined types. Organizing method with a class A class is used to organize methods * Methods that compute Mathematical functions * The Scanner.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
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?
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Object Oriented Programming (OOP) Lecture No.2 Downloaded From:
Eine By: Avinash Reddy 09/29/2016.
Procedural and Object-Oriented Programming
Classes (Part 1) Lecture 3
2 Chapter Classes & Objects.
Class and Objects UNIT II.
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?
By Muhammad Waris Zargar
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.
Review What is an object? What is a class?
Object Oriented Programming (OOP) Lecture No. 8
Final and Abstract Classes
Object-Oriented Programming (OOP) Lecture No. 21
Review: Two Programming Paradigms
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?
Object Lifetime and Dynamic Objects
Road Map Introduction to object oriented programming. Classes
Class: Special Topics Copy Constructors Static members Friends this
This technique is Called “Divide and Conquer”.
C++ for Engineers and Scientists Second Edition
C++ Classes & Object Oriented Programming
More Object Oriented Programming
CS212: Object Oriented Analysis and Design
Object Oriented Programming (OOP) Lecture No. 9
Learning Objectives Classes Constructors Principles of OOP
CONSTRUCTORS AND DESRUCTORS
Constructors and Other Tools
Dr. Bhargavi Dept of CS CHRIST
Local Variables, Global Variables and Variable Scope
Classes and Objects.
Static in Classes CSCE 121 J. Michael Moore.
Object Oriented Programming Using C++
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
Object oriented programming (OOP) Lecture No. 7
Dynamic Memory.
C Programming Lecture-13 Structures
Submitted By : Veenu Saini Lecturer (IT)
CS410 – Software Engineering Lecture #5: C++ Basics III
CPS120: Introduction to Computer Science
Final and Abstract Classes
Object Oriented Programming (OOP) Lecture No. 11
Object oriented programming (OOP) Lecture No. 6
Types of Computer Languages
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Classes Member Qualifiers
Chapter 7 Objects and Classes
SPL – PS3 C++ Classes.
Presentation transcript:

Object Oriented Programming (OOP) Lecture No. 12

Review Constant data members Constant objects Static data members

Static Data Member Definition “A variable that is part of a class, yet is not part of an object of that class, is called static data member”

Static Data Member They are shared by all instances of the class They do not belong to any particular instance of a class

Class vs. Instance Variable Student s1, s2, s3; Class Space s1(rollNo,…) s2(rollNo,…) s3(rollNo,…) Instance Variable Class Variable

Static Data Member (Syntax) Keyword static is used to make a data member static class ClassName{ … static DataType VariableName; };

Defining Static Data Member Static data member is declared inside the class But they are defined outside the class

Defining Static Data Member class ClassName{ … static DataType VariableName; }; DataType ClassName::VariableName;

Initializing Static Data Member Static data members should be initialized once at file scope They are initialized at the time of definition

Example class Student{ private: static int noOfStudents; public: … }; int Student::noOfStudents = 0; /*private static member cannot be accessed outside the class except for initialization*/

Initializing Static Data Member If static data members are not explicitly initialized at the time of definition then they are initialized to 0

Example int Student::noOfStudents; is equivalent to

Accessing Static Data Member To access a static data member there are two ways Access like a normal data member Access using a scope resolution operator ‘::’

Example class Student{ public: static int noOfStudents; }; int Student::noOfStudents; int main(){ Student aStudent; aStudent.noOfStudents = 1; Student::noOfStudents = 1; }

Life of Static Data Member They are created even when there is no object of a class They remain in memory even when all objects of a class are destroyed

Example class Student{ public: static int noOfStudents; }; int Student::noOfStudents; int main(){ Student::noOfStudents = 1; }

Example class Student{ public: static int noOfStudents; }; int Student::noOfStudents; int main(){ { Student aStudent; aStudent.noOfStudents = 1; } Student::noOfStudents = 1;

Uses They can be used to store information that is required by all objects, like global variables

Example Modify the class Student such that one can know the number of student created in a system

Example class Student{ … public: static int noOfStudents; Student(); }; int Student::noOfStudents = 0;

Example Student::Student(){ noOfStudents++; } Student::~Student(){

Example Output: 1 2 int Student::noOfStudents = 0; int main(){ cout <<Student::noOfStudents <<endl; Student studentA; Student studentB; } Output: 1 2

Problem noOfStudents is accessible outside the class Bad design as the local data member is kept public

Static Member Function Definition: “The function that needs access to the members of a class, yet does not need to be invoked by a particular object, is called static member function”

Static Member Function They are used to access static data members Access mechanism for static member functions is same as that of static data members They cannot access any non-static members

Example class Student{ static int noOfStudents; int rollNo; public: static int getTotalStudent(){ return noOfStudents; } }; int main(){ int i = Student::getTotalStudents();

Accessing non static data members int Student::getTotalStudents(){ return rollNo; } int main(){ int i = Student::getTotalStudents(); /*Error: There is no instance of Student, rollNo cannot be accessed*/

this Pointer this pointer is passed implicitly to member functions this pointer is not passed to static member functions Reason is static member functions cannot access non static data members

Global Variable vs. Static Members Alternative to static member is to use global variable Global variables are accessible to all entities of the program Against information hiding

Array of Objects Array of objects can only be created if an object can be created without supplying an explicit initializer There must always be a default constructor if we want to create array of objects

Example class Test{ public: }; int main(){ Test array[2]; // OK }

Example class Test{ public: Test(); }; int main(){ Test array[2]; // OK }

Example class Test{ public: Test(int i); }; int main(){ Test array[2]; // Error }

Example class Test{ public: Test(int i); } int main(){ Test array[2] = {Test(0),Test(0)};

Example class Test{ public: Test(int i); } int main(){ Test a(1),b(2); Test array[2] = {a,b};