0% found this document useful (0 votes)
174 views

Classes and Objects

When a class is declared, memory is not allocated for its data members. Memory is allocated only when objects of that class are created, to store the data members of each object. Member functions are stored separately in memory from objects. A class can contain arrays and static members. Objects can be passed as arguments to functions by value, reference, or pointer. Functions can also return objects.

Uploaded by

SUJITHMV
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
174 views

Classes and Objects

When a class is declared, memory is not allocated for its data members. Memory is allocated only when objects of that class are created, to store the data members of each object. Member functions are stored separately in memory from objects. A class can contain arrays and static members. Objects can be passed as arguments to functions by value, reference, or pointer. Functions can also return objects.

Uploaded by

SUJITHMV
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Classes,objects and

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.

Class and Object Diagrams


Relationship between a class and it objects
the Unified Modeling Language UML diagram

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

Arrays with in a class


A Class can have an array member as
its member variable.
It either can be private/public/
protected data member of a class

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

An object can be passed as a function


argument.
An object can be passed by
1. Value: A copy of entire object is passed
to the function
2. Reference: The address of the object is
passed implicitly to the function
3. Pointer : The address of the object is
passed explicitly to the function.

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.

Static member variables


To make a data member static two things are
required
1. Declaration within the class definition
2. Definition outside the class definition
Declaration within class is similar to any other
variable declaration
static type varname;
Definition outside the class is as follows:
type classname:: varname=initial value

Static member functions


A static function can access only static
members of the same class.
Use keyword static before the function
declaration.
It can be invoked by using the class name
instead of object name.
Also possible to invoke static member
functions using objects.

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.

You might also like