SlideShare a Scribd company logo
MORE ON CLASSES
AND OBJECTS
Chapter 4
Data Members
Types of data member :

   Constant data members.
   Mutable data members.
   Static data members.
Member Functions
Different types of member functions:
 Nested Member functions.

 Overloaded member functions.

 Constant member function.

 Member functions with default arguments.

 Inline member functions.

 Static member functions.
Constant data members
 The data members whose value cannot be
  changed throughout the execution of the
  program.
 They are declared by preceding the qualifier
  const.
Example:
const int x = 10;
Example:
#include<iostream>
void main()
{
const int x = 10;
x++;      // error
cout<< x<<endl;
}
Mutable data members
   If the need arises such that the constant
    member functions has to modify the value of
    the data members then the data member has
    to be declared by prefixing the keyword
    „mutable‟.
Example:
#include<iostream>
class x
{
    int a ;
    mutable int b;
public:
    void xyz() const
{
a++;                   // error
b++;                              // legal
}
};
void main()
{
X x2;
X2.xyz();
}
Constant member function
#include<iostream.h>
                       void main()
class x
                       {
{                      x x1;
                       x1.getdata(56);
int a;
                       Cout<< x1.setdata()<<endl;
public:                }
void getdata(int x)
{
a=x;
}
int setdata() const
{
a++; // error
return a;
}
};
Static data member
   Those members whose members are
    accessed by all the objects of a class.
   It is not own by any object of a class.
   Only one copy of a static data member is
    created for a class which can be accessed by
    all the objects of that class.
Example:
#include<iostream.h>
class x                void main()
                       {
{                      x x1,x2;
static int a;          x1.display();
                       x2.display();
int b;                 x1.getdata(1);
                       x2.getdata(2);
public:                x1.display();
                       x2.display();
void getdata(int x)    }
{
                       Output:
                       0
b=x;                   0
a++;                   2
                       2
}
void display(void)
{
cout<< a<< endl;
}
};
int x :: a;
Static member function
#include<iostream.h>
                             void main()
class sample
                             {
{                            sample s1,s2;
                             Sample :: getdata(1)//invoking static member function
static int a;
                             s1.display();
                             s2.getdata(2);// invoking static member function using object
                             s2.display();
public:                      }
                             Output:
                             1
Static void getdata(int x)
                             2

{

a=x;
}
void display(void)
{
cout<< a<< endl;
}
};
Int sample :: a;
Nested member function
#include<iostream>
                                     void main()
class sample                         {
{                                    sample e;
                                     t =e.get_data (34);
       int x;                        cout<< t << endl;
public:                               }
                                     Output:
       void get_data(int);           Nested member function
       void message(char *);         34
};
int sample :: get_data(int a)
{
x=a;
message(“Nested member function”);
return x;
}
void sample :: message(char *s)
{
cout<< s<< endl;
}
Overloaded member function
                           With single class:
Class A
{                                               Void main()
Public:                                         {
                                                A a1;
void display(void);                             a1.display(void);
void display(int);                              a1.display(20);
                                                }
};                                              Output:
void a :: display(void)                         Hello
{                                               20

cout<< “Hello”<< endl;
}
void a :: display(int d)
{
cout<<d<< endl;
}
Overloaded member function
      Two different classes.
Class A                        Void main()
{                              {
                               A a1;
Public:
                               B b1;
void display(void);            a1.display(void);
};                             b1.display(void);
Class B                        }
                               Output:
{                              Hello
Public:                        World
void display(void);
};


void A :: display(void)
{
cout<< “Hello”<< endl;
}
void B :: display(void)
{
cout<<“World”<< endl;
}
Member functions with default
arguments
#include<iostream>
class addition
{
Public:
     void add(int, int = 2);
};
void addition :: add(int a, int b)
{
return(a+b);
}
Void main()
{
addition a;
a.add(5,6);
a.add(6);
}
Output
11
8
Inline function
Class test
{
                                                  void main()
private :
                                                  {
               int a;                             test t;
               int b;                             int a,b;
public:                                           cout<<“enter the two numbers” <<
                                                  endl;
               void set_data(int , int )
                                                  cin>> a >> b;
              int big()                      //   t.set_data(a,b);
automatic inline function                         cout<<“the largest number is ” <<
               {                                  t.big() << endl;
               if (a > b)                         }
                              return a;
               else
                              return b;
               }
};
inline void test :: set_data(int x, int y)
               {
                              a=x;
                              b=y;
               }
Friend function
  To provide non-member function to access
   private data member of a class, c++ allow the
   non-member to be made friend of that class.
 Syntax:

friend<data_type> <func_name>();
Example
Friend non-member function:

Class sample
{
Int a;
Public:
Friend void change(sample &);
};
Void change(sample &x)
{
x.a= 5;
}
Void main()
{
Sample A;
Change(A);
}
Example
    Friend member function:
                                               Void test :: set_data(sample &a, int b)
Class sample; // forward declaration.          {
Class test                                     a.x= b;
                                               }
{                                              Int sample :: get_data(void)
Public:                                        {
                                               Return x;
Void set_data(sample &, int);                  }
};                                             Void main()
                                               {
Class sample                                   Sample e;
{                                              Test f;
                                               f.set_data(5);
Private:                                       Cout<< e.get_data()<< endl;
Int x;                                         }
Public:
Int get_data(void);
Friend void test :: set_data(sample &, int);
};
Friend class
  A class is made friend of another class. For
   example,
   If a class X is a friend of class Y then all the
   member function of class X can access the
   private data member of class Y.
Declaration:
friend class X;
Example of friend class
# include<iostream>
Class Y;                            void Y :: change_data(X &c, int p, int
                                    q)
Class X                             {
{                                   c.X = p;
                                    c.Y = q;
Int x,y;
                                    }
Public:                             void X :: show_data()
Void show_data();                   {
                                    Cout<< x << y << endl;
friend class Y;                     }
};                                  Int main()
                                    {
Class Y                             X x1;
{                                   Y y1;
                                    Y1.change_data(x1,5,6);
Public:
                                    x1.show_data();
void change_data( X &, int, int);   return 0;
};                                  }
Array of class objects
  Array of class objects is similar to the array of
   structures.
 Syntax:

Class <class_name>
{
// class body
};
<class_name><object_name[size]>;
Example:
Class Employee
{
Char name[20];
Float salary;
Public:
Void getdata();
Void display();
};
Employee e[5];
Passing object to functions
   By value
   By reference
   By pointer
By value
Here only the copy of the object is passed to the
 function definition.
The modification on objects made in the called
 function will not be reflected in the calling
 function.
By reference
Here when the object is passed to the function
 definition, the formal argument shares the
 memory location of the actual arguments.
Hence the modification on objects made in the
 called function will be reflected in the calling
 function.
By pointer
Here pointer to the object is passed. The
 member of the objects passed are accessed
 by the arrow operator(->).
The modification on objects made in the called
 function will be reflected in the calling function.
Example
#include<iostream>                         void set(A *z,int t)
                                           {
class A                                    z->a = t;
{                                          }
int a;                                     int main()
public:                                    {
                                           A a1;
void set(A, int); // call by value         a1.a = 10;
void set(int, A &); // call by reference   cout<<a;
                                           a1.set(a1,5);// by value
void set(A *, int); // call by pointer
                                           cout<<a;
};                                         a1.set(20,a1);// by reference
                                           cout<<a;
void set(A x,int p)
                                           a1.set(&a1,30);// by pointer
{                                          cout<<a;
                                           }
x.a = p;
}                                          Output:
                                           10
void set(int q, A &y)
                                           10
{                                          20
                                           30
y.a = q;
}
Nested class
   Class within a class
   Example:
    class A
    {
    // class body
         class B
         {
         // inner class body
         };
    }

More Related Content

What's hot

Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)
RichardWarburton
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsPrincess Sam
 
Introduction to Julia Language
Introduction to Julia LanguageIntroduction to Julia Language
Introduction to Julia Language
Diego Marinho de Oliveira
 
OOP Core Concept
OOP Core ConceptOOP Core Concept
OOP Core Concept
Rays Technologies
 
Lo Mejor Del Pdc2008 El Futrode C#
Lo Mejor Del Pdc2008 El Futrode C#Lo Mejor Del Pdc2008 El Futrode C#
Lo Mejor Del Pdc2008 El Futrode C#
Juan Pablo
 
OOP v3
OOP v3OOP v3
OOP v3
Sunil OS
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondMario Fusco
 
classes & objects in cpp overview
classes & objects in cpp overviewclasses & objects in cpp overview
classes & objects in cpp overview
gourav kottawar
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorJussi Pohjolainen
 
Class method
Class methodClass method
Class method
kamal kotecha
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScript
tmont
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and Operators
Sunil OS
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Gandhi Ravi
 
Tutconstructordes
TutconstructordesTutconstructordes
TutconstructordesNiti Arora
 
Introduction to julia
Introduction to juliaIntroduction to julia
Introduction to julia
岳華 杜
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
Upender Upr
 
C# Generics
C# GenericsC# Generics
C# Generics
Rohit Vipin Mathews
 

What's hot (20)

Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
 
Introduction to Julia Language
Introduction to Julia LanguageIntroduction to Julia Language
Introduction to Julia Language
 
OOP Core Concept
OOP Core ConceptOOP Core Concept
OOP Core Concept
 
Lo Mejor Del Pdc2008 El Futrode C#
Lo Mejor Del Pdc2008 El Futrode C#Lo Mejor Del Pdc2008 El Futrode C#
Lo Mejor Del Pdc2008 El Futrode C#
 
OOP v3
OOP v3OOP v3
OOP v3
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 
Op ps
Op psOp ps
Op ps
 
Constructor
ConstructorConstructor
Constructor
 
classes & objects in cpp overview
classes & objects in cpp overviewclasses & objects in cpp overview
classes & objects in cpp overview
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
Class method
Class methodClass method
Class method
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScript
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and Operators
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
 
Tutconstructordes
TutconstructordesTutconstructordes
Tutconstructordes
 
Introduction to julia
Introduction to juliaIntroduction to julia
Introduction to julia
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
C# Generics
C# GenericsC# Generics
C# Generics
 

Similar to More on Classes and Objects

class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
C++ prgms 3rd unit
C++ prgms 3rd unitC++ prgms 3rd unit
C++ prgms 3rd unit
Ananda Kumar HN
 
Presentation on template and exception
Presentation  on template and exceptionPresentation  on template and exception
Presentation on template and exception
Sajid Alee Mosavi
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.ppt
instaface
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
Jay Patel
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
STX Next
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projects
Bartosz Kosarzycki
 
C++11
C++11C++11
OBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptOBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .ppt
SaadAsim11
 
C++ prgms 4th unit Inheritance
C++ prgms 4th unit InheritanceC++ prgms 4th unit Inheritance
C++ prgms 4th unit Inheritance
Ananda Kumar HN
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
ROHIT JAISWAR
 
662305 10
662305 10662305 10
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
Abu Saleh
 
C++.pptx
C++.pptxC++.pptx

Similar to More on Classes and Objects (20)

class and objects
class and objectsclass and objects
class and objects
 
C++ prgms 3rd unit
C++ prgms 3rd unitC++ prgms 3rd unit
C++ prgms 3rd unit
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
C++ programs
C++ programsC++ programs
C++ programs
 
Presentation on template and exception
Presentation  on template and exceptionPresentation  on template and exception
Presentation on template and exception
 
Pre zen ta sion
Pre zen ta sionPre zen ta sion
Pre zen ta sion
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.ppt
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projects
 
C++11
C++11C++11
C++11
 
OBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptOBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .ppt
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
 
C++ prgms 4th unit Inheritance
C++ prgms 4th unit InheritanceC++ prgms 4th unit Inheritance
C++ prgms 4th unit Inheritance
 
Test Engine
Test EngineTest Engine
Test Engine
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
 
Lecture05
Lecture05Lecture05
Lecture05
 
662305 10
662305 10662305 10
662305 10
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 

Recently uploaded

Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 

Recently uploaded (20)

Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 

More on Classes and Objects

  • 1. MORE ON CLASSES AND OBJECTS Chapter 4
  • 2. Data Members Types of data member :  Constant data members.  Mutable data members.  Static data members.
  • 3. Member Functions Different types of member functions:  Nested Member functions.  Overloaded member functions.  Constant member function.  Member functions with default arguments.  Inline member functions.  Static member functions.
  • 4. Constant data members  The data members whose value cannot be changed throughout the execution of the program.  They are declared by preceding the qualifier const. Example: const int x = 10;
  • 5. Example: #include<iostream> void main() { const int x = 10; x++; // error cout<< x<<endl; }
  • 6. Mutable data members  If the need arises such that the constant member functions has to modify the value of the data members then the data member has to be declared by prefixing the keyword „mutable‟.
  • 7. Example: #include<iostream> class x { int a ; mutable int b; public: void xyz() const { a++; // error b++; // legal } }; void main() { X x2; X2.xyz(); }
  • 8. Constant member function #include<iostream.h> void main() class x { { x x1; x1.getdata(56); int a; Cout<< x1.setdata()<<endl; public: } void getdata(int x) { a=x; } int setdata() const { a++; // error return a; } };
  • 9. Static data member  Those members whose members are accessed by all the objects of a class.  It is not own by any object of a class.  Only one copy of a static data member is created for a class which can be accessed by all the objects of that class.
  • 10. Example: #include<iostream.h> class x void main() { { x x1,x2; static int a; x1.display(); x2.display(); int b; x1.getdata(1); x2.getdata(2); public: x1.display(); x2.display(); void getdata(int x) } { Output: 0 b=x; 0 a++; 2 2 } void display(void) { cout<< a<< endl; } }; int x :: a;
  • 11. Static member function #include<iostream.h> void main() class sample { { sample s1,s2; Sample :: getdata(1)//invoking static member function static int a; s1.display(); s2.getdata(2);// invoking static member function using object s2.display(); public: } Output: 1 Static void getdata(int x) 2 { a=x; } void display(void) { cout<< a<< endl; } }; Int sample :: a;
  • 12. Nested member function #include<iostream> void main() class sample { { sample e; t =e.get_data (34); int x; cout<< t << endl; public: } Output: void get_data(int); Nested member function void message(char *); 34 }; int sample :: get_data(int a) { x=a; message(“Nested member function”); return x; } void sample :: message(char *s) { cout<< s<< endl; }
  • 13. Overloaded member function With single class: Class A { Void main() Public: { A a1; void display(void); a1.display(void); void display(int); a1.display(20); } }; Output: void a :: display(void) Hello { 20 cout<< “Hello”<< endl; } void a :: display(int d) { cout<<d<< endl; }
  • 14. Overloaded member function Two different classes. Class A Void main() { { A a1; Public: B b1; void display(void); a1.display(void); }; b1.display(void); Class B } Output: { Hello Public: World void display(void); }; void A :: display(void) { cout<< “Hello”<< endl; } void B :: display(void) { cout<<“World”<< endl; }
  • 15. Member functions with default arguments #include<iostream> class addition { Public: void add(int, int = 2); }; void addition :: add(int a, int b) { return(a+b); } Void main() { addition a; a.add(5,6); a.add(6); } Output 11 8
  • 16. Inline function Class test { void main() private : { int a; test t; int b; int a,b; public: cout<<“enter the two numbers” << endl; void set_data(int , int ) cin>> a >> b; int big() // t.set_data(a,b); automatic inline function cout<<“the largest number is ” << { t.big() << endl; if (a > b) } return a; else return b; } }; inline void test :: set_data(int x, int y) { a=x; b=y; }
  • 17. Friend function  To provide non-member function to access private data member of a class, c++ allow the non-member to be made friend of that class.  Syntax: friend<data_type> <func_name>();
  • 18. Example Friend non-member function: Class sample { Int a; Public: Friend void change(sample &); }; Void change(sample &x) { x.a= 5; } Void main() { Sample A; Change(A); }
  • 19. Example  Friend member function: Void test :: set_data(sample &a, int b) Class sample; // forward declaration. { Class test a.x= b; } { Int sample :: get_data(void) Public: { Return x; Void set_data(sample &, int); } }; Void main() { Class sample Sample e; { Test f; f.set_data(5); Private: Cout<< e.get_data()<< endl; Int x; } Public: Int get_data(void); Friend void test :: set_data(sample &, int); };
  • 20. Friend class  A class is made friend of another class. For example, If a class X is a friend of class Y then all the member function of class X can access the private data member of class Y. Declaration: friend class X;
  • 21. Example of friend class # include<iostream> Class Y; void Y :: change_data(X &c, int p, int q) Class X { { c.X = p; c.Y = q; Int x,y; } Public: void X :: show_data() Void show_data(); { Cout<< x << y << endl; friend class Y; } }; Int main() { Class Y X x1; { Y y1; Y1.change_data(x1,5,6); Public: x1.show_data(); void change_data( X &, int, int); return 0; }; }
  • 22. Array of class objects  Array of class objects is similar to the array of structures.  Syntax: Class <class_name> { // class body }; <class_name><object_name[size]>;
  • 23. Example: Class Employee { Char name[20]; Float salary; Public: Void getdata(); Void display(); }; Employee e[5];
  • 24. Passing object to functions  By value  By reference  By pointer
  • 25. By value Here only the copy of the object is passed to the function definition. The modification on objects made in the called function will not be reflected in the calling function.
  • 26. By reference Here when the object is passed to the function definition, the formal argument shares the memory location of the actual arguments. Hence the modification on objects made in the called function will be reflected in the calling function.
  • 27. By pointer Here pointer to the object is passed. The member of the objects passed are accessed by the arrow operator(->). The modification on objects made in the called function will be reflected in the calling function.
  • 28. Example #include<iostream> void set(A *z,int t) { class A z->a = t; { } int a; int main() public: { A a1; void set(A, int); // call by value a1.a = 10; void set(int, A &); // call by reference cout<<a; a1.set(a1,5);// by value void set(A *, int); // call by pointer cout<<a; }; a1.set(20,a1);// by reference cout<<a; void set(A x,int p) a1.set(&a1,30);// by pointer { cout<<a; } x.a = p; } Output: 10 void set(int q, A &y) 10 { 20 30 y.a = q; }
  • 29. Nested class  Class within a class  Example: class A { // class body class B { // inner class body }; }