C++ 5M
C++ 5M
Disadvantage of OOP
● Increases runtime for dynamic binding mechanism
● Compiler overhead
● Runtime overhead
● Re-orientation of software developer to object oriented thinking
● Benefits only in long run while managing large software projects
● The length of the programmes developed using OOP language is much larger than the
procedural approach. Since the programme becomes larger in size, it requires more time
to be executed that leads to slower execution of the programme.
● OOP can not apply everywhere as it is not a universal language. It is applied only when it
is required. It is not suitable for all types of problems.
● Programmers need to have brilliant designing skill and programming skill along with
proper planning because using OOP is little bit tricky.
● OOPs take time to get used to it. The thought process involved in object-oriented
programming may not be natural for some people.
● Everything is treated as an object in OOP so before applying it we need to have excellent
thinking in terms of objects.
Promotion:
The variable of a smaller size or lower precision data type is converted to large size or higher
precision data type is called promotion
Demotion:
The variable of a large size or higher precision data type is converted to smaller size or lower
precision data type is called demotion
Explicit Conversion:
The type conversion is performed by the programmer explicitly in the mixed mode arithmetic
operation
Type casting is used to convert a value of particular type into desired type
C language type conversion
(type) expression
(type) variable
int x = 10, y = 3;
float z = (float) x / (float) y; // C type conversion
float f = float(x) / float(y); // C++ type conversion
3. How do you pass parameters using functions in C++? Explain with a suitable
example.
Parameter passing is a mechanism communication of data between the calling function and
called function
C++ supports three types of parameter passing
● pass by value
● pass by address
● pass by reference ( only in C++ )
Parameters are classified as
● formal parameters
● actual parameters
The formal parameters are specified in the function definition
The actual parameters are specified in the function call
1. The number of parameters in the function call and function declaration must be same
2. The data type of each parameters in the function call and in the function declaration must
be same
Pass by Value:
● The default mechanism of parameter passing is pass by value
● It does not change the value of the parameter variable in the calling function, even if the
value of the variable is changed
● The content of the actual parameters are copied to the formal parameters
● The formal parameters are stored in the the local data area of the function
● The changes made are affected only in the local data area
void swap(int x, int y)
{
int t = x;
x = y;
y = t;
}
Pass by Address:
● The address of the variable is passed as parameter
● The address of the parameter is copied
● The dereferencing operator is used to access the values
void swap(int *x, int *y)
{
int t = *x;
*x = *y;
*y = t;
}
Pass by Reference:
● Passing parameter by reference has the functionality of pass - by - address and syntax of
call by value
● Any modification made by the formal parameter is reflected in the actual parameter
● The function call is similar to that of call by value
● In the function declaration, the formal parameters are received by reference using &
operator
● The reference type formal parameters are accessed in the same way as normal variables
● Any modification to the formal parameter will affect actual parameter
Unit II
1. Describe the two ways of defining member functions.
class class_name
{
…………….
static data_type datamember;
……………..
};
class s
{
public:
static int cnt;
static void increment()
{
cnt++;
}
};
class A
{
public:
friend void decrement(A &); // friend function
};
Unit III
1. Write short notes on manipulators.
● Manipulating the formatting state associated with a stream
● The manipulators are functions that can be used with << or >>
● Manipulators are special functions that are specifically designed to modify the working of
a stream
● All the predefined manipulators are defined in the header file iomanip.h
● Manipulators are convenient to use than ios functions
setw(int width)
Sets the width of the output field specified by the integer parameter width
The output field width is reset to 0 every time an output is performed using the << operator
setprecision(int prec):
Sets the precision used for floating point output
The number of digits to be shown after the decimal point is given by prec
setfill(int fchar):
Sets the fill character to that specified in fchar
The fill character is used to fill (or pad) the empty space in the output field
setiosflags(long flags):
The flag can be any of the flags listed in ios stream
More than one flag can be set with the same manipulator by ORing the flags
int x = 100;
cout<< hex << x << ' ' << dec << x << endl;
float f = 122.3434;
cout<< f << endl;
cout << setprecision( 5 );
cout<< f << endl;
cout<< setw( 6 ) << setfill( '0' );
cout<< setiosflags( ios::internal | ios::showbase );
cout<< hex << x << endl;
cout<< setiosflags( ios::scientific) << f << endl;
Output:
64 100
122.343
122.34
0x0064
1.22343e+02
H e l l o W o r l d
Append Mode:
The existing contents of the file remain unaffected( if already exists ) and the output pointer is set
to point to the end of the file
H e l l o W o r l d
The output pointer points next to the letter d
The C++ I/O system supports four functions for setting a file pointer to desired position inside
the file or to get the current file pointer
Data Structures
Big-Oh Notation
The big-oh notation is a method that is used to express the upper bound of the running time of an
algorithm
It is denoted by ‘O’
we can compute the maximum possible amount of time that an algorithm will take for its
completion
Definition
Consider f (n) and g(n) to be two positive functions of n, where n is the size of the input data.
Then, f (n) is big-oh of g(n), if and only if there exists a positive constant C and an integer n0 ,
such that f (n) ≤ Cg(n) and n > n0 Here, f (n) = O(g(n))
Omega Notation
The omega notation is a method that is used to express the lower bound of the running time of an
algorithm
Omega notation is denoted by ‘Ω’
Using this notation, you can compute the minimum amount of time that an algorithm will take
for its completion
Definition
Consider f(n) and g(n) to be two positive functions of n, where n is the size of the input data.
Then, f(n) is omega of g(n), if and only if there exists a positive constant C and an integer n0 ,
such that f(n) ≥ Cg(n) and n > n0. Here, f(n) = Ω(g(n))
Theta Notation
The theta notation is a method that is used to express the running time of an algorithm between
the lower and upper bounds.
Theta notation is denoted by ‘θ’.
Using this notation, we can compute the average time that an algorithm will take for its
completion.
Definition
Consider f(n) and g(n) to be two positive functions of n, where n is the size of the input data.
Then, f(n) is theta of g(n), if and only if there exists two positive constants C 1 and C 2 , such
that, C1 g(n) ≤ f(n) ≤ C2 g(n). Here, f(n) = θ(g(n)).
Unit IV
1. What is a Circular Queue? Explain.
● A circular queue is a queue whose start and end locations are logically connected with
each other.
● The start location comes after the end location.
● Circular queues remove one of the main disadvantages of array implemented queues in
which a lot of memory space is wasted due to inefficient utilization.
● if the queue is filled till its capacity, i.e., the end location, then the start location will be
checked for space, and if it is empty, the new element will be added there.
Insertion
Algorithm insert(element)
Step 1: Start
Step 2: if (front = 0 and rear = MAX-1) OR front = rear+1 goto Step 3 else goto Step 4
Step 3: Display message, “Queue is Full” and goto Step 10
Step 4: if front = NULL goto Step 5 else goto Step 6
Step 5: Set front = rear = 0
Step 6: if rear = MAX-1 goto Step 7 else goto Step 8
Step 7: Set rear = 0
Step 8: Set rear = rear + 1
Step 9: Set queue[rear] = element
Step 10: Stop
Delete:
Algorithm delete()
Step 1: Start
Step 2: if front = NULL goto Step 3 else goto Step 4
Step 3: Display message, “Queue is Empty” and goto Step 13
Step 4: Set i = queue[front]
Step 5: if front = rear goto Step 6 else goto Step 8
Step 6: Set front = rear = NULL
Step 7: Return the deleted element i and go to Step 13
Step 8: if front = MAX-1 goto Step 9 else goto Step 11
Step 9: Set front = 0
Step 10: Return the deleted element i and go to Step 13
Step 11: Set front = front + 1
Step 12: Return the deleted element i
Step 13: Stop
2. Write about operations of singly linked list.
The implementation of a linked list involves two tasks:
1. Declaring the list node
2. Defining the linked list operations
struct node
{
int INFO;
struct node *NEXT;
};
typedef struct node NODE;
Deletion:
The delete operation removes an existing element from the linked list. The following tasks are
performed while deleting an existing element:
1. The location of the element is identified.
2. The element value is retrieved. In some cases, the element value is simply ignored.
3. The link pointer of the preceding node is reset
Depending on the location from where the element is to be deleted, there are three scenarios
possible, which are:
1. Deleting an element from the beginning of the list.
2. Deleting an element from the end of the list.
3. Deleting an element somewhere from the middle of the list.
Algorithm delete(value)
Step 1: Start
Step 2: Call the search module to search the location of the node to deleted and assign it to LOC
pointer
Step 3: If LOC=NULL goto Step 4 else goto Step 5
Step 4: Return “Delete operation unsuccessful: Element not present” and Stop
Step 5: If LOC=FIRST goto Step 6 else goto Step 10
Step 6: If FIRST=LAST goto Step 7 else goto Step 8
Step 7: Set FIRST=LAST=NULL and goto Step 9
Step 8: Set FIRST=FIRST->NEXT
Step 9: Return (“Delete operation successful”) and Stop
Step 10: Set TEMP=LOC-1
Step 11: Set TEMP->NEXT=LOC->NEXT
Step 12: If LOC=LAST goto Step 13 else goto Step 14
Step 13: Set LAST=TEMP
Step 14: Return “Delete operation successful”
Step 15: Stop
Searching:
The search operation helps to find an element in the linked list. The following tasks are
performed while searching an element:
1. Traverse the list sequentially starting from the first node.
2. Return the location of the searched node as soon as a match is found.
3. Return a search failure notification if the entire list is traversed without any match.
The NEXT pointers help in traversing the linked list from start till end.
Print:
The print operation prints or displays the linked list elements on the screen.
To print the elements, the linked list is traversed from start till end using NEXT pointers
Algorithm print ()
Step 1: Start
Step 2: If FIRST=NULL goto Step 3 else goto Step 4
Step 3: Display (“Empty List”) and Stop
Step 4: If FIRST=LAST goto Step 5 else goto Step 6
Step 5: Display (FIRST->INFO) and Stop
Step 6: Set PTR=FIRST
Step 7: Repeat Steps 8-9 until PTR!=LAST
Step 8: Display (PTR->INFO)
Step 9: Set PTR=PTR->NEXT
Step 10: Display (LAST->INFO)
Step 11: Stop
a new data type called NODE that represents a doubly linked list node.
The node structure contains three members
1. INFO for storing integer data values,
2. NEXT for storing address of the next node, and
3. PREVIOUS for storing the address of the previous node.
Insert:
The new node pointer’s PREVIOUS and NEXT is also required to be updated for the new node
at the time of insertion.
Delete:
The Selected node’s pointer PREVIOUS and NEXT of the adjacent node is required to be
updated at the time of deletion.
Also, define root(T1), ..., root(Tm) to be the children of root(T), with root(Ti) being the i-th
child. The nodes root(T1), ..., root(Tm) are siblings.
It is often more convenient to represent an ordered tree as a rooted binary tree, so that each
node can be stored in the same amount of memory. The conversion is performed by the
following steps:
1. remove all edges from each node to its children;
2. for each node, add an edge to its first child in T (if any) as the left child;
3. for each node, add an edge to its next sibling in T (if any) as the right child
In most cases, the height of the tree (the number of edges in the longest root-to-leaf path)
increases after the conversion. This is undesirable because the complexity of many algorithms on
trees depends on its height.
Tree is a non-linear data structure which organizes data in hierarchical structure and this is a
recursive definition.
A tree data structure can also be defined as follows...
A tree is a finite set of one or more nodes such that:
There is a specially designated node called the root. The remaining nodes are partitioned into
n>=0 disjoint sets T1, ..., Tn, where each of these sets is a tree. We call T1, ..., Tn are the
subtrees of the root.
Tree Representations
A tree data structure can be represented in two methods. Those methods are as follows...
1.List Representation
2. Left Child - Right Sibling Representation
Consider the following tree...
1. List Representation
In this representation, we use two types of nodes one for representing the node with data and
another for representing only references. We start with a node with data from root node in the
tree. Then it is linked to an internal node through a reference node and is linked to any other
node directly. This process repeats for all the nodes in the tree.
The above tree example can be represented using List representation as follows...
2. Left Child - Right Sibling Representation
In this representation, we use list with one type of node which consists of three fields namely
Data field, Left child reference field and Right sibling reference field. Data field stores the actual
value of a node, left reference field stores the address of the left child and right reference field
stores the address of the right sibling node. Graphical representation of that node is as follows...
In this representation, every node's data field stores the actual value of that node. If that node has
left child, then left reference field stores the address of that left child node otherwise that field
stores NULL. If that node has right sibling then right reference field stores the address of right
sibling node otherwise that field stores NULL.
Hashing is a common method of accessing data records using the hash table. Hashing can be
used to build, search, or delete from a table.
Hash Table:
A hash table is a data structure that stores records in an array, called a hash table. A Hash table
can be used for quick insertion and searching.
Hash Function:
● It is a method for computing table index from key.
● A good hash function is simple, so it can be computed quickly
● The major advantage of hash tables is their speed.
● If the hash function is slow, this speed will be degraded.
● The purpose of a hash function is to take a range of key values and transform them into
index values in such a way that the key values are distributed randomly across all the
indices of the hash table.
There are many hash functions approaches as follows:
1. Division Method
2. Mid Square Method
3. Folding Method
4. Radix Hashing
1. Division Method:
Mapping a key K into one of m slots by taking the remainder of K divided by m.
h(K) = K mod m
3. Folding Method:
Divide the key K into some sections, besides the last section having the same length.
Then, add these sections together.
4. Radix Hashing:
Transform the number into another base and then divide by the maximum address