SlideShare une entreprise Scribd logo
1  sur  26
Key Resource:
 Khalil Edappal
+91 98464 55192
Scope Resolution Operator

 Scope resolution operator is also used define and
 initialize the static data members of a class.

 The syntax for accessing a global variable using
 the scope resolution operator is as follows:

              : : GlobalVariableName
Local & Global Class n Objects
          Local                                Global
          1. Class definition occurs inside   1.Class definition
             the function body.               occurs outside the
                                              bodies of all functions
          2. Objects of this class can be in the program.
Classes   created only within the function.
                                              2.Objects of this class
                                              can be declared from
                                              anywhere in the
                                              program.
          1.Objects declared within the 1.Objects declared
            function.                         outside all the
                                              function.
          2.The objects are locally available
Objects                                       2.The objects are
          to the function. That is, they
          cannot be used outside the          globally available to all
          function.                           the function in the
                                              program.
Array of Objects

 It is a collection of objects of the same class type
  referenced under common name. It is declared with
  the usual syntax:
               className ArrayName [Size] ;
 A member of the class in accessed by specifying the
  object followed by the dot operator and member name
  as follows;
          ArrayName [Subcript]. memberName
Array of Objects

subcriber ob [50] ;

for (int i = 0; i < 50 ; i + +)
ob [i]. read_data ( ) ;
for (int i = 0; i < 50 ; i + +)
ob [i]. print_data ( ) ;
Objects as Function Arguments
 It is possible to have functions which object of classes as
    arguments, just as there are functions which accept other
    variable as arguments.
   Like any other data type, an object can be passed as an argument
    to the function by the following ways.
   Pass-by value, a copy of the entire object is passed to the
    function.
   Pass-by-reference, only the address of the object is passed
    implicitly to the function.
   In the case of pass-by-value, a copy of the object is passed to the
    function and any modification made to the object inside the
    function is not reflected in the object used to call the function.
   But in pass-by-reference the address of an object is passed to the
    function and any changes made to the object inside the function
    are reflected in the actual object.
Objects as Function Arguments

ob3.add(obl, ob2);
Returning Objects from Functions

 Even functions can return reference to the object if the
  function header has the format:

       ClassName & FunctionName (ArgumentList)
Examole: complex complex : : add (complex &dc)
            {
            complex temp ;
              temp.real=real+dc.real ;
              temp.imag=imag=iamg+dc.imag ;
             return temp ;
             }
Memory Allocation for Class and Objects

 When an object of a particular class is created, memory is
    allocated to both its data members and member function.
   Member functions are created and stored in memory only
    when a class specification is completed.
   All objects of that class have access to the same area in the
    memory where the member functions are stored.
   It is also logically true that the member functions are same
    for all the objects.
   However, separate storage is allocated for every object’s
    data members since they contain different values.
    It allows different objects to handle their data in a manner
    that suits them.
Memory Allocation for Class and Objects
Types of Functions in Classes

 Inline Function
 Friend Function
 Constant Member Function
 Static Member Functions
Inline Functions
 Inline function, the definition which begins with the
  keyword inline
 The inline functions are C++ enhancement designed to speed
  up the execution of programs.
 An inline member function is treated like a macro; any call to
  this function in a program is replaced by the function itself.
  This is called inline expansion.
 inline ReturnType ClassName : : FunctionName (Arguments)
        {
        Body_of Function
        }
Inline Functions
 If there is an inline function is the program the
  compiler replaces the function calling statement by
  the function code itself and compiles the entire code
  so that the program looks like a single function, which
  is stored in some other memory location and then
  jump back to the calling function.
 The disadvantage is in the case of memory wastage.
 That is, if an inline function is called ten times, there
  will be ten copies of the function code, which makes
  the program large in size and requires more memory.
Inline Functions
 For functions that return and are having a loop or a
  switch or a go to.
 For functions not returning values, if a return
  statement exists.
 For functions containing static variables.
 For recursive functions.
Friend Function
        It is required to allow functions outside a class to
    access and manipulate the private members of the class.
    In C++, this is achieved by using the concept of friends.
    The friends come in three forms: friend functions,
    member functions of other classes as friend, and friend
    class.
   Friend function is a non member function that is granted
    access to the private and protected members of a class.
   Now let us brief features of friend functions as follows:
   A function may be declared friend of more than one
    class.
   It does not have the class scope; rather it depends upon
    its original declaration and definition.
Friend Function

 A friend function cannot be called using the object of
  that class; it is not in the scope of a class.
 It can be invoked like a normal function. That is, it
  does not require that dot operator prefixed by the
  object.
 Since it is not a member function, it cannot access the
  class members directly but has to use the object name
  as the parameter and the membership operator (.)
  with each member name.
 It can be declared privately or publicly in the class
  without affecting its meaning.
Member Function of Class as Friend of another Class
Consider the following class definitions :
                       class PQR ; / / Prototyping of class PQR
                       class ABC / / Definition of class ABC
                         {      int a ;
                                float b:
                       public :
                                void abcprint(PQR) : / / friend of
                                                  PQR
                       class PQR
                                { int p ;
                                float q ;
                       public :
                                void pqrfun ( ) ;
                                friend void ABC : : abcprintint (PQR) ;
                                          / / prototype of friend
Friend Class
friend FormerClassName ;
The above class definitions can be modified as follows :
    class PQR ; / / Prototyping of class PQR is essential
    class ABC
              { int a ;
                flaot b ;
    public :
        int abcsum (PQR obl)
              {
              return (a + obl.p) ; / / private member of PQR is accessed by
                                                       function of ABC
              }
      in abcdif ( )
              {
              PQR ob2 ;
              return (b - ob2.q) ;
              }
    };
Class PQR
    {
int p ;
    float q ;
    public :
              void pqrfun ( ) ;
              friend ABC ; / / ABC is declared as friend of PQR
};
    Friend class is one, all the member functions of which are friends another class.
Friends as Bridge of Classes

Consider the following definitions:
  class PQR ;             / / Prototyping of the class PQR
  class ABC
        {         int a ;
                  float b :
        public :
        int r ;
        void pqrfun ( ) ;
        friend void com_fun (ABC, PQR) ;
  };
void com_ fun (ABC obl, PQR ob2)
  {
  cout<<obl.a * obl.b + ob2.p * ob2.q ;
}
Uses of Friend Functions

 Friend functions are useful in the following situations:

 Function operating on objects of two different classes.
  This is the ideal situation where the friend function
  can be used to bridge two classes.
 Friend functions can be used to increase the versatility
  of overloaded operators.
 Sometimes, a friend allows a more obvious syntax for
  calling a function, rather than what a member
  function can do.
Static Members of Class

 The declaration of such members begins with the keyword static.
Static Data Members
  Only one copy of such member will be maintained in the memory for
  the entire class, which will be shared by all the objects of that class.
  They are visible only within the class; however their lifetime is the
  entire program.
  The static data members declared as private can be a accessed within
  class by the static member function. (i.e; they are hidden from outside
  the class).
  The static data members declared as public can be accessed outside the
  class definition by using the following syntax:
                 ClassName : : StaticDataMember = Value ;
                                   Or
                 ObjectName. StaticDataMember = Value ;
Static Member Functions
  The following are the features of static member functions.
  They can access only the static data members of the same
  class. (That, is; non-static data members are unavailable to
  these functions)
  Only static member functions declared public can be
  invoked outside the class by using the following syntax :
       ClassName : : MemberFunction ( )
                       Or
       ObjectName. MemberFunction ( )
Constant Member Function

 If a member function does not alter any data in the
  class, then this member function may be declared as a
  constant member function using the keyword const as
  in the following format:
      ReturnType MemberFunctionName
  (ArgumentList) const ;
Structure v/s Classes

Structure                            Class
1.The keyword struct is used to      1.The keyword class in used in
 define.                              definition
2.The members are data elements      2.Both data and functions are used
 only and there is no member          members.
 function.                           3.Access labels sets the scope of
3.No access labels for the            members.
members.                             4.By default, the members are
4.The members are public by           private.
default.                             5.Variable declared with class are
5.Variable declared with structure    known as objects.
 tag is known as structure           6. There may be static members.
variable.
6. There is no static member.
Nested Classes
     A class declared inside another class is called nested class. The outer class is known as enclosing class.
     Consider the following class definition:
              Class outer
                           {
 int p ;
class inner                               / / nesting of a class
                                        {
 int q,r ;
                                        public :
                                        void prinfun ( )
                                                       {
cout<<q*r ;
}
                                                       } obl ;       / / object creation of nested class

                          public :
                                        void outprint ( )
                                        {
                                        obl. prinfun ( ) ;
                                        cout << p ;
                                        }
                          };

The objects of the nested class can be declared outside the enclosing class by specifying the full name of the
   nested class as :
   EnclosindgClassName : : NestedClassName ObjectName ;
   An object say ob would have been created outiside the clas outer with the expression.
                          outer : : inner ob ;
Never ENDS……………..

Contenu connexe

Tendances

Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Uzair Salman
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphismmcollison
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in javaMahmoud Ali
 
Method overloading, recursion, passing and returning objects from method, new...
Method overloading, recursion, passing and returning objects from method, new...Method overloading, recursion, passing and returning objects from method, new...
Method overloading, recursion, passing and returning objects from method, new...JAINAM KAPADIYA
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Javabackdoor
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packagesmanish kumar
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritancemanish kumar
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++Swarup Kumar Boro
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2Rakesh Madugula
 
Sdtl manual
Sdtl manualSdtl manual
Sdtl manualqaz8989
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Javabackdoor
 

Tendances (20)

Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Classes&amp;objects
Classes&amp;objectsClasses&amp;objects
Classes&amp;objects
 
Interface
InterfaceInterface
Interface
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in java
 
Method overloading, recursion, passing and returning objects from method, new...
Method overloading, recursion, passing and returning objects from method, new...Method overloading, recursion, passing and returning objects from method, new...
Method overloading, recursion, passing and returning objects from method, new...
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Java
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java unit2
Java unit2Java unit2
Java unit2
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritance
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
 
Sdtl manual
Sdtl manualSdtl manual
Sdtl manual
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 

Similaire à +2 CS class and objects

Classes & objects new
Classes & objects newClasses & objects new
Classes & objects newlykado0dles
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptmanomkpsg
 
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfismartshanker1
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsRai University
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objectsRai University
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismJawad Khan
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programmingnirajmandaliya
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)Durga Devi
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)nirajmandaliya
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++Mohamad Al_hsan
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++Swarup Boro
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3Atif Khan
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfstudy material
 

Similaire à +2 CS class and objects (20)

Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdf
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
 
My c++
My c++My c++
My 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
 
Class and object
Class and objectClass and object
Class and object
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 

Dernier

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 

Dernier (20)

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 

+2 CS class and objects

  • 1. Key Resource: Khalil Edappal +91 98464 55192
  • 2. Scope Resolution Operator  Scope resolution operator is also used define and initialize the static data members of a class.  The syntax for accessing a global variable using the scope resolution operator is as follows: : : GlobalVariableName
  • 3. Local & Global Class n Objects Local Global 1. Class definition occurs inside 1.Class definition the function body. occurs outside the bodies of all functions 2. Objects of this class can be in the program. Classes created only within the function. 2.Objects of this class can be declared from anywhere in the program. 1.Objects declared within the 1.Objects declared function. outside all the function. 2.The objects are locally available Objects 2.The objects are to the function. That is, they cannot be used outside the globally available to all function. the function in the program.
  • 4. Array of Objects  It is a collection of objects of the same class type referenced under common name. It is declared with the usual syntax: className ArrayName [Size] ;  A member of the class in accessed by specifying the object followed by the dot operator and member name as follows; ArrayName [Subcript]. memberName
  • 5. Array of Objects subcriber ob [50] ; for (int i = 0; i < 50 ; i + +) ob [i]. read_data ( ) ; for (int i = 0; i < 50 ; i + +) ob [i]. print_data ( ) ;
  • 6. Objects as Function Arguments  It is possible to have functions which object of classes as arguments, just as there are functions which accept other variable as arguments.  Like any other data type, an object can be passed as an argument to the function by the following ways.  Pass-by value, a copy of the entire object is passed to the function.  Pass-by-reference, only the address of the object is passed implicitly to the function.  In the case of pass-by-value, a copy of the object is passed to the function and any modification made to the object inside the function is not reflected in the object used to call the function.  But in pass-by-reference the address of an object is passed to the function and any changes made to the object inside the function are reflected in the actual object.
  • 7. Objects as Function Arguments ob3.add(obl, ob2);
  • 8. Returning Objects from Functions  Even functions can return reference to the object if the function header has the format: ClassName & FunctionName (ArgumentList) Examole: complex complex : : add (complex &dc) { complex temp ; temp.real=real+dc.real ; temp.imag=imag=iamg+dc.imag ; return temp ; }
  • 9. Memory Allocation for Class and Objects  When an object of a particular class is created, memory is allocated to both its data members and member function.  Member functions are created and stored in memory only when a class specification is completed.  All objects of that class have access to the same area in the memory where the member functions are stored.  It is also logically true that the member functions are same for all the objects.  However, separate storage is allocated for every object’s data members since they contain different values.  It allows different objects to handle their data in a manner that suits them.
  • 10. Memory Allocation for Class and Objects
  • 11. Types of Functions in Classes  Inline Function  Friend Function  Constant Member Function  Static Member Functions
  • 12. Inline Functions  Inline function, the definition which begins with the keyword inline  The inline functions are C++ enhancement designed to speed up the execution of programs.  An inline member function is treated like a macro; any call to this function in a program is replaced by the function itself. This is called inline expansion.  inline ReturnType ClassName : : FunctionName (Arguments) { Body_of Function }
  • 13. Inline Functions  If there is an inline function is the program the compiler replaces the function calling statement by the function code itself and compiles the entire code so that the program looks like a single function, which is stored in some other memory location and then jump back to the calling function.  The disadvantage is in the case of memory wastage.  That is, if an inline function is called ten times, there will be ten copies of the function code, which makes the program large in size and requires more memory.
  • 14. Inline Functions  For functions that return and are having a loop or a switch or a go to.  For functions not returning values, if a return statement exists.  For functions containing static variables.  For recursive functions.
  • 15. Friend Function  It is required to allow functions outside a class to access and manipulate the private members of the class. In C++, this is achieved by using the concept of friends. The friends come in three forms: friend functions, member functions of other classes as friend, and friend class.  Friend function is a non member function that is granted access to the private and protected members of a class.  Now let us brief features of friend functions as follows:  A function may be declared friend of more than one class.  It does not have the class scope; rather it depends upon its original declaration and definition.
  • 16. Friend Function  A friend function cannot be called using the object of that class; it is not in the scope of a class.  It can be invoked like a normal function. That is, it does not require that dot operator prefixed by the object.  Since it is not a member function, it cannot access the class members directly but has to use the object name as the parameter and the membership operator (.) with each member name.  It can be declared privately or publicly in the class without affecting its meaning.
  • 17. Member Function of Class as Friend of another Class Consider the following class definitions : class PQR ; / / Prototyping of class PQR class ABC / / Definition of class ABC { int a ; float b: public : void abcprint(PQR) : / / friend of PQR class PQR { int p ; float q ; public : void pqrfun ( ) ; friend void ABC : : abcprintint (PQR) ; / / prototype of friend
  • 18. Friend Class friend FormerClassName ; The above class definitions can be modified as follows : class PQR ; / / Prototyping of class PQR is essential class ABC { int a ; flaot b ; public : int abcsum (PQR obl) { return (a + obl.p) ; / / private member of PQR is accessed by function of ABC } in abcdif ( ) { PQR ob2 ; return (b - ob2.q) ; } }; Class PQR { int p ; float q ; public : void pqrfun ( ) ; friend ABC ; / / ABC is declared as friend of PQR }; Friend class is one, all the member functions of which are friends another class.
  • 19. Friends as Bridge of Classes Consider the following definitions: class PQR ; / / Prototyping of the class PQR class ABC { int a ; float b : public : int r ; void pqrfun ( ) ; friend void com_fun (ABC, PQR) ; }; void com_ fun (ABC obl, PQR ob2) { cout<<obl.a * obl.b + ob2.p * ob2.q ; }
  • 20. Uses of Friend Functions  Friend functions are useful in the following situations:  Function operating on objects of two different classes. This is the ideal situation where the friend function can be used to bridge two classes.  Friend functions can be used to increase the versatility of overloaded operators.  Sometimes, a friend allows a more obvious syntax for calling a function, rather than what a member function can do.
  • 21. Static Members of Class  The declaration of such members begins with the keyword static. Static Data Members Only one copy of such member will be maintained in the memory for the entire class, which will be shared by all the objects of that class. They are visible only within the class; however their lifetime is the entire program. The static data members declared as private can be a accessed within class by the static member function. (i.e; they are hidden from outside the class). The static data members declared as public can be accessed outside the class definition by using the following syntax: ClassName : : StaticDataMember = Value ; Or ObjectName. StaticDataMember = Value ;
  • 22. Static Member Functions The following are the features of static member functions. They can access only the static data members of the same class. (That, is; non-static data members are unavailable to these functions) Only static member functions declared public can be invoked outside the class by using the following syntax : ClassName : : MemberFunction ( ) Or ObjectName. MemberFunction ( )
  • 23. Constant Member Function  If a member function does not alter any data in the class, then this member function may be declared as a constant member function using the keyword const as in the following format:  ReturnType MemberFunctionName (ArgumentList) const ;
  • 24. Structure v/s Classes Structure Class 1.The keyword struct is used to 1.The keyword class in used in define. definition 2.The members are data elements 2.Both data and functions are used only and there is no member members. function. 3.Access labels sets the scope of 3.No access labels for the members. members. 4.By default, the members are 4.The members are public by private. default. 5.Variable declared with class are 5.Variable declared with structure known as objects. tag is known as structure 6. There may be static members. variable. 6. There is no static member.
  • 25. Nested Classes A class declared inside another class is called nested class. The outer class is known as enclosing class. Consider the following class definition: Class outer { int p ; class inner / / nesting of a class { int q,r ; public : void prinfun ( ) { cout<<q*r ; } } obl ; / / object creation of nested class public : void outprint ( ) { obl. prinfun ( ) ; cout << p ; } }; The objects of the nested class can be declared outside the enclosing class by specifying the full name of the nested class as : EnclosindgClassName : : NestedClassName ObjectName ; An object say ob would have been created outiside the clas outer with the expression. outer : : inner ob ;