0% found this document useful (0 votes)
81 views33 pages

OOPS

Uploaded by

Anand Duraiswamy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views33 pages

OOPS

Uploaded by

Anand Duraiswamy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 33

Question Bank- Two marks with answers

Object Oriented Programming

UNIT I

1. What is meant by Object Oriented Programming? (Nov/Dec 2016). (May/June 2016)


Object-oriented programming (OOP) is a programming paradigm based on the
concept of "objects", which may contain data, in the form of fields, often known as
attributes; and code, in the form of procedures, often known as methods.

2. Define Abstraction. (Nov/Dec 2016). (May/June 2016).


Abstraction refers to the act of focusing on important details or
characteristics and filtering out the unwanted details or explanations.

3. Differentiate a Constant Pointer and a Pointer to a Constant with an example.


(Nov/Dec 2015).
A constant pointer is a pointer that cannot change the address its holding. Pointer to
constant is a pointer through which one cannot change the value of variable it points is
known as a pointer to constant. These type of pointers can change the address they point
to but cannot change the value kept at those address.

4. Illustrate the usage of this pointer in C++. (Nov/Dec 2015).

Pointer is a variable which stores the address of another variable.


Eg: int i, *a;
a=&i;
Now the pointer variable ‘a’ holds the address of another variable.
5. What are the advantages of object oriented programming over structured
programming? (Apr/May 2015).
In object oriented programming, the emphasis is on data rather than on process. Programs
are divided into objects. Data is hidden and cannot be accessed by external functions.

6. What is the advantage of an inline function? (Apr/May 2015).


Inline function eliminates the cost of calls to small function. Inline function expands
inline when it is invoked and thus eliminates the overheads.

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.

8. What are abstract classes? (Nov/Dec 2014) (May/June 2014)


Abstract classes are classes that contain one or more abstract methods.
An abstract method is a method that is declared, but contains no
implementation. Abstract classes may not be instantiated, and require subclasses to
provide implementations for the abstract methods.

9. List out the characteristics of object oriented language.


 Emphasis is on data.
 Programs are divided into objects.
 Data Structures are designed such that they characterize objects.
 Data is hidden and cannot be accessed by external functions.
 Objects communicate with each other through functions.
 Follows bottom up approach.

10. With respect to C++ distinguish objects and classes.


Class Object
Class is a blueprint or template from which Object is an instance of class.
objects are created.
Class is a group of similar objects. Objects are real world entity.
Class is a physical entity. Object is a logical entity.
Class does not allocates memory Objects allocates memory as they are
created.

11. Evaluate Procedural programming with Object Oriented programming.


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.

12. Define Message Passing.


Message passing is defined as the communication of objects with each other through
sending and receiving datas.

13. Classify the native types in C++. Define Type casting.


The native types in C++ are: Comments, Identifiers, Variables, Operators, Constants,
Data Types. Data types are classified into User defined Type, Built in Type and Derived
Type.

14. Create an example for Enumerated data types.


Enum is a data type consisting of set of named values called elements, members.
Eg: enum color {red, blue, green}
15. Discuss a statement/expression with an example.
In computer programming, a statement is the smallest standalone element of an
imperative programming language that expresses some action to be carried out.
Eg: c= a + b;

16. Show the syntax to dynamically allocate memory.


C++ contains two operators called new to allocate memory.
Syntax: datatype var;
var = new int [ size ];

17. What is function overloading?


Function overloading is a programming concept that allows the programmers to define
two or more functions with the same name but with different parameters.

18. What is Abstract Data Type?


Abstract Data Type is a mathematical model for data types where a data type is defined
by its behavior from the point of view of a user.

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.

20. How struct relates with union/class. Give an example.


Structure is a collection of dissimilar data items. Union is also a collection of dissimilar
data items but it differs in memory allocation. Class can be defined as extension of
structure which provides security to the data which is achieved through the access
specifiers.
UNIT II

1. State the uses of inline function. (Nov/Dec 2016).


Inline function eliminates the cost of calls to small function. Inline function expands
inline when it is invoked and thus eliminates the overheads.

2. Define polymorphism. (Nov/Dec 2016).


The word polymorphism means having many forms. Polymorphism means that a call to
a member function will cause a different function to be executed depending on the type
of object that invokes the function.

3. Distinguish between class and object. (May/June 2016).


Class Object
Class is a blueprint or template from which Object is an instance of class.
objects are created.
Class is a group of similar objects. Objects are real world entity.
Class is a physical entity. Object is a logical entity.
Class does not allocates memory Objects allocates memory as they are
created.

4. What is the use of destructor? (May/June 2016).


A destructor is a special member function that is called when the lifetime of an object
ends. The purpose of the destructor is to free the resources that the object may have
acquired during its lifetime.

5. When do you call an Object destructor? (Nov/Dec 2015)


A Destructor is called automatically when the variable goes out of the scope. If the object
was created with new operator, then the destructor is called with delete operator.

6. What is a destructor? Illustrate with an example. (May/June 2015) (May/June 2014)


A destructor is a special member function that is called when the lifetime of an object
ends.
Syntax:
class a
{
//variables;
a( ) //constructor
{
}
~a ( ) //destructor
{
}
};
7. What is meant by data abstraction? (May/June 2015)
Abstraction, in general, is the process of taking away or removing characteristics from
something in order to reduce it to a set of essential characteristics.

8. List the two types of polymorphism?


The two types of polymorphism are as follows:
 Compile- Time Polymorphism.
o Compile Time Polymorphism achieved through function
overloading and operator overloading.

 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

Typeid Object Type information.

10. Define operator overloading. (May/June 2014)


Operator overloading is an important concept in C++. It is a type of polymorphism in
which an operator is overloaded to give user defined meaning to it. Overloaded operator
is used to perform operation on user-defined data type. For example '+' operator can be
overloaded to perform addition on various data types, like for Integer,
String(concatenation) etc.

11. Give the use of scope resolution operator.


The following are the uses of scope resolution operator:
 To define a function outside the class.
 To use a global variable but also a local variable with the same name.

12. Define abstract class.


Abstract classes are classes that contain one or more abstract methods.
An abstract method is a method that is declared, but contains no
implementation. Abstract classes may not be instantiated, and require subclasses to
provide implementations for the abstract methods.

13. Define constructor.


A constructor is a kind of member function that initializes an instance of its class.
A constructor has the same name as the class and no return value. A constructorcan
have any number of parameters and a class may have any number of
overloaded constructors.

14. Define friend function.


A friend function, that is a "friend" of a given class, is a function that is given the same
access as methods to private and protected data. A friend function is declared by the
class that is granting access, so friend functions are part of the class interface, like
methods.

15. What is dynamic binding?


Dynamic binding also called dynamic dispatch is the process of linking procedure call
to a specific sequence of code (method) at run-time. It means that the code to be executed
for a specific procedure call is not known until run-time.

16. Give the use of this pointer.


Every object in C++ has access to its own address through an important pointercalled
this pointer. The this pointer is an implicit parameter to all member functions.
Therefore, inside a member function, this may be used to refer to the invoking object

17. Classify predefined C++ stream objects.


C++ provides the following stream objects:
 cin – The standard input stream.
 cout – The standard output stream.
 cerr- The standard error stream.
 clog – The buffered error stream.

18. What is static member function?


Static Function Members. By declaring a function member as static, you make it
independent of any particular object of the class. A static member function can be
called even if no objects of the class exist and the static functions are accessed using
only the class name and the scope resolution operator.

19. What is const member function?


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.

20. Differentiate early binding and late binding.


In Early binding, an object is assigned to a variable declared during compile time.
In Late binding, an object is assigned to a variable or member function during run time.
UNIT III

1. What are the advantages of generic programming? (Nov/Dec 2016).


The advantages of generic programming are as follows:
 Generic Programming are easier to write. We create only one generic
version of the class or function instead of manually creating
specializations.
 Easier to understand.
 Generic Programming are type sage, because the types of templates act
upon are known at the compile time. The compiler can perform type
checking before error occurs.

2. What is an exception? (Nov/Dec 2016) (May/June 2016) (May/June 2015)


A C++ exception is a response to an exceptional circumstance that arises while a
program is running, such as an attempt to divide by zero. Exceptions provide a way to
transfer control from one part of a program to another.

3. What is generic programming? (May/June 2016).


Or
What do you mean by the term generic programming? (Nov/Dec 2015).
Generic programming is a style of computer programming in which algorithms are
written in terms of types to-be-specified-later that are then instantiated when needed for
specific types provided as parameters.

4. What is an iterator? List out the characteristics of an Iterator. (Nov/Dec 2015)


Iterator is an object that enables the programmer to traverse through a container.
Characteristics of an Iterator:
 Iterator is a pointer object.
 It can be advanced from one element to another element by incrementing them.
 It can be decremented also. Arbitrary jumps are allowed from one element to
another.

5. What is a pure virtual function? (Nov/Dec 2015).


A pure virtual function or pure virtual method is a virtual function that is required to
be implemented by a derived class if the derived class is not abstract. Classes
containing pure virtual methods are termed "abstract" and they cannot be instantiated
directly.

6. What is a template? (May/June 2015). (Nov/Dec 2014)


Templates are the foundation of generic programming, which involves writing code in a
way that is independent of any particular type. A template is a blueprint or formula for
creating a generic class or a function.

7. Construct the syntax of try-catch-throw in exception handling in C++.


try
{
// protected code;
}
catch ( ….)
{
}
throw object;

8. Differentiate class template and template class.


Template Class:
A class that has generic definition or a class with parameters which is not instantiated
until the information is provided by the client. It is referred to a jargon for plain
templates..
Class Template:
The individual construction of a class is specified by a class template which is almost
similar the way how individual objects are constructed by using a class. It is referred to a
jargon for plain classes.

9. What are the manipulators available in C++? (May/June 2014)


Manipulators are helper functions that make it possible to control input/output streams using
<< or >>. The available manipulators are:
C++ Manipulator Function
Hex Used to turn on hex flag
Oct Used to turn on oct flag
Boolalpha Used to turn on Boolalpha flag
Noboolalpha Used to turn off boolalpha flag

10. List out any four containers supported by STL.


Containers supported by STL are :
 List
 Map
 Deque
 Vector

11. What is rethrowing an exception?


The rethrow expression ( throw without assignment_expression) causes the originally
thrown object to be rethrown. Because the exception has already been caught at the
scope in which the rethrow expression occurs, it is rethrown out to the next dynamically
enclosing try block.

12. What is virtual function? (Nov/Dec 2014)


A virtual function is a member function that you expect to be redefined in derived
classes. When you refer to a derived class object using a pointer or a reference to the base
class, you can call a virtual function for that object and execute the derived class's
version of the function.

13. Define inheritance.


In object-oriented programming, inheritance is the concept that when a class of objects is
defined, any subclass that is defined can inherit the definitions of one or more general
classes.

14. What is single inheritance?


Single inheritance enables a derived class to inherit properties and behavior from
a single parent class. It allows a derived class to inherit the properties and behavior of a
base class, thus enabling code reusability as well as adding new features to the existing
code.

15. Define multiple inheritance.


Multiple inheritance is a feature of some object-oriented computer programming
languages in which an object or class can inheritcharacteristics and features from more
than one parent object or parent class.

16. Define hybrid inheritance.


Hybrid inheritance is a combination of multiple inheritance and
multilevel inheritance. A class is derived from two classes as in multiple inheritance.
However, one of the parent classes is not a base class. It is a derived class.

17. What is multilevel inheritance?


Multilevel inheritance refers to a mechanism in OO technology where one can inherit
from a derived class, thereby making this derived class the base class for the new class.

18. What is hierarchical inheritance?


When a class has more than one child classes (sub classes) or in other words more than
one child classes have the same parent class then such kind of inheritance is known
as hierarchical.

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

20. What is the use of assert.h?


C Library - <assert.h> The assert.h header file of the C Standard Library provides a
macro called assert which can be used to verify assumptions made by the program and
print a diagnostic message if this assumption is false.

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.

2. List out the applications of OOPs. (7 Marks [Nov/Dec 2016]).


The applications of OOPs are as follows:
 Real Time Systems Design
 Simulation and Modeling System
 Object Oriented Database
 Object Oriented Distributed Database
 Client-Server System
 Hypertext, Hypermedia
 Neural Networking and Parallel Programming
 Decision Support and Office Automation Systems
 CIM/CAD/CAM Systems
 AI and Expert Systems

3. Explain the characteristics of OOPs. (8 Marks [Nov/Dec 2016] [May/June 2016).


Refer Book Page.No: 2.2-2.9

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;
}

5. Explain about pointers with an example. (7 Marks [May/June 2016]).


Refer Book Page.No: 1.46-1.48

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);
};

void bin_search :: getdata(void)


{
cout<<endl<<endl;
cout<<"Enter how many Number of array you want to create:-";
cin>>s;
cout<<"\nEnter "<<s<<" Integers in Ascending order\n";
for(int i=0;i<s;i++)
cin>>num[i];
cout<<"\nEnter your Search :- ";
cin>>search;
}
int bin_search :: searchfun(void)
{
int bottom=0,top=s-1,mid;
while(top >= bottom)
{
mid=(top+bottom)/2;
if(num[mid]==search)
return(mid+1);
else
{
if(num[mid] < search)
bottom=mid+1;
else
top=mid;
}
}
return(-1);
}
void bin_search :: display(void)
{
int result;
result=searchfun();
if(result==-1)
cout<<"\n\nEntered search is Invalid\n";
else
cout<<"\n\nSearch is Located at "<<result<<" Postition\n";
}
void main()
{
clrscr();
bin_search o1;
o1.getdata();
o1.display();
getch();
}
7. Write short notes on casting primitive data types to Object type and vice versa with an
example for each. (6 Marks [Nov/Dec 2015).
Refer Book Page.No: 2.31-2.34

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

20. Explain default argument with an example.


#include <iostream.h>
void display(char = '*', int = 1);
int main()
{
cout << "No argument passed:\n";
display();

cout << "\nFirst argument passed:\n";


display('#');
cout << "\nBoth argument passed:\n";
display('$', 5);
return 0;
}
void display(char c, int n)
{
for(int i = 1; i <= n; ++i)
{
cout << c;
}
cout << endl;
}

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

2. Summarize various Type conversions with suitable example


Refer Book Page.No: 1.21-1.23

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 = &rect;
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";

// access 5 values from the vector


for(i = 0; i < 5; i++){
std::cout << "value of vec [" << i << "] = " << vec[name] <<"\n";
}
return 0;
}

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;
}

2. What is meant by polymorphism? Explain the various types of polymorphism in C++


with suitable examples. (16 Marks [May/June 2015]).
Refer Book Page.No: 2.29-2.31 & 2.38-2.54

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;
}

2. Explain Function Templates with an example. (7 Marks [Nov/Dec 2016]).


Refer Book Page.No: 3.2-3.3

3. What is inheritance? List out the advantages of inheritance. (7 Marks [May/June 2016]).
Refer Book Page.No: 3.14-3.15

4. Write a C++ program to implement multiple inheritance. (9 Marks [May/June 2016]).


#include<iostream.h>
class M
{
protected:
int m;
public:
void get_m(int);
};
class N
{
protected:
int n;
public:
void get_n(int);
};
class P:public M,public N
{
public:
void display(void);
};
void M::get_m(int x)
{
m=x;
}
void N::get_n(int y)
{
n=y;
}
void P::display()
{
cout<<"m="<<m<<"\n";
cout<<"n="<<n<<"\n";
cout<<"m*n"<<m*n<<"\n";
}
int main()
{
P p;
p.get_m(10);
p.get_n(20);
p.display();
return 0;
}

5. Discuss about exception and its advantages. (5 Marks [May/June 2016]).


Refer Book Page.No:3.54-3.55
6. Write a C++ program to generate an exception whenever user input is even number less
than 100. (11 Marks [May/June 2016]).
#include<iostream.h>
int main()
{
int a;
cout<<"Enter the value of a:";
cin>>a;
try
{
if(((a%2)!= 0) && (a>100)
{
cout<< “ Valid Number”;
}
else
{
throw a;
}
}
catch(int a)
{
cout<<Given Number is even and less than 100”<<a;
}
return 0;
}

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);

cout<<"\nSorted Order Characters: ";


for(int j=0;j<4;j++)
cout<<b[j]<<"\t";
getch();
}
12. What is inheritance? Discuss the various types of inheritance that are available in C++
with neat diagram. (16 Marks [May/June 2015]).
Refer Book Page.No:3.1-3.13
13. Describe template and its types. (8 Marks[May/June 2014]).
Refer Book Page.No:3.14-3.28
14. Explain the components of standard template library in detail. (8 Marks [May/June
2014])
Refer Book Page.No: 3.73-3.92
15. Discuss about exception handling with example program. (16 Marks[May/June 2014]).
Refer Book Page.No: 3.52-3.66

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

You might also like