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
> 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:
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