Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
2 views35 pages

Week 5 - Classes and Objects Part 1

Uploaded by

Dhanu Teotia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views35 pages

Week 5 - Classes and Objects Part 1

Uploaded by

Dhanu Teotia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Classes and Objects

INTRODUCTION
Classes form the building blocks of the object-oriented
programming paradigm. They simplify the development of large
and complex projects and help produce software which is easy to
understand, modular, re-usable, and easily expandable
Syntactically, classes are similar to structures, the only difference
between a structure and a class is that by default, members of a
class are private, while members of a structure are public.
Although there is no general rule as when to use a structure and
when to use a class, generally, C++ programmers use structures for
holding data and classes to hold data and functions.

© Oxford University Press 2015. All rights reserved.


SPECIFYING A CLASS
•A class is the basic mechanism to provide data encapsulation. Data
encapsulation is an important feature of object-oriented
programming paradigm.

•It binds data and member functions in a single entity in such a way
that data can be manipulated only through the functions defined in
that class.

•The process of specifying a class consists of two steps—class


declaration and function definitions (Fig.).

© Oxford University Press 2015. All rights reserved.


Class Declaration

The keyword class denotes that the class_name that follows


is user-defined data type. The body of the class is enclosed
within curly braces and terminated with a semicolon, as in
structures. Data members and functions are declared within
the body of the class.

© Oxford University Press 2015. All rights reserved.


Visibility Labels
• Data and functions are grouped under two sections :-Private
and Public data.
• Private Data:- All data members and member functions
declared private can be accessed only from within the class.
• They are strictly not accessible by any entity—function or
class—outside the class in which they have been declared. In
C++, data hiding is implemented through the private visibility
label.
• Public Data:- and functions that are public can be accessed
from outside the class.
• By default, members of the class, both data and function, are
private. If any visibility label is missing, they are automatically
treated as private members—private and public.
© Oxford University Press 2015. All rights reserved.
Defining a Function Inside the Class
• In this method, function declaration or prototype is replaced
with function definition inside the class.
• Though it increases the execution speed of the program, it
consumes more space.
• A function defined inside the class is treated as an inline
function by default, provided they do not fall into the restricted
category of inline functions.

© Oxford University Press 2015. All rights reserved.


Example:- Function inside the class

© Oxford University Press 2015. All rights reserved.


Defining a Function Outside the Class
• class_name:: and function_name tell the compiler that scope of
the function is restricted to the class_name. The name of the ::
operator is scope resolution operator.
• The importance of the :: is even more prominent in the
following cases.
• When different classes in the same program have functions
with the same name. In this case,it tells the compiler which
function belongs to which class to restrict the non-member
functions of the class to use its private members.
• It allow a member function of the class to call another member
function directly without using the dot operator.

© Oxford University Press 2015. All rights reserved.


Example:-Defining a function outside the class

© Oxford University Press 2015. All rights reserved.


CREATING OBJECTS

• To use a class, we must create variables of the class also


known as objects.
• The process of creating objects of the class is called class
instantiation.
• Memory is allocated only when we create object(s) of the
class.
• The syntax of defining an object (variable) of a class is as
follows:-class_name object_name; .

© Oxford University Press 2015. All rights reserved.


Example:- Object Creation.

© Oxford University Press 2015. All rights reserved.


ACCESSING OBJECT MEMBERS
• There are two types of class members—private and public.

• While private members can be only accessed through public


members, public members, on the other hand, can be called
from main() or any function outside the class.

• The syntax of calling an object member is as follows:-


object_name.function_name(arguments);

© Oxford University Press 2015. All rights reserved.


Example:- Calling of object.
© Oxford University Press 2015. All rights reserved.
NESTED MEMBER FUNCTIONS

© Oxford University Press 2015. All rights reserved.


MAKING A MEMBER FUNCTION INLINE

© Oxford University Press 2015. All rights reserved.


© Oxford University Press 2015. All rights reserved.
© Oxford University Press 2015. All rights reserved.
Memory Allocation for Classes and Objects
• An object is an instance of a class.
• Every object is identifiable.
• Objects communicate with classes through passing messages
known as function calls.
• Every object is defined by a state or value of variables.
• The state of the object can be changed by one or more operations
performed by member function(s) of that class.
• Memory for a class is allocated only when one or more objects of
that class are created.

© Oxford University Press 2015. All rights reserved.


Memory Allocation for Classes and Objects
• When a class is specified, memory for member functions of the class is
allocated.
• When one or more objects are created, separate chunks of memory for
storing data members are allocated to each object as shown in Fig.
• This type of memory allocation is quite justified because while each
object has a different set of values for its data members, they execute the
same piece of function code. Hence, it is better to store function code in
one place and let all the objects of that class share the function code.

© Oxford University Press 2015. All rights reserved.


Static Data Members
When a data member is declared as static, the following must be
noted:-
– Irrespective of the number of objects created, only a single
copy of the static member is created in memory.
– All objects of a class share the static member.
– All static data members are initialized to zero when the first
object of that class is created.
– Static data members are visible only within the class but
their lifetime is the entire program.

© Oxford University Press 2015. All rights reserved.


Static Data Members
• Relevance:-Static data members are usually used to maintain values
that are common for the entire class. For example, to keep track of
how many objects of a particular class have been created.
• Place of Storage:-
– Although static data members are declared inside a class, they are not
considered to be a part of the objects.
– Consequently, their declaration in the class is not considered as their
definition.
– A static data member is defined outside the class. This means that even though
the static data member is declared in class scope, their definition persists in
the entire file. A static member has a file scope.
– However, since a static data member is declared inside the class, it can be
accessed only by using the class name and the scope resolution operator.
Example-NonStatic Data Members

Example:- Non Static data members

© Oxford University Press 2015. All rights reserved.


Example-Static Data Members

Example:- Static data members

© Oxford University Press 2015. All rights reserved.


Static Member Functions
• It can access only the static members—data and/or functions—
declared in the same class.
• It cannot access non-static members because they belong to an
object but static functions have no object to work with.
• Since it is not a part of any object, it is called using the class
name and the scope resolution operator.
• As static member functions are not attached to an object, the
this pointer does not work on them.
• A static member function cannot be declared as virtual
function.
• A static member function can be declared with const, volatile
type qualifiers.

© Oxford University Press 2015. All rights reserved.


© Oxford University Press 2015. All rights reserved.
Static Object
• To initialize all the
variables of an object to
zero, we have two
techniques:-
• Make a constructor and
explicitly set each
variable to 0. We will
read about it in the next
chapter.
• To declare the object as
static, when an object of
a class is declared static,
all its members are
automatically initialized
to zero.

© Oxford University Press 2015. All rights reserved.


ARRAY OF OBJECTS
• Just as we have arrays of basic data types, C++ allows programmers
to create arrays of user-defined data types as well.
• We can declare an array of class student by simple writing student
s[20]; // assuming there are 20 students in a class.
• When this statement gets executed, the compiler will set aside
memory for storing details of 20 students.
• This means that in addition to the space required by member
functions, 20 * sizeof(student) bytes of consecutive memory
locations will be reserved for objects at the compile time.
• An individual object of the array of objects is referenced by using
an index, and the particular member is accessed using the dot
operator. Therefore, if we write, s[i].get_data() then the statement
when executed will take the details of the i th student.

© Oxford University Press 2015. All rights reserved.


© Oxford University Press 2015. All rights reserved.
Dynamic memory allocation for array of objects
• In the last slides, we have seen how to create a static array of
objects. We call it static, because memory was allocated for the
objects at the compile time.
• Moreover, the allocated memory had to be contiguous. Therefore, a
better idea is to use pointers to objects. This approach is far more
flexible as it allows objects to be dynamically allocated during
execution or run time, as and when required.

© Oxford University Press 2015. All rights reserved.


OBJECTS AS FUNCTION ARGUMENTS
Similar to variables, objects can also be passed as arguments to functions.
Similar to basic data type arguments, objects can also be passed to functions in
the following three ways:
• Pass-by-value: In this technique, a copy of the actual object is created
and passed to the called function. Therefore, the actual (object) and the
formal (copy of object) arguments are stored at different memory locations.
This means that any changes made in the formal object will not be reflected
in the actual object.
• Pass-by-reference: In this method, the address of the object is
implicitly passed to the called function.
• Pass-by-address: In this technique, the address of the object is
explicitly passed to the called function.
• Since both pass-by reference and pass-by-address techniques send the
address of the actual object to the called function, any changes made in the
object are reflected to actual object. Moreover, these two techniques must
be preferred as they prevent duplication of object and hence, reduce
memory space requirements of a program.
© Oxford University Press 2015. All rights reserved.
© Oxford University Press 2015. All rights
reserved.
© Oxford University Press 2015. All rights reserved.
RETURNING OBJECTS
• You can return an object using the following three methods:-
• Returning by value which makes a duplicate copy of the local
object.
• Returning by using a reference to the object. In this way, the
address of the object is passed implicitly to the calling
function.
• Returning by using the ‘this pointer’ which explicitly sends the
address of the object to the calling function.

© Oxford University Press 2015. All rights reserved.


Example:- Returning an object.
Return By Value:

Return By Reference and by pointer(address):

© Oxford University Press 2015. All rights reserved.

You might also like