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

C++ Important Topics For Placements.

1. Inline functions allow code to be inserted or substituted at the point of call to reduce function call overhead. 2. Functors are objects that can be treated like functions or function pointers. 3. Const member functions do not modify the object they are called on and help prevent accidental changes.

Uploaded by

baxila9033
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)
40 views

C++ Important Topics For Placements.

1. Inline functions allow code to be inserted or substituted at the point of call to reduce function call overhead. 2. Functors are objects that can be treated like functions or function pointers. 3. Const member functions do not modify the object they are called on and help prevent accidental changes.

Uploaded by

baxila9033
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/ 5

Inline Functions in C++

C++ provides an inline functions to reduce the function call overhead. Inline function is a function that is
expanded in line when it is called. When the inline function is called whole code of the inline function
gets inserted or substituted at the point of inline function call.

Functors
Functors are objects that can be treated as though they are a function or function pointer.
Ex:- comparator

Const member functions in C++


A function becomes const when const keyword is used in function’s declaration. The idea of const
functions is not allow them to modify the object on which they are called. It is recommended practice to
make as many functions const as possible so that accidental changes to objects are avoided.
When a function is declared as const, it can be called on any type of object. Non-const functions can only
be called by non-const objects.

What is Array Decay?


The loss of type and dimensions of an array is known as decay of an array.
How to prevent Array Decay?
A typical solution to handle decay is to pass size of array also as a parameter and not use sizeof on array
parameters.

References in C++
When a variable is declared as reference, it becomes an alternative name for an existing variable
References vs Pointers
Both references and pointers can be used to change local variables of one function inside another
function. Both of them can also be used to save copying of big objects when passed as arguments to
functions or returned from functions, to get efficiency gain.

1. Despite above similarities, there are following differences between references and pointers.
2. Memory Address: A pointer has its own memory address and size on the stack whereas a
reference shares the same memory address (with the original variable) but also takes up some
space on the stack. References may be passed to functions, stored in classes, etc. in a manner very
similar to pointers. Pointer is an independent variable and can be assigned NEW address values;
whereas a reference, once assigned, can never refer to any new object until the variable goes out
of scope.

Difference in Reference variable and pointer variable


References are generally implemented using pointers. A reference is same object, just with a different
name and reference must refer to an object. Since references can’t be NULL, they are safer to use.
1. A pointer can be re-assigned while reference cannot, and must be assigned at initialization only.
2. Pointer can be assigned NULL directly, whereas reference cannot.
3. Pointers can iterate over an array, we can use ++ to go to the next item that a pointer is pointing to.
4. A pointer is a variable that holds a memory address. A reference has the same memory address as
the item it references.
5. A pointer to a class/struct uses ‘->'(arrow operator) to access it’s members whereas a reference
uses a ‘.'(dot operator)
6. A pointer needs to be dereferenced with * to access the memory location it points to, whereas a
reference can be used directly.

A pointer can be declared as void but a reference can never be void


1. References are less powerful than pointers

KUNDAN_THAKUR 1
Once a reference is created, it cannot be later made to reference another object; it cannot be
reseated. This is often done with pointers.
2. References cannot be NULL. Pointers are often made NULL to indicate that they are not pointing
to any valid thing.
3. A reference must be initialized when declared. There is no such restriction with pointers

Due to the above limitations, references in C++ cannot be used for implementing data structures like
Linked List, Tree, etc. In Java, references don’t have above restrictions,

1. References are safer and easier to use:


Safer: Since references must be initialized, wild references like wild pointers are unlikely to exist.
It is still possible to have references that don’t refer to a valid location (See questions 5 and 6 in
the below exercise )
2. Easier to use: References don’t need dereferencing operator to access the value. They can be used
like normal variables. ‘&’ operator is needed only at the time of declaration. Also, members of an
object reference can be accessed with dot operator (‘.’), unlike pointers where arrow operator (->)
is needed to access members.

To avoid Object Slicing: If we pass an object of subclass to a function that expects an object of superclass
then the passed object is sliced if it is pass by value. For example, consider the following program, it
prints “This is Pet Class”.

Smart Pointers in C++


Using smart pointers, we can make pointers to work in way that we don’t need to explicitly call delete.
Smart pointer is a wrapper class over a pointer with operator like * and -> overloaded. The objects of
smart pointer class look like pointer, but can do many things that a normal pointer can’t like automatic
destruction (yes, we don’t have to explicitly use delete), reference counting and more.
The idea is to make a class with a pointer, destructor and overloaded operators like * and ->. Since
destructor is automatically called when an object goes out of scope, the dynamically allocated memory
would automatically deleted (or reference count can be decremented). Consider the following simple
smartPtr class.
#include<iostream>
using namespace std;

class SmartPtr
{
int *ptr; // Actual pointer
public:
// Constructor: Refer https://www.geeksforgeeks.org/g-
fact-93/
// for use of explicit keyword
explicit SmartPtr(int *p = NULL) { ptr = p; }

// Destructor
~SmartPtr() { delete(ptr); }

// Overloading dereferencing operator


int &operator *() { return *ptr; }

KUNDAN_THAKUR 2
};

int main()
{
SmartPtr ptr(new int());
*ptr = 20;
cout << *ptr;

// We don't need to call delete ptr: when the object


// ptr goes out of scope, destructor for it is
automatically
// called and destructor does delete ptr.

return 0;
}
20
Can we write one smart pointer class that works for all types? Yes, we can use
templates to write a generic smart pointer class. Following
C++ code demonstrates the same.

#include<iostream>
using namespace std;

// A generic smart pointer class


template <class T>
class SmartPtr
{
T *ptr; // Actual pointer
public:
// Constructor
explicit SmartPtr(T *p = NULL) { ptr = p; }

// Destructor
~SmartPtr() { delete(ptr); }

// Overloading dereferncing operator


T & operator * () { return *ptr; }

// Overloding arrow operator so that members of T can be


accessed
// like a pointer (useful if T represents a class or struct
or
// union type)
T * operator -> () { return ptr; }
};

int main()
{
SmartPtr<int> ptr(new int());

KUNDAN_THAKUR 3
*ptr = 20;
cout << *ptr;
return 0;
}

20

Smart pointers are also useful in management of resources, such as file handles or network sockets.
C++ libraries provide implementations of smart pointers in the form of auto_ptr, unique_ptr, shared_ptr
and weak_ptr

this’ pointer in C++



The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local
variable within the body of all nonstatic functions. ‘this’ pointer is a constant pointer that holds the memory address
of the current object. ‘this’ pointer is not available in static member functions as static member functions can be
called without any object (with class name).
Following are the situations where ‘this’ pointer is used:
1. When local variable’s name is same as member’s name

2. To return reference to the calling object

Object: Objects are basic run-time entities in an object oriented system, objects are instances of a class
these are defined user defined data types.
Object take up space in memory and have an associated address like a record in pascal or structure or
union in C.
When a program is executed the objects interact by sending messages to one another.
Each object contains data and code to manipulate the data. Objects can interact without having to know
details of each others data or code, it is sufficient to know the type of message accepted and type of
response returned by the objects.

Class: Class is a blueprint of data and functions or methods. Class does not take any space.
By default class variables are private but in case of structure it is public. in above example person is a
class.

Encapsulation and Data abstraction: Wrapping up(combing) of data and functions into a single unit is
known as encapsulation. The data is not accessible to the outside world and only those functions which
are wrapping in the class can access it. This insulation of the data from direct access by the program is
called data hiding or information hiding.

Data abstraction refers to, providing only needed information to the outside world and hiding
implementation details. For example, consider a class Complex with public functions as getReal() and
getImag(). We may implement the class as an array of size 2 or as two variables. The advantage of

KUNDAN_THAKUR 4
abstractions is, we can change implementation at any point, users of Complex class wont’t be affected as
out method interface remains same. Had our implementation be public, we would not have been able to
change it.

Inheritance: inheritance is the process by which objects of one class acquire the properties of objects of
another class. It supports the concept of hierarchical classification. Inheritance provides re usability. This
means that we can add additional features to an existing class without modifying it.

Polymorphism: polymorphism means ability to take more than one form. An operation may exhibit
different behaviors in different instances. The behavior depends upon the types of data used in the
operation.
C++ supports operator overloading and function overloading. Operator overloading is the process of
making an operator to exhibit different behaviors in different instances is known as operator overloading.
Function overloading is using a single function name to perform different types of tasks. Polymorphism is
extensively used in implementing inheritance.

Dynamic Binding: In dynamic binding, the code to be executed in response to function call is decided at
runtime. C++ has virtual functions to support this.

Message Passing: Objects communicate with one another by sending and receiving information to each
other. A message for an object is a request for execution of a procedure and therefore will invoke a
function in the receiving object that generates the desired results. Message passing involves specifying the
name of the object, the name of the function and the information to be sent.

1. https://www.geeksforgeeks.org/scope-resolution-operator-or-this-pointer-in-cpp/
2. https://www.geeksforgeeks.org/new-and-delete-operators-in-cpp-for-dynamic-memory/
3. https://www.geeksforgeeks.org/what-are-near-far-and-huge-pointers/
4. https://www.geeksforgeeks.org/singleton-design-pattern/
5. https://www.interviewcake.com/cpp-interview-questions

C++ THREAD
https://www.bogotobogo.com/cplusplus/C11/1_C11_creating_thread.php ( for thread most commonly
asked )

What is virtual inheritance?


Virtual inheritance facilitates you to create only one copy of each object even if the object appears
more than one in the hierarchy.

KUNDAN_THAKUR 5

You might also like