SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
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
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
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
 

Kürzlich hochgeladen (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
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
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
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
 

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