OOPS
OOPS
UNIT I
7. State how data encapsulation supports reusability. (Nov/Dec 2014) (May/June 2014)
Encapsulation is defined as the wrapping of data and member functions into a single unit
associated with access specifiers. A class can be derived from the existing class. Thus
data encapsulation supports reusability.
19. Differentiate between stack and queue and give its applications.
Stack Queue
Last in First Out Data Structure First in First out data structure.
Operations: Push and Pop Operations: Insert and Delete
Inserted only one end Insertion can be done in both front end and
in rear end.
Run-Time Polymorphism.
o Run Time Polymorphism is achieved through the concept of
virtual function.
9. What are the operators that cannot be overloaded in C++? (Nov/Dec 2014)
The operators that cannot be overloaded by c++ are as follows:
Operator Operator Name
:: Scope Resolution Operator
. Member access Operator
Sizeof Object Size information
? Conditional or Ternary Operator
19. List out the library functions that handle the uncaught exceptions.
The following are the library functions that handle uncaught exceptions:
terminate( )
set_terminate( )
unexpected( )
set_unexptected
UNIT I
PART B
1. List out the difference between procedure oriented programming and object oriented
programming. (9 Marks [Nov/Dec 2016] [May/June 2016]).
Procedural Oriented Programming Object Oriented Programming
Emphasis is on algorithms Emphasis is on data.
Large problems are divided into smaller Programs are divided into objects
programs known as functions.
Data is not hidden Data is hidden and cannot be accessed by
external functions.
A function transfers the data from one form Objects communicate by passing messages.
to another form. Communication is done by
function.
Follows Top Down approach. Follows bottom up approach.
4. Write a C++ program to list out the prime numbers between the given two limits.
(8 Marks [Nov/Dec 2016] [May/June 2016]).
#include <iostream>
using namespace std;
int main()
{
int low, high, i, flag;
cout << "Enter two numbers(intervals): ";
cin >> low >> high;
cout << "Prime numbers between " << low << " and " << high << " are: ";
while (low < high)
{
flag = 0;
for(i = 2; i <= low/2; ++i)
{
if(low % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
cout << low << " ";
++low;
}
return 0;
}
6. Write a C++ program to implement a Binary Search Procedure to find whether the given
element is present in the array or not using Objects and Classes. (6 Marks [Nov/Dec
2015).
#include <iostream.h>
#include <conio.h>
class bin_search
{
int num[50],s,search;
public:
void getdata(void);
int searchfun(void);
void display(void);
};
8. What is a namespace? How do you resolve the name conflicts using namespaces?
Explain with an example. (4 Marks [Nov/Dec 2015]).
Refer Book Page.No: 3.68-3.71
9. Write a C++ program to find maximum of two numbers using inline functions. (4 Marks
[Nov/Dec 2015])
#include<iostream.h>
inline int greatest(int a, int b)
{
int r;
r=(a>b)? a:b;
return r;
}
int main( )
{
int a,b;
cout<<”enter two values”;
cin>>a>>b;
cout<<”greatest of two numbers= “<<greatest(a,b);
}
10. Write a C++ program to find the area of the square, rectangle, circle using function
overloading. (8 Marks [Nov/Dec 2015]).
#include<iostream.h>
int area(int);
int area(int,int);
float area(float);
int main()
{
int s,l,b;
float r;
cout<<”enter the side of the square”;
cin>>s;
cout<<”enter the length and breadth of rectangle”;
cin>>l>>b;
cout<<”enter the radius of circle”;
cin>>r;
cout<<”area of square”<<area(s);
cout<<”area of rectangle”<<area(l,b);
cout<<”area of circle”<<area(r);
}
int area(int s)
{
return(s*s);
}
int area(int l,int b)
{
return(l*b);
}
float area(float r)
{
return(3.14*r*r);
}
11. Briefly describe on the object oriented features supported by C++. (4 Marks [Nov/Dec
2015).
Refer Book Page.No: 2.2-2.9
12. Explain the major principles of object oriented programming with illustrations and neat
diagram. (16 Marks [May/June 2015]).
Refer Book Page.No: 2.2-2.9
13. Explain the various operators that are available in C++ with neat illustration for each it.
(16 Marks [May/June 2015]).
Refer Book Page.No: 1.8 -1.14
14. Explain briefly about various object oriented programming concepts and show C++
supports them. (16 Marks [Nov/Dec 2014])
Refer Book Page.No: 2.2-2.9
15. What are constructors and destructors? With suitable example explain various forms of
constructors. (16 Marks [Nov/Dec 2014]) (8 Marks [May/June 2014]).
Refer Book Page.No: 2.24-2.28
16. Explain the following concepts of Object Oriented Programming in detail. (16 Marks
[May/June 2014]).
a. Data abstraction
b. Inheritance
Refer Book Page.No: 2.2-2.9
17. What are the needs of object oriented paradigm? (8 Marks [May/June 2014])
Refer Book Page.No: 1.1-1.4
18. Explain the various data types/control statements/operators/ storage classes that are
available in C++.
Refer Book Page.No: 1.23-1.39
19. Analyze the concept of call by value and call by reference with an example.
Refer Book Page.No: 1.60-1.62
PART C
1. Design an algorithm and diagrammatic illustrations the various operations that can be
performed on a Stack/Queue ADT.
Refer Book Page.No: 1.84-1.89
UNIT II
PART B
1. Explain function overloading in C++ with an example. (8 Marks [Nov/Dec 2016]).
Refer Book Page.No: 2.29-2.31
2. What are constructors? Explain the concept of constructors and destructors with an
example. (8 Marks [Nov/Dec 2016] [May/June 2016] (16 Marks [Nov/Dec 2014])
Refer Book Page.No:2.24-2.28
3. Write a C++ program to overload + operator to add two complex numbers. (8 Marks
[Nov/Dec 2016]).
#include<iostream.h>
class complex
{
float x,y;
public:
complex (){}
complex (float real,float imag)
{
x=real;
y=imag;
}
complex operator +(complex);
complex operator -(complex);
void display();
};
complex complex::operator +(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
int main()
{
complex c1,c2,c3;
c1=complex(25,35);
c2=complex(1.6,2.7);
c3=c1+c2;
c1.display();
c2.display();
c3.display();
return 0;
}
4. Explain the need of iterators using sufficient examples. (8 Marks [Nov/Dec 2016]).
Refer Book Page.No:2.62-2.67
5. Explain array of objects with an example. (8 Marks [May/June 2016]).
#include <iostream.h>
const int MAX =100;
class Details
{
private:
int salary;
float roll;
public:
void getname( )
{
cout << "\n Enter the Salary:";
cin >> salary;
cout << "\n Enter the roll:";
cin >> roll;
}
void putname( )
{
cout << "Employees" << salary <<
"and roll is" << roll << '\n';
}
};
void main()
{
Details det[MAX];
int n=0;
char ans;
do{
cout << "Enter the Employee Number::" << n+1;
det[n++].getname;
cout << "Enter another (y/n)?: " ;
cin >> ans;
} while ( ans != 'n' );
for (int j=0; j<n; j++)
{
cout << "\nEmployee Number is:: " << j+1;
det[j].putname( );
}
}
6. What is operator overloading? List out the rules to overload a binary operator. (7 Marks
[May/June 2016]). (8 Marks[May/June 2014])
Refer Book Page.No:2.44-2.47
7. Write a C++ program to add two vectors using + operator overloading. (9 Marks
[May/June 2016]).
#include<iostream.h>
#include<conio.h>
class vector
{
public:
int x,y,z;
void read()
{
cout<<"\n\nEnter the magnitude of i : ";
cin>>x;
cout<<"\n\nEnter the magnitude of j : ";
cin>>y;
cout<<"\n\nEnter the magnitude of k : ";
cin>>z;
}
vector operator +(vector b)
{
vector c;
c.x=x+b.x;
c.y=y+b.y;
c.z=z+b.z;
return c;
}
};
void main()
{
vector v1,v2,v3;
cout<<"\n\nEnter the First Vector : ";
v1.read();
cout<<"\n\nEnter the Second Vector : ";
v2.read();
v3=v1+v2;
cout<<"\n\nThe Sum of Vectors : ";
v3.display();
8. Develop an abstract class Polygon from which Triangle and Rectangle are derived. Each
Polygon should contain the function Area( ) to calculate the area of them. Invoke
appropriate Area( ) function to calculate the area using pointer to base class and pointers
to derived classes. (12 Marks [Nov/Dec 2015]).
#include <iostream.h>
class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
};
class Rectangle: public Polygon {
public:
int area()
{ return width*height; }
};
class Triangle: public Polygon {
public:
int area()
{ return width*height/2; }
};
int main () {
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = ▭
Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << rect.area() << '\n';
cout << trgl.area() << '\n';
return 0;
}
9. Create a vector named Student to add the names of the students in a class. Also display
the contents of the vector after adding necessary elements. (4 Marks [Nov/Dec 2015]).
#include <iostream.h>
#include <vector.h>
int main()
{
// create a vector to store names
std::vector<char> vec;
char name[20];
for(i = 0; i < 5; i++){
vec.push_back(name);
}
// display extended size of vec
cout << "extended vector size = " << vec.size() <<"\n";
10. Explain the various types of constructors that are available in C++ with suitable
examples. (16 Marks [May/June 2015]).
Refer Book Page.No: 2.24-2.28
11. With suitable example, explain how function overloading and operator overloading
supports compile-time polymorphism. (16 Marks [Nov/Dec2014]).
Refer Book Page.No: 2.29-2.31 & 2.38-2.54
PART C
1. Develop a class Polynomial whose internal representation is a term consisting of a
coefficient and an exponent. Develop a complete class containing proper constructor and
destructor functions as well as set and get functions. Overload the addition and
subtraction operator to add and subtract to polynomials and display the results. Overload
the assignment operator to assign one polynomial to another using friend function. (16
Marks [Nov/Dec 2015]).
#include<iostream.h>
#include<conio.h>
class poly
{
int n;
int deg[20];
int coeff[20];
public:
void getdata();
void putdata();
friend void putdatasum(poly);
poly operator +(poly);
};
void poly::getdata()
{
cout<<"\n\tEnter the no. of terms:";
cin>>n;
cout<<"\n\tEnter the degrees and coefficients:";
for(int i=0;i<n;i++)
{
cin>>deg[i];
cin>>coeff[i];
}
}
void poly::putdata()
{
for(int i=0;i<n;i++)
{
if (i==0)
cout<<coeff[i]<<"x^"<<deg[i];
else if(deg[i]==0)
cout<<"+"<<coeff[i];
else
cout<<"+"<<coeff[i]<<"x^"<<deg[i];
}
}
void putdatasum(poly c)
{
for(int i=0;i<c.n;i++)
{
if (i==0)
cout<<c.coeff[i]<<"x^"<<c.deg[i];
else
cout<<"+"<<c.coeff[i]<<"x^"<<c.deg[i];
}
}
poly poly::operator +(poly c)
{
poly d;
d.n=0;
int i=0;
int j=0;
int k=0;
while(n>0 && c.n>0)
{
if (deg[i]==c.deg[j])
{
d.coeff[k]=coeff[i]+c.coeff[j];
d.deg[k]=deg[j];
d.n++;
n--;
c.n--;
i++;
j++;
k++;
}
else if (deg[i]>c.deg[j])
{
d.coeff[k]=coeff[i];
d.deg[k]=deg[i];
d.n++;
n--;
i++;
k++;
}
else
{
d.coeff[k]=c.coeff[j];
d.deg[k]=c.deg[j];
d.n++;
c.n--;
j++;
k++;
}
}
while (n>0)
{
d.coeff[k]=coeff[i];
d.deg[k]=deg[i];
d.n++;
n--;
i++;
k++;
}
while (c.n>0)
{
d.coeff[k]=c.coeff[j];
d.deg[k]=c.deg[j];
d.n++;
c.n--;
j++;
k++;
}
return(d);
}
int main()
{
clrscr();
poly A,B,C;
cout<<"\n\tPolynomial Addition\n";
cout<<"\n\n\tEnter the details of 1st polynomial:";
A.getdata();
cout<<"\n\n\tEnter the details of 2nd polynomial:";
B.getdata();
cout<<"\n\n\t1st polynomial=";
A.putdata();
cout<<"\n\n\t2nd polynomial=";
B.putdata();
cout<<"\n\n\tSum=";
C=A+B;
putdatasum(C);
getch();
return 0;
}
UNIT III
PARTB
1. Write a C++ program to generate user define exception whenever user inputs odd
numbers. (9 Marks [Nov/Dec 2016]).
#include<iostream.h>
int main()
{
int a;
cout<<"Enter the value of a:";
cin>>a;
try
{
if(a%2= = 0)
{
cout<< “ Given Number is even”;
}
else
{
throw a;
}
}
catch(int a)
{
cout<<Given Number is odd”<<a;
}
return 0;
}
3. What is inheritance? List out the advantages of inheritance. (7 Marks [May/June 2016]).
Refer Book Page.No: 3.14-3.15
7. Implement a Circular Queue with proper insertion and deletion operation using Class
Templates. (8 Marks [Nov/Dec 2015]).
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
template<class T> // Template declaration
class CircularQ
{
T k[22];
int rear,front,N;
public:
CircularQ() // Constructor Function
{
cout<<"\n What is your Circular Queue Size:?\n";
cin>>N;
rear=front=N-1;
}
void Add(T elt)
{
if((rear+1)%n!=front)
{
rear=(rear+1)%N;
k[rear]=elt;
}
else
cout<<"\n Currently the queue is Full. Unable to Add more"<<elt<<endl;
}
void Delete()
{
if(front==rear)
cout<<"\n Currently the queue is Empty.\n";
else
{
front=(front+1)%N;
cout<<"\n Removed element : "<<k[front];
}
}
void CircularQ_oprn();
void Output_Display();
};
template<class T>
void CircularQ<T> :: Output_Display()
{
if(rear!=front)
{
cout<<"\n Circular Queue Elements:\n";
for(int i=(front+1)%n;;i=(i+1)%N)
{
cout<<k[i]<<"\t";
if(i==rear) break;
}
}
else
cout<<"\n There is no Queue elements to display \n";
}
template<class T>
void Queue<T> :: CircularQ_oprn()
{
int select=1,i;
T elt;
while(select>0 && select<3)
cout<<"\n 1. For Adding Queue Value \n 2.For Deleting Queue Value \n Press any key
toExit\n Enter your Choice Here:";
cin>>select;
switch(select)
{
case 1 : // For Adding
cout<<" Enter the Element for Adding:\n";
cin>>elt;
Add(elt);
Output_Display();
break;
case 2 : // For Deletion
Delete();
Output_Display();
break;
default : exit(0);
}
}
}
void main()
{
clrscr();
cout<<"\n\t CircularQueue implementation in C++ with Arrays \n";
cout<<" Circular Queue Integer Value\n";
CircularQ<int> Q1;
cout<<"Circular Queue Float Value\n";
CircularQ<float> Q2;
int cha;
while(1)
cout<<" Circular Queue implementation \n\n";
cout<" 1. For Integer Queue \n 2. For Float Queue \n Press any key to exit\n\n Enteryour
Choice Here:";
cin>>cha;
switch(cha)
{
case 1 : // It performs the CircularQueue operation on Integer
Q1.CircularQ_oprn();
break;
case 2 : // It performs the CircularQueue operation on Float
Q2.CircularQ_oprn();
break;
default : exit(0);
}
}
}
8. Write a C++ program to accept the integer or string values from the user within a
specified range. (Range has to be specified with minimum by the user). If the input
violates the range, appropriate exception needs to be raised. (6 Marks [Nov/Dec 2015]).
#include<iostream.h>
int main()
{
int a,min,max;
cout<<"Enter the value of a:";
cin>>a;
cout<<"Enter the value of min:";
cin>>min;
cout<<"Enter the value of max:";
cin>>max;
try
{
if((min>a)&&(max<a))
{
cout<< “ Valid Number”;
}
else
{
throw a;
}
}
catch(int a)
{
cout<< “Number not in the given range”<<a;
}
return 0;
}
9. Write a C++ program to sort a list of integers, floating point numbers and characters by
Quick sort mechanism using function templates. (6 Marks [Nov/Dec 2015]).
#include<iostream.h>
#include<conio.h>
template <class w>
class quick
{
w a[50];
int n;
public:
void get();
void sort(int,int);
int partition(int,int);
void put();
};
template <class w>
void quick <w>::get()
{
int i;
cout<<“\n Enter the no of terms:”;
cin>>n;
cout<<“\n Enter the values:\n”;
for(i=1;i<=n;i++)
cin>>a[i];
sort(1,n);
}
template <class w>
void quick <w>::sort(int p,int q)
{
int j;
if(p<q)
{
j=partition(p,q+1);
sort(p,j-1);
sort(j+1,q);
}
}
template <class w>
int quick <w>::partition(int m,int p)
{
int i,j,t;
w v;
v=a[m];
i=m;j=p;
do
{
do
i++;
while(a[i]<v);
do
j–;
while(a[j]>v);
if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}while(i<j);
a[m]=a[j];
a[j]=v;
return j;
}
template <class w>
void quick<w>::put()
{
int i;
for(i=1;i<=n;i++)
cout<<a[i]<<” “;
}
void main()
{
clrscr();
quick<int>q1;
quick<float>q2;
cout<<“\n\t\t QUICK SORT USING TEMPLATES”;
cout<<“\n\t\t ~~~~~ ~~~~ ~~~~~ ~~~~~~~~~”;
q1.get();
cout<<“\n\n Sorted array of integer values:-\n”;
q1.put();
q2.get();
cout<<“\n\n Sorted array of floating values:-\n”;
q2.put();
getch();
}
10. Write short notes on the storage structures available with Standard Template Libraries. (4
Marks [Nov/Dec 2015]).
Refer Book Page.No: 3.73-3.92
11. What is a function template? Write a template function to sort arrays of float and int
using bubble sort. (16 Marks [May/June 2015]).
#include<conio.h>
#include<iostream.h>
template<class bubble>
void bubble(bubble a[], int n)
{
int i, j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
bubble element;
element = a[i];
a[i] = a[j];
a[j] = element;
}
}
}
void main()
{
int a[6]={1,2,3,4,4,3};
char b[4]={'s','b','d','e'};
clrscr();
bubble(a,6);
cout<<"\nSorted Order Integers: ";
for(int i=0;i<6;i++)
cout<<a[i]<<"\t";
bubble(b,4);
PART C
1. Implement a Dictionary named ‘Index’ which consists of key terms and its descriptions
using MAP STL. Try to display all the terms and descriptions present in the dictionary
and if a key term has been provided as an input, the corresponding description should get
displayed as an output to the user by searching the entire dictionary. (8 Marks [Nov/Dec
2015]).
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/* list of words and meanings */
char *dic[][40] = {
"atlas", "A volume of maps.",
"car", "A motorized vehicle.",
"telephone", "A communication device.",
"airplane", "A flying machine.",
"", "" /* null terminate the list */
};
int main(void)
{
char word[80], ch;
char **p;
do {
puts("\nEnter word: ");
scanf("%s", word);
p = (char **)dic;
/* find matching word and print its meaning */
do {
if(!strcmp(*p, word)) {
puts("Meaning:");
puts(*(p+1));
break;
}
if(!strcmp(*p, word)) break;
p = p + 2; /* advance through the list */
} while(*p);
if(!*p) puts("Word not in dictionary.");
printf("Another? (y/n): ");
scanf(" %c%*c", &ch);
} while(toupper(ch) != 'N');
return 0;
}
2. Explain Run-Time Polymorphism in detail. (16 Marks[May/June 2014]).
Refer Book Page.No:3.34-3.36