SlideShare une entreprise Scribd logo
1  sur  24
CSC103 – Object Oriented
     Programming

                         Lecture 16

                      5th May, 2011


                  Engr. M. Anwar-ul-Haq
Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad   1
                                                                          1
Classes, Objects, and Memory
• Since now we know that each object created
  from a class contains separate copies of that
  class’s data and member functions.

• This is a good first approximation, since it
  emphasizes that objects are complete, self–
  contained entities, designed using the class
  declaration.

       Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                                 2
Classes, Objects, and Memory
• It’s true that each object has its own separate data items. On
  the other hand, contrary to what you may have been led to
  believe, all the objects in a given class use the same member
  functions.

• The member functions are created and placed in memory
  only once—when they are defined in the class declaration.
  This makes sense; there’s really no point in duplicating all the
  member functions in a class every time you create another
  object of that class, since the functions for each object are
  identical.


         Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                                   3
Classes, Objects, and Memory




  Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                            4
Classes, Objects, and Memory
• In most situations you don’t need to know that there
  is only one member function for an entire class.

• It’s simpler to visualize each object as containing
  both its own data and its own member functions.

• But in some situations, such as in estimating the size
  of an executing program, it’s helpful to know what’s
  happening behind the scenes.



        Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                                  5
Static Class Data
• Each object contains its own separate data,
  we must now amend that slightly.

• If a data item in a class is declared as static,
  then only one such item is created for the
  entire class, no matter how many objects
  there are.


       Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                                 6
Static Class Data
• A member variable defined as static is visible only
  within the class, but its lifetime is the entire
  program.

• It continues to exist even if there are no items of the
  class.

• static class member data is used to share information
  among the objects of a class.


        Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                                  7
Uses of Static Class Data
class X
{
     public:
       static int i;
};
     int X::i = 0; // definition outside class declaration



           Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                                     8
Uses of Static Class Data
• Classes can contain static member data and
  member functions.

• When a data member is declared as static,
  only one copy of the data is maintained for all
  objects of the class.



       Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                                 9
Uses of Static Class Data
class foo
{
    private:
           static int count;                      //only one data item for all objects
                                                  //note: *declaration* only!
    public:
              foo()                               //increments count when object created
              { count++; }
              int getcount()                      //returns count
              { return count; }
};
int foo::count = 0;                               //*definition* of count

int main()
{
     foo f1, f2, f3;                               //create three objects
     cout << “count is ” << f1.getcount() << endl; //each object
     cout << “count is ” << f2.getcount() << endl; //sees the
     cout << “count is ” << f3.getcount() << endl; //same value
    return 0;

}
              Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                                           10
Uses of Static Class Data
• The class foo in this example has one data item,
  count, which is type static int. The constructor for
  this class causes count to be incremented.

• In main() we define three objects of class foo. Since
  the constructor is called three times, count is
  incremented three times.

• Another member function, getcount(), returns the
  value in count.


        Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                                  11
Uses of Static Class Data




• Static class variables are not used as often as ordinary non–static variables, but they
are important in many situations.
            Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                                       12
Uses of Static Class Data




Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                          13
Uses of Static Class Data
• Static member data requires an unusual format.
  Ordinary variables are declared (the compiler is told
  about their name and type) and defined (the
  compiler sets aside memory to hold the variable) in
  the same statement.

• Static member data, on the other hand, requires two
  separate statements. The variable’s declaration
  appears in the class declaration, but the variable is
  actually defined outside the class, in much the same
  way as an external variable.

        Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                                  14
Uses of Static Class Data
• Putting the definition of static member data outside
  of the class also serves to emphasize that the
  memory space for such data is allocated only once,
  before the program starts to execute.

• One static member variable is accessed by an entire
  class; each object does not have its own version of
  the variable, as it would with ordinary member data.
  In this way a static member variable is more like a
  global variable.

        Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                                  15
Uses of Static Class Data
• It’s easy to handle static data incorrectly, and the
  compiler is not helpful about such errors.

• If you include the declaration of a static variable but
  forget its definition, there will be no warning from
  the compiler.

• Everything looks fine until you get to the linker.


        Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                                  16
const and Classes
• const used on normal variables to prevent
  them from being modified.

• A const member function guarantees that it
  will never modify any of its class’s member
  data.



       Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                                 17
Example: const and Classes




The non–const function nonFunc() can modify member data alpha, but the constant
function conFunc() can’t.
If it tries to, a compiler error results.

          Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                                    18
const and Classes
• Member functions that do nothing but acquire data
  from an object are obvious candidates for being
  made const, because they don’t need to modify any
  data.

• Making a function const helps the compiler flag
  errors, and tells anyone looking at the listing that you
  intended the function not to modify anything in its
  object.


       Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                                 19
Example: const and Classes




Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                          20
Example: const and Classes




Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                      OOP Fall 2010, FUIEMS, Rawalpindi                   21
const
• A compiler error is generated if you attempt
  to modify any of the data in the object for
  which this constant function was called.




       Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                                 22
const Objects




Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                          23
const Objects
• Non–const functions, such as getdist(), which
  gives the object a new value obtained from
  the user, are illegal. In this way the compiler
  enforces the const value of football.

• Make const any function that does not modify
  any of the data in its object.



       Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad
                                                                                 24

Contenu connexe

Tendances

Object Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionObject Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionPritom Chaki
 
Introduction to oop
Introduction to oop Introduction to oop
Introduction to oop Kumar
 
OOP Unit 1 - Foundation of Object- Oriented Programming
OOP Unit 1 - Foundation of Object- Oriented ProgrammingOOP Unit 1 - Foundation of Object- Oriented Programming
OOP Unit 1 - Foundation of Object- Oriented Programmingdkpawar
 
Object Oriented Programming in Java _lecture 1
Object Oriented Programming in Java _lecture 1Object Oriented Programming in Java _lecture 1
Object Oriented Programming in Java _lecture 1Mahmoud Alfarra
 
Cs8392 u1-1-oop intro
Cs8392 u1-1-oop introCs8392 u1-1-oop intro
Cs8392 u1-1-oop introRajasekaran S
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAhmed Nobi
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSkillwise Group
 
Std 12 computer chapter 6 object oriented concepts (part 1)
Std 12 computer chapter 6 object oriented concepts (part 1)Std 12 computer chapter 6 object oriented concepts (part 1)
Std 12 computer chapter 6 object oriented concepts (part 1)Nuzhat Memon
 
Lecture01 object oriented-programming
Lecture01 object oriented-programmingLecture01 object oriented-programming
Lecture01 object oriented-programmingHariz Mustafa
 
Object oriented concepts with java
Object oriented concepts with javaObject oriented concepts with java
Object oriented concepts with javaishmecse13
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming languageMd.Al-imran Roton
 
1 unit (oops)
1 unit (oops)1 unit (oops)
1 unit (oops)Jay Patel
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and InterfaceHaris Bin Zahid
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingAmit Soni (CTFL)
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops conceptsNilesh Dalvi
 

Tendances (20)

Object Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionObject Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaion
 
Introduction to oop
Introduction to oop Introduction to oop
Introduction to oop
 
OOP Unit 1 - Foundation of Object- Oriented Programming
OOP Unit 1 - Foundation of Object- Oriented ProgrammingOOP Unit 1 - Foundation of Object- Oriented Programming
OOP Unit 1 - Foundation of Object- Oriented Programming
 
Object Oriented Programming in Java _lecture 1
Object Oriented Programming in Java _lecture 1Object Oriented Programming in Java _lecture 1
Object Oriented Programming in Java _lecture 1
 
Cs8392 u1-1-oop intro
Cs8392 u1-1-oop introCs8392 u1-1-oop intro
Cs8392 u1-1-oop intro
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPT
 
Std 12 computer chapter 6 object oriented concepts (part 1)
Std 12 computer chapter 6 object oriented concepts (part 1)Std 12 computer chapter 6 object oriented concepts (part 1)
Std 12 computer chapter 6 object oriented concepts (part 1)
 
Oop ppt
Oop pptOop ppt
Oop ppt
 
Lecture01 object oriented-programming
Lecture01 object oriented-programmingLecture01 object oriented-programming
Lecture01 object oriented-programming
 
Java Object Oriented Programming
Java Object Oriented Programming Java Object Oriented Programming
Java Object Oriented Programming
 
Object oriented concepts with java
Object oriented concepts with javaObject oriented concepts with java
Object oriented concepts with java
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oop
 
1 unit (oops)
1 unit (oops)1 unit (oops)
1 unit (oops)
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
 
OOP java
OOP javaOOP java
OOP java
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
 
Oops
OopsOops
Oops
 

Similaire à oop Lecture 16

Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesIntroduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesDigitalDsms
 
Architecting Smarter Apps with Entity Framework
Architecting Smarter Apps with Entity FrameworkArchitecting Smarter Apps with Entity Framework
Architecting Smarter Apps with Entity FrameworkSaltmarch Media
 
A seminar report on core java
A  seminar report on core javaA  seminar report on core java
A seminar report on core javaAisha Siddiqui
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interviewKuntal Bhowmick
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxethiouniverse
 
Object Oriented Programming All Unit Notes
Object Oriented Programming All Unit NotesObject Oriented Programming All Unit Notes
Object Oriented Programming All Unit NotesBalamuruganV28
 
Java interview questions and answers for cognizant By Data Council Pune
Java interview questions and answers for cognizant By Data Council PuneJava interview questions and answers for cognizant By Data Council Pune
Java interview questions and answers for cognizant By Data Council PunePankaj kshirsagar
 

Similaire à oop Lecture 16 (20)

Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Summer Training.pptx
Summer Training.pptxSummer Training.pptx
Summer Training.pptx
 
Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesIntroduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book Notes
 
Simple Singleton Java
Simple Singleton JavaSimple Singleton Java
Simple Singleton Java
 
Java unit 7
Java unit 7Java unit 7
Java unit 7
 
JAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdfJAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdf
 
Architecting Smarter Apps with Entity Framework
Architecting Smarter Apps with Entity FrameworkArchitecting Smarter Apps with Entity Framework
Architecting Smarter Apps with Entity Framework
 
CS3391 OOP UT-I T4 JAVA BUZZWORDS.pptx
CS3391 OOP UT-I T4 JAVA BUZZWORDS.pptxCS3391 OOP UT-I T4 JAVA BUZZWORDS.pptx
CS3391 OOP UT-I T4 JAVA BUZZWORDS.pptx
 
A seminar report on core java
A  seminar report on core javaA  seminar report on core java
A seminar report on core java
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
 
JAVA PPT -3 BY ADI.pdf
JAVA PPT -3 BY ADI.pdfJAVA PPT -3 BY ADI.pdf
JAVA PPT -3 BY ADI.pdf
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
Java Basics
Java BasicsJava Basics
Java Basics
 
javaopps concepts
javaopps conceptsjavaopps concepts
javaopps concepts
 
Object Oriented Programming All Unit Notes
Object Oriented Programming All Unit NotesObject Oriented Programming All Unit Notes
Object Oriented Programming All Unit Notes
 
CS3391 -OOP -UNIT – I NOTES FINAL.pdf
CS3391 -OOP -UNIT – I  NOTES FINAL.pdfCS3391 -OOP -UNIT – I  NOTES FINAL.pdf
CS3391 -OOP -UNIT – I NOTES FINAL.pdf
 
Java_Interview Qns
Java_Interview QnsJava_Interview Qns
Java_Interview Qns
 
Java interview questions and answers for cognizant By Data Council Pune
Java interview questions and answers for cognizant By Data Council PuneJava interview questions and answers for cognizant By Data Council Pune
Java interview questions and answers for cognizant By Data Council Pune
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 

Dernier

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 

Dernier (20)

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 

oop Lecture 16

  • 1. CSC103 – Object Oriented Programming Lecture 16 5th May, 2011 Engr. M. Anwar-ul-Haq Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 1 1
  • 2. Classes, Objects, and Memory • Since now we know that each object created from a class contains separate copies of that class’s data and member functions. • This is a good first approximation, since it emphasizes that objects are complete, self– contained entities, designed using the class declaration. Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 2
  • 3. Classes, Objects, and Memory • It’s true that each object has its own separate data items. On the other hand, contrary to what you may have been led to believe, all the objects in a given class use the same member functions. • The member functions are created and placed in memory only once—when they are defined in the class declaration. This makes sense; there’s really no point in duplicating all the member functions in a class every time you create another object of that class, since the functions for each object are identical. Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 3
  • 4. Classes, Objects, and Memory Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 4
  • 5. Classes, Objects, and Memory • In most situations you don’t need to know that there is only one member function for an entire class. • It’s simpler to visualize each object as containing both its own data and its own member functions. • But in some situations, such as in estimating the size of an executing program, it’s helpful to know what’s happening behind the scenes. Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 5
  • 6. Static Class Data • Each object contains its own separate data, we must now amend that slightly. • If a data item in a class is declared as static, then only one such item is created for the entire class, no matter how many objects there are. Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 6
  • 7. Static Class Data • A member variable defined as static is visible only within the class, but its lifetime is the entire program. • It continues to exist even if there are no items of the class. • static class member data is used to share information among the objects of a class. Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 7
  • 8. Uses of Static Class Data class X { public: static int i; }; int X::i = 0; // definition outside class declaration Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 8
  • 9. Uses of Static Class Data • Classes can contain static member data and member functions. • When a data member is declared as static, only one copy of the data is maintained for all objects of the class. Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 9
  • 10. Uses of Static Class Data class foo { private: static int count; //only one data item for all objects //note: *declaration* only! public: foo() //increments count when object created { count++; } int getcount() //returns count { return count; } }; int foo::count = 0; //*definition* of count int main() { foo f1, f2, f3; //create three objects cout << “count is ” << f1.getcount() << endl; //each object cout << “count is ” << f2.getcount() << endl; //sees the cout << “count is ” << f3.getcount() << endl; //same value return 0; } Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 10
  • 11. Uses of Static Class Data • The class foo in this example has one data item, count, which is type static int. The constructor for this class causes count to be incremented. • In main() we define three objects of class foo. Since the constructor is called three times, count is incremented three times. • Another member function, getcount(), returns the value in count. Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 11
  • 12. Uses of Static Class Data • Static class variables are not used as often as ordinary non–static variables, but they are important in many situations. Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 12
  • 13. Uses of Static Class Data Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 13
  • 14. Uses of Static Class Data • Static member data requires an unusual format. Ordinary variables are declared (the compiler is told about their name and type) and defined (the compiler sets aside memory to hold the variable) in the same statement. • Static member data, on the other hand, requires two separate statements. The variable’s declaration appears in the class declaration, but the variable is actually defined outside the class, in much the same way as an external variable. Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 14
  • 15. Uses of Static Class Data • Putting the definition of static member data outside of the class also serves to emphasize that the memory space for such data is allocated only once, before the program starts to execute. • One static member variable is accessed by an entire class; each object does not have its own version of the variable, as it would with ordinary member data. In this way a static member variable is more like a global variable. Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 15
  • 16. Uses of Static Class Data • It’s easy to handle static data incorrectly, and the compiler is not helpful about such errors. • If you include the declaration of a static variable but forget its definition, there will be no warning from the compiler. • Everything looks fine until you get to the linker. Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 16
  • 17. const and Classes • const used on normal variables to prevent them from being modified. • A const member function guarantees that it will never modify any of its class’s member data. Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 17
  • 18. Example: const and Classes The non–const function nonFunc() can modify member data alpha, but the constant function conFunc() can’t. If it tries to, a compiler error results. Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 18
  • 19. const and Classes • Member functions that do nothing but acquire data from an object are obvious candidates for being made const, because they don’t need to modify any data. • Making a function const helps the compiler flag errors, and tells anyone looking at the listing that you intended the function not to modify anything in its object. Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 19
  • 20. Example: const and Classes Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 20
  • 21. Example: const and Classes Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad OOP Fall 2010, FUIEMS, Rawalpindi 21
  • 22. const • A compiler error is generated if you attempt to modify any of the data in the object for which this constant function was called. Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 22
  • 23. const Objects Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 23
  • 24. const Objects • Non–const functions, such as getdist(), which gives the object a new value obtained from the user, are illegal. In this way the compiler enforces the const value of football. • Make const any function that does not modify any of the data in its object. Engr. Anwar, OOP Spring 2011, Foundation University (FUIEMS), Islamabad 24