Classes and Objects
Classes and Objects
memory
When a class is declared memory is not allocated
to data members of a class.
Data members cannot be manipulated unless an
object is created for the class.
When an object is created, memory is allocated
only to its data members not to member functions.
Member functions are created and stored in
memory only once when a class specification is
declared.
object
class
bankAccount one
bankAccount
withdraw
deposit
balance
name
interface
state
my_name = "Stoers"
my_balance = 333.33
object
bankAccount two
my_name
my_balance
my_name = "Daly"
my_balance = 75.00
using
Array of objects
C++ supports arrays of any data type.
An array having class type elements is known
as array of objects.
It is used to handle a group of objects which
reside continuously in memory.
Only memory space for data members of the
objects is created.
Member functions are stored separately and
shared by all objects of the class
The individual element of an array of objects
is referenced by using its index and member of
an object is accessed using the dot operator
syntax:Arrayname[index].membername
Objects as function
Arguments
Returning objects
Similar to returning variables from
functions, it is also possible to return
objects from functions.
The return type of the function is
declared as the return object type.
Static variables
Are like global variables for its class.
A static member is globally available for all
objects of that class type.
Usually used to store values common to the
entire class.
Since they are associated with the class
itself, rather than any object also known as
class variables.
They are stored individually rather than as
an element of an object.
Only one copy of static member is shared
by all objects of a class.
This Pointer
How do member functions know which objects
data to manipulate???
A pointer called this pointer is implicitly defined
in each member function and points to the object
with which the member function is associated.
When a member is invoked, it comes into
existence with the value of this set to the
address of the object for which it is called.
It is not available in static member functions.
this pointer can be used to access data in the
object.
Constant Member
Functions
A constant member function can access a
data member without modifying it.
Keyword const is used to specify a
constant member function.
It is used to indicate that the function
does not alter the data fields of the
object but only inspects them.
Syntax :
type function name(arg list) const
2-D Arrays
Most scientific data can be modeled
using multi-dimensional arrays.
Format of a 2-D array is
type array_name[r_size][c_size];
Structures and
Encapsulation
Structures can have two types of
members: data members and member
functions.
The only difference between a structure
and a class is that, by default, the
members of a class are private while, by
default, members of a structure are
public.