ye Merey e |
ODPS IN C+t
The main aim of of is 40 bind teethty the datq and the
funuions that Operate on nem so that no other part att the Code
Can access this clota except this function. geal
It is q user defined dara types, vohith holds Its ew dara
members functions, which can be accessed and used by creating
an Instanw of thot class.
[Cejec 3] Lunen @ Class is defined no memory is allocattd bur
oe iS instantiated Ci.e., object ts Created) memory fs anlocated.
[Encapsuiatfon Jin o0P, Encapsulatton 1s defined as binding
Fogether the data and the Funttions thot manipwares them -
[Rbstraction 3] Abstraction means displaying. only essential
rome and hiding the detalls.
* fibshostion using classes
+ Arbshaction using reader Files (math +h Pow)
Polumorphism 3] Th simpie words, we can define Polymorphism
as the abiiity of a message to be dlsplayed In more than one form,
+ Operotor Overloading
« Function Overloading
Vint sum (10,20, 30)
int sum (10, 20)[inheritance {The Capability of q Class 4o derive Properties and
charactevistics from anomier class 15 called Inheritance .
+ Subclass + Gupercla ss . Reusabllity
Duynam\e Blading ) Tn dynamic binding, the Code 4o be exeuuted
§n vesponse to Funciion Cat ts decided at yuntIme.
[Constructors ‘]-fr Constructors 1g a member funuton of @ class
Onn Inielatizes object of a Class- In Cr+ Constyuctors 95
Qurematically Called When the Objeer Creates -
Tr has sqme name as class ftselr.
Constyuctor don't have a return type.
t+ Defoutt tonstrutror (No Payamerer “Passed )
2 Porametrined Constructor -
3B: Copy Constructor
estructor In Crt | Derived class destructor wlll be |nvoked first,
then the base class destruter willl be Vnvoked.
Access Moaihier +] Auntic-can be accessed by any class
Private > Can be accessed only by a Funetlon in a class
Cfraceessibje outclae the class).
Protected: Ti Cain also Inactessible ourcide the Class but Can be
accessed by subclass af that class + -
Nore: TF we do no Speutfy any access modifiety Inside rhe
Class then by default the actess medifity for the member whn
be Private +
Griend class t}A Friend class can qetess pyivare and protected
member’ oF other clast In Which It Ts cleclared as friend.
Ex-1 Friend class B;
ATut KUMAR CLINKEDIN)
SULNVULUDULULL UU tbl ddddddddddddddeelocal class In C+tt]A class declared inside q function become
focal to phat funuton qnd Is Called local class. All +he methods
of larat class must be defined inside the class only.
Vivtual Funurion and Runtime Polymorphism
A Virtual Finaion tsa member Function Which is declared within
@ base class Ana yeadeined (overriden) by clerived Class+
Funuon are declaved with vivtual Keyword in base class.
Yt vepresent q blok OF Code phat Can throw an exception .
Catch: yepresent q blot ef tode that get exetured When etToy
is thyown:
throtss Used te thro an exception «
There is q speclat catth blouk — catch (+++)
~ Te carches au types of error-
— Inline is @ vequest not command.
+ Tt is function that 1s expanded \n tine tonen it is Called -
WWhan the Inline funuion ts Catled, Whole Cede get inserted
or tubsttured at the point of Inline Function call.
Iniine yeturn- type fun)
» Funcdon Overloading] ts a Feature in C#+ where two
OT more Funttions can have sdme name but dlfferent
pavameters.
void psint ( fat 1)
cout co "Here te int “gefce endl;
é
SWUVVVULELELLEL ELE teed dd ddddddddd//yvied print ( Float i)
4 cource “Move is Float "ecice endl;
+
int main
£ Pring (10) 5
Print (10.12) 5
>
Ditfovena b)W c and Cre
2. C Supports procedural Prog. |* Ctt ts known as hybrid lenguage
because fr supports born Procedural
and objet oriented Programming.
2. As C does not Supports tre OOPS Cet has supports for polymorphism,
concert co it hac no spotic Vavlable Ina Funurions Iavhen a variable is
Aecared as static, Space for Ir get auyocated for tht UFetHme of
he program ( defoult InHallzed to 0). Even Tf +e Funtrlon
is coued multipre times, the spam for it fs allocated ona.
NWVVUUUEELEEELELEdddddddeddddddideveLe
~~
AUOLDUODELLLLLLI DEE odbddddddded
* Static Varlable in a Class:
— Declared inside the class body.
+ Also Known ag class member variable
> Static variahie doesn't belong 4o qny object, but to the cohdle
class.
They must be defined outside the class.
= There. wohl be only one copy of static member Variable for
tne Whole class.
ex
Class Account
Private *
int balance)
Staric Float ror;
Public?
Void Setbalancs (int b)
{ baanu=b; }
33
HI jotialised outside class :
i Float account 22 Or = 35 FS
Void main
{ Actount at 3
+
+ Object Can aise be declared a¢ static.
Static ActouNt at;
. Staric funutton In Q class
Starle member funulons qre atlowed to qeeess only +ne static 7
dara membirs et otney static member Functions.
Constructors: |
= Constructor is an spectol member faunetton of the clacs. It fs
qutomotically invoked then qn Objeur is Creared.
3 Tt has not yeturn type -
3 Constructor has same name as Class Ptself »
> TF we do not specify, then crt compiler generates a default
Constructor for us+peemed | eee
class_name (parameters)
(const class. name
Update Update Cat x, nt y ) Job} )
{ a=; fa=x;
h=20; bys
} 3
Comptiey Qenevotes two Constructor by itself
4. Default Constructor.
2+ Copy Constructor.
Rut je any of the Conshruuor is Created by USET, then default
Construudr Loi) Not be Created by compiler.
Construuion Overtoading can be done just like Function
Overloading +
Defawt(tompiier's) Copy tongrrucroy cin done only
shaiiow Copy.
Cfoints to same
Memory location )
Deep copy_is posstble onty with user defined
Constructers » In user defingd Copy construttoy, we
Moe sure phat pointers of lopied Object points
to Nevo memory locotion +
Can wt mae copy tonstyuuor privare D? Yes.
fan weomant copy _roncmacen priors Yes:
WUVbobbbbbuboeuilitdddcdedddddddd deladhy argument to copy Constyuttoy Must be passed as a
yereyence?
Because TF we passed value, then tt Would made to Cau tory
tonstruuey tokio) betome non- reminating «
a u
0 a
o De
obja ob’
4 Deep Copy. 4
= Deshuetey ts q member Function whith deshucts of dele
an objeue
— Destrurty don't take any arqument and don't have any
yeturn type.
2 Onty One destructey js Possible.
> Destruucr cannot ’e@ static +
— Fetually destructor doesn't destyoy object, it is the last
Funulon thar invoked before Objeu destroy.
Le——L_]
Objer Resource
Destruuer is used , so mat before deleton of objet
we can fre spare aiocared for pus yesourer.
Ble iF odjecr gars deletion tan Spa allocated for
Ober wil be fret but yesouree doesn’t:
» | Operottng Overloading
C++ hove the abiiity to proulde spetlod meaning ta.
Operator.
HPhhbbbuvebdddddddddddcceeeeer
BULLEClass Complex
Complex operator + Ctomplex £ C1)
{ Complex res;
WES. Q= CAs
yeseb = Crb}
}
+
int main (9
{-) ceu4tr
3
As! +) can't add complex no's directly» $0 We ean define fundion
with name + but we need write operator Keyword before t+
£0, we can use au operator IiKe shis-
A Friend class qececs the private and protected members of Other
class In When Tt ts declared as friend:
There can be friend class and friend funuion -
Ex: Class Box
£ Privore:
double width;
Publics
Friend vold Printwtdth (Bex box)>
>
Nold Boxz: setwidth (Roxdeuble oid)
{ width = wid; }
\old Printusiath (Box bex)
{ Cout << box. width; >}
fat maint )
{ Box box;
bex. setuidth (14);
Printoidth (bex) 5
MVOLELELELLULULEC Id ddddddddddd/d/c/c%