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

Lecture-5 Introduction to Classes

This document provides an introduction to classes in programming, highlighting their similarities to structs and their practical differences. It discusses the importance of const objects for preventing unintended modifications, clarifying intent, and optimizing code. Additionally, it covers references in C++, their characteristics, and the benefits of using them for efficiency and direct modification.

Uploaded by

Hunter008
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)
9 views

Lecture-5 Introduction to Classes

This document provides an introduction to classes in programming, highlighting their similarities to structs and their practical differences. It discusses the importance of const objects for preventing unintended modifications, clarifying intent, and optimizing code. Additionally, it covers references in C++, their characteristics, and the benefits of using them for efficiency and direct modification.

Uploaded by

Hunter008
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/ 20

Introduction to Classes

Lecture-5

Awesh Islam
Classes
• Just like structs, cl ss is progr m-
de ined compound type th t c n h ve
m ny member v ri bles with di erent
types.

• From technic l st ndpoint, structs


nd cl sses re lmost identic l --
therefore, ny ex mple th t is
implemented using struct could be
implemented using cl ss, or vice-
vers . However, from pr ctic l
st ndpoint, we use structs nd cl sses
di erently
a
a
ff
a
f
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
ff
a
a
a
f
De ining a class
Let’s revisit structure
Lets recreate it using class
Member functions
Implicit passing of object
A little complex member function
Overloading Member Function
Const Objects
Why const objects are needed?
• Prevents Unintended Modi ic tions : const objects
ensure th t their st te c nnot be ccident lly ltered
fter initi liz tion. Helps void bugs.

• Cl ri ies intent: Using const cle rly sign ls th t


cert in objects or functions should not modify d t .
Improves code re d bility nd communic tes
design intentions. Cl ri ies th t object is me nt to be
re d-only not upd ted.

• Optimis tions
a
a
a
a
f
a
a
a
a
a
a
a
a
a
f
a
a
f
a
a
a
a
a
a
a
a
a
a
a
a
a
Const object data member modify
Const object calling non const member function
Const member functions
Const member functions modifying members
Non-const object calling const member functions
Best Practise

A member function that does not


(and will not ever) modify the state of
the object should be made const, so
that it can be called on both const
and non-const objects.
Reference in C++
A reference is n Ali s for nother v ri ble.

It provides n ltern tive n me for n existing v ri ble llowing ccess to it directly


a
a
a
a
a
a
a
a
a
a
a
a
a
a
Key Characteristics

• Must be initialized when


declared.
• Cannot be reassigned to
reference another variable
after initialization.
• Acts as a pointer, but with
simpler syntax (no
dereferencing needed).
Why reference?

• E ciency: Avoids copying


large objects (faster
execution).

• Direct Modi cation:


Enables direct
modi cation of variables,
especially useful in
function parameters
ffi
fi
fi

You might also like