Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2K views

4-Const Vs Non-Const Functions Amd Static Data Members Functions

The document discusses constant and non-constant member functions and static data members in C++, explaining that constant member functions cannot modify the object and can be called by constant objects, while non-constant functions can only be called by non-constant objects; it also explains that static data members and static member functions exist as single copies shared across all objects rather than being created for each object.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

4-Const Vs Non-Const Functions Amd Static Data Members Functions

The document discusses constant and non-constant member functions and static data members in C++, explaining that constant member functions cannot modify the object and can be called by constant objects, while non-constant functions can only be called by non-constant objects; it also explains that static data members and static member functions exist as single copies shared across all objects rather than being created for each object.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Constant vs Non

Constant Functions and


Static Data Members &
Functions
Overvie
w
• Constant Member Function
• Non Constant Function
• Data members and Member Functions Memory Representation
• Static Data Member
• Static Member Functions
Constant Member
Function
• The const member functions are the functions which are declared as
constant in the program.
• The object called by these functions cannot be modified.
• It is recommended to use const keyword so that accidental changes to
object are avoided.
• A const member function can be called by any type of object.
• Non-const functions can be called by non-const objects only.
• The syntax of const member function in C++ language,
• Return Type function_name() const { function body}
Exampl
e

• By Compiling this code which Error You had Seen??


Non Constant
Function
• A const member function can be called by any type of object.
• Non-const functions can be called by non-const objects only.
• In this example the non constant function
• Can not be accessed by constant object
• It will show error
• passing 'const Test' as 'this' argument of 'int
• Test::getValue()' discards qualifiers
Data members and Member
Functions Memory Representation
• Each newly created object have copies of that class’s data and
member functions.
• The member functions are created and placed in memory only once when
they are defined in the class definition.
• This makes sense; there’s really no point in duplicating all the
member functions in a class every time you create another object of that
class,
• since the functions for each object are identical.
• The data items, however,
• will hold different values, so there must be a separate instance of each data
item for each object.
• Data is therefore placed in memory when each object is defined, so there is
a separate set of data for each object.
Exampl
e
Static Data
Member
• We can define class members static using static keyword.
• When we declare a member of a class as static it means no matter how
many objects of the class are created, there is only one copy of the static
member.
• A static member is shared by all objects of the class.
• All static data is initialized to zero when the first object is created, if no
other initialization is present.
• We can't put it in the class definition but it can be initialized outside the
class as done in the following example by redeclaring the static variable,
using the scope resolution operator :: to identify which class it belongs to.
Output

Representation of objects in Memory


Static Member
Functions
• By declaring a function member as static, you make it independent of any
particular object of the class.
• A static member function can be called even if no objects of the class exist
and the static functions are accessed using only the class name and the
scope resolution operator ::.
• A static member function can only access static data member, other static
member functions and any other functions from outside the class.
• Static member functions have a class scope
• You could use a static member function to determine whether some
objects of the class have been created or not.
Output

You might also like