Course File - Theory - Title Page
Course File - Theory - Title Page
SECTION : ECE
DESIGNATION : ASST.PROF
1|Page
Vi Institute of Technology
96, Sirunkundram Village, Chengalpattu Tk, Pincode - 603 108
Ph: +91-44-2740 1695 / 1696, FAX: 91-44– 2740 1696
S. No CONTENTS Page No
1 Title Page 3
2 Syllabus 4
3 Lesson Plan & Coverage 5-11
4 Students Nominal Roll 12
5 Subject Handlers of Yester Years 13
6 Assignment Plan 14
7 Self Study Topics 15
8 Internal I - Question Paper & Key 16-27
9 Internal I - Marks Statement 28
10 Internal I - Result Analysis & Corrective Action 29
11 Internal II - Question Paper & Key 30-40
12 Internal II - Marks Statement 41
13 Internal II - Result Analysis & Corrective Action 42
14 Model Exam - Question Paper & Key 43-52
15 Model Exam - Mark Statement 53
16 Model Exam – Result Analysis & Corrective Action 54
17 Final University Internal Mark Statement 55
18 University Question Paper
19 University Exam - Mark Statement
20 University Exam – Result Analysis
21 Notes of Lesson Unit I to V
Sample of 3 Answer Booklets & Assignment Papers –
22 i) Best ii) Medium iii) Poor Assignment
2|Page
Vi Institute of Technology
96, Sirunkundram Village, Chengalpattu Tk, Pincode - 603 108
Ph: +91-44-2740 1695 / 1696, FAX: 91-44– 2740 1696
3|Page
Vi Institute of Technology
96, Sirunkundram Village, Chengalpattu Tk, Pincode - 603 108
SYLLABUS
Ph: +91-44-2740 1695 / 1696, FAX: 91-44– 2740 1696
EC 2202 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
4|Page
Vi Institute of Technology
96, Sirunkundram Village, Chengalpattu Tk, Pincode - 603 108
Ph: +91-44-2740 1695 / 1696,
Vi INSTITUTE FAX: 91-44– 2740 1696
OF TECHOLOGY
LESSON PLAN
Branch : ECE
Semester : III
6. SATURDAY - -
5|Page
SYLLABUS:
Introduction- Tokens-Expressions-contour Structures –
Functions in C++, classes and objects, constructors and
destructors, operator overloading and type conversions.
Objective: To learn to program in C++ and basic OOPS concepts.
10. TEST-I 60 m - -
SYLLABUS:
6|Page
Inheritance, Extending classes, Pointers, Virtual functions and
polymorphism, File Handling Templates ,Exception handling,
Manipulating strings.
SYLLABUS:
7|Page
Algorithm, Analysis, Lists, Stacks and queues, Priority queues-Binary Heap-
Application, Heaps–hashing-hash tables without linked lists
Objective: To learn how various data structures can be implemented and also
to analyze the efficiency of algorithms.
SYLLABUS:
8|Page
Trees-Binary trees, search tree ADT, AVL trees, Graph
Algorithms-Topological sort, shortest path algorithm
network flow problems-minimum spanning tree -
Introduction to NP - completeness.
Objective: To implement various types of trees and also to learn about graphs and
their applications.
SYLLABUS:
9|Page
Sorting – Insertion sort, Shell sort, Heap sort, Merge sort, Quick sort,
Indirect sorting, Bucket sort, Introduction to Algorithm Design
Techniques –Greedy algorithm (Minimum Spanning Tree), Divide
and Conquer (Merge Sort), Dynamic Programming (All pairs
Shortest Path Problem).
TEXT BOOK
10 | P a g e
Vi Institute of Technology
2. E. Balagurusamy, “ Object Oriented Programming with C++”, McGraw
96,Ltd.,
Hill Company Sirunkundram
2007. Village, Chengalpattu Tk, Pincode - 603 108
Ph: +91-44-2740 1695 / 1696, FAX: 91-44– 2740 1696
REFERENCES
2000
Publication
11 | P a g e
STUDENTS NOMINAL ROLL
Vi Institute of Technology
96, Sirunkundram
Year & Branch with Section: II &ECE Village, Chengalpattu
Semester: III Tk,Academic
PincodeYear:
- 6032010-2011
108
S. No Reg. No Ph:Name
+91-44-2740 1695 / 1696,S.FAX:
of the Student No 91-44– 2740 1696
Reg. No Name of the Student
1 47609106001 CHITRAKUMAR.D 31 LE1 JOHNSIRANI.S
2 47609106002 DHINESHKUMAR.M 32
3 47609106003 DHINESH.V 33
4 47609106004 DIVYA.V 34
5 47609106005 JENNIFER JANE.J 35
6 47609106006 KARTHIK..M 36
7 47609106007 MANOHARAN.E 37
8 47609106008 NALANKILLI.E 38
9 47609106009 PARTHIBAN.P 39
10 47609106010 RAJA.G 40
11 47609106011 RAMANUJAM.P 41
12 47609106012 RAVIKUMAR.E 42
13 47609106014 SARASCHANDRA.M 43
14 47609106015 SATHISHKUMAR.C 44
15 47609106016 SELVAM.S 45
16 47609106017 SIVAPRALASH.A 46
17 47609106018 SRIRAM.T 47
18 47609106019 VEERASWAMY.P.M 48
19 47609106020 KAMESHKANNAN.S 49
20 LE1 VINAYAGAM 50
LE1 KUMARESAN
21 51
LE1 AUGESTINE
22 52
LE1 SATHISHKUMAR.R
23 53
LE1 SARAVANAN
24 54
LE1 ALLABAKSH
25 55
LE1 SATHISHKUMAR.T
26 56
LE1 JANAKIRAMAN
27 57
LE1 DILEEP SESARAJ
28 58
LE1 RAJARAJESWARAN
29 59
LE1 AMANULLA
30 60
13 | P a g e
ASSIGNMENT PLAN
Academic Year & Branch: 2010 – 2011 / ECE Semester & Section: 3nd Sem / ECE
Subject Name: Data Structures & OOP IN C++ Subject Code : EC2202
Date of
Unit Roll Nos. / Books / Journal to be Announce Date of
No Assignment Topics Batch Referred ment Submission
E. Balagurusamy, “
Object Oriented
Programming with
1. Functions in C++ All C++” 8/7/2010 13/7/2010
E. Balagurusamy, “
Object Oriented
Virtual functions and Programming with
2 polymorphism All C++” 1/10/2010 6/10/2010
Mark Allen Weiss,
“Data Structures and
28/7/2010 2/8/2010
Algorithm Analysis
3 Binary Heap All in C”
6/9/2010 11/9/2010
4 Graph Algorithms All Browse and find
Data structures and
algorithms by Mark
5 Greedy algorithm All Allien Weiss 21/9/2010 27/9/2010
14 | P a g e
Vi Institute of Technology
96, Sirunkundram Village, Chengalpattu Tk, Pincode - 603 108
Ph: +91-44-2740 1695 / 1696, FAX: 91-44– 2740 1696
Academic Year & Branch: 2010 – 2011 / ECE Semester & Section: 3nd Sem / ECE
Subject Name: Data Structures & OOP IN C++ Subject Code : EC2202
Unit
No Self Study Topics Roll Nos. / Batch Books / Journal to be Referred
E. Balagurusamy, “
Object Oriented
1. Control Structures All Programming with C++”
Mark Allen Weiss, “Data
Structures and Algorithm
2. Trees All Analysis in C”
Mark Allen Weiss, “Data
Structures and Algorithm
3. Dynamic Programming All Analysis in C”
15 | P a g e
Vi Institute of Technology
96, Sirunkundram Village, Chengalpattu Tk, Pincode - 603 108
Ph: +91-44-2740 1695 / 1696, FAX: 91-44– 2740 1696
PART A (5*2=10)
PART B (8*1=8)
6. Give array representation and implementation of Circular queue
(Or)
Give array representation and implementation of Stack
Part C (16*2=32)
7. Explain with suitable examples of basic heap operations and write algorithms
(Or)
How would you implement a stack of queues? Write routines for creation and
inserting of elements into it.
16 | P a g e
Vi Institute of Technology
96, Sirunkundram Village, Chengalpattu Tk, Pincode - 603 108
Ph: +91-44-2740 1695 / 1696, FAX: 91-44– 2740 1696
1. Define Algorithm :-
Definition: A set of instructions for solving a problem
Binary heap Binomial heap D-ary heap Fibonacci heap Leftist heap
5. Priority queue
An abstract data type to efficiently support finding the item with the highest
priority across a series of operations.
PART B (8*1=8)
17 | P a g e
6. Implementation of Circular queue :-
#include <stdio.h>
#include<ctype.h>
# define MAXSIZE 200
int cq[MAXSIZE];
int front,rear;
void main()
{
void add(int,int [],int,int,int);
int del(int [],int ,int ,int );
int will=1,i,num;
front = 1;
rear = 1;
clrscr();
printf("Program for Circular Queue demonstration through array");
while(will ==1)
{
printf("MAIN MENU: 1.Add element to Circular Queue
2.Delete element from the Circular Queue");
scanf("%d",&will);
switch(will)
{
case 1:
printf("Enter the data... ");
scanf("%d",&num);
add(num,cq,MAXSIZE,front,rear);
break;
case 2: i=del(cq,MAXSIZE,front,rear);
printf("Value returned from delete function is %d ",i);
break;
default:
printf("Invalid Choice . ");
}
18 | P a g e
printf(" Do you want to do more operations on Circular Queue ( 1 for
yes, any other key to exit) ");
scanf("%d" , &will);
} //end of outer while
} //end of main
void add(int item,int q[],int MAX,int front,int rear)
{
rear++;
rear= (rear%MAX);
if(front ==rear)
{
printf("CIRCULAR QUEUE FULL");
return;
}
else
{
cq[rear]=item;
printf("
Rear = %d Front = %d ",rear,front);
}}
int del(int q[],int MAX,int front,int rear)
{
int a;
if(front == rear)
{
printf("CIRCULAR STACK EMPTY");
return (0);
}else
{
front++;
front = front%MAX;
a=cq[front];
return(a);
printf("Rear = %d Front = %d ",rear,front);
}
(or)
19 | P a g e
Implementation of Stack
#include<stdio.h>
#include<conio.h>
#define MAX 5
Int top = -1;
int stack_arr[MAX];
main()
{
int choice;
while(1)
{
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}}/*End of while*/
}/*End of main()*/
push()
20 | P a g e
{
int pushed_item;
if(top == (MAX-1))
printf("Stack Overflow\n");
else
{
printf("Enter the item to be pushed in stack : ");
scanf("%d",&pushed_item);
top=top+1;
stack_arr[top] = pushed_item;
}
}/*End of push()*/
pop()
{
if(top == -1)
printf("Stack Underflow\n");
else
{
printf("Popped element is : %d\n",stack_arr[top]);
top=top-1;
}
}/*End of pop()*/
display()
{
int i;
if(top == -1)
printf("Stack is empty\n");
else
{
printf("Stack elements :\n");
for(i = top; i >=0; i--)
printf("%d\n", stack_arr[i] );
}
}/*End of display()*/
21 | P a g e
Part C (16*2=32)
7. Basic heap operations and algorithms:-
/* selection phase; repeatedly remove x[0] , insert it in its proper position*/
/* and adjust the heap */
# include<stdio.h>
# include<conio.h>
# define MAX 5
int queue_arr[MAX];
int rear = -1;
int front = -1;
main()
{
int choice;
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
insert();
break;
case 2 :
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
23 | P a g e
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
insert()
{
int added_item;
if (rear==MAX-1)
printf("Queue Overflow\n");
else
{
if (front==-1) /*If queue is initially empty */
front=0;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
rear=rear+1;
queue_arr[rear] = added_item ;
}
}/*End of insert()*/
del()
{
if (front == -1 || front > rear)
{
printf("Queue Underflow\n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_arr[front]);
front=front+1;
}
}/*End of del() */
display()
24 | P a g e
{
int i;
if (front == -1)
printf("Queue is empty\n");
else
{
printf("Queue is :\n");
for(i=front;i<= rear;i++)
printf("%d ",queue_arr[i]);
printf("\n");}
In normal algebra we use the infix notation like a+b*c. The corresponding postfix notation is
abc*+. The algorithm for the conversion is as follows :
(After all characters are scanned, we have to add any character that the stack may have
to the Postfix string.) If stack is not empty add topStack to Postfix string and Pop
the stack. Repeat this step as long as stack is not empty.
Return the Postfix string.
Example :
Let us see how the above algorithm will be imlemented using an example.
Infix String : a+b*c-d
Initially the Stack is empty and our Postfix string has no characters. Now, the first character
scanned is 'a'. 'a' is added to the Postfix string. The next character scanned is '+'. It being an
operator, it is pushed to the stack.
25 | P a g e
Postfix String
Stack
Next character scanned is 'b' which will be placed in the Postfix string. Next character is '*'
which is an operator. Now, the top element of the stack is '+' which has lower precedence
than '*', so '*' will be pushed to the stack.
Postfix String
Stack
The next character is 'c' which is placed in the Postfix string. Next character scanned is '-'. The
topmost character in the stack is '*' which has a higher precedence than '-'. Thus '*' will be
popped out from the stack and added to the Postfix string. Even now the stack is not empty.
Now the topmost element of the stack is '+' which has equal priority to '-'. So pop the '+' from
the stack and add it to the Postfix string. The '-' will be pushed to the stack.
Postfix String
Stack
Next character is 'd' which is added to Postfix string. Now all characters have been scanned so we
must pop the remaining elements from the stack and add it to the Postfix string. At this stage
we have only a '-' in the stack. It is popped out and added to the Postfix string. So, after all
characters are scanned, this is how the stack and Postfix string will be :
Postfix String
Stack
End result :
27 | P a g e
Vi Institute of Technology
96, Sirunkundram Village, Chengalpattu Tk, Pincode - 603 108
INTERNAL1695
Ph: +91-44-2740 MARK I STATEMETNT
/ 1696, FAX: 91-44– 2740 1696
Class - Section: II -ECE Semester: III Subject: Data Structures & OOP IN C++
S.
No Reg. No Name of the Student IT 1
1 47609106001 CHITRAKUMAR.D 30
2 47609106002 DHINESHKUMAR.M 02
3 47609106003 DHINESH.V 50
4 47609106004 DIVYA.V 80
5 47609106005 JENNIFER JANE.J 80
6 47609106006 KARTHIK..M 55
7 47609106007 MANOHARAN.E 36
8 47609106008 NALANKILLI.E 10
9 47609106009 PARTHIBAN.P 14
10 47609106010 RAJA.G 16
11 47609106011 RAMANUJAM.P 19
12 47609106012 RAVIKUMAR.E 50
13 47609106014 SARASCHANDRA.M 66
14 47609106015 SATHISHKUMAR.C 50
15 47609106016 SELVAM.S 11
16 47609106017 SIVAPRALASH.A 06
17 47609106018 SRIRAM.T 50
18 47609106019 VEERASWAMY.P.M 15
19 47609106020 KAMESHKANNAN.S 70
20 LE1 VINAYAGAM -
21 LE1 KUMARESAN -
22 LE1 AUGESTINE -
23 LE1 SATHISHKUMAR.R -
24 LE1 SARAVANAN -
25 LE1 ALLABAKSH -
26 LE1 SATHISHKUMAR.T -
27 LE1 JANAKIRAMAN -
28 LE1 DILEEP SESARAJ -
29 LE1 RAJARAJESWARAN -
30 LE1 AMANULLA -
31 LE1 JOHNSIRANI.S -
28 | P a g e
Vi Institute of Technology
96, Sirunkundram Village, Chengalpattu Tk, Pincode - 603 108
INTERNAL EXAM I RESULT ANALYSIS
Ph: +91-44-2740 1695 / 1696, FAX: 91-44– 2740 1696
Department : ECE Date of Exam :
Subject Title : DATA STRUCTURES & OOPS IN C++ Subject Code : EC2202
Name of Exam: IT 1
29 | P a g e
Vi Institute of Technology
96, Sirunkundram Village, Chengalpattu Tk, Pincode - 603 108
Ph: +91-44-2740 1695 / 1696, FAX: 91-44– 2740 1696
CYCLE TEST II -QUESTION PAPER
YEAR/SEM : II Year/ III SEM
EC2202-Data Structures & OOPS in C++
Date: Session: FN
PART A (5*2=10)
PART B (8*1=8)
6. Explain Merge Sort with example
(Or)
Explain Quick Sort with example
Part C (16*2=32)
7. Explain Prim’s & Kruskal’s algorithm for computing the MST weighted undirected
graph?
(Or)
Write a C++ program for Different methods of traversing a Binary Search Tree.
8. Write insertion algorithm for AVL tree .Write Suitable rotations algorithm.
(Or)
Show the result of inserting 2,1,4,5,9,3,6,7 into a empty AVL tree
30 | P a g e
CYCLE TEST II –ANSWER KEY
YEAR/SEM : II Year/ III SEM
EC2202-Data Structures & OOPS in C++
PART A (5*2=10)
1. Dynamic Programming:-
Dynamic programming is both a mathematical optimization method and a
computer programming method. In both contexts it refers to simplifying a
complicated problem by breaking it down into simpler subproblems in a
recursive manner
It contains an unique node which has no incoming edge but have all the outgoing
edges.This node will become the root of the tree
Remaining nodes of the graph have at the most one incoming edges
31 | P a g e
Merge sort is an O(n log n) comparison-based sorting algorithm. In most
implementations it is stable, meaning that it preserves the input order of equal
elements in the sorted output
Algorithm mergeSort(S, C)
Input sequence S with n
elements, comparator C
Output sequence S sorted
according to C
if S.size() > 1
(S1, S2) ← partition(S, n/2)
mergeSort(S1, C)
mergeSort(S2, C)
S ← merge(S1, S2)
Algorithm merge(A, B)
Input sequences A and B with
n/2 elements each
Output sorted sequence of A ∪ B
S ← empty sequence
while ¬A.isEmpty() ∧ ¬B.isEmpty()
if A.first().element() < B.first().element()
S.insertLast(A.remove(A.first()))
else
S.insertLast(B.remove(B.first()))
while ¬A.isEmpty()
S.insertLast(A.remove(A.first()))
while ¬B.isEmpty()
S.insertLast(B.remove(B.first()))
(Or)
Quick Sort with Example:-
32 | P a g e
Quick-Sort
Quick-sort is a randomized sorting algorithm based on the divide-and-conquer
paradigm:
Divide: pick a random element x (called pivot) and partition S into
L elements less than x
E elements equal x
G elements greater than x
Recur: sort L and G
Conquer: join L, E and G
Algorithm partition(S, p)
Input sequence S, position p of pivot
Output subsequences L, E, G of the
elements of S less than, equal to,
or greater than the pivot, resp.
L, E, G ← empty sequences
x ← S.remove(p)
while ¬S.isEmpty()
y ← S.remove(S.first())
if y < x
L.insertLast(y)
else if y = x
E.insertLast(y)
else { y > x }
G.insertLast(y)
An execution of quick-sort is depicted by a binary tree
Each node represents a recursive call of quick-sort and stores
Unsorted sequence before the execution and its pivot
Sorted sequence at the end of the execution
The root is the initial call
The leaves are calls on subsequences of size 0 or 1
Part C (16*2=32)
1. Set i=0, S0= {u0=s}, L(u0)=0, and L(v)=infinity for v <> u0. If |V| = 1 then stop,
otherwise go to step 2.
2. For each v in V\Si, replace L(v) by min{L(v), dvui}. If L(v) is replaced, put a label
(L(v), ui) on v.
3. Find a vertex v which minimizes {L(v): v in V\Si}, say ui+1.
4. Let Si+1 = Si cup {ui+1}.
5. Replace i by i+1. If i=|V|-1 then stop, otherwise go to step 2.
Kruskal's Algorithm
34 | P a g e
#include<stdio.h> // including headerfiles
#include<conio.h>
#include<stdlib.h>
struct tree //creating structure
{
int data; //data field of node
struct tree *lchild,*rchild; //left child & right child of node
};
struct tree *insert(struct tree *p,int n); //function for insertion
void inorder(struct tree *p); //function for inorder traversal
void preorder(struct tree *p); // function for preorder traversal
void postorder(struct tree *p); // function for postorder traversal
35 | P a g e
}
break;
case 3:
{
printf("The postorder traversal is\n");
postorder(root); // function calling
}
break;
}
getch();
}
//function definition for insertion
struct tree *insert(struct tree *p,int n)
{
static struct tree *temp1,*temp2;
if(p==NULL)
{
p=(struct tree *)malloc(sizeof(struct tree));
p->data=n;
p->lchild=p->rchild=NULL;
}
else
{
temp1=p;
while(temp1!=NULL)
{
temp2=temp1;
if(n<temp1->data)
temp1=temp1->lchild;
else
temp1=temp1->rchild;
}
if(temp2->data>n)
{
temp2->lchild=(struct tree *)malloc(sizeof(struct tree));
temp2=temp2->lchild;
temp2->data=n;
temp2->lchild=temp2->rchild=NULL;
}
else
{
temp2->rchild=(struct tree *)malloc(sizeof(struct tree));
temp2=temp2->rchild;
36 | P a g e
temp2->data=n;
temp2->lchild=temp2->rchild=NULL;
}
}
return p;
}
//function definition for inorder traversal
37 | P a g e
An AVL tree is a self-balancing binary search tree, and it was the first such
data structure to be invented.[1] In an AVL tree, the heights of the two child
subtrees of any node differ by at most one; therefore, it is also said to be height-
balanced
1. LL: new node is in the left subtree of the left subtree of A
2. LR: new node is in the right subtree of the left subtree of A
3. RR: new node is in the right subtree of the right subtree of A
4. RL: new node is in the left subtree of the right subtree of A
38 | P a g e
InsertAVL
TreeNode *InsertAVL(TreeNode *root, TreeNode *newnode, Boolean *taller)
{
if (!root) {
root = newnode;
root->left = root->right = NULL;
root->bf = EH;
*taller = TRUE;
} else if (EQ(newnode->entry.key, root->entry.key)) {
Error("Duplicate key is not allowed in AVL tree.");
} else if (LT(newnode->entry.key, root->entry.key)) {
root->left = InsertAVL(root->left, newnode, taller);
if (*taller) /* Left subtree is taller. */
switch(root->bf) {
case LH: /* Node was left high. */
root = LeftBalance(root, taller); break;
case EH:
root->bf = LH; break; /* Node is now left high. */
case RH:
root->bf = EH; /* Node now has balanced height.*/
*taller = FALSE; break;
}
} else {
root->right = InsertAVL(root->right, newnode, taller);
if (*taller) /* Right subtree is taller. */
switch(root->bf) {
case LH:
root->bf = EH; /* Node now has balanced height.*/
*taller = FALSE; break;
case EH:
root->bf = RH; break; /* Node is right high. */
case RH: /* Node was right high. */
root = RightBalance(root, taller); break;
}}
return root;}
39 | P a g e
(Or)
2, 1,4,5,9,3,6,7 into a empty AVL tree
40 | P a g e
ViInstitute
Vi InstituteofofTechnology
Technology
96,Sirunkundram
96, SirunkundramVillage,
Village,Chengalpattu
ChengalpattuTk,
Tk,Pincode
Pincode- 603
- 603108
108
Ph:+91-44-2740
Ph: +91-44-27401695
1695/ 1696,
/ 1696,FAX:
FAX:91-44–
91-44–2740
27401696
1696
41 | P a g e
Vi Institute of Technology
96, Sirunkundram Village, Chengalpattu Tk, Pincode - 603 108
Ph: +91-44-2740 1695 / 1696, FAX: 91-44– 2740 1696
INTERNAL EXAM II RESULT ANALYSIS
Subject Title : DATA STRUCTURES & OOPS IN C++ Subject Code : EC2202
Name of Exam: IT II
42 | P a g e
CYCLE TEST III -QUESTION PAPER
YEAR/SEM : II Year/ III SEM
EC2202-Data Structures & OOPS in C++
Date: Session: FN
PART A (5*2=10)
PART B (8*1=8)
6. What is a friend function? What are the merits and demerits of using friend
function?
(Or)
Define a class ‘string ‘Use overload ‘==’ operator to compare two strings
Part C (16*2=32)
43 | P a g e
Vi Institute of Technology
96, Sirunkundram Village, Chengalpattu Tk, Pincode - 603 108
Ph: +91-44-2740 1695 / 1696, FAX: 91-44– 2740 1696
CYCLE TEST III – ANSWER KEY
YEAR/SEM : II Year/ III SEM
EC2202-Data Structures & OOPS in C++
PART A (5*2=10)
#include
class exforsys
{
private:
int a,b;
public:
void test()
{
a=100;
b=200;
}
friend int compute(exforsys e1)
//Friend Function Declaration with keyword friend and with the object of class
exforsys to which it is friend passed to it
};
main()
{
exforsys e;
e.test();
cout<<"The result is:"<<COMPUTE(E);
//Calling of Friend Function with object as argument.
}
(Or)
Class ‘string ‘Use overload ‘==’ operator to compare two strings
45 | P a g e
C++ strings can be compared and assigned with the standard comparison operators:
==, !=, <=, >=, <, >, and =. Performing a comparison or assigning one string to another
takes linear time.
In addition to the normal container operators, strings can also be concatenated with the
+ operator and fed to the C++ I/O stream classes with the << and >> operators.
For example, the following code concatenates two strings and displays the result:
Furthermore, strings can be assigned values that are other strings, character arrays, or
even single characters. The following code is perfectly valid:
char ch = 'N';
string s;
s = ch;
Individual characters of a string can be examined with the [] operator, which runs in
constant time
Part C (16*2=32)
46 | P a g e
7. Operators overloading with syntax.
C++ also contains the type conversion operators const_cast, static_cast,
dynamic_cast, and reinterpret_cast which are not listed in the table for brevity.
The formatting of these operators means that their precedence level is
unimportant.
Operating overloading allows you to pass different variable types to the same
function and produce different results. In this article Ben gives us the low-down
on operator overloading in C++.Operator overloading is common-place among
many efficient C++ programmers. It allows you to use the same function name,
but as different functions.
If this sounds confusing, then just think about it like this: you can use the same
function name for as many functions as you like, but you *must* pass different
variable types to each function.
In this article I will show you exactly what function overloading is, and how you
can get it to work for you in C++. You should have an intermediate knowlede of
C++. Any compiler will do, as I will only use ISO-standard compliant syntax.
Sample Object
For my sample object, I'm going to implement a matrix. This won't be a full-scale
implementation of every imaginable matrix operation, but it should be enough to
cover the basics of operator overloading, and maybe whet your appetite to
complete the implementation for other operations (dot product, inverse,
determinant, etc.).
In order to completely encapsulate a matrix within a class, we actually need two
classes: Row and Matrix.
So let's start with Row:
template<class T>
class Row {
public:
Row(int cols=0):row(NULL) {SetRowSize(cols);}
~Row() {SetRowSize(0); }
Row(const Row &r):row(NULL) {
SetRowSize(r.numCols);
for (int i=0;i<numCols;i++)
row[i]=r.row[i];
}
void SetRowSize(int n) {
if(row) delete[] row;
if (n>0) {
47 | P a g e
row=new T[n];
memset(row,0,sizeof(T)*n/sizeof(char));
}
else row=NULL;
numCols=n;
}
int size() { return numCols;}
private:
int numCols;
T* row;
};
(Or)
Pointers are used to implement linked list structure.
}
49 | P a g e
void PrintDouble( double d )
{
…
}
A C++ language construct that allows the compiler to generate multiple
versions of a function by allowing parameterized data types.
• FunctionTemplate
Template < TemplateParamList >
FunctionDefinition
A C++ language construct that allows the compiler to generate
multiple versions of a class by allowing parameterized data types.
Class Template
When class a gas inherited in class has known as base class and B class is know as derived class. Here
only two classes have linked to each other.
2. Multilevel Inheritance:
In this type of inheritance, there are number of level and it has used in that cases where we want to use
all properties in number of levels according to the requirement. For example, class A inherited in class
b and class b has inherited in class c for class b so on. Where class A is base class c. In another way we
can say b is derived class a base class for c and a indirect base class for c is indirect base class for c and
c indirect derived class for class A.
3. Multiple Inheritances:
In this type of inheritance, number of classes has inherited in a single class. Where two or more classes
are, know as base class and one is derive class.
51 | P a g e
Vi Institute
4. Hierarchical Inheritance: of Technology
96, Sirunkundram Village, Chengalpattu Tk, Pincode - 603 108
This type of inheritance helps us to create a baseless for number of classes and those numbers of
Ph: +91-44-2740 1695 / 1696, FAX: 91-44– 2740 1696
classes can have further their branches of number of class.
5. Hybrid Inheritance:
In this type of inheritance, we can have mixture of number of inheritances but this can generate an
error of using same name function from no of classes, which will bother the compiler to how to use the
functions. Therefore, it will generate errors in the program. This has known as ambiguity or duplicity.
53 | P a g e
Vi Institute
Subject Title : DATA STRUCTURES & OOPS INof Technology
C++ Subject Code : EC2202
96, Sirunkundram Village, Chengalpattu Tk, Pincode - 603 108
Faculty Name : SATHIYAPRIYA.K Designation : AP/CSE
Ph: +91-44-2740 1695 / 1696, FAX: 91-44– 2740 1696
Name of Exam: IT II
55 | P a g e
56 | P a g e