SlideShare una empresa de Scribd logo
1 de 42
Basic Of C++ , Classes and Objects




            Presented By:
         Shashank Srivastava


                       31 July,2012 onwards
Structure of a Program




2                            16 August 2012
Structure of a Program
     #include
     <iostream>


    - Lines beginning with a hash sign (#) are directives
      for the preprocessor.
    - In this case the directive #include<iostream> tells
      the preprocessor to include the iostream standard
      file.
    - This specific file (iostream) includes the declarations
      of the basic standard input-output library in C++.


3                                                  16 August 2012
Structure of a Program
     using namespace
     std;


    - All the elements of the standard C++ library are declared
      within what is called a namespace, the namespace with
      the name std.
     int main()



      -    This line corresponds to the beginning of the definition of the
      main function.
       - The main function is the point by where all C++ programs start
      their execution, independently of its location within the source code.
4                                                               16 August 2012
Structure of a Program
     cout<<“Hello World”;


       cout is the name of the standard output stream in C++, and the
      meaning of the entire statement is to insert a sequence of
      characters into the standard output stream (cout, which usually
      corresponds to the screen).

       cout is declared in the iostream standard file within
      return 0;
      the std namespace



    The return statement causes the main function to finish.
5                                                              16 August 2012
Basic Structure of a Program( Without Class)




6                                          16 August 2012
Basic input/output in C++
           Standard Output

    By default, the standard output of a program is the screen, and the
    C++ stream object defined to access it is cout.



     cout is used in conjunction with the insertion operator, which is
    written as << (two "less than" signs).




7                                                            16 August 2012
Basic input/output in C++
              Standard Input

        The standard input device is usually the keyboard. Handling the
      standard input in C++ is done by applying the overloaded operator
      of extraction (>>) on the cin stream.
         The operator must be followed by the variable that will store the
      data that is going to be extracted from the stream. For example:




8                                                              16 August 2012
Introduction
     A class is an extension of the idea of structure used in ‘C’.


     It is a new way of creating and implementing user defined data

      types.



                Structure : Member are public(By Default)
                Class:    Members are private (By Default)




9                                                                 16 August 2012
Class specification
      When we are creating a class, we are creating a new abstract data

         types that can be treated like any other built in data types.

        Class declaration describes the type and scope of its member.

        Class function definitions describe how the class functions are

         implemented?
                Class Specification=Class declaration + Class
                              function definition




10                                                                   16 August 2012
Class Declarations



                      Data Member




                          Member
                          Function




11                                   16 August 2012
Private and public
     The data is hidden so it will be safe from accidental manipulation,
       while the functions that operate on the data are public so they can
       be accessed from outside the class.




12                                                            16 August 2012
A Simple Class




     Data members are usually declared as private and member
                       functions as public

13                                                      16 August 2012
Object Creation
     Creates a variable s1 of type smallobj.

                smallobj s1 ;      // Memory for x is created.

     Note-:   In C++, the class variables are known as objects . Therefore
                          x is called an object of type smallobj.


                We may also declare more than one objects in a single
                                    statements.
                                Smallobj s1, s2, s3;



14                                                                    16 August 2012
Accessing Class members
     Object-name . function-name( actual
                arguments)




15                                         16 August 2012
Accessing Class members
     A variable declared as public can be accessed by the objects
                               directly.




16                                                         16 August 2012
Accessing Class members
     A variable declared as public can be accessed by the objects
                               directly.




17                                                         16 August 2012
Defining Member function

              Defining
              Member
              Function


     Outside the      Inside the
        class            class
      definition       definition

18                                  16 August 2012
Member Functions Within Class Definition
       We can define the member function with the class to replace the
     function declaration by the actual function definition inside the class.




                                                                      Inline
                                                                    function




19                                                                16 August 2012
Member Functions outside Class Definition
       Function that are declared inside a class have to be defined
                       separately outside the class.




20                                                            16 August 2012
Member Functions outside Class Definition( implementation)




21                                                      16 August 2012
Member Functions :Some Special characteristics

      Several different classes can use the same function name.
       Membership level will resolve the issue.
      Membership function can access the private data of the class. A
       non member function can not do so.
                                - Friend function is the exception of this
      A member function can call another member function directly
       without using dot operator.




22                                                               16 August 2012
A C++ Program with a class




23                                16 August 2012
A C++ Program with a class(Multiple Data)




24                                               16 August 2012
C++ objects as a data type




25                                16 August 2012
Nested Function


     A member function can be called by using its
       name inside another function declaration




26                                            16 August 2012
Private member function


     Normal practices:
     To place all the data items in a private section
     and all the functions in public section



       But some situations may require certain functions to
                           be hidden




27                                                       16 August 2012
Private member function(Cont…)

     A private member function can only be called by
      another function that is a member of its class.




       Note: Even an object cannot invoke a private
           member function using dot operator




28                                              16 August 2012
Private member function(Cont…)




            If S1 is an object of class sample, then
     S1.read(); //illegal , object can not access the private
                             member




29                                                    16 August 2012
Private member function(Cont…)

     te




          Function read() can be called by the function update()
                        to update the value of m.

30                                                     16 August 2012
Memory allocation for objects

      Memory space for object is allocated when they are declared and
       not when the class is specified.

      Member functions are created and placed in the memory space
       only once when they are defined as apart of class specification.

      Since all the objects belonging to that class use the same member
       functions, no    separate space is allocated for member functions
       when the objects are created.

      Only space for the member variable is allocated separately for each
       object

31                                                              16 August 2012
Memory allocation for objects

                Member Function1                Memory
                                                Created
                                              when function
                Member Function2               is defined


     Object 1                      Object 2

                                   Var1
      Var1

      Var2                         Var2




32                                               16 August 2012
Static Data members

      In general, each object contains its own separate data.


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

      A static data item is useful when all objects of the same class must
       share a common item of information

      Static member variable is visible only within the class, but its
       lifetime is the entire program.



33                                                                16 August 2012
Static Data members(Exp1 &Exp2)




     Statdata.cpp           Statdata2.cp
                                 p




34                                         16 August 2012
Static Data members
        Note:


      The Type and scope of each static member variable must be
       defined outside the class definition.



      Because the static data members are stored separately rather than
       as a part of an object.

      Static variables are normally used to maintain values common to
       the entire class



35                                                           16 August 2012
Static data member

     Object 1                   Object 2

      Var1                       Var1


       Var2                        Var2




                  Static data        Common to all
                   variable           the objects




36                                              16 August 2012
Static function

      A static function can have access to only other static members (
       functions or variables) declared in the same class .

      A static member function can be called using the class name (
       instead of its objects)

                          Class-name :: function-name;


      However it can be called by objects of the class also.
      Static member can also be defined in the private region of a class
       also.


37                                                              16 August 2012
Static function (Ex.)




38                           16 August 2012
Objects as function arguments

     It is possible to have functions which accepts objects of a class
       as arguments, just as there are functions which accept other
                          variables as arguments.


           Pass-by-value                         Pass-by-reference
     A copy of entire object is             Only the address of the
       passed to the function                objects is passed to the
     Any modification made to                        function.
        the object inside the               Any changes made to the
     function is not reflected in           object inside the function is
      the object used to call a                reflected in the actual
               function                                object.




39                                                               16 August 2012
Objects as function arguments(Ex.)




40                                        16 August 2012
41   16 August 2012
42   16 August 2012

Más contenido relacionado

La actualidad más candente

How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
Syed Faizan Hassan
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
Majid Saeed
 
2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++
Jeff TUYISHIME
 

La actualidad más candente (20)

Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
class c++
class c++class c++
class c++
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Class or Object
Class or ObjectClass or Object
Class or Object
 
C++ classes
C++ classesC++ classes
C++ classes
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan college
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Object and class
Object and classObject and class
Object and class
 
2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
Lecture 4. mte 407
Lecture 4. mte 407Lecture 4. mte 407
Lecture 4. mte 407
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 

Destacado

C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
FALLEE31188
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array Pointer
Tareq Hasan
 

Destacado (12)

Big Data Visualization With ParaView
Big Data Visualization With ParaViewBig Data Visualization With ParaView
Big Data Visualization With ParaView
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Php, mysq lpart5(mysql)
Php, mysq lpart5(mysql)Php, mysq lpart5(mysql)
Php, mysq lpart5(mysql)
 
Pointer in c++ part3
Pointer in c++ part3Pointer in c++ part3
Pointer in c++ part3
 
OOP
OOPOOP
OOP
 
Pointer in c++ part2
Pointer in c++ part2Pointer in c++ part2
Pointer in c++ part2
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Array in c++
Array in c++Array in c++
Array in c++
 
Pointer in c++ part1
Pointer in c++ part1Pointer in c++ part1
Pointer in c++ part1
 
c++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programc++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ program
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array Pointer
 

Similar a Classes and objects till 16 aug

Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)
Asfand Hassan
 
Android coding guide lines
Android coding guide linesAndroid coding guide lines
Android coding guide lines
lokeshG38
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
Jay Patel
 
Getting Started with Android Application Development
Getting Started with Android Application DevelopmentGetting Started with Android Application Development
Getting Started with Android Application Development
Asanka Indrajith
 
C++ largest no between three nos
C++ largest no between three nosC++ largest no between three nos
C++ largest no between three nos
krismishra
 
Object oriented fundamentals_in_java
Object oriented fundamentals_in_javaObject oriented fundamentals_in_java
Object oriented fundamentals_in_java
Self
 

Similar a Classes and objects till 16 aug (20)

Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Chapter 1- Introduction.ppt
Chapter 1- Introduction.pptChapter 1- Introduction.ppt
Chapter 1- Introduction.ppt
 
OOP_chapter _1.pptx
OOP_chapter _1.pptxOOP_chapter _1.pptx
OOP_chapter _1.pptx
 
Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)
 
Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
 
iOS development introduction
iOS development introduction iOS development introduction
iOS development introduction
 
My c++
My c++My c++
My c++
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
Class and object
Class and objectClass and object
Class and object
 
Android coding guide lines
Android coding guide linesAndroid coding guide lines
Android coding guide lines
 
C++
C++C++
C++
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
 
Getting Started with Android Application Development
Getting Started with Android Application DevelopmentGetting Started with Android Application Development
Getting Started with Android Application Development
 
C++ Notes
C++ NotesC++ Notes
C++ Notes
 
Test02
Test02Test02
Test02
 
C++ largest no between three nos
C++ largest no between three nosC++ largest no between three nos
C++ largest no between three nos
 
Dojo Toolkit from a Flex developer's perspective
Dojo Toolkit from a Flex developer's perspectiveDojo Toolkit from a Flex developer's perspective
Dojo Toolkit from a Flex developer's perspective
 
Object oriented fundamentals_in_java
Object oriented fundamentals_in_javaObject oriented fundamentals_in_java
Object oriented fundamentals_in_java
 

Último

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
vu2urc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
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
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
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
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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...
 
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
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

Classes and objects till 16 aug

  • 1. Basic Of C++ , Classes and Objects Presented By: Shashank Srivastava 31 July,2012 onwards
  • 2. Structure of a Program 2 16 August 2012
  • 3. Structure of a Program #include <iostream> - Lines beginning with a hash sign (#) are directives for the preprocessor. - In this case the directive #include<iostream> tells the preprocessor to include the iostream standard file. - This specific file (iostream) includes the declarations of the basic standard input-output library in C++. 3 16 August 2012
  • 4. Structure of a Program using namespace std; - All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std. int main() - This line corresponds to the beginning of the definition of the main function. - The main function is the point by where all C++ programs start their execution, independently of its location within the source code. 4 16 August 2012
  • 5. Structure of a Program cout<<“Hello World”; cout is the name of the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters into the standard output stream (cout, which usually corresponds to the screen). cout is declared in the iostream standard file within return 0; the std namespace The return statement causes the main function to finish. 5 16 August 2012
  • 6. Basic Structure of a Program( Without Class) 6 16 August 2012
  • 7. Basic input/output in C++ Standard Output By default, the standard output of a program is the screen, and the C++ stream object defined to access it is cout. cout is used in conjunction with the insertion operator, which is written as << (two "less than" signs). 7 16 August 2012
  • 8. Basic input/output in C++ Standard Input  The standard input device is usually the keyboard. Handling the standard input in C++ is done by applying the overloaded operator of extraction (>>) on the cin stream.  The operator must be followed by the variable that will store the data that is going to be extracted from the stream. For example: 8 16 August 2012
  • 9. Introduction  A class is an extension of the idea of structure used in ‘C’.  It is a new way of creating and implementing user defined data types. Structure : Member are public(By Default) Class: Members are private (By Default) 9 16 August 2012
  • 10. Class specification  When we are creating a class, we are creating a new abstract data types that can be treated like any other built in data types.  Class declaration describes the type and scope of its member.  Class function definitions describe how the class functions are implemented? Class Specification=Class declaration + Class function definition 10 16 August 2012
  • 11. Class Declarations Data Member Member Function 11 16 August 2012
  • 12. Private and public The data is hidden so it will be safe from accidental manipulation, while the functions that operate on the data are public so they can be accessed from outside the class. 12 16 August 2012
  • 13. A Simple Class Data members are usually declared as private and member functions as public 13 16 August 2012
  • 14. Object Creation Creates a variable s1 of type smallobj. smallobj s1 ; // Memory for x is created. Note-: In C++, the class variables are known as objects . Therefore x is called an object of type smallobj. We may also declare more than one objects in a single statements. Smallobj s1, s2, s3; 14 16 August 2012
  • 15. Accessing Class members Object-name . function-name( actual arguments) 15 16 August 2012
  • 16. Accessing Class members A variable declared as public can be accessed by the objects directly. 16 16 August 2012
  • 17. Accessing Class members A variable declared as public can be accessed by the objects directly. 17 16 August 2012
  • 18. Defining Member function Defining Member Function Outside the Inside the class class definition definition 18 16 August 2012
  • 19. Member Functions Within Class Definition We can define the member function with the class to replace the function declaration by the actual function definition inside the class. Inline function 19 16 August 2012
  • 20. Member Functions outside Class Definition Function that are declared inside a class have to be defined separately outside the class. 20 16 August 2012
  • 21. Member Functions outside Class Definition( implementation) 21 16 August 2012
  • 22. Member Functions :Some Special characteristics  Several different classes can use the same function name. Membership level will resolve the issue.  Membership function can access the private data of the class. A non member function can not do so. - Friend function is the exception of this  A member function can call another member function directly without using dot operator. 22 16 August 2012
  • 23. A C++ Program with a class 23 16 August 2012
  • 24. A C++ Program with a class(Multiple Data) 24 16 August 2012
  • 25. C++ objects as a data type 25 16 August 2012
  • 26. Nested Function A member function can be called by using its name inside another function declaration 26 16 August 2012
  • 27. Private member function Normal practices: To place all the data items in a private section and all the functions in public section But some situations may require certain functions to be hidden 27 16 August 2012
  • 28. Private member function(Cont…) A private member function can only be called by another function that is a member of its class. Note: Even an object cannot invoke a private member function using dot operator 28 16 August 2012
  • 29. Private member function(Cont…) If S1 is an object of class sample, then S1.read(); //illegal , object can not access the private member 29 16 August 2012
  • 30. Private member function(Cont…) te Function read() can be called by the function update() to update the value of m. 30 16 August 2012
  • 31. Memory allocation for objects  Memory space for object is allocated when they are declared and not when the class is specified.  Member functions are created and placed in the memory space only once when they are defined as apart of class specification.  Since all the objects belonging to that class use the same member functions, no separate space is allocated for member functions when the objects are created.  Only space for the member variable is allocated separately for each object 31 16 August 2012
  • 32. Memory allocation for objects Member Function1 Memory Created when function Member Function2 is defined Object 1 Object 2 Var1 Var1 Var2 Var2 32 16 August 2012
  • 33. Static Data members  In general, each object contains its own separate data.  If a data item in a class is declared as static , only one such item is created for the entire class, no matter how many objects there are.  A static data item is useful when all objects of the same class must share a common item of information  Static member variable is visible only within the class, but its lifetime is the entire program. 33 16 August 2012
  • 34. Static Data members(Exp1 &Exp2) Statdata.cpp Statdata2.cp p 34 16 August 2012
  • 35. Static Data members Note:  The Type and scope of each static member variable must be defined outside the class definition.  Because the static data members are stored separately rather than as a part of an object.  Static variables are normally used to maintain values common to the entire class 35 16 August 2012
  • 36. Static data member Object 1 Object 2 Var1 Var1 Var2 Var2 Static data Common to all variable the objects 36 16 August 2012
  • 37. Static function  A static function can have access to only other static members ( functions or variables) declared in the same class .  A static member function can be called using the class name ( instead of its objects) Class-name :: function-name;  However it can be called by objects of the class also.  Static member can also be defined in the private region of a class also. 37 16 August 2012
  • 38. Static function (Ex.) 38 16 August 2012
  • 39. Objects as function arguments It is possible to have functions which accepts objects of a class as arguments, just as there are functions which accept other variables as arguments. Pass-by-value Pass-by-reference A copy of entire object is Only the address of the passed to the function objects is passed to the Any modification made to function. the object inside the Any changes made to the function is not reflected in object inside the function is the object used to call a reflected in the actual function object. 39 16 August 2012
  • 40. Objects as function arguments(Ex.) 40 16 August 2012
  • 41. 41 16 August 2012
  • 42. 42 16 August 2012