C++ Notes [PDF]

  • 0 0 0
  • Gefällt Ihnen dieses papier und der download? Sie können Ihre eigene PDF-Datei in wenigen Minuten kostenlos online veröffentlichen! Anmelden
Datei wird geladen, bitte warten...
Zitiervorschau

Context switching & Inline Functions : Whenever there is a function call, the control shifts to the func. declaration and after complete execution , the control come backs to the main function, for achieving this lot of overheads is faced by the system sas the current state needs to be saved somewhere , ex- registers,stack and this round trip is also called Context switching. In order to reduce this effort inline functions are introduced where function definition is pasted at the point of function call.

Advantage of Inline functions :  

Execution will be faster. No overhead of context switching

Disadvantage : 

If function is large , then making a function inline can increase the size of program

Exceptions : -

Only smaller function can become inline. Function which are recursive in nature , cannot become inline. Function with loops , switch , goto statements cannot become inline. Inline is a keyboard

Inline Member Function :  

Member function which are defined inside a class are by default innline. Member functions which are defined outside class are non-inline in nature , but we can make them inline with keyword “inline”.

Static Data Member :    

Only one copy is created , which is shared by all objects. It is visible only within the class , but the lifetime is entire program. It is mandatory to define static data member outside class with the help of scope resolution operator. They are initialized with default value zero.

Static Member Function :   

They belong to entire class , not to a particular object. Static member function can have only static data members inside their definition of same class. They can be accessed with the help class name and scope resolution operator.

Friend Function : 

Private data of a class can be accessed.

   

It is a non-member function There is no need of any object to access the friend function ( because it is a nonmember function) It will always accept object as an arrangement. It can be declared / defined in any section of class.

Pointer VS Reference Variable

1 ) Pointer can be initialised after its declaration , or we can initialise it at the point of declaration.

It is mandatory to initialise reference at the point of declaration.

Int *p; ✔

Int a = 10; ✔

Int a=0; ✔

int &b = a; ✔

p=&a; ✔

int a = 10; ✘ int &b; ✘ b = a; ✘

2 ) Reassignment / Re Initialisation of pointer is allowed.

Not Allowed.

int a = 2 , b = 3; ✔ int *p; ✔ p = &a; ✔

int a = 2 , c = 3; ✔ Int &b = a; ✔ b = c; ✔

3 ) Indirection or dereferencing operator is required for accessing the value pointed by the pointer.

No need of dereference operator for accessing the value.

int a = 2; ✔ int *p = &a; ✔ cout