狠狠撸

狠狠撸Share a Scribd company logo
2 Chapter
Classes & Objects
Structures in C
Example
Disadvantages of structure
?Standard C does not treat structure as an built-
in data type
?Suppose we have declared C1,C2,C3 as object
of above class at that time we can not perform
operation like
?C3=C1+C2
?Structure do not permit data hiding. All
structure members are public members.
Structures in C++
? C++ support all the features as defined in C, but
C++ has some expanded capabilities such as data
hiding, Inheritance.
? In C++ a structure can have both function and
variable as member(declaration)
? It also declare some members as private so that
they cannot accessed directly by external
function.
? C++ incorporates all these extension in another
user defined type known as Class.
What is Class in C++
? A class is a way to bind data and its associated
function together.
? It allows the data and function to be hidden, if
necessary.
? When defining class we are creating new abstract
data type that can be treated as other built-in
data type
Class specification has two parts:
1.Class declaration
2.Class function definition
The class declaration describes the type and
scope of its members, The class function
definition describe how the class function are
implemented
static member and static member fumctions.ppt
? The keyword class specifies, that what follows
is an abstract data of type class_name
? Body of a class is enclosed within braces and
terminated by semicolon.
? Class body contains declaration of variables
and functions
? These variabes are collectively called as class
members
? The keyword PUBLIC and PRIVATE are known
as visibility labels
? If we don’t specify this label by default they
are PRIVATE.
static member and static member fumctions.ppt
Example
Creating Object
Syntax
Class_name object_name;
Example: item x;
OR
item x,y,z //multiple object
We can also create object after class declaration as
we do for structures(i.e declaring after semicolon)
Defining member function
Member function can be defined in two places”
1.Outside the class definition
2.Inside the class definition
The function should perform the same task
irrespective of the place of definition.
Outside definition:
?An important difference between a member
function and normal function is that a member
function incorporates a membership ‘identity label’
in the header i.e class_name::
?This ‘label’ tells the compiler which class the
function belongs to.
Syntax
Example
The member function have special characteristics
that are often used in the program development.
Characteristics are:
?Several different classes can use the same
function name. the membership label will resolve
their scope.
?Member function can access private data of the
class
?The member function can call another member
function directly, without using the dot operator
Inside class definition:
?Another method of defining member function is
to replace the function declaration by the actual
function definition.
?When function is defined inside class is called as
inline function
?Normally small functions are defined inside a
class.
Example
Making outside function inline:
when we define member function outside the class
definition and still make it inline by just using the
qualifier inline in the header line of function
definition
Memory allocation for object
? The memory space for object is allocated when
they are declared and not when the class is
specified.
? This is only partly true the member funaction are
created and placed in the memory space olny once
when they are defined as a part of class
specification
? Since all the objects belonging to that class use the
same member function,no separate space is
allocated for member functions when the objects
static member and static member fumctions.ppt
Array of Object
? Array of a Variables that are of type class are
called array of object.
Syntax:
classname objectname[size];
Example:
employee e1[3];
Mememory allocation for array of
a object
Name
Age
Name
Age
Name
Age
EMP[0]
EMP[1]
EMP[2]
Object as function argument
Object can be used as function arguement and
this is done by two ways.
1. A copy of teh entire object is passed to the
function
2.Only the address of the object is transferred to
the function
1. A copy of teh entire object is passed to the
function
? The first method is called pass-by-value
? A copy of a object is passed to the function
any changes made to the object inside the
function do not affect the object used to call
the function
2.Only the address of the object is transferred
to the function
? The second method is called pass-by-
reference.
? When an address of the object is passed, the
called function directly work on actual object
used in the call.
? Any changes made to the object inside the
function will reflect in the actual object.
Static data members
Following are the characteristics of static
member
? It is initialized to zero when the first object of
its class is created. No other initialization is
permitted
? Only one copy of that member is crated for
the entire class and is shared by all the object
of that class, no matter how many objects are
created
? It is visible only within class, but its lifetime is
the entire program.
? Static variables are used to maintain values
common to the entire class.
Eg. This can be used as a counter that records
the occcurences of all the objects
Syntax:
static datatype variblename;
Eg:
static int a;
Static Member function
? Like a static member variable we can also
have static member function. A member
function declared with static keyword is
called as static member function. static
member function has following
characteristics:
1.Static function can have access to only other
static members/functions declared in same class
2.Static member function can be called using
class name(instead of object) as
Class_name:: function_name;
Friend Function
? We have seen that private members can't be
accessed from outside class. Non member
function cannot have access to the private
data of a class.
? There would be situation where we would
like two classes to share particular function.
Eg consider two classes manager and scientist.
We like to use a function income_tax() to
operate on object of both d classes.
? C++ allows the common function to be made
friendly with the both the classes to have
access to the private data of these classes
? To make an outside function “friendly” to a
class, we have to simply declare this function
as a friend function of that class.
Syntax:
Friend returntype function_name(arguements);
Eg:
Friend void add();
? The function that is declared with the
keyword friend is known as friend function
? This function can be defined else where in
the program.
? The function definition does not use either
keyword friend or the scope operator::
? A function can be declared as a friend in any
number of classes.
? A friend function although not a member
function, has full access right to the private
members of the class
A friend function has certain special
characteristics:
1. It is not in the scope of class to which it has
been declared as friend
2. Since it is not in the scope of class, it cannot
be us called using object of that class
3. It can be invoked like a normal function
without the help of any object.
4. Unlike member function it can’t access
member names directly and has to use an
object name and dot membership operator
with each member name
? It can be declared either in the public or
private part of a class without affecting its
meaning
Ad

Recommended

Class object
Class object
Dr. Anand Bihari
?
22 scheme OOPs with C++ BCS306B_module1.pdf
22 scheme OOPs with C++ BCS306B_module1.pdf
sindhus795217
?
Class and object
Class and object
prabhat kumar
?
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
manomkpsg
?
Implementation of oop concept in c++
Implementation of oop concept in c++
Swarup Boro
?
cpp class unitdfdsfasadfsdASsASass 4.ppt
cpp class unitdfdsfasadfsdASsASass 4.ppt
nandemprasanna
?
Class objects oopm
Class objects oopm
Shweta Shah
?
Classes in C++ computer language presentation.ppt
Classes in C++ computer language presentation.ppt
AjayLobo1
?
APL-2-classes and objects.ppt
APL-2-classes and objects.ppt
srividyal2
?
class c++
class c++
vinay chauhan
?
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.
Enam Khan
?
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
Dr. SURBHI SAROHA
?
APL-2-classes and objects.ppt data structures using c++
APL-2-classes and objects.ppt data structures using c++
ProfLSrividya
?
DS Unit 6.ppt
DS Unit 6.ppt
JITTAYASHWANTHREDDY
?
Class and object in C++
Class and object in C++
rprajat007
?
classes & objects.ppt
classes & objects.ppt
BArulmozhi
?
Classes and objects
Classes and objects
Shailendra Veeru
?
Object and class presentation
Object and class presentation
nafisa rahman
?
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
?
Mca 2nd sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
Rai University
?
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
Rai University
?
+2 CS class and objects
+2 CS class and objects
khaliledapal
?
Classes and objects
Classes and objects
Anil Kumar
?
cse l 5.pptx
cse l 5.pptx
KeshavKumar395652
?
Unit vi(dsc++)
Unit vi(dsc++)
Durga Devi
?
Data members and member functions
Data members and member functions
Marlom46
?
4 Classes & Objects
4 Classes & Objects
Praveen M Jigajinni
?
Class and objects
Class and objects
nafisa rahman
?
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
?
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
?

More Related Content

Similar to static member and static member fumctions.ppt (20)

APL-2-classes and objects.ppt
APL-2-classes and objects.ppt
srividyal2
?
class c++
class c++
vinay chauhan
?
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.
Enam Khan
?
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
Dr. SURBHI SAROHA
?
APL-2-classes and objects.ppt data structures using c++
APL-2-classes and objects.ppt data structures using c++
ProfLSrividya
?
DS Unit 6.ppt
DS Unit 6.ppt
JITTAYASHWANTHREDDY
?
Class and object in C++
Class and object in C++
rprajat007
?
classes & objects.ppt
classes & objects.ppt
BArulmozhi
?
Classes and objects
Classes and objects
Shailendra Veeru
?
Object and class presentation
Object and class presentation
nafisa rahman
?
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
?
Mca 2nd sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
Rai University
?
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
Rai University
?
+2 CS class and objects
+2 CS class and objects
khaliledapal
?
Classes and objects
Classes and objects
Anil Kumar
?
cse l 5.pptx
cse l 5.pptx
KeshavKumar395652
?
Unit vi(dsc++)
Unit vi(dsc++)
Durga Devi
?
Data members and member functions
Data members and member functions
Marlom46
?
4 Classes & Objects
4 Classes & Objects
Praveen M Jigajinni
?
Class and objects
Class and objects
nafisa rahman
?
APL-2-classes and objects.ppt
APL-2-classes and objects.ppt
srividyal2
?
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.
Enam Khan
?
APL-2-classes and objects.ppt data structures using c++
APL-2-classes and objects.ppt data structures using c++
ProfLSrividya
?
Class and object in C++
Class and object in C++
rprajat007
?
classes & objects.ppt
classes & objects.ppt
BArulmozhi
?
Object and class presentation
Object and class presentation
nafisa rahman
?
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
?
Mca 2nd sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
Rai University
?
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
Rai University
?
+2 CS class and objects
+2 CS class and objects
khaliledapal
?
Classes and objects
Classes and objects
Anil Kumar
?
Data members and member functions
Data members and member functions
Marlom46
?

Recently uploaded (20)

Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
?
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
?
20CE404-Soil Mechanics - 狠狠撸 Share PPT
20CE404-Soil Mechanics - 狠狠撸 Share PPT
saravananr808639
?
Quiz on EV , made fun and progressive !!!
Quiz on EV , made fun and progressive !!!
JaishreeAsokanEEE
?
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
?
Structured Programming with C++ :: Kjell Backman
Structured Programming with C++ :: Kjell Backman
Shabista Imam
?
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
?
20CE601- DESIGN OF STEEL STRUCTURES ,INTRODUCTION AND ALLOWABLE STRESS DESIGN
20CE601- DESIGN OF STEEL STRUCTURES ,INTRODUCTION AND ALLOWABLE STRESS DESIGN
gowthamvicky1
?
Cadastral Maps
Cadastral Maps
Google
?
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
?
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
?
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
?
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
?
最新版美国圣莫尼卡学院毕业证(厂惭颁毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(厂惭颁毕业证书)原版定制
Taqyea
?
special_edition_using_visual_foxpro_6.pdf
special_edition_using_visual_foxpro_6.pdf
Shabista Imam
?
ElysiumPro Company Profile 2025-2026.pdf
ElysiumPro Company Profile 2025-2026.pdf
info751436
?
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
?
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
Fran?ois Garillot
?
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
?
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
?
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
?
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
?
20CE404-Soil Mechanics - 狠狠撸 Share PPT
20CE404-Soil Mechanics - 狠狠撸 Share PPT
saravananr808639
?
Quiz on EV , made fun and progressive !!!
Quiz on EV , made fun and progressive !!!
JaishreeAsokanEEE
?
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
?
Structured Programming with C++ :: Kjell Backman
Structured Programming with C++ :: Kjell Backman
Shabista Imam
?
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
?
20CE601- DESIGN OF STEEL STRUCTURES ,INTRODUCTION AND ALLOWABLE STRESS DESIGN
20CE601- DESIGN OF STEEL STRUCTURES ,INTRODUCTION AND ALLOWABLE STRESS DESIGN
gowthamvicky1
?
Cadastral Maps
Cadastral Maps
Google
?
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
?
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
?
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
?
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
?
最新版美国圣莫尼卡学院毕业证(厂惭颁毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(厂惭颁毕业证书)原版定制
Taqyea
?
special_edition_using_visual_foxpro_6.pdf
special_edition_using_visual_foxpro_6.pdf
Shabista Imam
?
ElysiumPro Company Profile 2025-2026.pdf
ElysiumPro Company Profile 2025-2026.pdf
info751436
?
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
?
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
Fran?ois Garillot
?
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
?
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
?
Ad

static member and static member fumctions.ppt

  • 3. Disadvantages of structure ?Standard C does not treat structure as an built- in data type ?Suppose we have declared C1,C2,C3 as object of above class at that time we can not perform operation like ?C3=C1+C2 ?Structure do not permit data hiding. All structure members are public members.
  • 4. Structures in C++ ? C++ support all the features as defined in C, but C++ has some expanded capabilities such as data hiding, Inheritance. ? In C++ a structure can have both function and variable as member(declaration) ? It also declare some members as private so that they cannot accessed directly by external function. ? C++ incorporates all these extension in another user defined type known as Class.
  • 5. What is Class in C++ ? A class is a way to bind data and its associated function together. ? It allows the data and function to be hidden, if necessary. ? When defining class we are creating new abstract data type that can be treated as other built-in data type
  • 6. Class specification has two parts: 1.Class declaration 2.Class function definition The class declaration describes the type and scope of its members, The class function definition describe how the class function are implemented
  • 8. ? The keyword class specifies, that what follows is an abstract data of type class_name ? Body of a class is enclosed within braces and terminated by semicolon. ? Class body contains declaration of variables and functions ? These variabes are collectively called as class members ? The keyword PUBLIC and PRIVATE are known as visibility labels ? If we don’t specify this label by default they are PRIVATE.
  • 11. Creating Object Syntax Class_name object_name; Example: item x; OR item x,y,z //multiple object We can also create object after class declaration as we do for structures(i.e declaring after semicolon)
  • 12. Defining member function Member function can be defined in two places” 1.Outside the class definition 2.Inside the class definition The function should perform the same task irrespective of the place of definition.
  • 13. Outside definition: ?An important difference between a member function and normal function is that a member function incorporates a membership ‘identity label’ in the header i.e class_name:: ?This ‘label’ tells the compiler which class the function belongs to. Syntax
  • 15. The member function have special characteristics that are often used in the program development. Characteristics are: ?Several different classes can use the same function name. the membership label will resolve their scope. ?Member function can access private data of the class ?The member function can call another member function directly, without using the dot operator
  • 16. Inside class definition: ?Another method of defining member function is to replace the function declaration by the actual function definition. ?When function is defined inside class is called as inline function ?Normally small functions are defined inside a class.
  • 18. Making outside function inline: when we define member function outside the class definition and still make it inline by just using the qualifier inline in the header line of function definition
  • 19. Memory allocation for object ? The memory space for object is allocated when they are declared and not when the class is specified. ? This is only partly true the member funaction are created and placed in the memory space olny once when they are defined as a part of class specification ? Since all the objects belonging to that class use the same member function,no separate space is allocated for member functions when the objects
  • 21. Array of Object ? Array of a Variables that are of type class are called array of object. Syntax: classname objectname[size]; Example: employee e1[3];
  • 22. Mememory allocation for array of a object Name Age Name Age Name Age EMP[0] EMP[1] EMP[2]
  • 23. Object as function argument Object can be used as function arguement and this is done by two ways. 1. A copy of teh entire object is passed to the function 2.Only the address of the object is transferred to the function
  • 24. 1. A copy of teh entire object is passed to the function ? The first method is called pass-by-value ? A copy of a object is passed to the function any changes made to the object inside the function do not affect the object used to call the function
  • 25. 2.Only the address of the object is transferred to the function ? The second method is called pass-by- reference. ? When an address of the object is passed, the called function directly work on actual object used in the call. ? Any changes made to the object inside the function will reflect in the actual object.
  • 26. Static data members Following are the characteristics of static member ? It is initialized to zero when the first object of its class is created. No other initialization is permitted ? Only one copy of that member is crated for the entire class and is shared by all the object of that class, no matter how many objects are created
  • 27. ? It is visible only within class, but its lifetime is the entire program. ? Static variables are used to maintain values common to the entire class. Eg. This can be used as a counter that records the occcurences of all the objects Syntax: static datatype variblename; Eg: static int a;
  • 28. Static Member function ? Like a static member variable we can also have static member function. A member function declared with static keyword is called as static member function. static member function has following characteristics: 1.Static function can have access to only other static members/functions declared in same class 2.Static member function can be called using class name(instead of object) as Class_name:: function_name;
  • 29. Friend Function ? We have seen that private members can't be accessed from outside class. Non member function cannot have access to the private data of a class. ? There would be situation where we would like two classes to share particular function. Eg consider two classes manager and scientist. We like to use a function income_tax() to operate on object of both d classes.
  • 30. ? C++ allows the common function to be made friendly with the both the classes to have access to the private data of these classes ? To make an outside function “friendly” to a class, we have to simply declare this function as a friend function of that class. Syntax: Friend returntype function_name(arguements); Eg: Friend void add();
  • 31. ? The function that is declared with the keyword friend is known as friend function ? This function can be defined else where in the program. ? The function definition does not use either keyword friend or the scope operator:: ? A function can be declared as a friend in any number of classes. ? A friend function although not a member function, has full access right to the private members of the class
  • 32. A friend function has certain special characteristics: 1. It is not in the scope of class to which it has been declared as friend 2. Since it is not in the scope of class, it cannot be us called using object of that class 3. It can be invoked like a normal function without the help of any object. 4. Unlike member function it can’t access member names directly and has to use an object name and dot membership operator with each member name
  • 33. ? It can be declared either in the public or private part of a class without affecting its meaning