OOPS Notes(C++).pdf

5,435 views 24 slides Apr 13, 2023
Slide 1
Slide 1 of 24
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24

About This Presentation

oops


Slide Content

OOPS in C++

The Main aim of OoP is to bind Together the
data and he functions the operate on them so
that _no_other part of the code can access this
data_except this Function .

+ [Paismospnism ]

>.
E

=D Class: Tr is a user defined data types, which

———holds

_ functions, wihich can be accessed and used by
cxeating_an instance of that class.

its own data Members and member

Ji

e
LG.

ct: When a class is defined no memory
alocated but when it is ¡nstantiated
e., Object is created ) memory is allocated.

=> Encapsulation: In OoP , Encapsulation is defined

as binding together the data and the functions
“hat manipulates -them-

=> _ Abstraction : Abstvaction means displaying only

essential information and niding the details. _
Abstraction using classes
Abstraction using Header Files

(mam -h > Pow)

ism: Tn simple words , we can define
polymoxphism_as an ability of a message to be
displayed in More +nan one form. _
Opevatoy Overloading
function overloading

int sum (10,20, 30)
_ int sum (10,20)

=> _Thhevitance : The capability of a class to
dexive propevties and chavacteristics Fyom_

Anothexy class is called inhevitance-
Subclass

Supexclass =
Reusability

—D < a
¡In dynamic binding , the code to be

executed in yesponse +0 function cau is
¡decided at vun time

Constructors +

A_ constructor is a member function ef a
class which initializes objects of a class .In
C+ Constructor is automatically called when
the object cxeates. =
¡It has same name as class itself. =
Constyuctor don't have a veturn type :
1: Default constructor (NO paxametex passed)
2: |Parametrized Constructor =
3- |Copy Constyuctor

a Destzucto

Derived class destructor will be invoked

First , then the base class destructor will be

invoked :

_ | can be accessed by any class.
— Private can be accessed only by a function
‚pn a class (inaccessible outside tne class)

[Protected :- Tr is also inaccessible outside

— lane class but Can be accessed by Subclass
lar +nar class.

7 [Note IF me do not specify any access
|modiFier inside +he class then by default
the access modifiey for the Member will be

privare :

À Friend class can access private and
protected members of Omer class in which
it is declared as Friend
Ex :- Friend class B;
ATUL Ky mak (Linkenın.
= sie NOTES GALLERY (TELEGRAM)

Class Subclass : accessmode . baseclass

i _
Combination ef one
er more type,

2 Operator Overloading
Function everloading

|? Compile time Pory

=

4.

=>
— Runtime Poly
L> Function overriding occurs when a
dive. class has a definition of one oy more
members of base class =

Advantages of Data Abstraction,
* Avoid code duplication and inc. yeusability
Can change internal implementation ef class
independently

Struc! uye vs class u
Most impoxtant difference is security -
A_ Srructure is not Secure and cannot hide
its member function and variable while class
is secure and can hide ¡ts programming,

% designing details. _ _

Es i +:
A class declared inside a function becomes
local to tnat Function and is called local
class + ss =
All_the methods of local class must be
defined inside ne class only.

Vistual function and Runtime
=> |A virtual Function is a member function
which is declared with a base class and
yedefined Coverviden) by derived class.
functions ave declared wilth virtual Keyword —

base class. lp
5

oe
=P Runtime Polymorphism , also known as the
Dynamic Method Dispatch, is A process +hat
vesolves a call to an overridden Method at
yuntime. The process involves the use OF the
Yeference variable of a Superelass to call
foY an overridden method. B

EU! represent a block of code nat can _—
— throw an exception.
Gatch: represent a block of code +hnat get
executed when exroy is +hrowWn.
nrow:, Used + throw an exception .

Tnere is a special catch block —> catch (..

It catches all types of exvor.

e |
nine is a request not command.

¡Tr is function tnat is expanded in line when
iris called. When the inline Function is
called, whole code get inserted oy Substituted
at the point of inline Function call.

veturn-type function (__)

Function ovestoading is a Feature in c++
where two ox more functions can have —
same name but different parameters.

void print (int i)
cout << "Here ts int "<< i cc ends
bs

Void print CFloat i )
{cout << "Hexe is float" << i << endl) |

at main
t pinto);
paint (10-12) 5

je Crt is known as hybrid
language, because it

port both procedural
and object ovlented
Programmin
je: Rime Support. for

¡has no Support For | Ener and =
_| polymorphism ,_ 1 inhexitance as it is an

encapsulation and | oops language.
inheyitance.

C isa Subset oF ctr [e Car isa Supexset of

P+ C contains 32 Keymords|e C++ Contain 52 keywords
(public, privare ,
protected, try, catch,
throw
* € is a function dyiven [e Crt is an object
language. dxlven language
+ Function and operator [e C++ supports function
overloading ts not __ & operator _
_____|support in C. Overloading :
© C does not Support e Ctt supports exception
exception handling. _| handling using ty

© | Structure is a collection of dissimilar

| elements: =
ATUL comer (Linkeoın).
ic in Cee NOTES Gaureey(rerccaara),

« Static variable in q Function :
Winen a variable Is declared as static, <pace
fox it gets allocated for tne lifetime of the
program: ( defautt intialized to ©)
Even _if +he Function is called multiple times,
he Space for it is aulocated once.

> Static Variable In a class:
> Declared inside ne class body-
> ¡Also known as class member variable.
> [ey must be defined outside the class.
3 lso varlable doesn't belong to any object,
but to the whole class.

7 There will be only ene copy of static member
Variable for tne whole class

Class Account
private :
int balance ;
static Float voi,
public:
“veld setBalance Unt b)
X balance =b ;

/Lin+ialised outside class
Float Account :: yor
‘oid main

[EE A

Account at;

2 Object can also be declared as static.
u Static Account at,

| Static on 1

|
| Static member Functions are allowed to access
only the static data members oy other
Static member Functions. _

Constructors +,

> Constvuctors is an special member function oF
tne class. It is Qutomatically invoked when
an object is created:
It has no vetuyn type.
Constructor has same name as class itself:
IF me do not épecify , then C++ Compiler _
—Qeneyates a default constructor for us.

Parameterized Copy
clas: classname
C Parameters ) class_name Zobj)

upaate(int x,
int y)
a=x;
bey;

1
2:

Default Constructor

Copy Constructor. ATUL Komak (LukEpin).
= HOUR AU Tensa]

But if any of the constyuctoy is created by

lusey, then default constwctoy mill not be

loxeated by compiler

Compiler fe two Constructor by itself

Construction overloading can be done just
like function everloading .

Defauit Ccomptiex's) copy constructor can done
pony. Shallow copy:

(Points + same.
memory location)

“Deep “copy is possible only with user defined
constyuctors -

In user defined Copy constructor, we make _
<uye that pointers of copied object points
to new memory location:

_ Can me make copy constructor private ? Yes.

Why argument +0 copy constvuctoy must be
passed as a xefexence 2 —

fade 1 if we pass value , then it would.
made to Call Copy constructor Which becomes
Inon - teyminating -

ik
=

Obj 2.
Deep copy.

Destructor is a member Function wihich destyucts
ox deletes an object:

Destructor don't take any argument and don't

have any yeturn type.

Only one destructor is possible.

Destructor cannot be static -

Actually destructor doesn't destroy object, it

is the last function +ha+ invoked before

Object destvay: ATUL eon pe ( LINEEDIA)-

NOTES GALLERY (TELEURAM)
— al

7

Object ____ Resource

Destructor is used , sO that before deletion

DÉ Obj we can free space allocated for +his

wesource GB/c if obj gets deleted then Space
Allocated fox Obj will be free put yesouyce

doesn't -

fe) y R
Le ++ have the ability to provide special
meaning tp. the operator:

Class Complex
ES E

{_ Complex operator + (Complex &c1)
Complex Yes 5
ves.a =C1-95
Yes.b=C2:b;

>
E

int main C )

pur coman (Lin
C= circa NOTES GALLERY (TELEGRAM )

As‘+? can't add Complex no's directly: So we
can define a function with name + but we

need write operator keyword. before it: _
So, me can use all operator like this-

A friend class can access +ne private and
protected members of Other class in which it
is declared_as fyiend.

Mere can be friend class and Friend Function.

Class Box
À private : =
“double width ;
public :
Friend void printwidtn (Box box)

veld Setwidth (double Wid) >
T.

Void Box: : setwidth (Boxdouble Wid)
{width = mid; >

Void psintwidth (Box box _)
4. cout<< box. width; Y

int main)
Box box;
box. Setwidth (14);
psintwidth [box ) ;

Inheritance _
It isa process of inhesting_ properties and
behaviour of existing class into a new class.

Class Base_ciass
4.

Y;

Class der-class : visibility
=Mode Basel
Clas:

35 u

Ex: Class Car Class Sport. Car: public
x

O Es

{

à

Class C: public B
t

Y

=> „Multiple Inhexitance,

AL

Class As Class A2
ed

|
+5 xy I

Class B : Public As, Public Ar

d)-__Heivaychia! Tnheritance_

Class Bı ; Public A|

Y ==
Class Ba :

Public A

— 173

A- base class
B - Sub class

IF B is Subclass and visibility Mode is public.
Class A: (Public 8
t

+5

then public member of A_ mil be public in 8,
and protected will protected.
TF_visibility mode is private tne both protected
and public member of A will be private
Member of B - —
— Is _a Relationship is always implemented as

"a public _inhevitance. 7 =
Constructox and Destructor in Thhexitance _
„First child class constructor will yun during __
——_lCxeation of object of child class, but as soon
las obj is created child class Constructor un
and it will call Constructor of its parent
IL Iclass and after he execution of parent class
\constyuctox it mill wesume it Constructor

VE Cc Ra NOTES GALLERY) TELE en]
ua Constructor exe
Ob}
While in case oF destructor, cnitd const

Fixst child es mere
Parent Const

Comptete paren
Complete child.

Every Object _1M_Crr has access to its own

address thyough an important polnter called
nis_pointer. e

Friend function doesn't have a ‘this’
because friends ave not members of a class -
Oniy member Function have +his pointex-

| Class Box
Private :
int 1,b,h>;
public :
void set Cint 1, intb, inth) |
{mis 15
this + b be
this? = hi

int main) _
Sn ori
b:set (5,10,4) >

| Method OverRidina ( achieved at vun time)
Tr is +he vedefinifion of base class function
in its derived class , with same yetuyn
[type and same parameters

while method Overloading is achieved at
Compite time.

-

Aored EmLERy (TELEURAM)
Class Cay
Private :
int gear no 5
Public +
Void change. gear (int gear)

gear ++

Class Sports Car + Public Car
{

Void change. gear (int gear)
4 if (gear >5_) —

Function of Sports Car (sc ) class willl be called]

While eating change —Geay (), Fivs it check.
if any Fun with +his name exist in_callin
¡class , Otherwise it goes to baseclass
Useful: Like me have change _ gear For ail
except One Cay mihich have unique method
Of gjearchange :

Mistual Function.
A Vistual function is a member function
which is declaved with a ‘Virtual Keyword
in he base class and vedeclayed
Covervidden) in a derived class.
then you xefer to a e of derived
class using pointer to a base class, you can
call a rial Function Of that object and
execute the derived class'S version of _
tne function. 7 ———

They ave used to achieve Run time

|polymerphiem: __
|Virfual_ Function cannot be static and also

cannot be Friend function of ano

Compile -time (Earıy binding) _
Vs

— ap
Runtime ( Late Binding) Motes qm ree

| Ciass base -
Public : _

_t cout << This is base pi "<< eng}
J

Noid show | oa —
{cour << “Base show fun” << endl 5
bu

Class derived
x Public :
Void print C )
Lcoutec "derived Print" << endl;
y
_ Noid show ()

base * bpty ;

wed dev;
& der ;

bptr > Punt); // Run time

boty — Show (); 11 Compile time

_dexived print 11 Late Binding
Base show fun I] Early binding

=>
As during compiler +ime bptr behaviour
judge on the bases of which class it belong,
SO. bptx vepresent base class.
Tf the Function is not Virtual -then it will
«Allow binding at compiler time and print
fun oF base class will get binded because
bprY xepresent base class. _ _
— But ar run time bptr points to
the object of class ran: wi
bind function of derived at run time.

— Wlerking of virtual Function

7 IFa class Contains virtual Function then
compiler itself does two -things

fa panes (PRD is cxeared every time
—— Object is created foY that class which
É contains virtual function.

| Txxespective of object is Created oy not,
Sraric array of pointer called (VTABLE )
Where each cell point to each virtual
Function is Cyeated , in base class and
derived class.

duxin9 Early bindin

VTABLE for
base class

VIABLE For
—gexived class —

unctio

„and abstract Class
Sometimes | implementation of al Function
cannot be provide in the base class,
Such a class Is called abstract “class. =

Shape ATUL Kumar (Lnkepin).
NOTES GAL ER (TELE RA]

draw)
> ( But there can't be any implementa
=Fion of draw () fun in the
base class)

+
À pure virtual function in ctt is a
virtual Function for which we don't have
any Implementation, wie only declare it-

Class Test
— Public: _ -
Viytual void fun() = O; _
= 4 u
uxe Vivkual Func Hon

4A class js Qbstyact jf it has at least one
|pure viytual Function. u
We cannot declare object of abstract class .
Ex: Test t, mall show exvoy
We can have peintey or yefeyence of
abstract class. ü
We_can access +ne other functions except
virtual buy object of its derived class.
IF we don't override he pure virtual
Function in derived class tnen It becomes
labstvact-
An abstract class can have constructors,
(Read From GEQ )

y

23

Tepaplate In C++
check (a, xb )

template <class x >
t

if (a>b )
__xeturn a;

Tk just help in data type. So that y

generic Function nat can be used for

different data type

using dynamic memory allocator ‘New? Ir
Y

— constyuctor y
Class qeeks AUL rompre (LINKEDIN).
4 Pabiie : er =
void fun() À P= new chaxl6] ¿Y

x

int main)
t geeks 9
x

= new geeks (9,
Tags