(mca) – semester ii mc0066 – oops using c++

23
December 2010 Master of Computer Application (MCA) – Semester 2 MC0066 – OOPS using C++ – 4 Credits (Book ID: B0681 & B0715) Assignment Set – 1 (40 Marks) Answer all Questions Each Question carries FIVE Marks Book ID: B0681 1. Distinguished between procedural language and OOP language. And Explain the key features of OOP. . Difference Between Procedural Language and OOP Language : 1. Procedural language focuses on organizing program statements into procedures or functions. Larger programs were either broken into functions or modules whereas in Object Oriented Programming bundles both data and functions into one unit known as object. 2. One of the main problems with Procedural approach for programming was data being completely forgotten. The emphasis was on the action and the data was only used in the entire process. Whereas in Object Oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data. 3. The procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled whereas in OOP language provides this flexibility through the concept of class. 4. Another limitation of the procedural languages is that the program model is not closer to real world objects For example, if you want to develop a gaming application of car race, what data would you use and what functions you would require is difficult questions to answer in a procedural approach. In the object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality. In the car game example, we would create several objects such as player, car, traffic signal and so on. Key Features of OOP : Encapsulation : Data and functions are said to be encapsulated in an single entity as object. Data Hiding : The data is said to be hidden thus not allowing accidental modification. Inheritance : Inheritance is one of the most powerful feature of Object Oriented Programming Languages that allows you to derive a class from an existing class and inherit all the characteristics and behaviour of the parent class. This feature allows easy modification of existing code and also reuse code. The ability to reuse components of a program is an important feature for any programming language. 1

Upload: abhinav-duggal

Post on 14-Oct-2014

373 views

Category:

Documents


0 download

DESCRIPTION

Spring 2012Master of Computer Application (MCA) – Semester IIMC0066 – OOPS using C++ – 4 Credits (Book ID: B0681 & B0715)Assignment Set – 1 (40 Marks)

TRANSCRIPT

Page 1: (MCA) – Semester II MC0066 – OOPS using C++

December 2010

Master of Computer Application (MCA) ndash Semester 2

MC0066 ndash OOPS using C++ ndash 4 Credits

(Book ID B0681 amp B0715)

Assignment Set ndash 1 (40 Marks)

Answer all Questions Each Question carries FIVE Marks

Book ID B0681

1 Distinguished between procedural language and OOP language And Explain the key features of OOP Difference Between Procedural Language and OOP Language

1 Procedural language focuses on organizing program statements into procedures or functions Larger programs were either broken into functions or modules whereas in Object Oriented Programming bundles both data and functions into one unit known as object

2 One of the main problems with Procedural approach for programming was data being completely forgotten The emphasis was on the action and the data was only used in the entire process Whereas in Object Oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data

3 The procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled whereas in OOP language provides this flexibility through the concept of class

4 Another limitation of the procedural languages is that the program model is not closer to real world objects For example if you want to develop a gaming application of car race what data would you use and what functions you would require is difficult questions to answer in a procedural approach

In the object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality In the car game example we would create several objects such as player car traffic signal and so on

Key Features of OOP

Encapsulation Data and functions are said to be encapsulated in an single entity as object

Data Hiding The data is said to be hidden thus not allowing accidental modification

Inheritance Inheritance is one of the most powerful feature of Object Oriented Programming Languages that allows you to derive a class from an existing class and inherit all the characteristics and behaviour of the parent class This feature allows easy modification of existing code and also reuse code The ability to reuse components of a program is an important feature for any programming language

1

Polymorphism Polymorphism is the ability of objects belonging to different types to respond to method calls of methods of the same name each one according to an appropriate type-specific behavior The programmer (and the program) does not have to know the exact type of the object in advance so this behavior can be implemented at run time (this is called late binding or dynamic binding)

Overloading Operator overloading feature allows users to define how basic operators work with objects The operator + will be adding two numbers when used with integer variables However when used with user defined string class + operator may concatenate two strings Similarly same functions with same function name can perform different actions depending upon which object calls the function This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms) Operator overloading is a kind of polymorphism

2 Describe the theory with programming examples the selection control statements in C++

There are basically two types of control statements in c++ which allows the programmer to modify the regular sequential execution of statements They are selection and iteration statements The selection statements allow to choose a set of statements for execution depending on a condition

1 If statement

Syntax if (expression or condition) statement 1statement 2else statement 3statement 4

The expression or condition is any expression built using relational operators which either yields true or false condition If no relational operators are used for comparison then the expression will be evaluated and zero is taken as false and non zero value is taken as true If the condition is true statement1 and statement2 is executed otherwise statement 3 and statement 4 is executed Else part in the if statement is optional If there is no else part then the next statement after the if statement is exceuted if the condition is false If there is only one statement to be executed in the if part or in the else part braces can be omitted

Following example program implements the if statement

evenoddcpp include ltiostreamhgt include ltconiohgtvoid main()

int numcoutltltrdquoPlease enter a numberrdquoltltendlcingtgtnum

2

If ((num2) == 0)coutltltnum ltltrdquo is a even numberrdquoelsecoutltltnum ltltrdquo is a odd numberrdquogetch()

The above program accepts a number from the user and divides it by 2 and if the remainder (remainder is obtained by modulus operator) is zero it displays the number is even otherwise as odd We make use of the relational operator == to compare whether remainder is equal to zero or not

2 Nested If statement

If statement can be nested in another if statement to check multipleconditionsIf (condition1) if (condition 2)

statement1Statement2

else if (condition3)

statement3

else statement4

Example

Largecpp include ltiostreamhgtvoid main()

int abccoutltltrdquoPlease enter three numbersrdquocingtgtagtgtbgtgtcIf ((agtb) ampamp (bgtc))

coutltltaltlt ldquo is the largest numberrdquoelse if ((bgta) ampamp (bgtc))

coutltltbltlt ldquo is the largest numberrdquoelse if ((cgta) ampamp (cgtb))

coutltltcltlt ldquo is the largest numberrdquo

The above program accepts three numbers from the user and displays which is the largest number among the three( assumption is that all the numbers are unique the program has to be modified if you would like to allow same number twice)

3

3 Switch statement One alternative to nested if is the switch statement which can be used to increase clarity in case of checking the different values of the same variable and execute statements accordingly

Syntax

Switch (variablename)

case value1 statement1breakcase value2 statement2breakcase value3 statement3breakdefault statement4

Example

includeltiostreamhgtvoid main()

char posint x=15 y=15cout ltlt ldquo you are currently located atrdquo ltltxltltrdquo ldquoltltyltltendlcoutgtgtrdquoplease choose the letter to move l for left r for right u for up and d for downrdquo ltltendlcingtgtposswitch (pos) case lsquolrsquo x--breakcase lsquorrsquo x++breakcase lsquoursquo y++breakcase lsquodrsquo y--breakdefault coutltltrdquoYou selected a wrong optionrdquocoutltltldquo you are now located atrdquo ltltxltltrdquo ldquoltlty

The above program asks the user to enter l r u d for allowing him to move left right up and down respectively The position is initialised to 15 and 15 which are x and y coordinates of his position Depending upon the what user

4

has selected the the x and y co-ordinates are incremented or decremented by one(x++ is same as x=x+1) If the user types a letter other than l r u d he gets an error message Since the switch variable is a character l u r d and enclosed within single quote

4 Conditional Operator Conditional operator () is a handy operator which acts like a shortcut for if else statement If you had to compare two variables a and b and then depending on which is larger you wanted to store that variable in anothervariable called large You would do this using if else statement in the following wayif (agtb)large=aelselarge=b

The above can be done using conditional operator in the following way large= (agtb) a b

3 Write a program in C++ for matrix multiplication The program should accept the dimensions of both the matrices to be multiplied and check for compatibility with appropriate messages and give the output

include matrixMultiplyhinclude ltiostreamgtinclude ltstdlibhgt

using namespace std

int main()

int rows1 = 3 int columns1 = 1 float matrixOne = (float)malloc(rows1 sizeof(matrixOne))

for(int i = 0 i lt rows1 i++)

matrixOne[i] = (float)malloc(columns1 sizeof(float))

matrixOne[0][0] = 1 matrixOne[1][0] = -1 matrixOne[2][0] = 1

int rows2 = 1 int columns2 = 3 float matrixTwo = (float)malloc(rows2 sizeof(matrixTwo))

for(int i = 0 i lt rows2 i++)

matrixTwo[i] = (float)malloc(columns2 sizeof(float))

matrixTwo[0][0] = 1

5

matrixTwo[0][1] = -1 matrixTwo[0][2] = 1

float result result = (float)malloc(rows1 sizeof(result))

for(int i = 0 i lt rows1 i++)

result[i] = (float)malloc(columns2 sizeof(float))

matrixMultiply(31matrixOne13matrixTwo result)

return 0

void matrixMultiply(int rows1 int cols1 float mat1 int rows2 int cols2 float mat2 float result)

if( cols1 = rows2 )

cout ltlt Cant Multiply These Matricies

else

float tempResult

for (int i=0iltrows1i++)

for(int j=0jltcols2j++)

tempResult = 0

for(int k=0kltrows2k++) tempResult += mat1[i][k]mat2[k][j]

result[i][j] = tempResult

6

4 What do you mean by operator overloading Illustrate with suitable example for overloading Unary operators

Operator overloading Operator overloading is an interesting feature of C++ that allows programmers to specify how various arithmetic relational and many other operators work with user defined data-types or classes To perform addition of two distance objects we used a call d3add(d1d2) Instead of such statements it would be more clear if we could use statements like d3=d1+d2 This is possible only if we inform compiler about how + operator works with distance class

It helps to use the default operators with the user defined objects for making the code simpler However there are several problems with operator overloading which you should be aware of When using operator overloading the operator should perform only the most obvious function Otherwise it will lead to more confusion If you are overloading + operator for distance class it should add two distance objects and should not do something else

Several operators such as dot operator scope resolution () operator conditional operator () etc cannot be overloaded Therefore operator overloading should be used for a class where it is required to perform obvious functions with the default operators and there can be no other meaning for the same

Overloading Unary Operators

We have seen that unary operators are those operators which work on one operator Some of the unary operators are ++ -- and minus (-) Operator overloading works similar to any member function of a class But it is not invoked using dot operator Just like member function the operator has a return value and takes arguments It is invoked by the operand which uses it In case of overloading of unary operators the calling operand can be either left or right of the operator like in case of increment and decrement operators While defining the operator functionality for the class the keyword operator is used

Example

unarycpp include ltiostreamhgtclass counter

unsigned int countpubliccounter()count=0int getcount()

7

return count

void operator ++() count++

void main()

counter c1c1++++c1coutltltc1getcount()

In the above example the operator ++ is defined to return void and take no arguments All unary operators do not take no arguments as it operates on only one operand and that operand itself invokes the operator Therefore the operand is sent by default

Book ID B0715

5 Write C++ program which demonstrate the difference between static and dynamic binding

Dynamic Binding You can specify that the compiler match a function call with the correct function definition at run time this is called dynamic binding You declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific function

The following examples demonstrate the differences between static and dynamic binding

The first example demonstrates static binding

include ltiostreamgtusing namespace stdstruct A

void f()

cout ltlt Class A ltlt endl

struct B A

void f()

cout ltlt Class B ltlt endl

8

void g(Aamp arg)

argf()

int main()

B xg(x)

The following is the output of the above example

Class A

When function g() is called function Af() is called although the argument refers to an object of type B At compile time the compiler knows only that the argument of function g() will be a reference to an object derived from A it cannot determine whether the argument will be a reference to an object of type A or type B However this can be determined at run time

The following example is the same as the previous example except that Af() is declared with the virtual keyword

include ltiostreamgtusing namespace stdstruct A

virtual void f()

cout ltlt Class A ltlt endl

struct B A

void f()

cout ltlt Class B ltlt endl

void g(Aamp arg )

argf()

int main()

B xg(x)

9

The following is the output of the above example

Class B

The virtual keyword indicates to the compiler that it should choose the appropriate definition of f() not by the type of reference but by the type of object that the reference refers to

6 Describe the Friend functions and friend classes with programming examples

Friend Functions Friend Functions are the functions which can have access to the private and protected members of the class They can be declared anywhere within a class declaration but it is common practice to list friends at the beginning of the class The public and protected keywords do not apply to friend functions as the class has no control over the scope of friends If we want to declare an external function as friend of a class thus allowing this function to have access to the private and protected members of this class we do it by declaring a prototype of this external function within the class and preceding it with the keyword friend

friend functionsinclude ltiostreamgtusing namespace stdclass CRectangle

int width heightpublicvoid set_values (int int)int area () return (width height)friend CRectangle duplicate (CRectangle)

void CRectangleset_values (int a int b)

width = aheight = b

CRectangle duplicate (CRectangle rectparam)

CRectangle rectresrectreswidth = rectparamwidth2rectresheight = rectparamheight2return (rectres)

int main () CRectangle rect rectbrectset_values (23)rectb = duplicate (rect)cout ltlt rectbarea()

return 0

10

The duplicate function is a friend of CRectangle From within that function we have been able to access the members width and height of different objects of type CRectangle which are private members Notice that neither in the declaration of duplicate() nor in its later use in main() have we considered duplicate a member of class CRectangle It isnt It simply has access to its private and protected members without being a member The friend functions can serve for example to conduct operations between two different classes Generally the use of friend functions is out of an object-oriented programming methodology so whenever possible it is better to use members of the same class to perform operations with them Such as in the previous example it would have been shorter to integrate duplicate() within the class CRectangle

Friend Classes A class can declare a member function of another class as a friend or declare another class as a friend class Friend classes are used in cases where one class is tightly coupled to another class For example suppose we have a class CPoint that represents a coordinate and a class CPointCollection that holds a list of points Since the collection class may need to manipulate point objects we could declare CPointCollection as a friend of the CPoint class

Forward declaration of friend classclass CPointCollection Point classclass CPoint friend CPointCollectionprivatedouble m_xdouble m_ypublicCPoint(const double x const double y) m_x(x)m_y(y) ~CPoint(void)

Since the collection class is a friend of CPoint it has access to the internal data of any point object This is useful when individual elements of the collection need to be manipulated For example a set method of the CPointCollection class could set all CPoint elements to a particular value (vector is a STL container which is discussed in detail in Chapter 8)

class CPointCollection privatevectorltCPointgt m_vecPointspublicCPointCollection(const int nSize) m_vecPoints(nSize) ~CPointCollection(void)void set(const double x const double y)

The set member can iterate over the collection and reset each point

11

void CPointCollectionset(const double x const double y) Get the number of elements in the collectionconst int nElements = m_vecPointssize() Set each elementfor(int i=0 iltnElements i++) m_vecPoints[i]m_x = xm_vecPoints[i]m_y = y

One thing important about friend classes is that friendship is not mutual although CPointCollection can access CPoint the converse is not true Friendship is also not something that is passed down a class hierarchy Derived classes of CPointCollection will not be able to access CPoint The principle behind this is that friendship is not implicitly granted each class must explicitly choose its friends

7 Illustrate with suitable examples various file handling methods in C++

File handling is an important part of all programs Most of the applications will have their own features to save some data to the local disk and read data from the disk again C++ File IO classes simplify such file readwrite operations for the programmer by providing easier to use classes

C++ File IO Classes and Functions

There are 3 File IO classes in C++ which are used for File ReadWrite operations They areifstream - Can be used for File readinput operationsofstream - Can be used for File writeoutput operationsfstream - Can be used for both readwrite c++ file IO operations

The most important methods which will be used for any file operations are

fstreamopen method - to open the filefstreamOperator gtgt and fstreamOperator ltlt - For reading from or writing to the filefstreamclose - Flushes all buffer and close the fileReading a text file using fstream class

There are several ways of reading the text from a file But all of them have a common approach as follows

Open the fileRead the dataClose the file

This sample code snippet explains how to use the c++ file io stream operators to read data from a file In all cases the header file fstreamh must be included

includeltfstreamhgt int main() char str[2000]

12

fstream file_op(ctest_filetxtiosin) while(file_op gtgt str) cout ltlt str file_opclose()

return 0

The class fstream which is used above is the one which is commonly used for c++ file io manipulations The constructor of fstream takes 2 parameters One is the file path and the second is the File Open mode There are several open modes each of them with a different purpose

But if we look at the output closely there is a draw back in using this stream operator read The output misses the white spaces and the end of line characters In order not to miss these characters we can either use fstreamget() or fstreamgetline() methods Here is the example for using fstream getline method

include ltfstreamhgt int main() char str[2000] fstream file_op(ctest_filetxtiosin) while(file_opeof()) file_opgetline(str2000) cout ltltstr file_opclose() cout ltltendl

return 0

Writing to a text file using fstream class

Writing to a text file can also be achieved with the stream operators This also follows the same order of operations though with a slight difference 1 open a file - in write mode 2 Write to a file 3 close the file Look at the following sample code to see the difference

include ltfstreamhgt int main() fstream file_op(cCoderSource_filetxtiosout)

file_opltltTest Write to file file_opclose()

13

return 0

To modify the data or to seek to a different position inside the file the c++ file io class fstream provides member functionslike seekg() etc These functions can be used to relocate the record insert position to the desired locations

After all the C++ File IO operations we do a fstreamclose() to close the file pointer This is not mandatory Even if this function is not called by the application the destructor of the fstream class will close the file when the object goes out of scope

8 What is template specialization Describe a scenario in which template class partial specialization is considered appropriate

Class Template Specialization In some cases it is possible to override the template-generated code by providing special definitions for specific types This is called template specialization

The following example defines a template class specialization for template class stream

include ltiostreamgtusing namespace stdtemplate ltclass Tgtclass stream publicvoid f() cout ltlt streamltTgtf()ltlt endl template ltgtclass streamltchargt publicvoid f() cout ltlt streamltchargtf()ltlt endl int main()

streamltintgt sistreamltchargt scsif()scf()return 0

Output

streamltTgtf()streamltchargtf()

In the above example streamltchargt is used as the definition of streams of chars other streams will be handled by the template class generated from the class template

14

Template Class Partial Specialization You may want to generate a specialization of the class for just one parameter for example

base template classtemplatelttypename T1 typename T2gtclass X partial specializationtemplatelttypename T1gtclass XltT1 intgt int main() generates an instantiation from the base templateXltchar chargt xcc generates an instantiation from the partial specializationXltchar intgt xii return 0

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list

15

December 2010

Master of Computer Application (MCA) ndash Semester 2

MC0066 ndash OOPS using C++ ndash 4 Credits

(Book ID B0681 amp B0715)

Assignment Set ndash 2 (40 Marks)

Answer all Questions Each Question carries FIVE Marks

Book ID B0681

1 Distinguished between procedural language and OOP language And Explain the key features of OOP

1 Procedural language focuses on organizing program statements into procedures or functions Larger programs were either broken into functions or modules whereas in Object Oriented Programming bundles both data and functions into one unitknown as object

2 One of the main problems with Procedural approach for programming was data being completely forgotten The emphasis was on the action and the data was only used in the entire process Whereas in Object Oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data

3 The procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled whereas in OOP language provides this flexibility through the concept of class

4 Another limitation of the procedural languages is that the program model is not closer to real world objects For example if you want to develop a gaming application of car race what data would you use and what functions you would require is difficult questions to answer in a procedural approach In the object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality In the car game example we would create several objects such as player car traffic signal and so on

Key Features of OOP

Encapsulation Data and functions are said to be encapsulated in an single entity as object

Data Hiding The data is said to be hidden thus not allowing accidental modification

Inheritance Inheritance is one of the most powerful feature of Object Oriented Programming Languages that allows you to derive a class from an existing class and inherit all the characteristics and behaviour of the parent class This feature allows easy modification of existing code and also reuse code The ability to reuse components of a program is an important feature for any programming language

16

Polymorphism and Overloading Operator overloading feature allows users to define how basic operators work with objects The operator + will be adding two numbers when used with integer variables However when used with user defined string class + operator may concatenate two strings Similarly same functions with same function name can perform different actions depending upon which object calls the function This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms) Operator overloading is a kind of polymorphism

2 Discuss the constructors and Destructors with suitable example

Constructors Constructors are member functions of a class which have same name as the class name Constructors are called automatically whenever an object of the class is created This feature makes it very useful to initialize the classdata members whenever a new object is created It also can perform any other function that needs to be performed for all the objects of the class without explicitly specifying it

Destructors Destructors on the other hand are also member functions with the same name as class but are prefixed with tilde (~) sign to differentiate it from theconstructor They are invoked automatically whenever the objectrsquos life expires or it is destroyed It can be used to return the memory back to the operating system if the memory was dynamically allocated

The following program implements the constructor and destructors for aclass

constdestcpp includeltiostreamhgtclass sample

privateint datapublicsample()

data=0 coutltltrdquoConstructor invokedrdquoltltendl

~sample()

coutltltrdquoDestructor invokedrdquovoid display()

coutltltrdquoData=rdquoltltdataltltendlvoid main()

sample obj1 objdisplay()

17

If you run the above program you will get the output as follows

Constructor invokedData=0Destructor invoked

When object of sample class object is created automatically the constructor is invoked and data is initialized to zero When the program ends the object is destroyed which invokes the destructor Please note that both the constructor and destructor is declared as public and they have no return value However constructors can have arguments and can be overloaded so that different constructors can be called depending upon the arguments that is passed Destructors on the other hand cannot be overloaded and cannot have any arguments

3 Describe the theory behind Virtual Functions and Polymorphism along with suitable programming examples for each

Virtual Functions Virtual means existing in effect but not in reality Virtual functions are primarily used in inheritance Let us suppose you have a class base as shown in the following program and two classes derv1 and derv2 are publicly derived from class base You would like to create a pointer that points to any of the derived class objects If you create a pointer of derv1 then it can point to derv1 object only Compiler will complain if you assign any other object is assigned to the pointer The solution is to create a pointer to Base class

Even though the address of derived classes is assigned to the pointer the compiler executes the base class function However if the base class function is made virtual we get the desired result In the following program we have made the base class function show() as virtual function by prefixing with the keyword virtual

virtualcpp include ltiostreamhgtclass base

publicvirtual void show() virtual functioncoutltltldquobaserdquoltltendl

class derv1public base

publicvoid show()

coutltltldquoderv1rdquoltltendl

class derv2 public base

publicvoid show()

18

coutltltldquoderv2rdquoltltendl

void main()derv1 dv1derv2 dv2base ptrptr=ampdv1ptr-gtshow()ptr=ampdv2ptr-gtshow()

By declaring the base class function as virtual we now get the output as

derv1derv2

In the above program depending on the contents in the pointer the compiler decides which class function to call during runtime This is known as late binding or dynamic binding

Polymorphism Polymorphism means same content but different forms In C++ polymorphism enables the same program code calling different functions of different classes For Example Letrsquos us suppose that we want to create a class shape and derive classes such as rectangle circle triangle etc Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen You would like to write a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime

Shape ptrarr[100]for (int j=0jltnj++)Ptrarr[j]-gtdraw()

This is an very desirable capability that completely different functions are executed by same function call If the ptrarr is pointing to a rectangle a rectangle is drawn If it is point int to circle circle is drawn

This is exactly what is polymorphism However to implement this approach several conditions should be met Firstly all the classes rectangle circle triangle should be derived from a single class (Here it is shape class) Secondly the draw() function must be declared as virtual in the base class (in the shape class)Operator overloading is a type of polymorphism which allows the same operators to behave differently with different datatypesoperands

4 Discuss the types of Inheritance with suitable example for each

Inheritance is a very powerful featu re of object oriented programming It allows reuse of code without modifying the original code It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort

19

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 2: (MCA) – Semester II MC0066 – OOPS using C++

Polymorphism Polymorphism is the ability of objects belonging to different types to respond to method calls of methods of the same name each one according to an appropriate type-specific behavior The programmer (and the program) does not have to know the exact type of the object in advance so this behavior can be implemented at run time (this is called late binding or dynamic binding)

Overloading Operator overloading feature allows users to define how basic operators work with objects The operator + will be adding two numbers when used with integer variables However when used with user defined string class + operator may concatenate two strings Similarly same functions with same function name can perform different actions depending upon which object calls the function This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms) Operator overloading is a kind of polymorphism

2 Describe the theory with programming examples the selection control statements in C++

There are basically two types of control statements in c++ which allows the programmer to modify the regular sequential execution of statements They are selection and iteration statements The selection statements allow to choose a set of statements for execution depending on a condition

1 If statement

Syntax if (expression or condition) statement 1statement 2else statement 3statement 4

The expression or condition is any expression built using relational operators which either yields true or false condition If no relational operators are used for comparison then the expression will be evaluated and zero is taken as false and non zero value is taken as true If the condition is true statement1 and statement2 is executed otherwise statement 3 and statement 4 is executed Else part in the if statement is optional If there is no else part then the next statement after the if statement is exceuted if the condition is false If there is only one statement to be executed in the if part or in the else part braces can be omitted

Following example program implements the if statement

evenoddcpp include ltiostreamhgt include ltconiohgtvoid main()

int numcoutltltrdquoPlease enter a numberrdquoltltendlcingtgtnum

2

If ((num2) == 0)coutltltnum ltltrdquo is a even numberrdquoelsecoutltltnum ltltrdquo is a odd numberrdquogetch()

The above program accepts a number from the user and divides it by 2 and if the remainder (remainder is obtained by modulus operator) is zero it displays the number is even otherwise as odd We make use of the relational operator == to compare whether remainder is equal to zero or not

2 Nested If statement

If statement can be nested in another if statement to check multipleconditionsIf (condition1) if (condition 2)

statement1Statement2

else if (condition3)

statement3

else statement4

Example

Largecpp include ltiostreamhgtvoid main()

int abccoutltltrdquoPlease enter three numbersrdquocingtgtagtgtbgtgtcIf ((agtb) ampamp (bgtc))

coutltltaltlt ldquo is the largest numberrdquoelse if ((bgta) ampamp (bgtc))

coutltltbltlt ldquo is the largest numberrdquoelse if ((cgta) ampamp (cgtb))

coutltltcltlt ldquo is the largest numberrdquo

The above program accepts three numbers from the user and displays which is the largest number among the three( assumption is that all the numbers are unique the program has to be modified if you would like to allow same number twice)

3

3 Switch statement One alternative to nested if is the switch statement which can be used to increase clarity in case of checking the different values of the same variable and execute statements accordingly

Syntax

Switch (variablename)

case value1 statement1breakcase value2 statement2breakcase value3 statement3breakdefault statement4

Example

includeltiostreamhgtvoid main()

char posint x=15 y=15cout ltlt ldquo you are currently located atrdquo ltltxltltrdquo ldquoltltyltltendlcoutgtgtrdquoplease choose the letter to move l for left r for right u for up and d for downrdquo ltltendlcingtgtposswitch (pos) case lsquolrsquo x--breakcase lsquorrsquo x++breakcase lsquoursquo y++breakcase lsquodrsquo y--breakdefault coutltltrdquoYou selected a wrong optionrdquocoutltltldquo you are now located atrdquo ltltxltltrdquo ldquoltlty

The above program asks the user to enter l r u d for allowing him to move left right up and down respectively The position is initialised to 15 and 15 which are x and y coordinates of his position Depending upon the what user

4

has selected the the x and y co-ordinates are incremented or decremented by one(x++ is same as x=x+1) If the user types a letter other than l r u d he gets an error message Since the switch variable is a character l u r d and enclosed within single quote

4 Conditional Operator Conditional operator () is a handy operator which acts like a shortcut for if else statement If you had to compare two variables a and b and then depending on which is larger you wanted to store that variable in anothervariable called large You would do this using if else statement in the following wayif (agtb)large=aelselarge=b

The above can be done using conditional operator in the following way large= (agtb) a b

3 Write a program in C++ for matrix multiplication The program should accept the dimensions of both the matrices to be multiplied and check for compatibility with appropriate messages and give the output

include matrixMultiplyhinclude ltiostreamgtinclude ltstdlibhgt

using namespace std

int main()

int rows1 = 3 int columns1 = 1 float matrixOne = (float)malloc(rows1 sizeof(matrixOne))

for(int i = 0 i lt rows1 i++)

matrixOne[i] = (float)malloc(columns1 sizeof(float))

matrixOne[0][0] = 1 matrixOne[1][0] = -1 matrixOne[2][0] = 1

int rows2 = 1 int columns2 = 3 float matrixTwo = (float)malloc(rows2 sizeof(matrixTwo))

for(int i = 0 i lt rows2 i++)

matrixTwo[i] = (float)malloc(columns2 sizeof(float))

matrixTwo[0][0] = 1

5

matrixTwo[0][1] = -1 matrixTwo[0][2] = 1

float result result = (float)malloc(rows1 sizeof(result))

for(int i = 0 i lt rows1 i++)

result[i] = (float)malloc(columns2 sizeof(float))

matrixMultiply(31matrixOne13matrixTwo result)

return 0

void matrixMultiply(int rows1 int cols1 float mat1 int rows2 int cols2 float mat2 float result)

if( cols1 = rows2 )

cout ltlt Cant Multiply These Matricies

else

float tempResult

for (int i=0iltrows1i++)

for(int j=0jltcols2j++)

tempResult = 0

for(int k=0kltrows2k++) tempResult += mat1[i][k]mat2[k][j]

result[i][j] = tempResult

6

4 What do you mean by operator overloading Illustrate with suitable example for overloading Unary operators

Operator overloading Operator overloading is an interesting feature of C++ that allows programmers to specify how various arithmetic relational and many other operators work with user defined data-types or classes To perform addition of two distance objects we used a call d3add(d1d2) Instead of such statements it would be more clear if we could use statements like d3=d1+d2 This is possible only if we inform compiler about how + operator works with distance class

It helps to use the default operators with the user defined objects for making the code simpler However there are several problems with operator overloading which you should be aware of When using operator overloading the operator should perform only the most obvious function Otherwise it will lead to more confusion If you are overloading + operator for distance class it should add two distance objects and should not do something else

Several operators such as dot operator scope resolution () operator conditional operator () etc cannot be overloaded Therefore operator overloading should be used for a class where it is required to perform obvious functions with the default operators and there can be no other meaning for the same

Overloading Unary Operators

We have seen that unary operators are those operators which work on one operator Some of the unary operators are ++ -- and minus (-) Operator overloading works similar to any member function of a class But it is not invoked using dot operator Just like member function the operator has a return value and takes arguments It is invoked by the operand which uses it In case of overloading of unary operators the calling operand can be either left or right of the operator like in case of increment and decrement operators While defining the operator functionality for the class the keyword operator is used

Example

unarycpp include ltiostreamhgtclass counter

unsigned int countpubliccounter()count=0int getcount()

7

return count

void operator ++() count++

void main()

counter c1c1++++c1coutltltc1getcount()

In the above example the operator ++ is defined to return void and take no arguments All unary operators do not take no arguments as it operates on only one operand and that operand itself invokes the operator Therefore the operand is sent by default

Book ID B0715

5 Write C++ program which demonstrate the difference between static and dynamic binding

Dynamic Binding You can specify that the compiler match a function call with the correct function definition at run time this is called dynamic binding You declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific function

The following examples demonstrate the differences between static and dynamic binding

The first example demonstrates static binding

include ltiostreamgtusing namespace stdstruct A

void f()

cout ltlt Class A ltlt endl

struct B A

void f()

cout ltlt Class B ltlt endl

8

void g(Aamp arg)

argf()

int main()

B xg(x)

The following is the output of the above example

Class A

When function g() is called function Af() is called although the argument refers to an object of type B At compile time the compiler knows only that the argument of function g() will be a reference to an object derived from A it cannot determine whether the argument will be a reference to an object of type A or type B However this can be determined at run time

The following example is the same as the previous example except that Af() is declared with the virtual keyword

include ltiostreamgtusing namespace stdstruct A

virtual void f()

cout ltlt Class A ltlt endl

struct B A

void f()

cout ltlt Class B ltlt endl

void g(Aamp arg )

argf()

int main()

B xg(x)

9

The following is the output of the above example

Class B

The virtual keyword indicates to the compiler that it should choose the appropriate definition of f() not by the type of reference but by the type of object that the reference refers to

6 Describe the Friend functions and friend classes with programming examples

Friend Functions Friend Functions are the functions which can have access to the private and protected members of the class They can be declared anywhere within a class declaration but it is common practice to list friends at the beginning of the class The public and protected keywords do not apply to friend functions as the class has no control over the scope of friends If we want to declare an external function as friend of a class thus allowing this function to have access to the private and protected members of this class we do it by declaring a prototype of this external function within the class and preceding it with the keyword friend

friend functionsinclude ltiostreamgtusing namespace stdclass CRectangle

int width heightpublicvoid set_values (int int)int area () return (width height)friend CRectangle duplicate (CRectangle)

void CRectangleset_values (int a int b)

width = aheight = b

CRectangle duplicate (CRectangle rectparam)

CRectangle rectresrectreswidth = rectparamwidth2rectresheight = rectparamheight2return (rectres)

int main () CRectangle rect rectbrectset_values (23)rectb = duplicate (rect)cout ltlt rectbarea()

return 0

10

The duplicate function is a friend of CRectangle From within that function we have been able to access the members width and height of different objects of type CRectangle which are private members Notice that neither in the declaration of duplicate() nor in its later use in main() have we considered duplicate a member of class CRectangle It isnt It simply has access to its private and protected members without being a member The friend functions can serve for example to conduct operations between two different classes Generally the use of friend functions is out of an object-oriented programming methodology so whenever possible it is better to use members of the same class to perform operations with them Such as in the previous example it would have been shorter to integrate duplicate() within the class CRectangle

Friend Classes A class can declare a member function of another class as a friend or declare another class as a friend class Friend classes are used in cases where one class is tightly coupled to another class For example suppose we have a class CPoint that represents a coordinate and a class CPointCollection that holds a list of points Since the collection class may need to manipulate point objects we could declare CPointCollection as a friend of the CPoint class

Forward declaration of friend classclass CPointCollection Point classclass CPoint friend CPointCollectionprivatedouble m_xdouble m_ypublicCPoint(const double x const double y) m_x(x)m_y(y) ~CPoint(void)

Since the collection class is a friend of CPoint it has access to the internal data of any point object This is useful when individual elements of the collection need to be manipulated For example a set method of the CPointCollection class could set all CPoint elements to a particular value (vector is a STL container which is discussed in detail in Chapter 8)

class CPointCollection privatevectorltCPointgt m_vecPointspublicCPointCollection(const int nSize) m_vecPoints(nSize) ~CPointCollection(void)void set(const double x const double y)

The set member can iterate over the collection and reset each point

11

void CPointCollectionset(const double x const double y) Get the number of elements in the collectionconst int nElements = m_vecPointssize() Set each elementfor(int i=0 iltnElements i++) m_vecPoints[i]m_x = xm_vecPoints[i]m_y = y

One thing important about friend classes is that friendship is not mutual although CPointCollection can access CPoint the converse is not true Friendship is also not something that is passed down a class hierarchy Derived classes of CPointCollection will not be able to access CPoint The principle behind this is that friendship is not implicitly granted each class must explicitly choose its friends

7 Illustrate with suitable examples various file handling methods in C++

File handling is an important part of all programs Most of the applications will have their own features to save some data to the local disk and read data from the disk again C++ File IO classes simplify such file readwrite operations for the programmer by providing easier to use classes

C++ File IO Classes and Functions

There are 3 File IO classes in C++ which are used for File ReadWrite operations They areifstream - Can be used for File readinput operationsofstream - Can be used for File writeoutput operationsfstream - Can be used for both readwrite c++ file IO operations

The most important methods which will be used for any file operations are

fstreamopen method - to open the filefstreamOperator gtgt and fstreamOperator ltlt - For reading from or writing to the filefstreamclose - Flushes all buffer and close the fileReading a text file using fstream class

There are several ways of reading the text from a file But all of them have a common approach as follows

Open the fileRead the dataClose the file

This sample code snippet explains how to use the c++ file io stream operators to read data from a file In all cases the header file fstreamh must be included

includeltfstreamhgt int main() char str[2000]

12

fstream file_op(ctest_filetxtiosin) while(file_op gtgt str) cout ltlt str file_opclose()

return 0

The class fstream which is used above is the one which is commonly used for c++ file io manipulations The constructor of fstream takes 2 parameters One is the file path and the second is the File Open mode There are several open modes each of them with a different purpose

But if we look at the output closely there is a draw back in using this stream operator read The output misses the white spaces and the end of line characters In order not to miss these characters we can either use fstreamget() or fstreamgetline() methods Here is the example for using fstream getline method

include ltfstreamhgt int main() char str[2000] fstream file_op(ctest_filetxtiosin) while(file_opeof()) file_opgetline(str2000) cout ltltstr file_opclose() cout ltltendl

return 0

Writing to a text file using fstream class

Writing to a text file can also be achieved with the stream operators This also follows the same order of operations though with a slight difference 1 open a file - in write mode 2 Write to a file 3 close the file Look at the following sample code to see the difference

include ltfstreamhgt int main() fstream file_op(cCoderSource_filetxtiosout)

file_opltltTest Write to file file_opclose()

13

return 0

To modify the data or to seek to a different position inside the file the c++ file io class fstream provides member functionslike seekg() etc These functions can be used to relocate the record insert position to the desired locations

After all the C++ File IO operations we do a fstreamclose() to close the file pointer This is not mandatory Even if this function is not called by the application the destructor of the fstream class will close the file when the object goes out of scope

8 What is template specialization Describe a scenario in which template class partial specialization is considered appropriate

Class Template Specialization In some cases it is possible to override the template-generated code by providing special definitions for specific types This is called template specialization

The following example defines a template class specialization for template class stream

include ltiostreamgtusing namespace stdtemplate ltclass Tgtclass stream publicvoid f() cout ltlt streamltTgtf()ltlt endl template ltgtclass streamltchargt publicvoid f() cout ltlt streamltchargtf()ltlt endl int main()

streamltintgt sistreamltchargt scsif()scf()return 0

Output

streamltTgtf()streamltchargtf()

In the above example streamltchargt is used as the definition of streams of chars other streams will be handled by the template class generated from the class template

14

Template Class Partial Specialization You may want to generate a specialization of the class for just one parameter for example

base template classtemplatelttypename T1 typename T2gtclass X partial specializationtemplatelttypename T1gtclass XltT1 intgt int main() generates an instantiation from the base templateXltchar chargt xcc generates an instantiation from the partial specializationXltchar intgt xii return 0

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list

15

December 2010

Master of Computer Application (MCA) ndash Semester 2

MC0066 ndash OOPS using C++ ndash 4 Credits

(Book ID B0681 amp B0715)

Assignment Set ndash 2 (40 Marks)

Answer all Questions Each Question carries FIVE Marks

Book ID B0681

1 Distinguished between procedural language and OOP language And Explain the key features of OOP

1 Procedural language focuses on organizing program statements into procedures or functions Larger programs were either broken into functions or modules whereas in Object Oriented Programming bundles both data and functions into one unitknown as object

2 One of the main problems with Procedural approach for programming was data being completely forgotten The emphasis was on the action and the data was only used in the entire process Whereas in Object Oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data

3 The procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled whereas in OOP language provides this flexibility through the concept of class

4 Another limitation of the procedural languages is that the program model is not closer to real world objects For example if you want to develop a gaming application of car race what data would you use and what functions you would require is difficult questions to answer in a procedural approach In the object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality In the car game example we would create several objects such as player car traffic signal and so on

Key Features of OOP

Encapsulation Data and functions are said to be encapsulated in an single entity as object

Data Hiding The data is said to be hidden thus not allowing accidental modification

Inheritance Inheritance is one of the most powerful feature of Object Oriented Programming Languages that allows you to derive a class from an existing class and inherit all the characteristics and behaviour of the parent class This feature allows easy modification of existing code and also reuse code The ability to reuse components of a program is an important feature for any programming language

16

Polymorphism and Overloading Operator overloading feature allows users to define how basic operators work with objects The operator + will be adding two numbers when used with integer variables However when used with user defined string class + operator may concatenate two strings Similarly same functions with same function name can perform different actions depending upon which object calls the function This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms) Operator overloading is a kind of polymorphism

2 Discuss the constructors and Destructors with suitable example

Constructors Constructors are member functions of a class which have same name as the class name Constructors are called automatically whenever an object of the class is created This feature makes it very useful to initialize the classdata members whenever a new object is created It also can perform any other function that needs to be performed for all the objects of the class without explicitly specifying it

Destructors Destructors on the other hand are also member functions with the same name as class but are prefixed with tilde (~) sign to differentiate it from theconstructor They are invoked automatically whenever the objectrsquos life expires or it is destroyed It can be used to return the memory back to the operating system if the memory was dynamically allocated

The following program implements the constructor and destructors for aclass

constdestcpp includeltiostreamhgtclass sample

privateint datapublicsample()

data=0 coutltltrdquoConstructor invokedrdquoltltendl

~sample()

coutltltrdquoDestructor invokedrdquovoid display()

coutltltrdquoData=rdquoltltdataltltendlvoid main()

sample obj1 objdisplay()

17

If you run the above program you will get the output as follows

Constructor invokedData=0Destructor invoked

When object of sample class object is created automatically the constructor is invoked and data is initialized to zero When the program ends the object is destroyed which invokes the destructor Please note that both the constructor and destructor is declared as public and they have no return value However constructors can have arguments and can be overloaded so that different constructors can be called depending upon the arguments that is passed Destructors on the other hand cannot be overloaded and cannot have any arguments

3 Describe the theory behind Virtual Functions and Polymorphism along with suitable programming examples for each

Virtual Functions Virtual means existing in effect but not in reality Virtual functions are primarily used in inheritance Let us suppose you have a class base as shown in the following program and two classes derv1 and derv2 are publicly derived from class base You would like to create a pointer that points to any of the derived class objects If you create a pointer of derv1 then it can point to derv1 object only Compiler will complain if you assign any other object is assigned to the pointer The solution is to create a pointer to Base class

Even though the address of derived classes is assigned to the pointer the compiler executes the base class function However if the base class function is made virtual we get the desired result In the following program we have made the base class function show() as virtual function by prefixing with the keyword virtual

virtualcpp include ltiostreamhgtclass base

publicvirtual void show() virtual functioncoutltltldquobaserdquoltltendl

class derv1public base

publicvoid show()

coutltltldquoderv1rdquoltltendl

class derv2 public base

publicvoid show()

18

coutltltldquoderv2rdquoltltendl

void main()derv1 dv1derv2 dv2base ptrptr=ampdv1ptr-gtshow()ptr=ampdv2ptr-gtshow()

By declaring the base class function as virtual we now get the output as

derv1derv2

In the above program depending on the contents in the pointer the compiler decides which class function to call during runtime This is known as late binding or dynamic binding

Polymorphism Polymorphism means same content but different forms In C++ polymorphism enables the same program code calling different functions of different classes For Example Letrsquos us suppose that we want to create a class shape and derive classes such as rectangle circle triangle etc Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen You would like to write a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime

Shape ptrarr[100]for (int j=0jltnj++)Ptrarr[j]-gtdraw()

This is an very desirable capability that completely different functions are executed by same function call If the ptrarr is pointing to a rectangle a rectangle is drawn If it is point int to circle circle is drawn

This is exactly what is polymorphism However to implement this approach several conditions should be met Firstly all the classes rectangle circle triangle should be derived from a single class (Here it is shape class) Secondly the draw() function must be declared as virtual in the base class (in the shape class)Operator overloading is a type of polymorphism which allows the same operators to behave differently with different datatypesoperands

4 Discuss the types of Inheritance with suitable example for each

Inheritance is a very powerful featu re of object oriented programming It allows reuse of code without modifying the original code It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort

19

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 3: (MCA) – Semester II MC0066 – OOPS using C++

If ((num2) == 0)coutltltnum ltltrdquo is a even numberrdquoelsecoutltltnum ltltrdquo is a odd numberrdquogetch()

The above program accepts a number from the user and divides it by 2 and if the remainder (remainder is obtained by modulus operator) is zero it displays the number is even otherwise as odd We make use of the relational operator == to compare whether remainder is equal to zero or not

2 Nested If statement

If statement can be nested in another if statement to check multipleconditionsIf (condition1) if (condition 2)

statement1Statement2

else if (condition3)

statement3

else statement4

Example

Largecpp include ltiostreamhgtvoid main()

int abccoutltltrdquoPlease enter three numbersrdquocingtgtagtgtbgtgtcIf ((agtb) ampamp (bgtc))

coutltltaltlt ldquo is the largest numberrdquoelse if ((bgta) ampamp (bgtc))

coutltltbltlt ldquo is the largest numberrdquoelse if ((cgta) ampamp (cgtb))

coutltltcltlt ldquo is the largest numberrdquo

The above program accepts three numbers from the user and displays which is the largest number among the three( assumption is that all the numbers are unique the program has to be modified if you would like to allow same number twice)

3

3 Switch statement One alternative to nested if is the switch statement which can be used to increase clarity in case of checking the different values of the same variable and execute statements accordingly

Syntax

Switch (variablename)

case value1 statement1breakcase value2 statement2breakcase value3 statement3breakdefault statement4

Example

includeltiostreamhgtvoid main()

char posint x=15 y=15cout ltlt ldquo you are currently located atrdquo ltltxltltrdquo ldquoltltyltltendlcoutgtgtrdquoplease choose the letter to move l for left r for right u for up and d for downrdquo ltltendlcingtgtposswitch (pos) case lsquolrsquo x--breakcase lsquorrsquo x++breakcase lsquoursquo y++breakcase lsquodrsquo y--breakdefault coutltltrdquoYou selected a wrong optionrdquocoutltltldquo you are now located atrdquo ltltxltltrdquo ldquoltlty

The above program asks the user to enter l r u d for allowing him to move left right up and down respectively The position is initialised to 15 and 15 which are x and y coordinates of his position Depending upon the what user

4

has selected the the x and y co-ordinates are incremented or decremented by one(x++ is same as x=x+1) If the user types a letter other than l r u d he gets an error message Since the switch variable is a character l u r d and enclosed within single quote

4 Conditional Operator Conditional operator () is a handy operator which acts like a shortcut for if else statement If you had to compare two variables a and b and then depending on which is larger you wanted to store that variable in anothervariable called large You would do this using if else statement in the following wayif (agtb)large=aelselarge=b

The above can be done using conditional operator in the following way large= (agtb) a b

3 Write a program in C++ for matrix multiplication The program should accept the dimensions of both the matrices to be multiplied and check for compatibility with appropriate messages and give the output

include matrixMultiplyhinclude ltiostreamgtinclude ltstdlibhgt

using namespace std

int main()

int rows1 = 3 int columns1 = 1 float matrixOne = (float)malloc(rows1 sizeof(matrixOne))

for(int i = 0 i lt rows1 i++)

matrixOne[i] = (float)malloc(columns1 sizeof(float))

matrixOne[0][0] = 1 matrixOne[1][0] = -1 matrixOne[2][0] = 1

int rows2 = 1 int columns2 = 3 float matrixTwo = (float)malloc(rows2 sizeof(matrixTwo))

for(int i = 0 i lt rows2 i++)

matrixTwo[i] = (float)malloc(columns2 sizeof(float))

matrixTwo[0][0] = 1

5

matrixTwo[0][1] = -1 matrixTwo[0][2] = 1

float result result = (float)malloc(rows1 sizeof(result))

for(int i = 0 i lt rows1 i++)

result[i] = (float)malloc(columns2 sizeof(float))

matrixMultiply(31matrixOne13matrixTwo result)

return 0

void matrixMultiply(int rows1 int cols1 float mat1 int rows2 int cols2 float mat2 float result)

if( cols1 = rows2 )

cout ltlt Cant Multiply These Matricies

else

float tempResult

for (int i=0iltrows1i++)

for(int j=0jltcols2j++)

tempResult = 0

for(int k=0kltrows2k++) tempResult += mat1[i][k]mat2[k][j]

result[i][j] = tempResult

6

4 What do you mean by operator overloading Illustrate with suitable example for overloading Unary operators

Operator overloading Operator overloading is an interesting feature of C++ that allows programmers to specify how various arithmetic relational and many other operators work with user defined data-types or classes To perform addition of two distance objects we used a call d3add(d1d2) Instead of such statements it would be more clear if we could use statements like d3=d1+d2 This is possible only if we inform compiler about how + operator works with distance class

It helps to use the default operators with the user defined objects for making the code simpler However there are several problems with operator overloading which you should be aware of When using operator overloading the operator should perform only the most obvious function Otherwise it will lead to more confusion If you are overloading + operator for distance class it should add two distance objects and should not do something else

Several operators such as dot operator scope resolution () operator conditional operator () etc cannot be overloaded Therefore operator overloading should be used for a class where it is required to perform obvious functions with the default operators and there can be no other meaning for the same

Overloading Unary Operators

We have seen that unary operators are those operators which work on one operator Some of the unary operators are ++ -- and minus (-) Operator overloading works similar to any member function of a class But it is not invoked using dot operator Just like member function the operator has a return value and takes arguments It is invoked by the operand which uses it In case of overloading of unary operators the calling operand can be either left or right of the operator like in case of increment and decrement operators While defining the operator functionality for the class the keyword operator is used

Example

unarycpp include ltiostreamhgtclass counter

unsigned int countpubliccounter()count=0int getcount()

7

return count

void operator ++() count++

void main()

counter c1c1++++c1coutltltc1getcount()

In the above example the operator ++ is defined to return void and take no arguments All unary operators do not take no arguments as it operates on only one operand and that operand itself invokes the operator Therefore the operand is sent by default

Book ID B0715

5 Write C++ program which demonstrate the difference between static and dynamic binding

Dynamic Binding You can specify that the compiler match a function call with the correct function definition at run time this is called dynamic binding You declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific function

The following examples demonstrate the differences between static and dynamic binding

The first example demonstrates static binding

include ltiostreamgtusing namespace stdstruct A

void f()

cout ltlt Class A ltlt endl

struct B A

void f()

cout ltlt Class B ltlt endl

8

void g(Aamp arg)

argf()

int main()

B xg(x)

The following is the output of the above example

Class A

When function g() is called function Af() is called although the argument refers to an object of type B At compile time the compiler knows only that the argument of function g() will be a reference to an object derived from A it cannot determine whether the argument will be a reference to an object of type A or type B However this can be determined at run time

The following example is the same as the previous example except that Af() is declared with the virtual keyword

include ltiostreamgtusing namespace stdstruct A

virtual void f()

cout ltlt Class A ltlt endl

struct B A

void f()

cout ltlt Class B ltlt endl

void g(Aamp arg )

argf()

int main()

B xg(x)

9

The following is the output of the above example

Class B

The virtual keyword indicates to the compiler that it should choose the appropriate definition of f() not by the type of reference but by the type of object that the reference refers to

6 Describe the Friend functions and friend classes with programming examples

Friend Functions Friend Functions are the functions which can have access to the private and protected members of the class They can be declared anywhere within a class declaration but it is common practice to list friends at the beginning of the class The public and protected keywords do not apply to friend functions as the class has no control over the scope of friends If we want to declare an external function as friend of a class thus allowing this function to have access to the private and protected members of this class we do it by declaring a prototype of this external function within the class and preceding it with the keyword friend

friend functionsinclude ltiostreamgtusing namespace stdclass CRectangle

int width heightpublicvoid set_values (int int)int area () return (width height)friend CRectangle duplicate (CRectangle)

void CRectangleset_values (int a int b)

width = aheight = b

CRectangle duplicate (CRectangle rectparam)

CRectangle rectresrectreswidth = rectparamwidth2rectresheight = rectparamheight2return (rectres)

int main () CRectangle rect rectbrectset_values (23)rectb = duplicate (rect)cout ltlt rectbarea()

return 0

10

The duplicate function is a friend of CRectangle From within that function we have been able to access the members width and height of different objects of type CRectangle which are private members Notice that neither in the declaration of duplicate() nor in its later use in main() have we considered duplicate a member of class CRectangle It isnt It simply has access to its private and protected members without being a member The friend functions can serve for example to conduct operations between two different classes Generally the use of friend functions is out of an object-oriented programming methodology so whenever possible it is better to use members of the same class to perform operations with them Such as in the previous example it would have been shorter to integrate duplicate() within the class CRectangle

Friend Classes A class can declare a member function of another class as a friend or declare another class as a friend class Friend classes are used in cases where one class is tightly coupled to another class For example suppose we have a class CPoint that represents a coordinate and a class CPointCollection that holds a list of points Since the collection class may need to manipulate point objects we could declare CPointCollection as a friend of the CPoint class

Forward declaration of friend classclass CPointCollection Point classclass CPoint friend CPointCollectionprivatedouble m_xdouble m_ypublicCPoint(const double x const double y) m_x(x)m_y(y) ~CPoint(void)

Since the collection class is a friend of CPoint it has access to the internal data of any point object This is useful when individual elements of the collection need to be manipulated For example a set method of the CPointCollection class could set all CPoint elements to a particular value (vector is a STL container which is discussed in detail in Chapter 8)

class CPointCollection privatevectorltCPointgt m_vecPointspublicCPointCollection(const int nSize) m_vecPoints(nSize) ~CPointCollection(void)void set(const double x const double y)

The set member can iterate over the collection and reset each point

11

void CPointCollectionset(const double x const double y) Get the number of elements in the collectionconst int nElements = m_vecPointssize() Set each elementfor(int i=0 iltnElements i++) m_vecPoints[i]m_x = xm_vecPoints[i]m_y = y

One thing important about friend classes is that friendship is not mutual although CPointCollection can access CPoint the converse is not true Friendship is also not something that is passed down a class hierarchy Derived classes of CPointCollection will not be able to access CPoint The principle behind this is that friendship is not implicitly granted each class must explicitly choose its friends

7 Illustrate with suitable examples various file handling methods in C++

File handling is an important part of all programs Most of the applications will have their own features to save some data to the local disk and read data from the disk again C++ File IO classes simplify such file readwrite operations for the programmer by providing easier to use classes

C++ File IO Classes and Functions

There are 3 File IO classes in C++ which are used for File ReadWrite operations They areifstream - Can be used for File readinput operationsofstream - Can be used for File writeoutput operationsfstream - Can be used for both readwrite c++ file IO operations

The most important methods which will be used for any file operations are

fstreamopen method - to open the filefstreamOperator gtgt and fstreamOperator ltlt - For reading from or writing to the filefstreamclose - Flushes all buffer and close the fileReading a text file using fstream class

There are several ways of reading the text from a file But all of them have a common approach as follows

Open the fileRead the dataClose the file

This sample code snippet explains how to use the c++ file io stream operators to read data from a file In all cases the header file fstreamh must be included

includeltfstreamhgt int main() char str[2000]

12

fstream file_op(ctest_filetxtiosin) while(file_op gtgt str) cout ltlt str file_opclose()

return 0

The class fstream which is used above is the one which is commonly used for c++ file io manipulations The constructor of fstream takes 2 parameters One is the file path and the second is the File Open mode There are several open modes each of them with a different purpose

But if we look at the output closely there is a draw back in using this stream operator read The output misses the white spaces and the end of line characters In order not to miss these characters we can either use fstreamget() or fstreamgetline() methods Here is the example for using fstream getline method

include ltfstreamhgt int main() char str[2000] fstream file_op(ctest_filetxtiosin) while(file_opeof()) file_opgetline(str2000) cout ltltstr file_opclose() cout ltltendl

return 0

Writing to a text file using fstream class

Writing to a text file can also be achieved with the stream operators This also follows the same order of operations though with a slight difference 1 open a file - in write mode 2 Write to a file 3 close the file Look at the following sample code to see the difference

include ltfstreamhgt int main() fstream file_op(cCoderSource_filetxtiosout)

file_opltltTest Write to file file_opclose()

13

return 0

To modify the data or to seek to a different position inside the file the c++ file io class fstream provides member functionslike seekg() etc These functions can be used to relocate the record insert position to the desired locations

After all the C++ File IO operations we do a fstreamclose() to close the file pointer This is not mandatory Even if this function is not called by the application the destructor of the fstream class will close the file when the object goes out of scope

8 What is template specialization Describe a scenario in which template class partial specialization is considered appropriate

Class Template Specialization In some cases it is possible to override the template-generated code by providing special definitions for specific types This is called template specialization

The following example defines a template class specialization for template class stream

include ltiostreamgtusing namespace stdtemplate ltclass Tgtclass stream publicvoid f() cout ltlt streamltTgtf()ltlt endl template ltgtclass streamltchargt publicvoid f() cout ltlt streamltchargtf()ltlt endl int main()

streamltintgt sistreamltchargt scsif()scf()return 0

Output

streamltTgtf()streamltchargtf()

In the above example streamltchargt is used as the definition of streams of chars other streams will be handled by the template class generated from the class template

14

Template Class Partial Specialization You may want to generate a specialization of the class for just one parameter for example

base template classtemplatelttypename T1 typename T2gtclass X partial specializationtemplatelttypename T1gtclass XltT1 intgt int main() generates an instantiation from the base templateXltchar chargt xcc generates an instantiation from the partial specializationXltchar intgt xii return 0

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list

15

December 2010

Master of Computer Application (MCA) ndash Semester 2

MC0066 ndash OOPS using C++ ndash 4 Credits

(Book ID B0681 amp B0715)

Assignment Set ndash 2 (40 Marks)

Answer all Questions Each Question carries FIVE Marks

Book ID B0681

1 Distinguished between procedural language and OOP language And Explain the key features of OOP

1 Procedural language focuses on organizing program statements into procedures or functions Larger programs were either broken into functions or modules whereas in Object Oriented Programming bundles both data and functions into one unitknown as object

2 One of the main problems with Procedural approach for programming was data being completely forgotten The emphasis was on the action and the data was only used in the entire process Whereas in Object Oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data

3 The procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled whereas in OOP language provides this flexibility through the concept of class

4 Another limitation of the procedural languages is that the program model is not closer to real world objects For example if you want to develop a gaming application of car race what data would you use and what functions you would require is difficult questions to answer in a procedural approach In the object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality In the car game example we would create several objects such as player car traffic signal and so on

Key Features of OOP

Encapsulation Data and functions are said to be encapsulated in an single entity as object

Data Hiding The data is said to be hidden thus not allowing accidental modification

Inheritance Inheritance is one of the most powerful feature of Object Oriented Programming Languages that allows you to derive a class from an existing class and inherit all the characteristics and behaviour of the parent class This feature allows easy modification of existing code and also reuse code The ability to reuse components of a program is an important feature for any programming language

16

Polymorphism and Overloading Operator overloading feature allows users to define how basic operators work with objects The operator + will be adding two numbers when used with integer variables However when used with user defined string class + operator may concatenate two strings Similarly same functions with same function name can perform different actions depending upon which object calls the function This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms) Operator overloading is a kind of polymorphism

2 Discuss the constructors and Destructors with suitable example

Constructors Constructors are member functions of a class which have same name as the class name Constructors are called automatically whenever an object of the class is created This feature makes it very useful to initialize the classdata members whenever a new object is created It also can perform any other function that needs to be performed for all the objects of the class without explicitly specifying it

Destructors Destructors on the other hand are also member functions with the same name as class but are prefixed with tilde (~) sign to differentiate it from theconstructor They are invoked automatically whenever the objectrsquos life expires or it is destroyed It can be used to return the memory back to the operating system if the memory was dynamically allocated

The following program implements the constructor and destructors for aclass

constdestcpp includeltiostreamhgtclass sample

privateint datapublicsample()

data=0 coutltltrdquoConstructor invokedrdquoltltendl

~sample()

coutltltrdquoDestructor invokedrdquovoid display()

coutltltrdquoData=rdquoltltdataltltendlvoid main()

sample obj1 objdisplay()

17

If you run the above program you will get the output as follows

Constructor invokedData=0Destructor invoked

When object of sample class object is created automatically the constructor is invoked and data is initialized to zero When the program ends the object is destroyed which invokes the destructor Please note that both the constructor and destructor is declared as public and they have no return value However constructors can have arguments and can be overloaded so that different constructors can be called depending upon the arguments that is passed Destructors on the other hand cannot be overloaded and cannot have any arguments

3 Describe the theory behind Virtual Functions and Polymorphism along with suitable programming examples for each

Virtual Functions Virtual means existing in effect but not in reality Virtual functions are primarily used in inheritance Let us suppose you have a class base as shown in the following program and two classes derv1 and derv2 are publicly derived from class base You would like to create a pointer that points to any of the derived class objects If you create a pointer of derv1 then it can point to derv1 object only Compiler will complain if you assign any other object is assigned to the pointer The solution is to create a pointer to Base class

Even though the address of derived classes is assigned to the pointer the compiler executes the base class function However if the base class function is made virtual we get the desired result In the following program we have made the base class function show() as virtual function by prefixing with the keyword virtual

virtualcpp include ltiostreamhgtclass base

publicvirtual void show() virtual functioncoutltltldquobaserdquoltltendl

class derv1public base

publicvoid show()

coutltltldquoderv1rdquoltltendl

class derv2 public base

publicvoid show()

18

coutltltldquoderv2rdquoltltendl

void main()derv1 dv1derv2 dv2base ptrptr=ampdv1ptr-gtshow()ptr=ampdv2ptr-gtshow()

By declaring the base class function as virtual we now get the output as

derv1derv2

In the above program depending on the contents in the pointer the compiler decides which class function to call during runtime This is known as late binding or dynamic binding

Polymorphism Polymorphism means same content but different forms In C++ polymorphism enables the same program code calling different functions of different classes For Example Letrsquos us suppose that we want to create a class shape and derive classes such as rectangle circle triangle etc Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen You would like to write a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime

Shape ptrarr[100]for (int j=0jltnj++)Ptrarr[j]-gtdraw()

This is an very desirable capability that completely different functions are executed by same function call If the ptrarr is pointing to a rectangle a rectangle is drawn If it is point int to circle circle is drawn

This is exactly what is polymorphism However to implement this approach several conditions should be met Firstly all the classes rectangle circle triangle should be derived from a single class (Here it is shape class) Secondly the draw() function must be declared as virtual in the base class (in the shape class)Operator overloading is a type of polymorphism which allows the same operators to behave differently with different datatypesoperands

4 Discuss the types of Inheritance with suitable example for each

Inheritance is a very powerful featu re of object oriented programming It allows reuse of code without modifying the original code It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort

19

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 4: (MCA) – Semester II MC0066 – OOPS using C++

3 Switch statement One alternative to nested if is the switch statement which can be used to increase clarity in case of checking the different values of the same variable and execute statements accordingly

Syntax

Switch (variablename)

case value1 statement1breakcase value2 statement2breakcase value3 statement3breakdefault statement4

Example

includeltiostreamhgtvoid main()

char posint x=15 y=15cout ltlt ldquo you are currently located atrdquo ltltxltltrdquo ldquoltltyltltendlcoutgtgtrdquoplease choose the letter to move l for left r for right u for up and d for downrdquo ltltendlcingtgtposswitch (pos) case lsquolrsquo x--breakcase lsquorrsquo x++breakcase lsquoursquo y++breakcase lsquodrsquo y--breakdefault coutltltrdquoYou selected a wrong optionrdquocoutltltldquo you are now located atrdquo ltltxltltrdquo ldquoltlty

The above program asks the user to enter l r u d for allowing him to move left right up and down respectively The position is initialised to 15 and 15 which are x and y coordinates of his position Depending upon the what user

4

has selected the the x and y co-ordinates are incremented or decremented by one(x++ is same as x=x+1) If the user types a letter other than l r u d he gets an error message Since the switch variable is a character l u r d and enclosed within single quote

4 Conditional Operator Conditional operator () is a handy operator which acts like a shortcut for if else statement If you had to compare two variables a and b and then depending on which is larger you wanted to store that variable in anothervariable called large You would do this using if else statement in the following wayif (agtb)large=aelselarge=b

The above can be done using conditional operator in the following way large= (agtb) a b

3 Write a program in C++ for matrix multiplication The program should accept the dimensions of both the matrices to be multiplied and check for compatibility with appropriate messages and give the output

include matrixMultiplyhinclude ltiostreamgtinclude ltstdlibhgt

using namespace std

int main()

int rows1 = 3 int columns1 = 1 float matrixOne = (float)malloc(rows1 sizeof(matrixOne))

for(int i = 0 i lt rows1 i++)

matrixOne[i] = (float)malloc(columns1 sizeof(float))

matrixOne[0][0] = 1 matrixOne[1][0] = -1 matrixOne[2][0] = 1

int rows2 = 1 int columns2 = 3 float matrixTwo = (float)malloc(rows2 sizeof(matrixTwo))

for(int i = 0 i lt rows2 i++)

matrixTwo[i] = (float)malloc(columns2 sizeof(float))

matrixTwo[0][0] = 1

5

matrixTwo[0][1] = -1 matrixTwo[0][2] = 1

float result result = (float)malloc(rows1 sizeof(result))

for(int i = 0 i lt rows1 i++)

result[i] = (float)malloc(columns2 sizeof(float))

matrixMultiply(31matrixOne13matrixTwo result)

return 0

void matrixMultiply(int rows1 int cols1 float mat1 int rows2 int cols2 float mat2 float result)

if( cols1 = rows2 )

cout ltlt Cant Multiply These Matricies

else

float tempResult

for (int i=0iltrows1i++)

for(int j=0jltcols2j++)

tempResult = 0

for(int k=0kltrows2k++) tempResult += mat1[i][k]mat2[k][j]

result[i][j] = tempResult

6

4 What do you mean by operator overloading Illustrate with suitable example for overloading Unary operators

Operator overloading Operator overloading is an interesting feature of C++ that allows programmers to specify how various arithmetic relational and many other operators work with user defined data-types or classes To perform addition of two distance objects we used a call d3add(d1d2) Instead of such statements it would be more clear if we could use statements like d3=d1+d2 This is possible only if we inform compiler about how + operator works with distance class

It helps to use the default operators with the user defined objects for making the code simpler However there are several problems with operator overloading which you should be aware of When using operator overloading the operator should perform only the most obvious function Otherwise it will lead to more confusion If you are overloading + operator for distance class it should add two distance objects and should not do something else

Several operators such as dot operator scope resolution () operator conditional operator () etc cannot be overloaded Therefore operator overloading should be used for a class where it is required to perform obvious functions with the default operators and there can be no other meaning for the same

Overloading Unary Operators

We have seen that unary operators are those operators which work on one operator Some of the unary operators are ++ -- and minus (-) Operator overloading works similar to any member function of a class But it is not invoked using dot operator Just like member function the operator has a return value and takes arguments It is invoked by the operand which uses it In case of overloading of unary operators the calling operand can be either left or right of the operator like in case of increment and decrement operators While defining the operator functionality for the class the keyword operator is used

Example

unarycpp include ltiostreamhgtclass counter

unsigned int countpubliccounter()count=0int getcount()

7

return count

void operator ++() count++

void main()

counter c1c1++++c1coutltltc1getcount()

In the above example the operator ++ is defined to return void and take no arguments All unary operators do not take no arguments as it operates on only one operand and that operand itself invokes the operator Therefore the operand is sent by default

Book ID B0715

5 Write C++ program which demonstrate the difference between static and dynamic binding

Dynamic Binding You can specify that the compiler match a function call with the correct function definition at run time this is called dynamic binding You declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific function

The following examples demonstrate the differences between static and dynamic binding

The first example demonstrates static binding

include ltiostreamgtusing namespace stdstruct A

void f()

cout ltlt Class A ltlt endl

struct B A

void f()

cout ltlt Class B ltlt endl

8

void g(Aamp arg)

argf()

int main()

B xg(x)

The following is the output of the above example

Class A

When function g() is called function Af() is called although the argument refers to an object of type B At compile time the compiler knows only that the argument of function g() will be a reference to an object derived from A it cannot determine whether the argument will be a reference to an object of type A or type B However this can be determined at run time

The following example is the same as the previous example except that Af() is declared with the virtual keyword

include ltiostreamgtusing namespace stdstruct A

virtual void f()

cout ltlt Class A ltlt endl

struct B A

void f()

cout ltlt Class B ltlt endl

void g(Aamp arg )

argf()

int main()

B xg(x)

9

The following is the output of the above example

Class B

The virtual keyword indicates to the compiler that it should choose the appropriate definition of f() not by the type of reference but by the type of object that the reference refers to

6 Describe the Friend functions and friend classes with programming examples

Friend Functions Friend Functions are the functions which can have access to the private and protected members of the class They can be declared anywhere within a class declaration but it is common practice to list friends at the beginning of the class The public and protected keywords do not apply to friend functions as the class has no control over the scope of friends If we want to declare an external function as friend of a class thus allowing this function to have access to the private and protected members of this class we do it by declaring a prototype of this external function within the class and preceding it with the keyword friend

friend functionsinclude ltiostreamgtusing namespace stdclass CRectangle

int width heightpublicvoid set_values (int int)int area () return (width height)friend CRectangle duplicate (CRectangle)

void CRectangleset_values (int a int b)

width = aheight = b

CRectangle duplicate (CRectangle rectparam)

CRectangle rectresrectreswidth = rectparamwidth2rectresheight = rectparamheight2return (rectres)

int main () CRectangle rect rectbrectset_values (23)rectb = duplicate (rect)cout ltlt rectbarea()

return 0

10

The duplicate function is a friend of CRectangle From within that function we have been able to access the members width and height of different objects of type CRectangle which are private members Notice that neither in the declaration of duplicate() nor in its later use in main() have we considered duplicate a member of class CRectangle It isnt It simply has access to its private and protected members without being a member The friend functions can serve for example to conduct operations between two different classes Generally the use of friend functions is out of an object-oriented programming methodology so whenever possible it is better to use members of the same class to perform operations with them Such as in the previous example it would have been shorter to integrate duplicate() within the class CRectangle

Friend Classes A class can declare a member function of another class as a friend or declare another class as a friend class Friend classes are used in cases where one class is tightly coupled to another class For example suppose we have a class CPoint that represents a coordinate and a class CPointCollection that holds a list of points Since the collection class may need to manipulate point objects we could declare CPointCollection as a friend of the CPoint class

Forward declaration of friend classclass CPointCollection Point classclass CPoint friend CPointCollectionprivatedouble m_xdouble m_ypublicCPoint(const double x const double y) m_x(x)m_y(y) ~CPoint(void)

Since the collection class is a friend of CPoint it has access to the internal data of any point object This is useful when individual elements of the collection need to be manipulated For example a set method of the CPointCollection class could set all CPoint elements to a particular value (vector is a STL container which is discussed in detail in Chapter 8)

class CPointCollection privatevectorltCPointgt m_vecPointspublicCPointCollection(const int nSize) m_vecPoints(nSize) ~CPointCollection(void)void set(const double x const double y)

The set member can iterate over the collection and reset each point

11

void CPointCollectionset(const double x const double y) Get the number of elements in the collectionconst int nElements = m_vecPointssize() Set each elementfor(int i=0 iltnElements i++) m_vecPoints[i]m_x = xm_vecPoints[i]m_y = y

One thing important about friend classes is that friendship is not mutual although CPointCollection can access CPoint the converse is not true Friendship is also not something that is passed down a class hierarchy Derived classes of CPointCollection will not be able to access CPoint The principle behind this is that friendship is not implicitly granted each class must explicitly choose its friends

7 Illustrate with suitable examples various file handling methods in C++

File handling is an important part of all programs Most of the applications will have their own features to save some data to the local disk and read data from the disk again C++ File IO classes simplify such file readwrite operations for the programmer by providing easier to use classes

C++ File IO Classes and Functions

There are 3 File IO classes in C++ which are used for File ReadWrite operations They areifstream - Can be used for File readinput operationsofstream - Can be used for File writeoutput operationsfstream - Can be used for both readwrite c++ file IO operations

The most important methods which will be used for any file operations are

fstreamopen method - to open the filefstreamOperator gtgt and fstreamOperator ltlt - For reading from or writing to the filefstreamclose - Flushes all buffer and close the fileReading a text file using fstream class

There are several ways of reading the text from a file But all of them have a common approach as follows

Open the fileRead the dataClose the file

This sample code snippet explains how to use the c++ file io stream operators to read data from a file In all cases the header file fstreamh must be included

includeltfstreamhgt int main() char str[2000]

12

fstream file_op(ctest_filetxtiosin) while(file_op gtgt str) cout ltlt str file_opclose()

return 0

The class fstream which is used above is the one which is commonly used for c++ file io manipulations The constructor of fstream takes 2 parameters One is the file path and the second is the File Open mode There are several open modes each of them with a different purpose

But if we look at the output closely there is a draw back in using this stream operator read The output misses the white spaces and the end of line characters In order not to miss these characters we can either use fstreamget() or fstreamgetline() methods Here is the example for using fstream getline method

include ltfstreamhgt int main() char str[2000] fstream file_op(ctest_filetxtiosin) while(file_opeof()) file_opgetline(str2000) cout ltltstr file_opclose() cout ltltendl

return 0

Writing to a text file using fstream class

Writing to a text file can also be achieved with the stream operators This also follows the same order of operations though with a slight difference 1 open a file - in write mode 2 Write to a file 3 close the file Look at the following sample code to see the difference

include ltfstreamhgt int main() fstream file_op(cCoderSource_filetxtiosout)

file_opltltTest Write to file file_opclose()

13

return 0

To modify the data or to seek to a different position inside the file the c++ file io class fstream provides member functionslike seekg() etc These functions can be used to relocate the record insert position to the desired locations

After all the C++ File IO operations we do a fstreamclose() to close the file pointer This is not mandatory Even if this function is not called by the application the destructor of the fstream class will close the file when the object goes out of scope

8 What is template specialization Describe a scenario in which template class partial specialization is considered appropriate

Class Template Specialization In some cases it is possible to override the template-generated code by providing special definitions for specific types This is called template specialization

The following example defines a template class specialization for template class stream

include ltiostreamgtusing namespace stdtemplate ltclass Tgtclass stream publicvoid f() cout ltlt streamltTgtf()ltlt endl template ltgtclass streamltchargt publicvoid f() cout ltlt streamltchargtf()ltlt endl int main()

streamltintgt sistreamltchargt scsif()scf()return 0

Output

streamltTgtf()streamltchargtf()

In the above example streamltchargt is used as the definition of streams of chars other streams will be handled by the template class generated from the class template

14

Template Class Partial Specialization You may want to generate a specialization of the class for just one parameter for example

base template classtemplatelttypename T1 typename T2gtclass X partial specializationtemplatelttypename T1gtclass XltT1 intgt int main() generates an instantiation from the base templateXltchar chargt xcc generates an instantiation from the partial specializationXltchar intgt xii return 0

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list

15

December 2010

Master of Computer Application (MCA) ndash Semester 2

MC0066 ndash OOPS using C++ ndash 4 Credits

(Book ID B0681 amp B0715)

Assignment Set ndash 2 (40 Marks)

Answer all Questions Each Question carries FIVE Marks

Book ID B0681

1 Distinguished between procedural language and OOP language And Explain the key features of OOP

1 Procedural language focuses on organizing program statements into procedures or functions Larger programs were either broken into functions or modules whereas in Object Oriented Programming bundles both data and functions into one unitknown as object

2 One of the main problems with Procedural approach for programming was data being completely forgotten The emphasis was on the action and the data was only used in the entire process Whereas in Object Oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data

3 The procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled whereas in OOP language provides this flexibility through the concept of class

4 Another limitation of the procedural languages is that the program model is not closer to real world objects For example if you want to develop a gaming application of car race what data would you use and what functions you would require is difficult questions to answer in a procedural approach In the object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality In the car game example we would create several objects such as player car traffic signal and so on

Key Features of OOP

Encapsulation Data and functions are said to be encapsulated in an single entity as object

Data Hiding The data is said to be hidden thus not allowing accidental modification

Inheritance Inheritance is one of the most powerful feature of Object Oriented Programming Languages that allows you to derive a class from an existing class and inherit all the characteristics and behaviour of the parent class This feature allows easy modification of existing code and also reuse code The ability to reuse components of a program is an important feature for any programming language

16

Polymorphism and Overloading Operator overloading feature allows users to define how basic operators work with objects The operator + will be adding two numbers when used with integer variables However when used with user defined string class + operator may concatenate two strings Similarly same functions with same function name can perform different actions depending upon which object calls the function This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms) Operator overloading is a kind of polymorphism

2 Discuss the constructors and Destructors with suitable example

Constructors Constructors are member functions of a class which have same name as the class name Constructors are called automatically whenever an object of the class is created This feature makes it very useful to initialize the classdata members whenever a new object is created It also can perform any other function that needs to be performed for all the objects of the class without explicitly specifying it

Destructors Destructors on the other hand are also member functions with the same name as class but are prefixed with tilde (~) sign to differentiate it from theconstructor They are invoked automatically whenever the objectrsquos life expires or it is destroyed It can be used to return the memory back to the operating system if the memory was dynamically allocated

The following program implements the constructor and destructors for aclass

constdestcpp includeltiostreamhgtclass sample

privateint datapublicsample()

data=0 coutltltrdquoConstructor invokedrdquoltltendl

~sample()

coutltltrdquoDestructor invokedrdquovoid display()

coutltltrdquoData=rdquoltltdataltltendlvoid main()

sample obj1 objdisplay()

17

If you run the above program you will get the output as follows

Constructor invokedData=0Destructor invoked

When object of sample class object is created automatically the constructor is invoked and data is initialized to zero When the program ends the object is destroyed which invokes the destructor Please note that both the constructor and destructor is declared as public and they have no return value However constructors can have arguments and can be overloaded so that different constructors can be called depending upon the arguments that is passed Destructors on the other hand cannot be overloaded and cannot have any arguments

3 Describe the theory behind Virtual Functions and Polymorphism along with suitable programming examples for each

Virtual Functions Virtual means existing in effect but not in reality Virtual functions are primarily used in inheritance Let us suppose you have a class base as shown in the following program and two classes derv1 and derv2 are publicly derived from class base You would like to create a pointer that points to any of the derived class objects If you create a pointer of derv1 then it can point to derv1 object only Compiler will complain if you assign any other object is assigned to the pointer The solution is to create a pointer to Base class

Even though the address of derived classes is assigned to the pointer the compiler executes the base class function However if the base class function is made virtual we get the desired result In the following program we have made the base class function show() as virtual function by prefixing with the keyword virtual

virtualcpp include ltiostreamhgtclass base

publicvirtual void show() virtual functioncoutltltldquobaserdquoltltendl

class derv1public base

publicvoid show()

coutltltldquoderv1rdquoltltendl

class derv2 public base

publicvoid show()

18

coutltltldquoderv2rdquoltltendl

void main()derv1 dv1derv2 dv2base ptrptr=ampdv1ptr-gtshow()ptr=ampdv2ptr-gtshow()

By declaring the base class function as virtual we now get the output as

derv1derv2

In the above program depending on the contents in the pointer the compiler decides which class function to call during runtime This is known as late binding or dynamic binding

Polymorphism Polymorphism means same content but different forms In C++ polymorphism enables the same program code calling different functions of different classes For Example Letrsquos us suppose that we want to create a class shape and derive classes such as rectangle circle triangle etc Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen You would like to write a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime

Shape ptrarr[100]for (int j=0jltnj++)Ptrarr[j]-gtdraw()

This is an very desirable capability that completely different functions are executed by same function call If the ptrarr is pointing to a rectangle a rectangle is drawn If it is point int to circle circle is drawn

This is exactly what is polymorphism However to implement this approach several conditions should be met Firstly all the classes rectangle circle triangle should be derived from a single class (Here it is shape class) Secondly the draw() function must be declared as virtual in the base class (in the shape class)Operator overloading is a type of polymorphism which allows the same operators to behave differently with different datatypesoperands

4 Discuss the types of Inheritance with suitable example for each

Inheritance is a very powerful featu re of object oriented programming It allows reuse of code without modifying the original code It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort

19

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 5: (MCA) – Semester II MC0066 – OOPS using C++

has selected the the x and y co-ordinates are incremented or decremented by one(x++ is same as x=x+1) If the user types a letter other than l r u d he gets an error message Since the switch variable is a character l u r d and enclosed within single quote

4 Conditional Operator Conditional operator () is a handy operator which acts like a shortcut for if else statement If you had to compare two variables a and b and then depending on which is larger you wanted to store that variable in anothervariable called large You would do this using if else statement in the following wayif (agtb)large=aelselarge=b

The above can be done using conditional operator in the following way large= (agtb) a b

3 Write a program in C++ for matrix multiplication The program should accept the dimensions of both the matrices to be multiplied and check for compatibility with appropriate messages and give the output

include matrixMultiplyhinclude ltiostreamgtinclude ltstdlibhgt

using namespace std

int main()

int rows1 = 3 int columns1 = 1 float matrixOne = (float)malloc(rows1 sizeof(matrixOne))

for(int i = 0 i lt rows1 i++)

matrixOne[i] = (float)malloc(columns1 sizeof(float))

matrixOne[0][0] = 1 matrixOne[1][0] = -1 matrixOne[2][0] = 1

int rows2 = 1 int columns2 = 3 float matrixTwo = (float)malloc(rows2 sizeof(matrixTwo))

for(int i = 0 i lt rows2 i++)

matrixTwo[i] = (float)malloc(columns2 sizeof(float))

matrixTwo[0][0] = 1

5

matrixTwo[0][1] = -1 matrixTwo[0][2] = 1

float result result = (float)malloc(rows1 sizeof(result))

for(int i = 0 i lt rows1 i++)

result[i] = (float)malloc(columns2 sizeof(float))

matrixMultiply(31matrixOne13matrixTwo result)

return 0

void matrixMultiply(int rows1 int cols1 float mat1 int rows2 int cols2 float mat2 float result)

if( cols1 = rows2 )

cout ltlt Cant Multiply These Matricies

else

float tempResult

for (int i=0iltrows1i++)

for(int j=0jltcols2j++)

tempResult = 0

for(int k=0kltrows2k++) tempResult += mat1[i][k]mat2[k][j]

result[i][j] = tempResult

6

4 What do you mean by operator overloading Illustrate with suitable example for overloading Unary operators

Operator overloading Operator overloading is an interesting feature of C++ that allows programmers to specify how various arithmetic relational and many other operators work with user defined data-types or classes To perform addition of two distance objects we used a call d3add(d1d2) Instead of such statements it would be more clear if we could use statements like d3=d1+d2 This is possible only if we inform compiler about how + operator works with distance class

It helps to use the default operators with the user defined objects for making the code simpler However there are several problems with operator overloading which you should be aware of When using operator overloading the operator should perform only the most obvious function Otherwise it will lead to more confusion If you are overloading + operator for distance class it should add two distance objects and should not do something else

Several operators such as dot operator scope resolution () operator conditional operator () etc cannot be overloaded Therefore operator overloading should be used for a class where it is required to perform obvious functions with the default operators and there can be no other meaning for the same

Overloading Unary Operators

We have seen that unary operators are those operators which work on one operator Some of the unary operators are ++ -- and minus (-) Operator overloading works similar to any member function of a class But it is not invoked using dot operator Just like member function the operator has a return value and takes arguments It is invoked by the operand which uses it In case of overloading of unary operators the calling operand can be either left or right of the operator like in case of increment and decrement operators While defining the operator functionality for the class the keyword operator is used

Example

unarycpp include ltiostreamhgtclass counter

unsigned int countpubliccounter()count=0int getcount()

7

return count

void operator ++() count++

void main()

counter c1c1++++c1coutltltc1getcount()

In the above example the operator ++ is defined to return void and take no arguments All unary operators do not take no arguments as it operates on only one operand and that operand itself invokes the operator Therefore the operand is sent by default

Book ID B0715

5 Write C++ program which demonstrate the difference between static and dynamic binding

Dynamic Binding You can specify that the compiler match a function call with the correct function definition at run time this is called dynamic binding You declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific function

The following examples demonstrate the differences between static and dynamic binding

The first example demonstrates static binding

include ltiostreamgtusing namespace stdstruct A

void f()

cout ltlt Class A ltlt endl

struct B A

void f()

cout ltlt Class B ltlt endl

8

void g(Aamp arg)

argf()

int main()

B xg(x)

The following is the output of the above example

Class A

When function g() is called function Af() is called although the argument refers to an object of type B At compile time the compiler knows only that the argument of function g() will be a reference to an object derived from A it cannot determine whether the argument will be a reference to an object of type A or type B However this can be determined at run time

The following example is the same as the previous example except that Af() is declared with the virtual keyword

include ltiostreamgtusing namespace stdstruct A

virtual void f()

cout ltlt Class A ltlt endl

struct B A

void f()

cout ltlt Class B ltlt endl

void g(Aamp arg )

argf()

int main()

B xg(x)

9

The following is the output of the above example

Class B

The virtual keyword indicates to the compiler that it should choose the appropriate definition of f() not by the type of reference but by the type of object that the reference refers to

6 Describe the Friend functions and friend classes with programming examples

Friend Functions Friend Functions are the functions which can have access to the private and protected members of the class They can be declared anywhere within a class declaration but it is common practice to list friends at the beginning of the class The public and protected keywords do not apply to friend functions as the class has no control over the scope of friends If we want to declare an external function as friend of a class thus allowing this function to have access to the private and protected members of this class we do it by declaring a prototype of this external function within the class and preceding it with the keyword friend

friend functionsinclude ltiostreamgtusing namespace stdclass CRectangle

int width heightpublicvoid set_values (int int)int area () return (width height)friend CRectangle duplicate (CRectangle)

void CRectangleset_values (int a int b)

width = aheight = b

CRectangle duplicate (CRectangle rectparam)

CRectangle rectresrectreswidth = rectparamwidth2rectresheight = rectparamheight2return (rectres)

int main () CRectangle rect rectbrectset_values (23)rectb = duplicate (rect)cout ltlt rectbarea()

return 0

10

The duplicate function is a friend of CRectangle From within that function we have been able to access the members width and height of different objects of type CRectangle which are private members Notice that neither in the declaration of duplicate() nor in its later use in main() have we considered duplicate a member of class CRectangle It isnt It simply has access to its private and protected members without being a member The friend functions can serve for example to conduct operations between two different classes Generally the use of friend functions is out of an object-oriented programming methodology so whenever possible it is better to use members of the same class to perform operations with them Such as in the previous example it would have been shorter to integrate duplicate() within the class CRectangle

Friend Classes A class can declare a member function of another class as a friend or declare another class as a friend class Friend classes are used in cases where one class is tightly coupled to another class For example suppose we have a class CPoint that represents a coordinate and a class CPointCollection that holds a list of points Since the collection class may need to manipulate point objects we could declare CPointCollection as a friend of the CPoint class

Forward declaration of friend classclass CPointCollection Point classclass CPoint friend CPointCollectionprivatedouble m_xdouble m_ypublicCPoint(const double x const double y) m_x(x)m_y(y) ~CPoint(void)

Since the collection class is a friend of CPoint it has access to the internal data of any point object This is useful when individual elements of the collection need to be manipulated For example a set method of the CPointCollection class could set all CPoint elements to a particular value (vector is a STL container which is discussed in detail in Chapter 8)

class CPointCollection privatevectorltCPointgt m_vecPointspublicCPointCollection(const int nSize) m_vecPoints(nSize) ~CPointCollection(void)void set(const double x const double y)

The set member can iterate over the collection and reset each point

11

void CPointCollectionset(const double x const double y) Get the number of elements in the collectionconst int nElements = m_vecPointssize() Set each elementfor(int i=0 iltnElements i++) m_vecPoints[i]m_x = xm_vecPoints[i]m_y = y

One thing important about friend classes is that friendship is not mutual although CPointCollection can access CPoint the converse is not true Friendship is also not something that is passed down a class hierarchy Derived classes of CPointCollection will not be able to access CPoint The principle behind this is that friendship is not implicitly granted each class must explicitly choose its friends

7 Illustrate with suitable examples various file handling methods in C++

File handling is an important part of all programs Most of the applications will have their own features to save some data to the local disk and read data from the disk again C++ File IO classes simplify such file readwrite operations for the programmer by providing easier to use classes

C++ File IO Classes and Functions

There are 3 File IO classes in C++ which are used for File ReadWrite operations They areifstream - Can be used for File readinput operationsofstream - Can be used for File writeoutput operationsfstream - Can be used for both readwrite c++ file IO operations

The most important methods which will be used for any file operations are

fstreamopen method - to open the filefstreamOperator gtgt and fstreamOperator ltlt - For reading from or writing to the filefstreamclose - Flushes all buffer and close the fileReading a text file using fstream class

There are several ways of reading the text from a file But all of them have a common approach as follows

Open the fileRead the dataClose the file

This sample code snippet explains how to use the c++ file io stream operators to read data from a file In all cases the header file fstreamh must be included

includeltfstreamhgt int main() char str[2000]

12

fstream file_op(ctest_filetxtiosin) while(file_op gtgt str) cout ltlt str file_opclose()

return 0

The class fstream which is used above is the one which is commonly used for c++ file io manipulations The constructor of fstream takes 2 parameters One is the file path and the second is the File Open mode There are several open modes each of them with a different purpose

But if we look at the output closely there is a draw back in using this stream operator read The output misses the white spaces and the end of line characters In order not to miss these characters we can either use fstreamget() or fstreamgetline() methods Here is the example for using fstream getline method

include ltfstreamhgt int main() char str[2000] fstream file_op(ctest_filetxtiosin) while(file_opeof()) file_opgetline(str2000) cout ltltstr file_opclose() cout ltltendl

return 0

Writing to a text file using fstream class

Writing to a text file can also be achieved with the stream operators This also follows the same order of operations though with a slight difference 1 open a file - in write mode 2 Write to a file 3 close the file Look at the following sample code to see the difference

include ltfstreamhgt int main() fstream file_op(cCoderSource_filetxtiosout)

file_opltltTest Write to file file_opclose()

13

return 0

To modify the data or to seek to a different position inside the file the c++ file io class fstream provides member functionslike seekg() etc These functions can be used to relocate the record insert position to the desired locations

After all the C++ File IO operations we do a fstreamclose() to close the file pointer This is not mandatory Even if this function is not called by the application the destructor of the fstream class will close the file when the object goes out of scope

8 What is template specialization Describe a scenario in which template class partial specialization is considered appropriate

Class Template Specialization In some cases it is possible to override the template-generated code by providing special definitions for specific types This is called template specialization

The following example defines a template class specialization for template class stream

include ltiostreamgtusing namespace stdtemplate ltclass Tgtclass stream publicvoid f() cout ltlt streamltTgtf()ltlt endl template ltgtclass streamltchargt publicvoid f() cout ltlt streamltchargtf()ltlt endl int main()

streamltintgt sistreamltchargt scsif()scf()return 0

Output

streamltTgtf()streamltchargtf()

In the above example streamltchargt is used as the definition of streams of chars other streams will be handled by the template class generated from the class template

14

Template Class Partial Specialization You may want to generate a specialization of the class for just one parameter for example

base template classtemplatelttypename T1 typename T2gtclass X partial specializationtemplatelttypename T1gtclass XltT1 intgt int main() generates an instantiation from the base templateXltchar chargt xcc generates an instantiation from the partial specializationXltchar intgt xii return 0

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list

15

December 2010

Master of Computer Application (MCA) ndash Semester 2

MC0066 ndash OOPS using C++ ndash 4 Credits

(Book ID B0681 amp B0715)

Assignment Set ndash 2 (40 Marks)

Answer all Questions Each Question carries FIVE Marks

Book ID B0681

1 Distinguished between procedural language and OOP language And Explain the key features of OOP

1 Procedural language focuses on organizing program statements into procedures or functions Larger programs were either broken into functions or modules whereas in Object Oriented Programming bundles both data and functions into one unitknown as object

2 One of the main problems with Procedural approach for programming was data being completely forgotten The emphasis was on the action and the data was only used in the entire process Whereas in Object Oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data

3 The procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled whereas in OOP language provides this flexibility through the concept of class

4 Another limitation of the procedural languages is that the program model is not closer to real world objects For example if you want to develop a gaming application of car race what data would you use and what functions you would require is difficult questions to answer in a procedural approach In the object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality In the car game example we would create several objects such as player car traffic signal and so on

Key Features of OOP

Encapsulation Data and functions are said to be encapsulated in an single entity as object

Data Hiding The data is said to be hidden thus not allowing accidental modification

Inheritance Inheritance is one of the most powerful feature of Object Oriented Programming Languages that allows you to derive a class from an existing class and inherit all the characteristics and behaviour of the parent class This feature allows easy modification of existing code and also reuse code The ability to reuse components of a program is an important feature for any programming language

16

Polymorphism and Overloading Operator overloading feature allows users to define how basic operators work with objects The operator + will be adding two numbers when used with integer variables However when used with user defined string class + operator may concatenate two strings Similarly same functions with same function name can perform different actions depending upon which object calls the function This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms) Operator overloading is a kind of polymorphism

2 Discuss the constructors and Destructors with suitable example

Constructors Constructors are member functions of a class which have same name as the class name Constructors are called automatically whenever an object of the class is created This feature makes it very useful to initialize the classdata members whenever a new object is created It also can perform any other function that needs to be performed for all the objects of the class without explicitly specifying it

Destructors Destructors on the other hand are also member functions with the same name as class but are prefixed with tilde (~) sign to differentiate it from theconstructor They are invoked automatically whenever the objectrsquos life expires or it is destroyed It can be used to return the memory back to the operating system if the memory was dynamically allocated

The following program implements the constructor and destructors for aclass

constdestcpp includeltiostreamhgtclass sample

privateint datapublicsample()

data=0 coutltltrdquoConstructor invokedrdquoltltendl

~sample()

coutltltrdquoDestructor invokedrdquovoid display()

coutltltrdquoData=rdquoltltdataltltendlvoid main()

sample obj1 objdisplay()

17

If you run the above program you will get the output as follows

Constructor invokedData=0Destructor invoked

When object of sample class object is created automatically the constructor is invoked and data is initialized to zero When the program ends the object is destroyed which invokes the destructor Please note that both the constructor and destructor is declared as public and they have no return value However constructors can have arguments and can be overloaded so that different constructors can be called depending upon the arguments that is passed Destructors on the other hand cannot be overloaded and cannot have any arguments

3 Describe the theory behind Virtual Functions and Polymorphism along with suitable programming examples for each

Virtual Functions Virtual means existing in effect but not in reality Virtual functions are primarily used in inheritance Let us suppose you have a class base as shown in the following program and two classes derv1 and derv2 are publicly derived from class base You would like to create a pointer that points to any of the derived class objects If you create a pointer of derv1 then it can point to derv1 object only Compiler will complain if you assign any other object is assigned to the pointer The solution is to create a pointer to Base class

Even though the address of derived classes is assigned to the pointer the compiler executes the base class function However if the base class function is made virtual we get the desired result In the following program we have made the base class function show() as virtual function by prefixing with the keyword virtual

virtualcpp include ltiostreamhgtclass base

publicvirtual void show() virtual functioncoutltltldquobaserdquoltltendl

class derv1public base

publicvoid show()

coutltltldquoderv1rdquoltltendl

class derv2 public base

publicvoid show()

18

coutltltldquoderv2rdquoltltendl

void main()derv1 dv1derv2 dv2base ptrptr=ampdv1ptr-gtshow()ptr=ampdv2ptr-gtshow()

By declaring the base class function as virtual we now get the output as

derv1derv2

In the above program depending on the contents in the pointer the compiler decides which class function to call during runtime This is known as late binding or dynamic binding

Polymorphism Polymorphism means same content but different forms In C++ polymorphism enables the same program code calling different functions of different classes For Example Letrsquos us suppose that we want to create a class shape and derive classes such as rectangle circle triangle etc Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen You would like to write a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime

Shape ptrarr[100]for (int j=0jltnj++)Ptrarr[j]-gtdraw()

This is an very desirable capability that completely different functions are executed by same function call If the ptrarr is pointing to a rectangle a rectangle is drawn If it is point int to circle circle is drawn

This is exactly what is polymorphism However to implement this approach several conditions should be met Firstly all the classes rectangle circle triangle should be derived from a single class (Here it is shape class) Secondly the draw() function must be declared as virtual in the base class (in the shape class)Operator overloading is a type of polymorphism which allows the same operators to behave differently with different datatypesoperands

4 Discuss the types of Inheritance with suitable example for each

Inheritance is a very powerful featu re of object oriented programming It allows reuse of code without modifying the original code It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort

19

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 6: (MCA) – Semester II MC0066 – OOPS using C++

matrixTwo[0][1] = -1 matrixTwo[0][2] = 1

float result result = (float)malloc(rows1 sizeof(result))

for(int i = 0 i lt rows1 i++)

result[i] = (float)malloc(columns2 sizeof(float))

matrixMultiply(31matrixOne13matrixTwo result)

return 0

void matrixMultiply(int rows1 int cols1 float mat1 int rows2 int cols2 float mat2 float result)

if( cols1 = rows2 )

cout ltlt Cant Multiply These Matricies

else

float tempResult

for (int i=0iltrows1i++)

for(int j=0jltcols2j++)

tempResult = 0

for(int k=0kltrows2k++) tempResult += mat1[i][k]mat2[k][j]

result[i][j] = tempResult

6

4 What do you mean by operator overloading Illustrate with suitable example for overloading Unary operators

Operator overloading Operator overloading is an interesting feature of C++ that allows programmers to specify how various arithmetic relational and many other operators work with user defined data-types or classes To perform addition of two distance objects we used a call d3add(d1d2) Instead of such statements it would be more clear if we could use statements like d3=d1+d2 This is possible only if we inform compiler about how + operator works with distance class

It helps to use the default operators with the user defined objects for making the code simpler However there are several problems with operator overloading which you should be aware of When using operator overloading the operator should perform only the most obvious function Otherwise it will lead to more confusion If you are overloading + operator for distance class it should add two distance objects and should not do something else

Several operators such as dot operator scope resolution () operator conditional operator () etc cannot be overloaded Therefore operator overloading should be used for a class where it is required to perform obvious functions with the default operators and there can be no other meaning for the same

Overloading Unary Operators

We have seen that unary operators are those operators which work on one operator Some of the unary operators are ++ -- and minus (-) Operator overloading works similar to any member function of a class But it is not invoked using dot operator Just like member function the operator has a return value and takes arguments It is invoked by the operand which uses it In case of overloading of unary operators the calling operand can be either left or right of the operator like in case of increment and decrement operators While defining the operator functionality for the class the keyword operator is used

Example

unarycpp include ltiostreamhgtclass counter

unsigned int countpubliccounter()count=0int getcount()

7

return count

void operator ++() count++

void main()

counter c1c1++++c1coutltltc1getcount()

In the above example the operator ++ is defined to return void and take no arguments All unary operators do not take no arguments as it operates on only one operand and that operand itself invokes the operator Therefore the operand is sent by default

Book ID B0715

5 Write C++ program which demonstrate the difference between static and dynamic binding

Dynamic Binding You can specify that the compiler match a function call with the correct function definition at run time this is called dynamic binding You declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific function

The following examples demonstrate the differences between static and dynamic binding

The first example demonstrates static binding

include ltiostreamgtusing namespace stdstruct A

void f()

cout ltlt Class A ltlt endl

struct B A

void f()

cout ltlt Class B ltlt endl

8

void g(Aamp arg)

argf()

int main()

B xg(x)

The following is the output of the above example

Class A

When function g() is called function Af() is called although the argument refers to an object of type B At compile time the compiler knows only that the argument of function g() will be a reference to an object derived from A it cannot determine whether the argument will be a reference to an object of type A or type B However this can be determined at run time

The following example is the same as the previous example except that Af() is declared with the virtual keyword

include ltiostreamgtusing namespace stdstruct A

virtual void f()

cout ltlt Class A ltlt endl

struct B A

void f()

cout ltlt Class B ltlt endl

void g(Aamp arg )

argf()

int main()

B xg(x)

9

The following is the output of the above example

Class B

The virtual keyword indicates to the compiler that it should choose the appropriate definition of f() not by the type of reference but by the type of object that the reference refers to

6 Describe the Friend functions and friend classes with programming examples

Friend Functions Friend Functions are the functions which can have access to the private and protected members of the class They can be declared anywhere within a class declaration but it is common practice to list friends at the beginning of the class The public and protected keywords do not apply to friend functions as the class has no control over the scope of friends If we want to declare an external function as friend of a class thus allowing this function to have access to the private and protected members of this class we do it by declaring a prototype of this external function within the class and preceding it with the keyword friend

friend functionsinclude ltiostreamgtusing namespace stdclass CRectangle

int width heightpublicvoid set_values (int int)int area () return (width height)friend CRectangle duplicate (CRectangle)

void CRectangleset_values (int a int b)

width = aheight = b

CRectangle duplicate (CRectangle rectparam)

CRectangle rectresrectreswidth = rectparamwidth2rectresheight = rectparamheight2return (rectres)

int main () CRectangle rect rectbrectset_values (23)rectb = duplicate (rect)cout ltlt rectbarea()

return 0

10

The duplicate function is a friend of CRectangle From within that function we have been able to access the members width and height of different objects of type CRectangle which are private members Notice that neither in the declaration of duplicate() nor in its later use in main() have we considered duplicate a member of class CRectangle It isnt It simply has access to its private and protected members without being a member The friend functions can serve for example to conduct operations between two different classes Generally the use of friend functions is out of an object-oriented programming methodology so whenever possible it is better to use members of the same class to perform operations with them Such as in the previous example it would have been shorter to integrate duplicate() within the class CRectangle

Friend Classes A class can declare a member function of another class as a friend or declare another class as a friend class Friend classes are used in cases where one class is tightly coupled to another class For example suppose we have a class CPoint that represents a coordinate and a class CPointCollection that holds a list of points Since the collection class may need to manipulate point objects we could declare CPointCollection as a friend of the CPoint class

Forward declaration of friend classclass CPointCollection Point classclass CPoint friend CPointCollectionprivatedouble m_xdouble m_ypublicCPoint(const double x const double y) m_x(x)m_y(y) ~CPoint(void)

Since the collection class is a friend of CPoint it has access to the internal data of any point object This is useful when individual elements of the collection need to be manipulated For example a set method of the CPointCollection class could set all CPoint elements to a particular value (vector is a STL container which is discussed in detail in Chapter 8)

class CPointCollection privatevectorltCPointgt m_vecPointspublicCPointCollection(const int nSize) m_vecPoints(nSize) ~CPointCollection(void)void set(const double x const double y)

The set member can iterate over the collection and reset each point

11

void CPointCollectionset(const double x const double y) Get the number of elements in the collectionconst int nElements = m_vecPointssize() Set each elementfor(int i=0 iltnElements i++) m_vecPoints[i]m_x = xm_vecPoints[i]m_y = y

One thing important about friend classes is that friendship is not mutual although CPointCollection can access CPoint the converse is not true Friendship is also not something that is passed down a class hierarchy Derived classes of CPointCollection will not be able to access CPoint The principle behind this is that friendship is not implicitly granted each class must explicitly choose its friends

7 Illustrate with suitable examples various file handling methods in C++

File handling is an important part of all programs Most of the applications will have their own features to save some data to the local disk and read data from the disk again C++ File IO classes simplify such file readwrite operations for the programmer by providing easier to use classes

C++ File IO Classes and Functions

There are 3 File IO classes in C++ which are used for File ReadWrite operations They areifstream - Can be used for File readinput operationsofstream - Can be used for File writeoutput operationsfstream - Can be used for both readwrite c++ file IO operations

The most important methods which will be used for any file operations are

fstreamopen method - to open the filefstreamOperator gtgt and fstreamOperator ltlt - For reading from or writing to the filefstreamclose - Flushes all buffer and close the fileReading a text file using fstream class

There are several ways of reading the text from a file But all of them have a common approach as follows

Open the fileRead the dataClose the file

This sample code snippet explains how to use the c++ file io stream operators to read data from a file In all cases the header file fstreamh must be included

includeltfstreamhgt int main() char str[2000]

12

fstream file_op(ctest_filetxtiosin) while(file_op gtgt str) cout ltlt str file_opclose()

return 0

The class fstream which is used above is the one which is commonly used for c++ file io manipulations The constructor of fstream takes 2 parameters One is the file path and the second is the File Open mode There are several open modes each of them with a different purpose

But if we look at the output closely there is a draw back in using this stream operator read The output misses the white spaces and the end of line characters In order not to miss these characters we can either use fstreamget() or fstreamgetline() methods Here is the example for using fstream getline method

include ltfstreamhgt int main() char str[2000] fstream file_op(ctest_filetxtiosin) while(file_opeof()) file_opgetline(str2000) cout ltltstr file_opclose() cout ltltendl

return 0

Writing to a text file using fstream class

Writing to a text file can also be achieved with the stream operators This also follows the same order of operations though with a slight difference 1 open a file - in write mode 2 Write to a file 3 close the file Look at the following sample code to see the difference

include ltfstreamhgt int main() fstream file_op(cCoderSource_filetxtiosout)

file_opltltTest Write to file file_opclose()

13

return 0

To modify the data or to seek to a different position inside the file the c++ file io class fstream provides member functionslike seekg() etc These functions can be used to relocate the record insert position to the desired locations

After all the C++ File IO operations we do a fstreamclose() to close the file pointer This is not mandatory Even if this function is not called by the application the destructor of the fstream class will close the file when the object goes out of scope

8 What is template specialization Describe a scenario in which template class partial specialization is considered appropriate

Class Template Specialization In some cases it is possible to override the template-generated code by providing special definitions for specific types This is called template specialization

The following example defines a template class specialization for template class stream

include ltiostreamgtusing namespace stdtemplate ltclass Tgtclass stream publicvoid f() cout ltlt streamltTgtf()ltlt endl template ltgtclass streamltchargt publicvoid f() cout ltlt streamltchargtf()ltlt endl int main()

streamltintgt sistreamltchargt scsif()scf()return 0

Output

streamltTgtf()streamltchargtf()

In the above example streamltchargt is used as the definition of streams of chars other streams will be handled by the template class generated from the class template

14

Template Class Partial Specialization You may want to generate a specialization of the class for just one parameter for example

base template classtemplatelttypename T1 typename T2gtclass X partial specializationtemplatelttypename T1gtclass XltT1 intgt int main() generates an instantiation from the base templateXltchar chargt xcc generates an instantiation from the partial specializationXltchar intgt xii return 0

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list

15

December 2010

Master of Computer Application (MCA) ndash Semester 2

MC0066 ndash OOPS using C++ ndash 4 Credits

(Book ID B0681 amp B0715)

Assignment Set ndash 2 (40 Marks)

Answer all Questions Each Question carries FIVE Marks

Book ID B0681

1 Distinguished between procedural language and OOP language And Explain the key features of OOP

1 Procedural language focuses on organizing program statements into procedures or functions Larger programs were either broken into functions or modules whereas in Object Oriented Programming bundles both data and functions into one unitknown as object

2 One of the main problems with Procedural approach for programming was data being completely forgotten The emphasis was on the action and the data was only used in the entire process Whereas in Object Oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data

3 The procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled whereas in OOP language provides this flexibility through the concept of class

4 Another limitation of the procedural languages is that the program model is not closer to real world objects For example if you want to develop a gaming application of car race what data would you use and what functions you would require is difficult questions to answer in a procedural approach In the object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality In the car game example we would create several objects such as player car traffic signal and so on

Key Features of OOP

Encapsulation Data and functions are said to be encapsulated in an single entity as object

Data Hiding The data is said to be hidden thus not allowing accidental modification

Inheritance Inheritance is one of the most powerful feature of Object Oriented Programming Languages that allows you to derive a class from an existing class and inherit all the characteristics and behaviour of the parent class This feature allows easy modification of existing code and also reuse code The ability to reuse components of a program is an important feature for any programming language

16

Polymorphism and Overloading Operator overloading feature allows users to define how basic operators work with objects The operator + will be adding two numbers when used with integer variables However when used with user defined string class + operator may concatenate two strings Similarly same functions with same function name can perform different actions depending upon which object calls the function This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms) Operator overloading is a kind of polymorphism

2 Discuss the constructors and Destructors with suitable example

Constructors Constructors are member functions of a class which have same name as the class name Constructors are called automatically whenever an object of the class is created This feature makes it very useful to initialize the classdata members whenever a new object is created It also can perform any other function that needs to be performed for all the objects of the class without explicitly specifying it

Destructors Destructors on the other hand are also member functions with the same name as class but are prefixed with tilde (~) sign to differentiate it from theconstructor They are invoked automatically whenever the objectrsquos life expires or it is destroyed It can be used to return the memory back to the operating system if the memory was dynamically allocated

The following program implements the constructor and destructors for aclass

constdestcpp includeltiostreamhgtclass sample

privateint datapublicsample()

data=0 coutltltrdquoConstructor invokedrdquoltltendl

~sample()

coutltltrdquoDestructor invokedrdquovoid display()

coutltltrdquoData=rdquoltltdataltltendlvoid main()

sample obj1 objdisplay()

17

If you run the above program you will get the output as follows

Constructor invokedData=0Destructor invoked

When object of sample class object is created automatically the constructor is invoked and data is initialized to zero When the program ends the object is destroyed which invokes the destructor Please note that both the constructor and destructor is declared as public and they have no return value However constructors can have arguments and can be overloaded so that different constructors can be called depending upon the arguments that is passed Destructors on the other hand cannot be overloaded and cannot have any arguments

3 Describe the theory behind Virtual Functions and Polymorphism along with suitable programming examples for each

Virtual Functions Virtual means existing in effect but not in reality Virtual functions are primarily used in inheritance Let us suppose you have a class base as shown in the following program and two classes derv1 and derv2 are publicly derived from class base You would like to create a pointer that points to any of the derived class objects If you create a pointer of derv1 then it can point to derv1 object only Compiler will complain if you assign any other object is assigned to the pointer The solution is to create a pointer to Base class

Even though the address of derived classes is assigned to the pointer the compiler executes the base class function However if the base class function is made virtual we get the desired result In the following program we have made the base class function show() as virtual function by prefixing with the keyword virtual

virtualcpp include ltiostreamhgtclass base

publicvirtual void show() virtual functioncoutltltldquobaserdquoltltendl

class derv1public base

publicvoid show()

coutltltldquoderv1rdquoltltendl

class derv2 public base

publicvoid show()

18

coutltltldquoderv2rdquoltltendl

void main()derv1 dv1derv2 dv2base ptrptr=ampdv1ptr-gtshow()ptr=ampdv2ptr-gtshow()

By declaring the base class function as virtual we now get the output as

derv1derv2

In the above program depending on the contents in the pointer the compiler decides which class function to call during runtime This is known as late binding or dynamic binding

Polymorphism Polymorphism means same content but different forms In C++ polymorphism enables the same program code calling different functions of different classes For Example Letrsquos us suppose that we want to create a class shape and derive classes such as rectangle circle triangle etc Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen You would like to write a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime

Shape ptrarr[100]for (int j=0jltnj++)Ptrarr[j]-gtdraw()

This is an very desirable capability that completely different functions are executed by same function call If the ptrarr is pointing to a rectangle a rectangle is drawn If it is point int to circle circle is drawn

This is exactly what is polymorphism However to implement this approach several conditions should be met Firstly all the classes rectangle circle triangle should be derived from a single class (Here it is shape class) Secondly the draw() function must be declared as virtual in the base class (in the shape class)Operator overloading is a type of polymorphism which allows the same operators to behave differently with different datatypesoperands

4 Discuss the types of Inheritance with suitable example for each

Inheritance is a very powerful featu re of object oriented programming It allows reuse of code without modifying the original code It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort

19

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 7: (MCA) – Semester II MC0066 – OOPS using C++

4 What do you mean by operator overloading Illustrate with suitable example for overloading Unary operators

Operator overloading Operator overloading is an interesting feature of C++ that allows programmers to specify how various arithmetic relational and many other operators work with user defined data-types or classes To perform addition of two distance objects we used a call d3add(d1d2) Instead of such statements it would be more clear if we could use statements like d3=d1+d2 This is possible only if we inform compiler about how + operator works with distance class

It helps to use the default operators with the user defined objects for making the code simpler However there are several problems with operator overloading which you should be aware of When using operator overloading the operator should perform only the most obvious function Otherwise it will lead to more confusion If you are overloading + operator for distance class it should add two distance objects and should not do something else

Several operators such as dot operator scope resolution () operator conditional operator () etc cannot be overloaded Therefore operator overloading should be used for a class where it is required to perform obvious functions with the default operators and there can be no other meaning for the same

Overloading Unary Operators

We have seen that unary operators are those operators which work on one operator Some of the unary operators are ++ -- and minus (-) Operator overloading works similar to any member function of a class But it is not invoked using dot operator Just like member function the operator has a return value and takes arguments It is invoked by the operand which uses it In case of overloading of unary operators the calling operand can be either left or right of the operator like in case of increment and decrement operators While defining the operator functionality for the class the keyword operator is used

Example

unarycpp include ltiostreamhgtclass counter

unsigned int countpubliccounter()count=0int getcount()

7

return count

void operator ++() count++

void main()

counter c1c1++++c1coutltltc1getcount()

In the above example the operator ++ is defined to return void and take no arguments All unary operators do not take no arguments as it operates on only one operand and that operand itself invokes the operator Therefore the operand is sent by default

Book ID B0715

5 Write C++ program which demonstrate the difference between static and dynamic binding

Dynamic Binding You can specify that the compiler match a function call with the correct function definition at run time this is called dynamic binding You declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific function

The following examples demonstrate the differences between static and dynamic binding

The first example demonstrates static binding

include ltiostreamgtusing namespace stdstruct A

void f()

cout ltlt Class A ltlt endl

struct B A

void f()

cout ltlt Class B ltlt endl

8

void g(Aamp arg)

argf()

int main()

B xg(x)

The following is the output of the above example

Class A

When function g() is called function Af() is called although the argument refers to an object of type B At compile time the compiler knows only that the argument of function g() will be a reference to an object derived from A it cannot determine whether the argument will be a reference to an object of type A or type B However this can be determined at run time

The following example is the same as the previous example except that Af() is declared with the virtual keyword

include ltiostreamgtusing namespace stdstruct A

virtual void f()

cout ltlt Class A ltlt endl

struct B A

void f()

cout ltlt Class B ltlt endl

void g(Aamp arg )

argf()

int main()

B xg(x)

9

The following is the output of the above example

Class B

The virtual keyword indicates to the compiler that it should choose the appropriate definition of f() not by the type of reference but by the type of object that the reference refers to

6 Describe the Friend functions and friend classes with programming examples

Friend Functions Friend Functions are the functions which can have access to the private and protected members of the class They can be declared anywhere within a class declaration but it is common practice to list friends at the beginning of the class The public and protected keywords do not apply to friend functions as the class has no control over the scope of friends If we want to declare an external function as friend of a class thus allowing this function to have access to the private and protected members of this class we do it by declaring a prototype of this external function within the class and preceding it with the keyword friend

friend functionsinclude ltiostreamgtusing namespace stdclass CRectangle

int width heightpublicvoid set_values (int int)int area () return (width height)friend CRectangle duplicate (CRectangle)

void CRectangleset_values (int a int b)

width = aheight = b

CRectangle duplicate (CRectangle rectparam)

CRectangle rectresrectreswidth = rectparamwidth2rectresheight = rectparamheight2return (rectres)

int main () CRectangle rect rectbrectset_values (23)rectb = duplicate (rect)cout ltlt rectbarea()

return 0

10

The duplicate function is a friend of CRectangle From within that function we have been able to access the members width and height of different objects of type CRectangle which are private members Notice that neither in the declaration of duplicate() nor in its later use in main() have we considered duplicate a member of class CRectangle It isnt It simply has access to its private and protected members without being a member The friend functions can serve for example to conduct operations between two different classes Generally the use of friend functions is out of an object-oriented programming methodology so whenever possible it is better to use members of the same class to perform operations with them Such as in the previous example it would have been shorter to integrate duplicate() within the class CRectangle

Friend Classes A class can declare a member function of another class as a friend or declare another class as a friend class Friend classes are used in cases where one class is tightly coupled to another class For example suppose we have a class CPoint that represents a coordinate and a class CPointCollection that holds a list of points Since the collection class may need to manipulate point objects we could declare CPointCollection as a friend of the CPoint class

Forward declaration of friend classclass CPointCollection Point classclass CPoint friend CPointCollectionprivatedouble m_xdouble m_ypublicCPoint(const double x const double y) m_x(x)m_y(y) ~CPoint(void)

Since the collection class is a friend of CPoint it has access to the internal data of any point object This is useful when individual elements of the collection need to be manipulated For example a set method of the CPointCollection class could set all CPoint elements to a particular value (vector is a STL container which is discussed in detail in Chapter 8)

class CPointCollection privatevectorltCPointgt m_vecPointspublicCPointCollection(const int nSize) m_vecPoints(nSize) ~CPointCollection(void)void set(const double x const double y)

The set member can iterate over the collection and reset each point

11

void CPointCollectionset(const double x const double y) Get the number of elements in the collectionconst int nElements = m_vecPointssize() Set each elementfor(int i=0 iltnElements i++) m_vecPoints[i]m_x = xm_vecPoints[i]m_y = y

One thing important about friend classes is that friendship is not mutual although CPointCollection can access CPoint the converse is not true Friendship is also not something that is passed down a class hierarchy Derived classes of CPointCollection will not be able to access CPoint The principle behind this is that friendship is not implicitly granted each class must explicitly choose its friends

7 Illustrate with suitable examples various file handling methods in C++

File handling is an important part of all programs Most of the applications will have their own features to save some data to the local disk and read data from the disk again C++ File IO classes simplify such file readwrite operations for the programmer by providing easier to use classes

C++ File IO Classes and Functions

There are 3 File IO classes in C++ which are used for File ReadWrite operations They areifstream - Can be used for File readinput operationsofstream - Can be used for File writeoutput operationsfstream - Can be used for both readwrite c++ file IO operations

The most important methods which will be used for any file operations are

fstreamopen method - to open the filefstreamOperator gtgt and fstreamOperator ltlt - For reading from or writing to the filefstreamclose - Flushes all buffer and close the fileReading a text file using fstream class

There are several ways of reading the text from a file But all of them have a common approach as follows

Open the fileRead the dataClose the file

This sample code snippet explains how to use the c++ file io stream operators to read data from a file In all cases the header file fstreamh must be included

includeltfstreamhgt int main() char str[2000]

12

fstream file_op(ctest_filetxtiosin) while(file_op gtgt str) cout ltlt str file_opclose()

return 0

The class fstream which is used above is the one which is commonly used for c++ file io manipulations The constructor of fstream takes 2 parameters One is the file path and the second is the File Open mode There are several open modes each of them with a different purpose

But if we look at the output closely there is a draw back in using this stream operator read The output misses the white spaces and the end of line characters In order not to miss these characters we can either use fstreamget() or fstreamgetline() methods Here is the example for using fstream getline method

include ltfstreamhgt int main() char str[2000] fstream file_op(ctest_filetxtiosin) while(file_opeof()) file_opgetline(str2000) cout ltltstr file_opclose() cout ltltendl

return 0

Writing to a text file using fstream class

Writing to a text file can also be achieved with the stream operators This also follows the same order of operations though with a slight difference 1 open a file - in write mode 2 Write to a file 3 close the file Look at the following sample code to see the difference

include ltfstreamhgt int main() fstream file_op(cCoderSource_filetxtiosout)

file_opltltTest Write to file file_opclose()

13

return 0

To modify the data or to seek to a different position inside the file the c++ file io class fstream provides member functionslike seekg() etc These functions can be used to relocate the record insert position to the desired locations

After all the C++ File IO operations we do a fstreamclose() to close the file pointer This is not mandatory Even if this function is not called by the application the destructor of the fstream class will close the file when the object goes out of scope

8 What is template specialization Describe a scenario in which template class partial specialization is considered appropriate

Class Template Specialization In some cases it is possible to override the template-generated code by providing special definitions for specific types This is called template specialization

The following example defines a template class specialization for template class stream

include ltiostreamgtusing namespace stdtemplate ltclass Tgtclass stream publicvoid f() cout ltlt streamltTgtf()ltlt endl template ltgtclass streamltchargt publicvoid f() cout ltlt streamltchargtf()ltlt endl int main()

streamltintgt sistreamltchargt scsif()scf()return 0

Output

streamltTgtf()streamltchargtf()

In the above example streamltchargt is used as the definition of streams of chars other streams will be handled by the template class generated from the class template

14

Template Class Partial Specialization You may want to generate a specialization of the class for just one parameter for example

base template classtemplatelttypename T1 typename T2gtclass X partial specializationtemplatelttypename T1gtclass XltT1 intgt int main() generates an instantiation from the base templateXltchar chargt xcc generates an instantiation from the partial specializationXltchar intgt xii return 0

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list

15

December 2010

Master of Computer Application (MCA) ndash Semester 2

MC0066 ndash OOPS using C++ ndash 4 Credits

(Book ID B0681 amp B0715)

Assignment Set ndash 2 (40 Marks)

Answer all Questions Each Question carries FIVE Marks

Book ID B0681

1 Distinguished between procedural language and OOP language And Explain the key features of OOP

1 Procedural language focuses on organizing program statements into procedures or functions Larger programs were either broken into functions or modules whereas in Object Oriented Programming bundles both data and functions into one unitknown as object

2 One of the main problems with Procedural approach for programming was data being completely forgotten The emphasis was on the action and the data was only used in the entire process Whereas in Object Oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data

3 The procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled whereas in OOP language provides this flexibility through the concept of class

4 Another limitation of the procedural languages is that the program model is not closer to real world objects For example if you want to develop a gaming application of car race what data would you use and what functions you would require is difficult questions to answer in a procedural approach In the object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality In the car game example we would create several objects such as player car traffic signal and so on

Key Features of OOP

Encapsulation Data and functions are said to be encapsulated in an single entity as object

Data Hiding The data is said to be hidden thus not allowing accidental modification

Inheritance Inheritance is one of the most powerful feature of Object Oriented Programming Languages that allows you to derive a class from an existing class and inherit all the characteristics and behaviour of the parent class This feature allows easy modification of existing code and also reuse code The ability to reuse components of a program is an important feature for any programming language

16

Polymorphism and Overloading Operator overloading feature allows users to define how basic operators work with objects The operator + will be adding two numbers when used with integer variables However when used with user defined string class + operator may concatenate two strings Similarly same functions with same function name can perform different actions depending upon which object calls the function This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms) Operator overloading is a kind of polymorphism

2 Discuss the constructors and Destructors with suitable example

Constructors Constructors are member functions of a class which have same name as the class name Constructors are called automatically whenever an object of the class is created This feature makes it very useful to initialize the classdata members whenever a new object is created It also can perform any other function that needs to be performed for all the objects of the class without explicitly specifying it

Destructors Destructors on the other hand are also member functions with the same name as class but are prefixed with tilde (~) sign to differentiate it from theconstructor They are invoked automatically whenever the objectrsquos life expires or it is destroyed It can be used to return the memory back to the operating system if the memory was dynamically allocated

The following program implements the constructor and destructors for aclass

constdestcpp includeltiostreamhgtclass sample

privateint datapublicsample()

data=0 coutltltrdquoConstructor invokedrdquoltltendl

~sample()

coutltltrdquoDestructor invokedrdquovoid display()

coutltltrdquoData=rdquoltltdataltltendlvoid main()

sample obj1 objdisplay()

17

If you run the above program you will get the output as follows

Constructor invokedData=0Destructor invoked

When object of sample class object is created automatically the constructor is invoked and data is initialized to zero When the program ends the object is destroyed which invokes the destructor Please note that both the constructor and destructor is declared as public and they have no return value However constructors can have arguments and can be overloaded so that different constructors can be called depending upon the arguments that is passed Destructors on the other hand cannot be overloaded and cannot have any arguments

3 Describe the theory behind Virtual Functions and Polymorphism along with suitable programming examples for each

Virtual Functions Virtual means existing in effect but not in reality Virtual functions are primarily used in inheritance Let us suppose you have a class base as shown in the following program and two classes derv1 and derv2 are publicly derived from class base You would like to create a pointer that points to any of the derived class objects If you create a pointer of derv1 then it can point to derv1 object only Compiler will complain if you assign any other object is assigned to the pointer The solution is to create a pointer to Base class

Even though the address of derived classes is assigned to the pointer the compiler executes the base class function However if the base class function is made virtual we get the desired result In the following program we have made the base class function show() as virtual function by prefixing with the keyword virtual

virtualcpp include ltiostreamhgtclass base

publicvirtual void show() virtual functioncoutltltldquobaserdquoltltendl

class derv1public base

publicvoid show()

coutltltldquoderv1rdquoltltendl

class derv2 public base

publicvoid show()

18

coutltltldquoderv2rdquoltltendl

void main()derv1 dv1derv2 dv2base ptrptr=ampdv1ptr-gtshow()ptr=ampdv2ptr-gtshow()

By declaring the base class function as virtual we now get the output as

derv1derv2

In the above program depending on the contents in the pointer the compiler decides which class function to call during runtime This is known as late binding or dynamic binding

Polymorphism Polymorphism means same content but different forms In C++ polymorphism enables the same program code calling different functions of different classes For Example Letrsquos us suppose that we want to create a class shape and derive classes such as rectangle circle triangle etc Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen You would like to write a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime

Shape ptrarr[100]for (int j=0jltnj++)Ptrarr[j]-gtdraw()

This is an very desirable capability that completely different functions are executed by same function call If the ptrarr is pointing to a rectangle a rectangle is drawn If it is point int to circle circle is drawn

This is exactly what is polymorphism However to implement this approach several conditions should be met Firstly all the classes rectangle circle triangle should be derived from a single class (Here it is shape class) Secondly the draw() function must be declared as virtual in the base class (in the shape class)Operator overloading is a type of polymorphism which allows the same operators to behave differently with different datatypesoperands

4 Discuss the types of Inheritance with suitable example for each

Inheritance is a very powerful featu re of object oriented programming It allows reuse of code without modifying the original code It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort

19

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 8: (MCA) – Semester II MC0066 – OOPS using C++

return count

void operator ++() count++

void main()

counter c1c1++++c1coutltltc1getcount()

In the above example the operator ++ is defined to return void and take no arguments All unary operators do not take no arguments as it operates on only one operand and that operand itself invokes the operator Therefore the operand is sent by default

Book ID B0715

5 Write C++ program which demonstrate the difference between static and dynamic binding

Dynamic Binding You can specify that the compiler match a function call with the correct function definition at run time this is called dynamic binding You declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific function

The following examples demonstrate the differences between static and dynamic binding

The first example demonstrates static binding

include ltiostreamgtusing namespace stdstruct A

void f()

cout ltlt Class A ltlt endl

struct B A

void f()

cout ltlt Class B ltlt endl

8

void g(Aamp arg)

argf()

int main()

B xg(x)

The following is the output of the above example

Class A

When function g() is called function Af() is called although the argument refers to an object of type B At compile time the compiler knows only that the argument of function g() will be a reference to an object derived from A it cannot determine whether the argument will be a reference to an object of type A or type B However this can be determined at run time

The following example is the same as the previous example except that Af() is declared with the virtual keyword

include ltiostreamgtusing namespace stdstruct A

virtual void f()

cout ltlt Class A ltlt endl

struct B A

void f()

cout ltlt Class B ltlt endl

void g(Aamp arg )

argf()

int main()

B xg(x)

9

The following is the output of the above example

Class B

The virtual keyword indicates to the compiler that it should choose the appropriate definition of f() not by the type of reference but by the type of object that the reference refers to

6 Describe the Friend functions and friend classes with programming examples

Friend Functions Friend Functions are the functions which can have access to the private and protected members of the class They can be declared anywhere within a class declaration but it is common practice to list friends at the beginning of the class The public and protected keywords do not apply to friend functions as the class has no control over the scope of friends If we want to declare an external function as friend of a class thus allowing this function to have access to the private and protected members of this class we do it by declaring a prototype of this external function within the class and preceding it with the keyword friend

friend functionsinclude ltiostreamgtusing namespace stdclass CRectangle

int width heightpublicvoid set_values (int int)int area () return (width height)friend CRectangle duplicate (CRectangle)

void CRectangleset_values (int a int b)

width = aheight = b

CRectangle duplicate (CRectangle rectparam)

CRectangle rectresrectreswidth = rectparamwidth2rectresheight = rectparamheight2return (rectres)

int main () CRectangle rect rectbrectset_values (23)rectb = duplicate (rect)cout ltlt rectbarea()

return 0

10

The duplicate function is a friend of CRectangle From within that function we have been able to access the members width and height of different objects of type CRectangle which are private members Notice that neither in the declaration of duplicate() nor in its later use in main() have we considered duplicate a member of class CRectangle It isnt It simply has access to its private and protected members without being a member The friend functions can serve for example to conduct operations between two different classes Generally the use of friend functions is out of an object-oriented programming methodology so whenever possible it is better to use members of the same class to perform operations with them Such as in the previous example it would have been shorter to integrate duplicate() within the class CRectangle

Friend Classes A class can declare a member function of another class as a friend or declare another class as a friend class Friend classes are used in cases where one class is tightly coupled to another class For example suppose we have a class CPoint that represents a coordinate and a class CPointCollection that holds a list of points Since the collection class may need to manipulate point objects we could declare CPointCollection as a friend of the CPoint class

Forward declaration of friend classclass CPointCollection Point classclass CPoint friend CPointCollectionprivatedouble m_xdouble m_ypublicCPoint(const double x const double y) m_x(x)m_y(y) ~CPoint(void)

Since the collection class is a friend of CPoint it has access to the internal data of any point object This is useful when individual elements of the collection need to be manipulated For example a set method of the CPointCollection class could set all CPoint elements to a particular value (vector is a STL container which is discussed in detail in Chapter 8)

class CPointCollection privatevectorltCPointgt m_vecPointspublicCPointCollection(const int nSize) m_vecPoints(nSize) ~CPointCollection(void)void set(const double x const double y)

The set member can iterate over the collection and reset each point

11

void CPointCollectionset(const double x const double y) Get the number of elements in the collectionconst int nElements = m_vecPointssize() Set each elementfor(int i=0 iltnElements i++) m_vecPoints[i]m_x = xm_vecPoints[i]m_y = y

One thing important about friend classes is that friendship is not mutual although CPointCollection can access CPoint the converse is not true Friendship is also not something that is passed down a class hierarchy Derived classes of CPointCollection will not be able to access CPoint The principle behind this is that friendship is not implicitly granted each class must explicitly choose its friends

7 Illustrate with suitable examples various file handling methods in C++

File handling is an important part of all programs Most of the applications will have their own features to save some data to the local disk and read data from the disk again C++ File IO classes simplify such file readwrite operations for the programmer by providing easier to use classes

C++ File IO Classes and Functions

There are 3 File IO classes in C++ which are used for File ReadWrite operations They areifstream - Can be used for File readinput operationsofstream - Can be used for File writeoutput operationsfstream - Can be used for both readwrite c++ file IO operations

The most important methods which will be used for any file operations are

fstreamopen method - to open the filefstreamOperator gtgt and fstreamOperator ltlt - For reading from or writing to the filefstreamclose - Flushes all buffer and close the fileReading a text file using fstream class

There are several ways of reading the text from a file But all of them have a common approach as follows

Open the fileRead the dataClose the file

This sample code snippet explains how to use the c++ file io stream operators to read data from a file In all cases the header file fstreamh must be included

includeltfstreamhgt int main() char str[2000]

12

fstream file_op(ctest_filetxtiosin) while(file_op gtgt str) cout ltlt str file_opclose()

return 0

The class fstream which is used above is the one which is commonly used for c++ file io manipulations The constructor of fstream takes 2 parameters One is the file path and the second is the File Open mode There are several open modes each of them with a different purpose

But if we look at the output closely there is a draw back in using this stream operator read The output misses the white spaces and the end of line characters In order not to miss these characters we can either use fstreamget() or fstreamgetline() methods Here is the example for using fstream getline method

include ltfstreamhgt int main() char str[2000] fstream file_op(ctest_filetxtiosin) while(file_opeof()) file_opgetline(str2000) cout ltltstr file_opclose() cout ltltendl

return 0

Writing to a text file using fstream class

Writing to a text file can also be achieved with the stream operators This also follows the same order of operations though with a slight difference 1 open a file - in write mode 2 Write to a file 3 close the file Look at the following sample code to see the difference

include ltfstreamhgt int main() fstream file_op(cCoderSource_filetxtiosout)

file_opltltTest Write to file file_opclose()

13

return 0

To modify the data or to seek to a different position inside the file the c++ file io class fstream provides member functionslike seekg() etc These functions can be used to relocate the record insert position to the desired locations

After all the C++ File IO operations we do a fstreamclose() to close the file pointer This is not mandatory Even if this function is not called by the application the destructor of the fstream class will close the file when the object goes out of scope

8 What is template specialization Describe a scenario in which template class partial specialization is considered appropriate

Class Template Specialization In some cases it is possible to override the template-generated code by providing special definitions for specific types This is called template specialization

The following example defines a template class specialization for template class stream

include ltiostreamgtusing namespace stdtemplate ltclass Tgtclass stream publicvoid f() cout ltlt streamltTgtf()ltlt endl template ltgtclass streamltchargt publicvoid f() cout ltlt streamltchargtf()ltlt endl int main()

streamltintgt sistreamltchargt scsif()scf()return 0

Output

streamltTgtf()streamltchargtf()

In the above example streamltchargt is used as the definition of streams of chars other streams will be handled by the template class generated from the class template

14

Template Class Partial Specialization You may want to generate a specialization of the class for just one parameter for example

base template classtemplatelttypename T1 typename T2gtclass X partial specializationtemplatelttypename T1gtclass XltT1 intgt int main() generates an instantiation from the base templateXltchar chargt xcc generates an instantiation from the partial specializationXltchar intgt xii return 0

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list

15

December 2010

Master of Computer Application (MCA) ndash Semester 2

MC0066 ndash OOPS using C++ ndash 4 Credits

(Book ID B0681 amp B0715)

Assignment Set ndash 2 (40 Marks)

Answer all Questions Each Question carries FIVE Marks

Book ID B0681

1 Distinguished between procedural language and OOP language And Explain the key features of OOP

1 Procedural language focuses on organizing program statements into procedures or functions Larger programs were either broken into functions or modules whereas in Object Oriented Programming bundles both data and functions into one unitknown as object

2 One of the main problems with Procedural approach for programming was data being completely forgotten The emphasis was on the action and the data was only used in the entire process Whereas in Object Oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data

3 The procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled whereas in OOP language provides this flexibility through the concept of class

4 Another limitation of the procedural languages is that the program model is not closer to real world objects For example if you want to develop a gaming application of car race what data would you use and what functions you would require is difficult questions to answer in a procedural approach In the object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality In the car game example we would create several objects such as player car traffic signal and so on

Key Features of OOP

Encapsulation Data and functions are said to be encapsulated in an single entity as object

Data Hiding The data is said to be hidden thus not allowing accidental modification

Inheritance Inheritance is one of the most powerful feature of Object Oriented Programming Languages that allows you to derive a class from an existing class and inherit all the characteristics and behaviour of the parent class This feature allows easy modification of existing code and also reuse code The ability to reuse components of a program is an important feature for any programming language

16

Polymorphism and Overloading Operator overloading feature allows users to define how basic operators work with objects The operator + will be adding two numbers when used with integer variables However when used with user defined string class + operator may concatenate two strings Similarly same functions with same function name can perform different actions depending upon which object calls the function This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms) Operator overloading is a kind of polymorphism

2 Discuss the constructors and Destructors with suitable example

Constructors Constructors are member functions of a class which have same name as the class name Constructors are called automatically whenever an object of the class is created This feature makes it very useful to initialize the classdata members whenever a new object is created It also can perform any other function that needs to be performed for all the objects of the class without explicitly specifying it

Destructors Destructors on the other hand are also member functions with the same name as class but are prefixed with tilde (~) sign to differentiate it from theconstructor They are invoked automatically whenever the objectrsquos life expires or it is destroyed It can be used to return the memory back to the operating system if the memory was dynamically allocated

The following program implements the constructor and destructors for aclass

constdestcpp includeltiostreamhgtclass sample

privateint datapublicsample()

data=0 coutltltrdquoConstructor invokedrdquoltltendl

~sample()

coutltltrdquoDestructor invokedrdquovoid display()

coutltltrdquoData=rdquoltltdataltltendlvoid main()

sample obj1 objdisplay()

17

If you run the above program you will get the output as follows

Constructor invokedData=0Destructor invoked

When object of sample class object is created automatically the constructor is invoked and data is initialized to zero When the program ends the object is destroyed which invokes the destructor Please note that both the constructor and destructor is declared as public and they have no return value However constructors can have arguments and can be overloaded so that different constructors can be called depending upon the arguments that is passed Destructors on the other hand cannot be overloaded and cannot have any arguments

3 Describe the theory behind Virtual Functions and Polymorphism along with suitable programming examples for each

Virtual Functions Virtual means existing in effect but not in reality Virtual functions are primarily used in inheritance Let us suppose you have a class base as shown in the following program and two classes derv1 and derv2 are publicly derived from class base You would like to create a pointer that points to any of the derived class objects If you create a pointer of derv1 then it can point to derv1 object only Compiler will complain if you assign any other object is assigned to the pointer The solution is to create a pointer to Base class

Even though the address of derived classes is assigned to the pointer the compiler executes the base class function However if the base class function is made virtual we get the desired result In the following program we have made the base class function show() as virtual function by prefixing with the keyword virtual

virtualcpp include ltiostreamhgtclass base

publicvirtual void show() virtual functioncoutltltldquobaserdquoltltendl

class derv1public base

publicvoid show()

coutltltldquoderv1rdquoltltendl

class derv2 public base

publicvoid show()

18

coutltltldquoderv2rdquoltltendl

void main()derv1 dv1derv2 dv2base ptrptr=ampdv1ptr-gtshow()ptr=ampdv2ptr-gtshow()

By declaring the base class function as virtual we now get the output as

derv1derv2

In the above program depending on the contents in the pointer the compiler decides which class function to call during runtime This is known as late binding or dynamic binding

Polymorphism Polymorphism means same content but different forms In C++ polymorphism enables the same program code calling different functions of different classes For Example Letrsquos us suppose that we want to create a class shape and derive classes such as rectangle circle triangle etc Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen You would like to write a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime

Shape ptrarr[100]for (int j=0jltnj++)Ptrarr[j]-gtdraw()

This is an very desirable capability that completely different functions are executed by same function call If the ptrarr is pointing to a rectangle a rectangle is drawn If it is point int to circle circle is drawn

This is exactly what is polymorphism However to implement this approach several conditions should be met Firstly all the classes rectangle circle triangle should be derived from a single class (Here it is shape class) Secondly the draw() function must be declared as virtual in the base class (in the shape class)Operator overloading is a type of polymorphism which allows the same operators to behave differently with different datatypesoperands

4 Discuss the types of Inheritance with suitable example for each

Inheritance is a very powerful featu re of object oriented programming It allows reuse of code without modifying the original code It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort

19

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 9: (MCA) – Semester II MC0066 – OOPS using C++

void g(Aamp arg)

argf()

int main()

B xg(x)

The following is the output of the above example

Class A

When function g() is called function Af() is called although the argument refers to an object of type B At compile time the compiler knows only that the argument of function g() will be a reference to an object derived from A it cannot determine whether the argument will be a reference to an object of type A or type B However this can be determined at run time

The following example is the same as the previous example except that Af() is declared with the virtual keyword

include ltiostreamgtusing namespace stdstruct A

virtual void f()

cout ltlt Class A ltlt endl

struct B A

void f()

cout ltlt Class B ltlt endl

void g(Aamp arg )

argf()

int main()

B xg(x)

9

The following is the output of the above example

Class B

The virtual keyword indicates to the compiler that it should choose the appropriate definition of f() not by the type of reference but by the type of object that the reference refers to

6 Describe the Friend functions and friend classes with programming examples

Friend Functions Friend Functions are the functions which can have access to the private and protected members of the class They can be declared anywhere within a class declaration but it is common practice to list friends at the beginning of the class The public and protected keywords do not apply to friend functions as the class has no control over the scope of friends If we want to declare an external function as friend of a class thus allowing this function to have access to the private and protected members of this class we do it by declaring a prototype of this external function within the class and preceding it with the keyword friend

friend functionsinclude ltiostreamgtusing namespace stdclass CRectangle

int width heightpublicvoid set_values (int int)int area () return (width height)friend CRectangle duplicate (CRectangle)

void CRectangleset_values (int a int b)

width = aheight = b

CRectangle duplicate (CRectangle rectparam)

CRectangle rectresrectreswidth = rectparamwidth2rectresheight = rectparamheight2return (rectres)

int main () CRectangle rect rectbrectset_values (23)rectb = duplicate (rect)cout ltlt rectbarea()

return 0

10

The duplicate function is a friend of CRectangle From within that function we have been able to access the members width and height of different objects of type CRectangle which are private members Notice that neither in the declaration of duplicate() nor in its later use in main() have we considered duplicate a member of class CRectangle It isnt It simply has access to its private and protected members without being a member The friend functions can serve for example to conduct operations between two different classes Generally the use of friend functions is out of an object-oriented programming methodology so whenever possible it is better to use members of the same class to perform operations with them Such as in the previous example it would have been shorter to integrate duplicate() within the class CRectangle

Friend Classes A class can declare a member function of another class as a friend or declare another class as a friend class Friend classes are used in cases where one class is tightly coupled to another class For example suppose we have a class CPoint that represents a coordinate and a class CPointCollection that holds a list of points Since the collection class may need to manipulate point objects we could declare CPointCollection as a friend of the CPoint class

Forward declaration of friend classclass CPointCollection Point classclass CPoint friend CPointCollectionprivatedouble m_xdouble m_ypublicCPoint(const double x const double y) m_x(x)m_y(y) ~CPoint(void)

Since the collection class is a friend of CPoint it has access to the internal data of any point object This is useful when individual elements of the collection need to be manipulated For example a set method of the CPointCollection class could set all CPoint elements to a particular value (vector is a STL container which is discussed in detail in Chapter 8)

class CPointCollection privatevectorltCPointgt m_vecPointspublicCPointCollection(const int nSize) m_vecPoints(nSize) ~CPointCollection(void)void set(const double x const double y)

The set member can iterate over the collection and reset each point

11

void CPointCollectionset(const double x const double y) Get the number of elements in the collectionconst int nElements = m_vecPointssize() Set each elementfor(int i=0 iltnElements i++) m_vecPoints[i]m_x = xm_vecPoints[i]m_y = y

One thing important about friend classes is that friendship is not mutual although CPointCollection can access CPoint the converse is not true Friendship is also not something that is passed down a class hierarchy Derived classes of CPointCollection will not be able to access CPoint The principle behind this is that friendship is not implicitly granted each class must explicitly choose its friends

7 Illustrate with suitable examples various file handling methods in C++

File handling is an important part of all programs Most of the applications will have their own features to save some data to the local disk and read data from the disk again C++ File IO classes simplify such file readwrite operations for the programmer by providing easier to use classes

C++ File IO Classes and Functions

There are 3 File IO classes in C++ which are used for File ReadWrite operations They areifstream - Can be used for File readinput operationsofstream - Can be used for File writeoutput operationsfstream - Can be used for both readwrite c++ file IO operations

The most important methods which will be used for any file operations are

fstreamopen method - to open the filefstreamOperator gtgt and fstreamOperator ltlt - For reading from or writing to the filefstreamclose - Flushes all buffer and close the fileReading a text file using fstream class

There are several ways of reading the text from a file But all of them have a common approach as follows

Open the fileRead the dataClose the file

This sample code snippet explains how to use the c++ file io stream operators to read data from a file In all cases the header file fstreamh must be included

includeltfstreamhgt int main() char str[2000]

12

fstream file_op(ctest_filetxtiosin) while(file_op gtgt str) cout ltlt str file_opclose()

return 0

The class fstream which is used above is the one which is commonly used for c++ file io manipulations The constructor of fstream takes 2 parameters One is the file path and the second is the File Open mode There are several open modes each of them with a different purpose

But if we look at the output closely there is a draw back in using this stream operator read The output misses the white spaces and the end of line characters In order not to miss these characters we can either use fstreamget() or fstreamgetline() methods Here is the example for using fstream getline method

include ltfstreamhgt int main() char str[2000] fstream file_op(ctest_filetxtiosin) while(file_opeof()) file_opgetline(str2000) cout ltltstr file_opclose() cout ltltendl

return 0

Writing to a text file using fstream class

Writing to a text file can also be achieved with the stream operators This also follows the same order of operations though with a slight difference 1 open a file - in write mode 2 Write to a file 3 close the file Look at the following sample code to see the difference

include ltfstreamhgt int main() fstream file_op(cCoderSource_filetxtiosout)

file_opltltTest Write to file file_opclose()

13

return 0

To modify the data or to seek to a different position inside the file the c++ file io class fstream provides member functionslike seekg() etc These functions can be used to relocate the record insert position to the desired locations

After all the C++ File IO operations we do a fstreamclose() to close the file pointer This is not mandatory Even if this function is not called by the application the destructor of the fstream class will close the file when the object goes out of scope

8 What is template specialization Describe a scenario in which template class partial specialization is considered appropriate

Class Template Specialization In some cases it is possible to override the template-generated code by providing special definitions for specific types This is called template specialization

The following example defines a template class specialization for template class stream

include ltiostreamgtusing namespace stdtemplate ltclass Tgtclass stream publicvoid f() cout ltlt streamltTgtf()ltlt endl template ltgtclass streamltchargt publicvoid f() cout ltlt streamltchargtf()ltlt endl int main()

streamltintgt sistreamltchargt scsif()scf()return 0

Output

streamltTgtf()streamltchargtf()

In the above example streamltchargt is used as the definition of streams of chars other streams will be handled by the template class generated from the class template

14

Template Class Partial Specialization You may want to generate a specialization of the class for just one parameter for example

base template classtemplatelttypename T1 typename T2gtclass X partial specializationtemplatelttypename T1gtclass XltT1 intgt int main() generates an instantiation from the base templateXltchar chargt xcc generates an instantiation from the partial specializationXltchar intgt xii return 0

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list

15

December 2010

Master of Computer Application (MCA) ndash Semester 2

MC0066 ndash OOPS using C++ ndash 4 Credits

(Book ID B0681 amp B0715)

Assignment Set ndash 2 (40 Marks)

Answer all Questions Each Question carries FIVE Marks

Book ID B0681

1 Distinguished between procedural language and OOP language And Explain the key features of OOP

1 Procedural language focuses on organizing program statements into procedures or functions Larger programs were either broken into functions or modules whereas in Object Oriented Programming bundles both data and functions into one unitknown as object

2 One of the main problems with Procedural approach for programming was data being completely forgotten The emphasis was on the action and the data was only used in the entire process Whereas in Object Oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data

3 The procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled whereas in OOP language provides this flexibility through the concept of class

4 Another limitation of the procedural languages is that the program model is not closer to real world objects For example if you want to develop a gaming application of car race what data would you use and what functions you would require is difficult questions to answer in a procedural approach In the object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality In the car game example we would create several objects such as player car traffic signal and so on

Key Features of OOP

Encapsulation Data and functions are said to be encapsulated in an single entity as object

Data Hiding The data is said to be hidden thus not allowing accidental modification

Inheritance Inheritance is one of the most powerful feature of Object Oriented Programming Languages that allows you to derive a class from an existing class and inherit all the characteristics and behaviour of the parent class This feature allows easy modification of existing code and also reuse code The ability to reuse components of a program is an important feature for any programming language

16

Polymorphism and Overloading Operator overloading feature allows users to define how basic operators work with objects The operator + will be adding two numbers when used with integer variables However when used with user defined string class + operator may concatenate two strings Similarly same functions with same function name can perform different actions depending upon which object calls the function This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms) Operator overloading is a kind of polymorphism

2 Discuss the constructors and Destructors with suitable example

Constructors Constructors are member functions of a class which have same name as the class name Constructors are called automatically whenever an object of the class is created This feature makes it very useful to initialize the classdata members whenever a new object is created It also can perform any other function that needs to be performed for all the objects of the class without explicitly specifying it

Destructors Destructors on the other hand are also member functions with the same name as class but are prefixed with tilde (~) sign to differentiate it from theconstructor They are invoked automatically whenever the objectrsquos life expires or it is destroyed It can be used to return the memory back to the operating system if the memory was dynamically allocated

The following program implements the constructor and destructors for aclass

constdestcpp includeltiostreamhgtclass sample

privateint datapublicsample()

data=0 coutltltrdquoConstructor invokedrdquoltltendl

~sample()

coutltltrdquoDestructor invokedrdquovoid display()

coutltltrdquoData=rdquoltltdataltltendlvoid main()

sample obj1 objdisplay()

17

If you run the above program you will get the output as follows

Constructor invokedData=0Destructor invoked

When object of sample class object is created automatically the constructor is invoked and data is initialized to zero When the program ends the object is destroyed which invokes the destructor Please note that both the constructor and destructor is declared as public and they have no return value However constructors can have arguments and can be overloaded so that different constructors can be called depending upon the arguments that is passed Destructors on the other hand cannot be overloaded and cannot have any arguments

3 Describe the theory behind Virtual Functions and Polymorphism along with suitable programming examples for each

Virtual Functions Virtual means existing in effect but not in reality Virtual functions are primarily used in inheritance Let us suppose you have a class base as shown in the following program and two classes derv1 and derv2 are publicly derived from class base You would like to create a pointer that points to any of the derived class objects If you create a pointer of derv1 then it can point to derv1 object only Compiler will complain if you assign any other object is assigned to the pointer The solution is to create a pointer to Base class

Even though the address of derived classes is assigned to the pointer the compiler executes the base class function However if the base class function is made virtual we get the desired result In the following program we have made the base class function show() as virtual function by prefixing with the keyword virtual

virtualcpp include ltiostreamhgtclass base

publicvirtual void show() virtual functioncoutltltldquobaserdquoltltendl

class derv1public base

publicvoid show()

coutltltldquoderv1rdquoltltendl

class derv2 public base

publicvoid show()

18

coutltltldquoderv2rdquoltltendl

void main()derv1 dv1derv2 dv2base ptrptr=ampdv1ptr-gtshow()ptr=ampdv2ptr-gtshow()

By declaring the base class function as virtual we now get the output as

derv1derv2

In the above program depending on the contents in the pointer the compiler decides which class function to call during runtime This is known as late binding or dynamic binding

Polymorphism Polymorphism means same content but different forms In C++ polymorphism enables the same program code calling different functions of different classes For Example Letrsquos us suppose that we want to create a class shape and derive classes such as rectangle circle triangle etc Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen You would like to write a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime

Shape ptrarr[100]for (int j=0jltnj++)Ptrarr[j]-gtdraw()

This is an very desirable capability that completely different functions are executed by same function call If the ptrarr is pointing to a rectangle a rectangle is drawn If it is point int to circle circle is drawn

This is exactly what is polymorphism However to implement this approach several conditions should be met Firstly all the classes rectangle circle triangle should be derived from a single class (Here it is shape class) Secondly the draw() function must be declared as virtual in the base class (in the shape class)Operator overloading is a type of polymorphism which allows the same operators to behave differently with different datatypesoperands

4 Discuss the types of Inheritance with suitable example for each

Inheritance is a very powerful featu re of object oriented programming It allows reuse of code without modifying the original code It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort

19

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 10: (MCA) – Semester II MC0066 – OOPS using C++

The following is the output of the above example

Class B

The virtual keyword indicates to the compiler that it should choose the appropriate definition of f() not by the type of reference but by the type of object that the reference refers to

6 Describe the Friend functions and friend classes with programming examples

Friend Functions Friend Functions are the functions which can have access to the private and protected members of the class They can be declared anywhere within a class declaration but it is common practice to list friends at the beginning of the class The public and protected keywords do not apply to friend functions as the class has no control over the scope of friends If we want to declare an external function as friend of a class thus allowing this function to have access to the private and protected members of this class we do it by declaring a prototype of this external function within the class and preceding it with the keyword friend

friend functionsinclude ltiostreamgtusing namespace stdclass CRectangle

int width heightpublicvoid set_values (int int)int area () return (width height)friend CRectangle duplicate (CRectangle)

void CRectangleset_values (int a int b)

width = aheight = b

CRectangle duplicate (CRectangle rectparam)

CRectangle rectresrectreswidth = rectparamwidth2rectresheight = rectparamheight2return (rectres)

int main () CRectangle rect rectbrectset_values (23)rectb = duplicate (rect)cout ltlt rectbarea()

return 0

10

The duplicate function is a friend of CRectangle From within that function we have been able to access the members width and height of different objects of type CRectangle which are private members Notice that neither in the declaration of duplicate() nor in its later use in main() have we considered duplicate a member of class CRectangle It isnt It simply has access to its private and protected members without being a member The friend functions can serve for example to conduct operations between two different classes Generally the use of friend functions is out of an object-oriented programming methodology so whenever possible it is better to use members of the same class to perform operations with them Such as in the previous example it would have been shorter to integrate duplicate() within the class CRectangle

Friend Classes A class can declare a member function of another class as a friend or declare another class as a friend class Friend classes are used in cases where one class is tightly coupled to another class For example suppose we have a class CPoint that represents a coordinate and a class CPointCollection that holds a list of points Since the collection class may need to manipulate point objects we could declare CPointCollection as a friend of the CPoint class

Forward declaration of friend classclass CPointCollection Point classclass CPoint friend CPointCollectionprivatedouble m_xdouble m_ypublicCPoint(const double x const double y) m_x(x)m_y(y) ~CPoint(void)

Since the collection class is a friend of CPoint it has access to the internal data of any point object This is useful when individual elements of the collection need to be manipulated For example a set method of the CPointCollection class could set all CPoint elements to a particular value (vector is a STL container which is discussed in detail in Chapter 8)

class CPointCollection privatevectorltCPointgt m_vecPointspublicCPointCollection(const int nSize) m_vecPoints(nSize) ~CPointCollection(void)void set(const double x const double y)

The set member can iterate over the collection and reset each point

11

void CPointCollectionset(const double x const double y) Get the number of elements in the collectionconst int nElements = m_vecPointssize() Set each elementfor(int i=0 iltnElements i++) m_vecPoints[i]m_x = xm_vecPoints[i]m_y = y

One thing important about friend classes is that friendship is not mutual although CPointCollection can access CPoint the converse is not true Friendship is also not something that is passed down a class hierarchy Derived classes of CPointCollection will not be able to access CPoint The principle behind this is that friendship is not implicitly granted each class must explicitly choose its friends

7 Illustrate with suitable examples various file handling methods in C++

File handling is an important part of all programs Most of the applications will have their own features to save some data to the local disk and read data from the disk again C++ File IO classes simplify such file readwrite operations for the programmer by providing easier to use classes

C++ File IO Classes and Functions

There are 3 File IO classes in C++ which are used for File ReadWrite operations They areifstream - Can be used for File readinput operationsofstream - Can be used for File writeoutput operationsfstream - Can be used for both readwrite c++ file IO operations

The most important methods which will be used for any file operations are

fstreamopen method - to open the filefstreamOperator gtgt and fstreamOperator ltlt - For reading from or writing to the filefstreamclose - Flushes all buffer and close the fileReading a text file using fstream class

There are several ways of reading the text from a file But all of them have a common approach as follows

Open the fileRead the dataClose the file

This sample code snippet explains how to use the c++ file io stream operators to read data from a file In all cases the header file fstreamh must be included

includeltfstreamhgt int main() char str[2000]

12

fstream file_op(ctest_filetxtiosin) while(file_op gtgt str) cout ltlt str file_opclose()

return 0

The class fstream which is used above is the one which is commonly used for c++ file io manipulations The constructor of fstream takes 2 parameters One is the file path and the second is the File Open mode There are several open modes each of them with a different purpose

But if we look at the output closely there is a draw back in using this stream operator read The output misses the white spaces and the end of line characters In order not to miss these characters we can either use fstreamget() or fstreamgetline() methods Here is the example for using fstream getline method

include ltfstreamhgt int main() char str[2000] fstream file_op(ctest_filetxtiosin) while(file_opeof()) file_opgetline(str2000) cout ltltstr file_opclose() cout ltltendl

return 0

Writing to a text file using fstream class

Writing to a text file can also be achieved with the stream operators This also follows the same order of operations though with a slight difference 1 open a file - in write mode 2 Write to a file 3 close the file Look at the following sample code to see the difference

include ltfstreamhgt int main() fstream file_op(cCoderSource_filetxtiosout)

file_opltltTest Write to file file_opclose()

13

return 0

To modify the data or to seek to a different position inside the file the c++ file io class fstream provides member functionslike seekg() etc These functions can be used to relocate the record insert position to the desired locations

After all the C++ File IO operations we do a fstreamclose() to close the file pointer This is not mandatory Even if this function is not called by the application the destructor of the fstream class will close the file when the object goes out of scope

8 What is template specialization Describe a scenario in which template class partial specialization is considered appropriate

Class Template Specialization In some cases it is possible to override the template-generated code by providing special definitions for specific types This is called template specialization

The following example defines a template class specialization for template class stream

include ltiostreamgtusing namespace stdtemplate ltclass Tgtclass stream publicvoid f() cout ltlt streamltTgtf()ltlt endl template ltgtclass streamltchargt publicvoid f() cout ltlt streamltchargtf()ltlt endl int main()

streamltintgt sistreamltchargt scsif()scf()return 0

Output

streamltTgtf()streamltchargtf()

In the above example streamltchargt is used as the definition of streams of chars other streams will be handled by the template class generated from the class template

14

Template Class Partial Specialization You may want to generate a specialization of the class for just one parameter for example

base template classtemplatelttypename T1 typename T2gtclass X partial specializationtemplatelttypename T1gtclass XltT1 intgt int main() generates an instantiation from the base templateXltchar chargt xcc generates an instantiation from the partial specializationXltchar intgt xii return 0

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list

15

December 2010

Master of Computer Application (MCA) ndash Semester 2

MC0066 ndash OOPS using C++ ndash 4 Credits

(Book ID B0681 amp B0715)

Assignment Set ndash 2 (40 Marks)

Answer all Questions Each Question carries FIVE Marks

Book ID B0681

1 Distinguished between procedural language and OOP language And Explain the key features of OOP

1 Procedural language focuses on organizing program statements into procedures or functions Larger programs were either broken into functions or modules whereas in Object Oriented Programming bundles both data and functions into one unitknown as object

2 One of the main problems with Procedural approach for programming was data being completely forgotten The emphasis was on the action and the data was only used in the entire process Whereas in Object Oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data

3 The procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled whereas in OOP language provides this flexibility through the concept of class

4 Another limitation of the procedural languages is that the program model is not closer to real world objects For example if you want to develop a gaming application of car race what data would you use and what functions you would require is difficult questions to answer in a procedural approach In the object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality In the car game example we would create several objects such as player car traffic signal and so on

Key Features of OOP

Encapsulation Data and functions are said to be encapsulated in an single entity as object

Data Hiding The data is said to be hidden thus not allowing accidental modification

Inheritance Inheritance is one of the most powerful feature of Object Oriented Programming Languages that allows you to derive a class from an existing class and inherit all the characteristics and behaviour of the parent class This feature allows easy modification of existing code and also reuse code The ability to reuse components of a program is an important feature for any programming language

16

Polymorphism and Overloading Operator overloading feature allows users to define how basic operators work with objects The operator + will be adding two numbers when used with integer variables However when used with user defined string class + operator may concatenate two strings Similarly same functions with same function name can perform different actions depending upon which object calls the function This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms) Operator overloading is a kind of polymorphism

2 Discuss the constructors and Destructors with suitable example

Constructors Constructors are member functions of a class which have same name as the class name Constructors are called automatically whenever an object of the class is created This feature makes it very useful to initialize the classdata members whenever a new object is created It also can perform any other function that needs to be performed for all the objects of the class without explicitly specifying it

Destructors Destructors on the other hand are also member functions with the same name as class but are prefixed with tilde (~) sign to differentiate it from theconstructor They are invoked automatically whenever the objectrsquos life expires or it is destroyed It can be used to return the memory back to the operating system if the memory was dynamically allocated

The following program implements the constructor and destructors for aclass

constdestcpp includeltiostreamhgtclass sample

privateint datapublicsample()

data=0 coutltltrdquoConstructor invokedrdquoltltendl

~sample()

coutltltrdquoDestructor invokedrdquovoid display()

coutltltrdquoData=rdquoltltdataltltendlvoid main()

sample obj1 objdisplay()

17

If you run the above program you will get the output as follows

Constructor invokedData=0Destructor invoked

When object of sample class object is created automatically the constructor is invoked and data is initialized to zero When the program ends the object is destroyed which invokes the destructor Please note that both the constructor and destructor is declared as public and they have no return value However constructors can have arguments and can be overloaded so that different constructors can be called depending upon the arguments that is passed Destructors on the other hand cannot be overloaded and cannot have any arguments

3 Describe the theory behind Virtual Functions and Polymorphism along with suitable programming examples for each

Virtual Functions Virtual means existing in effect but not in reality Virtual functions are primarily used in inheritance Let us suppose you have a class base as shown in the following program and two classes derv1 and derv2 are publicly derived from class base You would like to create a pointer that points to any of the derived class objects If you create a pointer of derv1 then it can point to derv1 object only Compiler will complain if you assign any other object is assigned to the pointer The solution is to create a pointer to Base class

Even though the address of derived classes is assigned to the pointer the compiler executes the base class function However if the base class function is made virtual we get the desired result In the following program we have made the base class function show() as virtual function by prefixing with the keyword virtual

virtualcpp include ltiostreamhgtclass base

publicvirtual void show() virtual functioncoutltltldquobaserdquoltltendl

class derv1public base

publicvoid show()

coutltltldquoderv1rdquoltltendl

class derv2 public base

publicvoid show()

18

coutltltldquoderv2rdquoltltendl

void main()derv1 dv1derv2 dv2base ptrptr=ampdv1ptr-gtshow()ptr=ampdv2ptr-gtshow()

By declaring the base class function as virtual we now get the output as

derv1derv2

In the above program depending on the contents in the pointer the compiler decides which class function to call during runtime This is known as late binding or dynamic binding

Polymorphism Polymorphism means same content but different forms In C++ polymorphism enables the same program code calling different functions of different classes For Example Letrsquos us suppose that we want to create a class shape and derive classes such as rectangle circle triangle etc Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen You would like to write a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime

Shape ptrarr[100]for (int j=0jltnj++)Ptrarr[j]-gtdraw()

This is an very desirable capability that completely different functions are executed by same function call If the ptrarr is pointing to a rectangle a rectangle is drawn If it is point int to circle circle is drawn

This is exactly what is polymorphism However to implement this approach several conditions should be met Firstly all the classes rectangle circle triangle should be derived from a single class (Here it is shape class) Secondly the draw() function must be declared as virtual in the base class (in the shape class)Operator overloading is a type of polymorphism which allows the same operators to behave differently with different datatypesoperands

4 Discuss the types of Inheritance with suitable example for each

Inheritance is a very powerful featu re of object oriented programming It allows reuse of code without modifying the original code It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort

19

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 11: (MCA) – Semester II MC0066 – OOPS using C++

The duplicate function is a friend of CRectangle From within that function we have been able to access the members width and height of different objects of type CRectangle which are private members Notice that neither in the declaration of duplicate() nor in its later use in main() have we considered duplicate a member of class CRectangle It isnt It simply has access to its private and protected members without being a member The friend functions can serve for example to conduct operations between two different classes Generally the use of friend functions is out of an object-oriented programming methodology so whenever possible it is better to use members of the same class to perform operations with them Such as in the previous example it would have been shorter to integrate duplicate() within the class CRectangle

Friend Classes A class can declare a member function of another class as a friend or declare another class as a friend class Friend classes are used in cases where one class is tightly coupled to another class For example suppose we have a class CPoint that represents a coordinate and a class CPointCollection that holds a list of points Since the collection class may need to manipulate point objects we could declare CPointCollection as a friend of the CPoint class

Forward declaration of friend classclass CPointCollection Point classclass CPoint friend CPointCollectionprivatedouble m_xdouble m_ypublicCPoint(const double x const double y) m_x(x)m_y(y) ~CPoint(void)

Since the collection class is a friend of CPoint it has access to the internal data of any point object This is useful when individual elements of the collection need to be manipulated For example a set method of the CPointCollection class could set all CPoint elements to a particular value (vector is a STL container which is discussed in detail in Chapter 8)

class CPointCollection privatevectorltCPointgt m_vecPointspublicCPointCollection(const int nSize) m_vecPoints(nSize) ~CPointCollection(void)void set(const double x const double y)

The set member can iterate over the collection and reset each point

11

void CPointCollectionset(const double x const double y) Get the number of elements in the collectionconst int nElements = m_vecPointssize() Set each elementfor(int i=0 iltnElements i++) m_vecPoints[i]m_x = xm_vecPoints[i]m_y = y

One thing important about friend classes is that friendship is not mutual although CPointCollection can access CPoint the converse is not true Friendship is also not something that is passed down a class hierarchy Derived classes of CPointCollection will not be able to access CPoint The principle behind this is that friendship is not implicitly granted each class must explicitly choose its friends

7 Illustrate with suitable examples various file handling methods in C++

File handling is an important part of all programs Most of the applications will have their own features to save some data to the local disk and read data from the disk again C++ File IO classes simplify such file readwrite operations for the programmer by providing easier to use classes

C++ File IO Classes and Functions

There are 3 File IO classes in C++ which are used for File ReadWrite operations They areifstream - Can be used for File readinput operationsofstream - Can be used for File writeoutput operationsfstream - Can be used for both readwrite c++ file IO operations

The most important methods which will be used for any file operations are

fstreamopen method - to open the filefstreamOperator gtgt and fstreamOperator ltlt - For reading from or writing to the filefstreamclose - Flushes all buffer and close the fileReading a text file using fstream class

There are several ways of reading the text from a file But all of them have a common approach as follows

Open the fileRead the dataClose the file

This sample code snippet explains how to use the c++ file io stream operators to read data from a file In all cases the header file fstreamh must be included

includeltfstreamhgt int main() char str[2000]

12

fstream file_op(ctest_filetxtiosin) while(file_op gtgt str) cout ltlt str file_opclose()

return 0

The class fstream which is used above is the one which is commonly used for c++ file io manipulations The constructor of fstream takes 2 parameters One is the file path and the second is the File Open mode There are several open modes each of them with a different purpose

But if we look at the output closely there is a draw back in using this stream operator read The output misses the white spaces and the end of line characters In order not to miss these characters we can either use fstreamget() or fstreamgetline() methods Here is the example for using fstream getline method

include ltfstreamhgt int main() char str[2000] fstream file_op(ctest_filetxtiosin) while(file_opeof()) file_opgetline(str2000) cout ltltstr file_opclose() cout ltltendl

return 0

Writing to a text file using fstream class

Writing to a text file can also be achieved with the stream operators This also follows the same order of operations though with a slight difference 1 open a file - in write mode 2 Write to a file 3 close the file Look at the following sample code to see the difference

include ltfstreamhgt int main() fstream file_op(cCoderSource_filetxtiosout)

file_opltltTest Write to file file_opclose()

13

return 0

To modify the data or to seek to a different position inside the file the c++ file io class fstream provides member functionslike seekg() etc These functions can be used to relocate the record insert position to the desired locations

After all the C++ File IO operations we do a fstreamclose() to close the file pointer This is not mandatory Even if this function is not called by the application the destructor of the fstream class will close the file when the object goes out of scope

8 What is template specialization Describe a scenario in which template class partial specialization is considered appropriate

Class Template Specialization In some cases it is possible to override the template-generated code by providing special definitions for specific types This is called template specialization

The following example defines a template class specialization for template class stream

include ltiostreamgtusing namespace stdtemplate ltclass Tgtclass stream publicvoid f() cout ltlt streamltTgtf()ltlt endl template ltgtclass streamltchargt publicvoid f() cout ltlt streamltchargtf()ltlt endl int main()

streamltintgt sistreamltchargt scsif()scf()return 0

Output

streamltTgtf()streamltchargtf()

In the above example streamltchargt is used as the definition of streams of chars other streams will be handled by the template class generated from the class template

14

Template Class Partial Specialization You may want to generate a specialization of the class for just one parameter for example

base template classtemplatelttypename T1 typename T2gtclass X partial specializationtemplatelttypename T1gtclass XltT1 intgt int main() generates an instantiation from the base templateXltchar chargt xcc generates an instantiation from the partial specializationXltchar intgt xii return 0

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list

15

December 2010

Master of Computer Application (MCA) ndash Semester 2

MC0066 ndash OOPS using C++ ndash 4 Credits

(Book ID B0681 amp B0715)

Assignment Set ndash 2 (40 Marks)

Answer all Questions Each Question carries FIVE Marks

Book ID B0681

1 Distinguished between procedural language and OOP language And Explain the key features of OOP

1 Procedural language focuses on organizing program statements into procedures or functions Larger programs were either broken into functions or modules whereas in Object Oriented Programming bundles both data and functions into one unitknown as object

2 One of the main problems with Procedural approach for programming was data being completely forgotten The emphasis was on the action and the data was only used in the entire process Whereas in Object Oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data

3 The procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled whereas in OOP language provides this flexibility through the concept of class

4 Another limitation of the procedural languages is that the program model is not closer to real world objects For example if you want to develop a gaming application of car race what data would you use and what functions you would require is difficult questions to answer in a procedural approach In the object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality In the car game example we would create several objects such as player car traffic signal and so on

Key Features of OOP

Encapsulation Data and functions are said to be encapsulated in an single entity as object

Data Hiding The data is said to be hidden thus not allowing accidental modification

Inheritance Inheritance is one of the most powerful feature of Object Oriented Programming Languages that allows you to derive a class from an existing class and inherit all the characteristics and behaviour of the parent class This feature allows easy modification of existing code and also reuse code The ability to reuse components of a program is an important feature for any programming language

16

Polymorphism and Overloading Operator overloading feature allows users to define how basic operators work with objects The operator + will be adding two numbers when used with integer variables However when used with user defined string class + operator may concatenate two strings Similarly same functions with same function name can perform different actions depending upon which object calls the function This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms) Operator overloading is a kind of polymorphism

2 Discuss the constructors and Destructors with suitable example

Constructors Constructors are member functions of a class which have same name as the class name Constructors are called automatically whenever an object of the class is created This feature makes it very useful to initialize the classdata members whenever a new object is created It also can perform any other function that needs to be performed for all the objects of the class without explicitly specifying it

Destructors Destructors on the other hand are also member functions with the same name as class but are prefixed with tilde (~) sign to differentiate it from theconstructor They are invoked automatically whenever the objectrsquos life expires or it is destroyed It can be used to return the memory back to the operating system if the memory was dynamically allocated

The following program implements the constructor and destructors for aclass

constdestcpp includeltiostreamhgtclass sample

privateint datapublicsample()

data=0 coutltltrdquoConstructor invokedrdquoltltendl

~sample()

coutltltrdquoDestructor invokedrdquovoid display()

coutltltrdquoData=rdquoltltdataltltendlvoid main()

sample obj1 objdisplay()

17

If you run the above program you will get the output as follows

Constructor invokedData=0Destructor invoked

When object of sample class object is created automatically the constructor is invoked and data is initialized to zero When the program ends the object is destroyed which invokes the destructor Please note that both the constructor and destructor is declared as public and they have no return value However constructors can have arguments and can be overloaded so that different constructors can be called depending upon the arguments that is passed Destructors on the other hand cannot be overloaded and cannot have any arguments

3 Describe the theory behind Virtual Functions and Polymorphism along with suitable programming examples for each

Virtual Functions Virtual means existing in effect but not in reality Virtual functions are primarily used in inheritance Let us suppose you have a class base as shown in the following program and two classes derv1 and derv2 are publicly derived from class base You would like to create a pointer that points to any of the derived class objects If you create a pointer of derv1 then it can point to derv1 object only Compiler will complain if you assign any other object is assigned to the pointer The solution is to create a pointer to Base class

Even though the address of derived classes is assigned to the pointer the compiler executes the base class function However if the base class function is made virtual we get the desired result In the following program we have made the base class function show() as virtual function by prefixing with the keyword virtual

virtualcpp include ltiostreamhgtclass base

publicvirtual void show() virtual functioncoutltltldquobaserdquoltltendl

class derv1public base

publicvoid show()

coutltltldquoderv1rdquoltltendl

class derv2 public base

publicvoid show()

18

coutltltldquoderv2rdquoltltendl

void main()derv1 dv1derv2 dv2base ptrptr=ampdv1ptr-gtshow()ptr=ampdv2ptr-gtshow()

By declaring the base class function as virtual we now get the output as

derv1derv2

In the above program depending on the contents in the pointer the compiler decides which class function to call during runtime This is known as late binding or dynamic binding

Polymorphism Polymorphism means same content but different forms In C++ polymorphism enables the same program code calling different functions of different classes For Example Letrsquos us suppose that we want to create a class shape and derive classes such as rectangle circle triangle etc Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen You would like to write a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime

Shape ptrarr[100]for (int j=0jltnj++)Ptrarr[j]-gtdraw()

This is an very desirable capability that completely different functions are executed by same function call If the ptrarr is pointing to a rectangle a rectangle is drawn If it is point int to circle circle is drawn

This is exactly what is polymorphism However to implement this approach several conditions should be met Firstly all the classes rectangle circle triangle should be derived from a single class (Here it is shape class) Secondly the draw() function must be declared as virtual in the base class (in the shape class)Operator overloading is a type of polymorphism which allows the same operators to behave differently with different datatypesoperands

4 Discuss the types of Inheritance with suitable example for each

Inheritance is a very powerful featu re of object oriented programming It allows reuse of code without modifying the original code It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort

19

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 12: (MCA) – Semester II MC0066 – OOPS using C++

void CPointCollectionset(const double x const double y) Get the number of elements in the collectionconst int nElements = m_vecPointssize() Set each elementfor(int i=0 iltnElements i++) m_vecPoints[i]m_x = xm_vecPoints[i]m_y = y

One thing important about friend classes is that friendship is not mutual although CPointCollection can access CPoint the converse is not true Friendship is also not something that is passed down a class hierarchy Derived classes of CPointCollection will not be able to access CPoint The principle behind this is that friendship is not implicitly granted each class must explicitly choose its friends

7 Illustrate with suitable examples various file handling methods in C++

File handling is an important part of all programs Most of the applications will have their own features to save some data to the local disk and read data from the disk again C++ File IO classes simplify such file readwrite operations for the programmer by providing easier to use classes

C++ File IO Classes and Functions

There are 3 File IO classes in C++ which are used for File ReadWrite operations They areifstream - Can be used for File readinput operationsofstream - Can be used for File writeoutput operationsfstream - Can be used for both readwrite c++ file IO operations

The most important methods which will be used for any file operations are

fstreamopen method - to open the filefstreamOperator gtgt and fstreamOperator ltlt - For reading from or writing to the filefstreamclose - Flushes all buffer and close the fileReading a text file using fstream class

There are several ways of reading the text from a file But all of them have a common approach as follows

Open the fileRead the dataClose the file

This sample code snippet explains how to use the c++ file io stream operators to read data from a file In all cases the header file fstreamh must be included

includeltfstreamhgt int main() char str[2000]

12

fstream file_op(ctest_filetxtiosin) while(file_op gtgt str) cout ltlt str file_opclose()

return 0

The class fstream which is used above is the one which is commonly used for c++ file io manipulations The constructor of fstream takes 2 parameters One is the file path and the second is the File Open mode There are several open modes each of them with a different purpose

But if we look at the output closely there is a draw back in using this stream operator read The output misses the white spaces and the end of line characters In order not to miss these characters we can either use fstreamget() or fstreamgetline() methods Here is the example for using fstream getline method

include ltfstreamhgt int main() char str[2000] fstream file_op(ctest_filetxtiosin) while(file_opeof()) file_opgetline(str2000) cout ltltstr file_opclose() cout ltltendl

return 0

Writing to a text file using fstream class

Writing to a text file can also be achieved with the stream operators This also follows the same order of operations though with a slight difference 1 open a file - in write mode 2 Write to a file 3 close the file Look at the following sample code to see the difference

include ltfstreamhgt int main() fstream file_op(cCoderSource_filetxtiosout)

file_opltltTest Write to file file_opclose()

13

return 0

To modify the data or to seek to a different position inside the file the c++ file io class fstream provides member functionslike seekg() etc These functions can be used to relocate the record insert position to the desired locations

After all the C++ File IO operations we do a fstreamclose() to close the file pointer This is not mandatory Even if this function is not called by the application the destructor of the fstream class will close the file when the object goes out of scope

8 What is template specialization Describe a scenario in which template class partial specialization is considered appropriate

Class Template Specialization In some cases it is possible to override the template-generated code by providing special definitions for specific types This is called template specialization

The following example defines a template class specialization for template class stream

include ltiostreamgtusing namespace stdtemplate ltclass Tgtclass stream publicvoid f() cout ltlt streamltTgtf()ltlt endl template ltgtclass streamltchargt publicvoid f() cout ltlt streamltchargtf()ltlt endl int main()

streamltintgt sistreamltchargt scsif()scf()return 0

Output

streamltTgtf()streamltchargtf()

In the above example streamltchargt is used as the definition of streams of chars other streams will be handled by the template class generated from the class template

14

Template Class Partial Specialization You may want to generate a specialization of the class for just one parameter for example

base template classtemplatelttypename T1 typename T2gtclass X partial specializationtemplatelttypename T1gtclass XltT1 intgt int main() generates an instantiation from the base templateXltchar chargt xcc generates an instantiation from the partial specializationXltchar intgt xii return 0

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list

15

December 2010

Master of Computer Application (MCA) ndash Semester 2

MC0066 ndash OOPS using C++ ndash 4 Credits

(Book ID B0681 amp B0715)

Assignment Set ndash 2 (40 Marks)

Answer all Questions Each Question carries FIVE Marks

Book ID B0681

1 Distinguished between procedural language and OOP language And Explain the key features of OOP

1 Procedural language focuses on organizing program statements into procedures or functions Larger programs were either broken into functions or modules whereas in Object Oriented Programming bundles both data and functions into one unitknown as object

2 One of the main problems with Procedural approach for programming was data being completely forgotten The emphasis was on the action and the data was only used in the entire process Whereas in Object Oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data

3 The procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled whereas in OOP language provides this flexibility through the concept of class

4 Another limitation of the procedural languages is that the program model is not closer to real world objects For example if you want to develop a gaming application of car race what data would you use and what functions you would require is difficult questions to answer in a procedural approach In the object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality In the car game example we would create several objects such as player car traffic signal and so on

Key Features of OOP

Encapsulation Data and functions are said to be encapsulated in an single entity as object

Data Hiding The data is said to be hidden thus not allowing accidental modification

Inheritance Inheritance is one of the most powerful feature of Object Oriented Programming Languages that allows you to derive a class from an existing class and inherit all the characteristics and behaviour of the parent class This feature allows easy modification of existing code and also reuse code The ability to reuse components of a program is an important feature for any programming language

16

Polymorphism and Overloading Operator overloading feature allows users to define how basic operators work with objects The operator + will be adding two numbers when used with integer variables However when used with user defined string class + operator may concatenate two strings Similarly same functions with same function name can perform different actions depending upon which object calls the function This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms) Operator overloading is a kind of polymorphism

2 Discuss the constructors and Destructors with suitable example

Constructors Constructors are member functions of a class which have same name as the class name Constructors are called automatically whenever an object of the class is created This feature makes it very useful to initialize the classdata members whenever a new object is created It also can perform any other function that needs to be performed for all the objects of the class without explicitly specifying it

Destructors Destructors on the other hand are also member functions with the same name as class but are prefixed with tilde (~) sign to differentiate it from theconstructor They are invoked automatically whenever the objectrsquos life expires or it is destroyed It can be used to return the memory back to the operating system if the memory was dynamically allocated

The following program implements the constructor and destructors for aclass

constdestcpp includeltiostreamhgtclass sample

privateint datapublicsample()

data=0 coutltltrdquoConstructor invokedrdquoltltendl

~sample()

coutltltrdquoDestructor invokedrdquovoid display()

coutltltrdquoData=rdquoltltdataltltendlvoid main()

sample obj1 objdisplay()

17

If you run the above program you will get the output as follows

Constructor invokedData=0Destructor invoked

When object of sample class object is created automatically the constructor is invoked and data is initialized to zero When the program ends the object is destroyed which invokes the destructor Please note that both the constructor and destructor is declared as public and they have no return value However constructors can have arguments and can be overloaded so that different constructors can be called depending upon the arguments that is passed Destructors on the other hand cannot be overloaded and cannot have any arguments

3 Describe the theory behind Virtual Functions and Polymorphism along with suitable programming examples for each

Virtual Functions Virtual means existing in effect but not in reality Virtual functions are primarily used in inheritance Let us suppose you have a class base as shown in the following program and two classes derv1 and derv2 are publicly derived from class base You would like to create a pointer that points to any of the derived class objects If you create a pointer of derv1 then it can point to derv1 object only Compiler will complain if you assign any other object is assigned to the pointer The solution is to create a pointer to Base class

Even though the address of derived classes is assigned to the pointer the compiler executes the base class function However if the base class function is made virtual we get the desired result In the following program we have made the base class function show() as virtual function by prefixing with the keyword virtual

virtualcpp include ltiostreamhgtclass base

publicvirtual void show() virtual functioncoutltltldquobaserdquoltltendl

class derv1public base

publicvoid show()

coutltltldquoderv1rdquoltltendl

class derv2 public base

publicvoid show()

18

coutltltldquoderv2rdquoltltendl

void main()derv1 dv1derv2 dv2base ptrptr=ampdv1ptr-gtshow()ptr=ampdv2ptr-gtshow()

By declaring the base class function as virtual we now get the output as

derv1derv2

In the above program depending on the contents in the pointer the compiler decides which class function to call during runtime This is known as late binding or dynamic binding

Polymorphism Polymorphism means same content but different forms In C++ polymorphism enables the same program code calling different functions of different classes For Example Letrsquos us suppose that we want to create a class shape and derive classes such as rectangle circle triangle etc Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen You would like to write a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime

Shape ptrarr[100]for (int j=0jltnj++)Ptrarr[j]-gtdraw()

This is an very desirable capability that completely different functions are executed by same function call If the ptrarr is pointing to a rectangle a rectangle is drawn If it is point int to circle circle is drawn

This is exactly what is polymorphism However to implement this approach several conditions should be met Firstly all the classes rectangle circle triangle should be derived from a single class (Here it is shape class) Secondly the draw() function must be declared as virtual in the base class (in the shape class)Operator overloading is a type of polymorphism which allows the same operators to behave differently with different datatypesoperands

4 Discuss the types of Inheritance with suitable example for each

Inheritance is a very powerful featu re of object oriented programming It allows reuse of code without modifying the original code It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort

19

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 13: (MCA) – Semester II MC0066 – OOPS using C++

fstream file_op(ctest_filetxtiosin) while(file_op gtgt str) cout ltlt str file_opclose()

return 0

The class fstream which is used above is the one which is commonly used for c++ file io manipulations The constructor of fstream takes 2 parameters One is the file path and the second is the File Open mode There are several open modes each of them with a different purpose

But if we look at the output closely there is a draw back in using this stream operator read The output misses the white spaces and the end of line characters In order not to miss these characters we can either use fstreamget() or fstreamgetline() methods Here is the example for using fstream getline method

include ltfstreamhgt int main() char str[2000] fstream file_op(ctest_filetxtiosin) while(file_opeof()) file_opgetline(str2000) cout ltltstr file_opclose() cout ltltendl

return 0

Writing to a text file using fstream class

Writing to a text file can also be achieved with the stream operators This also follows the same order of operations though with a slight difference 1 open a file - in write mode 2 Write to a file 3 close the file Look at the following sample code to see the difference

include ltfstreamhgt int main() fstream file_op(cCoderSource_filetxtiosout)

file_opltltTest Write to file file_opclose()

13

return 0

To modify the data or to seek to a different position inside the file the c++ file io class fstream provides member functionslike seekg() etc These functions can be used to relocate the record insert position to the desired locations

After all the C++ File IO operations we do a fstreamclose() to close the file pointer This is not mandatory Even if this function is not called by the application the destructor of the fstream class will close the file when the object goes out of scope

8 What is template specialization Describe a scenario in which template class partial specialization is considered appropriate

Class Template Specialization In some cases it is possible to override the template-generated code by providing special definitions for specific types This is called template specialization

The following example defines a template class specialization for template class stream

include ltiostreamgtusing namespace stdtemplate ltclass Tgtclass stream publicvoid f() cout ltlt streamltTgtf()ltlt endl template ltgtclass streamltchargt publicvoid f() cout ltlt streamltchargtf()ltlt endl int main()

streamltintgt sistreamltchargt scsif()scf()return 0

Output

streamltTgtf()streamltchargtf()

In the above example streamltchargt is used as the definition of streams of chars other streams will be handled by the template class generated from the class template

14

Template Class Partial Specialization You may want to generate a specialization of the class for just one parameter for example

base template classtemplatelttypename T1 typename T2gtclass X partial specializationtemplatelttypename T1gtclass XltT1 intgt int main() generates an instantiation from the base templateXltchar chargt xcc generates an instantiation from the partial specializationXltchar intgt xii return 0

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list

15

December 2010

Master of Computer Application (MCA) ndash Semester 2

MC0066 ndash OOPS using C++ ndash 4 Credits

(Book ID B0681 amp B0715)

Assignment Set ndash 2 (40 Marks)

Answer all Questions Each Question carries FIVE Marks

Book ID B0681

1 Distinguished between procedural language and OOP language And Explain the key features of OOP

1 Procedural language focuses on organizing program statements into procedures or functions Larger programs were either broken into functions or modules whereas in Object Oriented Programming bundles both data and functions into one unitknown as object

2 One of the main problems with Procedural approach for programming was data being completely forgotten The emphasis was on the action and the data was only used in the entire process Whereas in Object Oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data

3 The procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled whereas in OOP language provides this flexibility through the concept of class

4 Another limitation of the procedural languages is that the program model is not closer to real world objects For example if you want to develop a gaming application of car race what data would you use and what functions you would require is difficult questions to answer in a procedural approach In the object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality In the car game example we would create several objects such as player car traffic signal and so on

Key Features of OOP

Encapsulation Data and functions are said to be encapsulated in an single entity as object

Data Hiding The data is said to be hidden thus not allowing accidental modification

Inheritance Inheritance is one of the most powerful feature of Object Oriented Programming Languages that allows you to derive a class from an existing class and inherit all the characteristics and behaviour of the parent class This feature allows easy modification of existing code and also reuse code The ability to reuse components of a program is an important feature for any programming language

16

Polymorphism and Overloading Operator overloading feature allows users to define how basic operators work with objects The operator + will be adding two numbers when used with integer variables However when used with user defined string class + operator may concatenate two strings Similarly same functions with same function name can perform different actions depending upon which object calls the function This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms) Operator overloading is a kind of polymorphism

2 Discuss the constructors and Destructors with suitable example

Constructors Constructors are member functions of a class which have same name as the class name Constructors are called automatically whenever an object of the class is created This feature makes it very useful to initialize the classdata members whenever a new object is created It also can perform any other function that needs to be performed for all the objects of the class without explicitly specifying it

Destructors Destructors on the other hand are also member functions with the same name as class but are prefixed with tilde (~) sign to differentiate it from theconstructor They are invoked automatically whenever the objectrsquos life expires or it is destroyed It can be used to return the memory back to the operating system if the memory was dynamically allocated

The following program implements the constructor and destructors for aclass

constdestcpp includeltiostreamhgtclass sample

privateint datapublicsample()

data=0 coutltltrdquoConstructor invokedrdquoltltendl

~sample()

coutltltrdquoDestructor invokedrdquovoid display()

coutltltrdquoData=rdquoltltdataltltendlvoid main()

sample obj1 objdisplay()

17

If you run the above program you will get the output as follows

Constructor invokedData=0Destructor invoked

When object of sample class object is created automatically the constructor is invoked and data is initialized to zero When the program ends the object is destroyed which invokes the destructor Please note that both the constructor and destructor is declared as public and they have no return value However constructors can have arguments and can be overloaded so that different constructors can be called depending upon the arguments that is passed Destructors on the other hand cannot be overloaded and cannot have any arguments

3 Describe the theory behind Virtual Functions and Polymorphism along with suitable programming examples for each

Virtual Functions Virtual means existing in effect but not in reality Virtual functions are primarily used in inheritance Let us suppose you have a class base as shown in the following program and two classes derv1 and derv2 are publicly derived from class base You would like to create a pointer that points to any of the derived class objects If you create a pointer of derv1 then it can point to derv1 object only Compiler will complain if you assign any other object is assigned to the pointer The solution is to create a pointer to Base class

Even though the address of derived classes is assigned to the pointer the compiler executes the base class function However if the base class function is made virtual we get the desired result In the following program we have made the base class function show() as virtual function by prefixing with the keyword virtual

virtualcpp include ltiostreamhgtclass base

publicvirtual void show() virtual functioncoutltltldquobaserdquoltltendl

class derv1public base

publicvoid show()

coutltltldquoderv1rdquoltltendl

class derv2 public base

publicvoid show()

18

coutltltldquoderv2rdquoltltendl

void main()derv1 dv1derv2 dv2base ptrptr=ampdv1ptr-gtshow()ptr=ampdv2ptr-gtshow()

By declaring the base class function as virtual we now get the output as

derv1derv2

In the above program depending on the contents in the pointer the compiler decides which class function to call during runtime This is known as late binding or dynamic binding

Polymorphism Polymorphism means same content but different forms In C++ polymorphism enables the same program code calling different functions of different classes For Example Letrsquos us suppose that we want to create a class shape and derive classes such as rectangle circle triangle etc Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen You would like to write a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime

Shape ptrarr[100]for (int j=0jltnj++)Ptrarr[j]-gtdraw()

This is an very desirable capability that completely different functions are executed by same function call If the ptrarr is pointing to a rectangle a rectangle is drawn If it is point int to circle circle is drawn

This is exactly what is polymorphism However to implement this approach several conditions should be met Firstly all the classes rectangle circle triangle should be derived from a single class (Here it is shape class) Secondly the draw() function must be declared as virtual in the base class (in the shape class)Operator overloading is a type of polymorphism which allows the same operators to behave differently with different datatypesoperands

4 Discuss the types of Inheritance with suitable example for each

Inheritance is a very powerful featu re of object oriented programming It allows reuse of code without modifying the original code It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort

19

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 14: (MCA) – Semester II MC0066 – OOPS using C++

return 0

To modify the data or to seek to a different position inside the file the c++ file io class fstream provides member functionslike seekg() etc These functions can be used to relocate the record insert position to the desired locations

After all the C++ File IO operations we do a fstreamclose() to close the file pointer This is not mandatory Even if this function is not called by the application the destructor of the fstream class will close the file when the object goes out of scope

8 What is template specialization Describe a scenario in which template class partial specialization is considered appropriate

Class Template Specialization In some cases it is possible to override the template-generated code by providing special definitions for specific types This is called template specialization

The following example defines a template class specialization for template class stream

include ltiostreamgtusing namespace stdtemplate ltclass Tgtclass stream publicvoid f() cout ltlt streamltTgtf()ltlt endl template ltgtclass streamltchargt publicvoid f() cout ltlt streamltchargtf()ltlt endl int main()

streamltintgt sistreamltchargt scsif()scf()return 0

Output

streamltTgtf()streamltchargtf()

In the above example streamltchargt is used as the definition of streams of chars other streams will be handled by the template class generated from the class template

14

Template Class Partial Specialization You may want to generate a specialization of the class for just one parameter for example

base template classtemplatelttypename T1 typename T2gtclass X partial specializationtemplatelttypename T1gtclass XltT1 intgt int main() generates an instantiation from the base templateXltchar chargt xcc generates an instantiation from the partial specializationXltchar intgt xii return 0

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list

15

December 2010

Master of Computer Application (MCA) ndash Semester 2

MC0066 ndash OOPS using C++ ndash 4 Credits

(Book ID B0681 amp B0715)

Assignment Set ndash 2 (40 Marks)

Answer all Questions Each Question carries FIVE Marks

Book ID B0681

1 Distinguished between procedural language and OOP language And Explain the key features of OOP

1 Procedural language focuses on organizing program statements into procedures or functions Larger programs were either broken into functions or modules whereas in Object Oriented Programming bundles both data and functions into one unitknown as object

2 One of the main problems with Procedural approach for programming was data being completely forgotten The emphasis was on the action and the data was only used in the entire process Whereas in Object Oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data

3 The procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled whereas in OOP language provides this flexibility through the concept of class

4 Another limitation of the procedural languages is that the program model is not closer to real world objects For example if you want to develop a gaming application of car race what data would you use and what functions you would require is difficult questions to answer in a procedural approach In the object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality In the car game example we would create several objects such as player car traffic signal and so on

Key Features of OOP

Encapsulation Data and functions are said to be encapsulated in an single entity as object

Data Hiding The data is said to be hidden thus not allowing accidental modification

Inheritance Inheritance is one of the most powerful feature of Object Oriented Programming Languages that allows you to derive a class from an existing class and inherit all the characteristics and behaviour of the parent class This feature allows easy modification of existing code and also reuse code The ability to reuse components of a program is an important feature for any programming language

16

Polymorphism and Overloading Operator overloading feature allows users to define how basic operators work with objects The operator + will be adding two numbers when used with integer variables However when used with user defined string class + operator may concatenate two strings Similarly same functions with same function name can perform different actions depending upon which object calls the function This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms) Operator overloading is a kind of polymorphism

2 Discuss the constructors and Destructors with suitable example

Constructors Constructors are member functions of a class which have same name as the class name Constructors are called automatically whenever an object of the class is created This feature makes it very useful to initialize the classdata members whenever a new object is created It also can perform any other function that needs to be performed for all the objects of the class without explicitly specifying it

Destructors Destructors on the other hand are also member functions with the same name as class but are prefixed with tilde (~) sign to differentiate it from theconstructor They are invoked automatically whenever the objectrsquos life expires or it is destroyed It can be used to return the memory back to the operating system if the memory was dynamically allocated

The following program implements the constructor and destructors for aclass

constdestcpp includeltiostreamhgtclass sample

privateint datapublicsample()

data=0 coutltltrdquoConstructor invokedrdquoltltendl

~sample()

coutltltrdquoDestructor invokedrdquovoid display()

coutltltrdquoData=rdquoltltdataltltendlvoid main()

sample obj1 objdisplay()

17

If you run the above program you will get the output as follows

Constructor invokedData=0Destructor invoked

When object of sample class object is created automatically the constructor is invoked and data is initialized to zero When the program ends the object is destroyed which invokes the destructor Please note that both the constructor and destructor is declared as public and they have no return value However constructors can have arguments and can be overloaded so that different constructors can be called depending upon the arguments that is passed Destructors on the other hand cannot be overloaded and cannot have any arguments

3 Describe the theory behind Virtual Functions and Polymorphism along with suitable programming examples for each

Virtual Functions Virtual means existing in effect but not in reality Virtual functions are primarily used in inheritance Let us suppose you have a class base as shown in the following program and two classes derv1 and derv2 are publicly derived from class base You would like to create a pointer that points to any of the derived class objects If you create a pointer of derv1 then it can point to derv1 object only Compiler will complain if you assign any other object is assigned to the pointer The solution is to create a pointer to Base class

Even though the address of derived classes is assigned to the pointer the compiler executes the base class function However if the base class function is made virtual we get the desired result In the following program we have made the base class function show() as virtual function by prefixing with the keyword virtual

virtualcpp include ltiostreamhgtclass base

publicvirtual void show() virtual functioncoutltltldquobaserdquoltltendl

class derv1public base

publicvoid show()

coutltltldquoderv1rdquoltltendl

class derv2 public base

publicvoid show()

18

coutltltldquoderv2rdquoltltendl

void main()derv1 dv1derv2 dv2base ptrptr=ampdv1ptr-gtshow()ptr=ampdv2ptr-gtshow()

By declaring the base class function as virtual we now get the output as

derv1derv2

In the above program depending on the contents in the pointer the compiler decides which class function to call during runtime This is known as late binding or dynamic binding

Polymorphism Polymorphism means same content but different forms In C++ polymorphism enables the same program code calling different functions of different classes For Example Letrsquos us suppose that we want to create a class shape and derive classes such as rectangle circle triangle etc Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen You would like to write a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime

Shape ptrarr[100]for (int j=0jltnj++)Ptrarr[j]-gtdraw()

This is an very desirable capability that completely different functions are executed by same function call If the ptrarr is pointing to a rectangle a rectangle is drawn If it is point int to circle circle is drawn

This is exactly what is polymorphism However to implement this approach several conditions should be met Firstly all the classes rectangle circle triangle should be derived from a single class (Here it is shape class) Secondly the draw() function must be declared as virtual in the base class (in the shape class)Operator overloading is a type of polymorphism which allows the same operators to behave differently with different datatypesoperands

4 Discuss the types of Inheritance with suitable example for each

Inheritance is a very powerful featu re of object oriented programming It allows reuse of code without modifying the original code It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort

19

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 15: (MCA) – Semester II MC0066 – OOPS using C++

Template Class Partial Specialization You may want to generate a specialization of the class for just one parameter for example

base template classtemplatelttypename T1 typename T2gtclass X partial specializationtemplatelttypename T1gtclass XltT1 intgt int main() generates an instantiation from the base templateXltchar chargt xcc generates an instantiation from the partial specializationXltchar intgt xii return 0

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list

15

December 2010

Master of Computer Application (MCA) ndash Semester 2

MC0066 ndash OOPS using C++ ndash 4 Credits

(Book ID B0681 amp B0715)

Assignment Set ndash 2 (40 Marks)

Answer all Questions Each Question carries FIVE Marks

Book ID B0681

1 Distinguished between procedural language and OOP language And Explain the key features of OOP

1 Procedural language focuses on organizing program statements into procedures or functions Larger programs were either broken into functions or modules whereas in Object Oriented Programming bundles both data and functions into one unitknown as object

2 One of the main problems with Procedural approach for programming was data being completely forgotten The emphasis was on the action and the data was only used in the entire process Whereas in Object Oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data

3 The procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled whereas in OOP language provides this flexibility through the concept of class

4 Another limitation of the procedural languages is that the program model is not closer to real world objects For example if you want to develop a gaming application of car race what data would you use and what functions you would require is difficult questions to answer in a procedural approach In the object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality In the car game example we would create several objects such as player car traffic signal and so on

Key Features of OOP

Encapsulation Data and functions are said to be encapsulated in an single entity as object

Data Hiding The data is said to be hidden thus not allowing accidental modification

Inheritance Inheritance is one of the most powerful feature of Object Oriented Programming Languages that allows you to derive a class from an existing class and inherit all the characteristics and behaviour of the parent class This feature allows easy modification of existing code and also reuse code The ability to reuse components of a program is an important feature for any programming language

16

Polymorphism and Overloading Operator overloading feature allows users to define how basic operators work with objects The operator + will be adding two numbers when used with integer variables However when used with user defined string class + operator may concatenate two strings Similarly same functions with same function name can perform different actions depending upon which object calls the function This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms) Operator overloading is a kind of polymorphism

2 Discuss the constructors and Destructors with suitable example

Constructors Constructors are member functions of a class which have same name as the class name Constructors are called automatically whenever an object of the class is created This feature makes it very useful to initialize the classdata members whenever a new object is created It also can perform any other function that needs to be performed for all the objects of the class without explicitly specifying it

Destructors Destructors on the other hand are also member functions with the same name as class but are prefixed with tilde (~) sign to differentiate it from theconstructor They are invoked automatically whenever the objectrsquos life expires or it is destroyed It can be used to return the memory back to the operating system if the memory was dynamically allocated

The following program implements the constructor and destructors for aclass

constdestcpp includeltiostreamhgtclass sample

privateint datapublicsample()

data=0 coutltltrdquoConstructor invokedrdquoltltendl

~sample()

coutltltrdquoDestructor invokedrdquovoid display()

coutltltrdquoData=rdquoltltdataltltendlvoid main()

sample obj1 objdisplay()

17

If you run the above program you will get the output as follows

Constructor invokedData=0Destructor invoked

When object of sample class object is created automatically the constructor is invoked and data is initialized to zero When the program ends the object is destroyed which invokes the destructor Please note that both the constructor and destructor is declared as public and they have no return value However constructors can have arguments and can be overloaded so that different constructors can be called depending upon the arguments that is passed Destructors on the other hand cannot be overloaded and cannot have any arguments

3 Describe the theory behind Virtual Functions and Polymorphism along with suitable programming examples for each

Virtual Functions Virtual means existing in effect but not in reality Virtual functions are primarily used in inheritance Let us suppose you have a class base as shown in the following program and two classes derv1 and derv2 are publicly derived from class base You would like to create a pointer that points to any of the derived class objects If you create a pointer of derv1 then it can point to derv1 object only Compiler will complain if you assign any other object is assigned to the pointer The solution is to create a pointer to Base class

Even though the address of derived classes is assigned to the pointer the compiler executes the base class function However if the base class function is made virtual we get the desired result In the following program we have made the base class function show() as virtual function by prefixing with the keyword virtual

virtualcpp include ltiostreamhgtclass base

publicvirtual void show() virtual functioncoutltltldquobaserdquoltltendl

class derv1public base

publicvoid show()

coutltltldquoderv1rdquoltltendl

class derv2 public base

publicvoid show()

18

coutltltldquoderv2rdquoltltendl

void main()derv1 dv1derv2 dv2base ptrptr=ampdv1ptr-gtshow()ptr=ampdv2ptr-gtshow()

By declaring the base class function as virtual we now get the output as

derv1derv2

In the above program depending on the contents in the pointer the compiler decides which class function to call during runtime This is known as late binding or dynamic binding

Polymorphism Polymorphism means same content but different forms In C++ polymorphism enables the same program code calling different functions of different classes For Example Letrsquos us suppose that we want to create a class shape and derive classes such as rectangle circle triangle etc Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen You would like to write a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime

Shape ptrarr[100]for (int j=0jltnj++)Ptrarr[j]-gtdraw()

This is an very desirable capability that completely different functions are executed by same function call If the ptrarr is pointing to a rectangle a rectangle is drawn If it is point int to circle circle is drawn

This is exactly what is polymorphism However to implement this approach several conditions should be met Firstly all the classes rectangle circle triangle should be derived from a single class (Here it is shape class) Secondly the draw() function must be declared as virtual in the base class (in the shape class)Operator overloading is a type of polymorphism which allows the same operators to behave differently with different datatypesoperands

4 Discuss the types of Inheritance with suitable example for each

Inheritance is a very powerful featu re of object oriented programming It allows reuse of code without modifying the original code It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort

19

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 16: (MCA) – Semester II MC0066 – OOPS using C++

December 2010

Master of Computer Application (MCA) ndash Semester 2

MC0066 ndash OOPS using C++ ndash 4 Credits

(Book ID B0681 amp B0715)

Assignment Set ndash 2 (40 Marks)

Answer all Questions Each Question carries FIVE Marks

Book ID B0681

1 Distinguished between procedural language and OOP language And Explain the key features of OOP

1 Procedural language focuses on organizing program statements into procedures or functions Larger programs were either broken into functions or modules whereas in Object Oriented Programming bundles both data and functions into one unitknown as object

2 One of the main problems with Procedural approach for programming was data being completely forgotten The emphasis was on the action and the data was only used in the entire process Whereas in Object Oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data

3 The procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled whereas in OOP language provides this flexibility through the concept of class

4 Another limitation of the procedural languages is that the program model is not closer to real world objects For example if you want to develop a gaming application of car race what data would you use and what functions you would require is difficult questions to answer in a procedural approach In the object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality In the car game example we would create several objects such as player car traffic signal and so on

Key Features of OOP

Encapsulation Data and functions are said to be encapsulated in an single entity as object

Data Hiding The data is said to be hidden thus not allowing accidental modification

Inheritance Inheritance is one of the most powerful feature of Object Oriented Programming Languages that allows you to derive a class from an existing class and inherit all the characteristics and behaviour of the parent class This feature allows easy modification of existing code and also reuse code The ability to reuse components of a program is an important feature for any programming language

16

Polymorphism and Overloading Operator overloading feature allows users to define how basic operators work with objects The operator + will be adding two numbers when used with integer variables However when used with user defined string class + operator may concatenate two strings Similarly same functions with same function name can perform different actions depending upon which object calls the function This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms) Operator overloading is a kind of polymorphism

2 Discuss the constructors and Destructors with suitable example

Constructors Constructors are member functions of a class which have same name as the class name Constructors are called automatically whenever an object of the class is created This feature makes it very useful to initialize the classdata members whenever a new object is created It also can perform any other function that needs to be performed for all the objects of the class without explicitly specifying it

Destructors Destructors on the other hand are also member functions with the same name as class but are prefixed with tilde (~) sign to differentiate it from theconstructor They are invoked automatically whenever the objectrsquos life expires or it is destroyed It can be used to return the memory back to the operating system if the memory was dynamically allocated

The following program implements the constructor and destructors for aclass

constdestcpp includeltiostreamhgtclass sample

privateint datapublicsample()

data=0 coutltltrdquoConstructor invokedrdquoltltendl

~sample()

coutltltrdquoDestructor invokedrdquovoid display()

coutltltrdquoData=rdquoltltdataltltendlvoid main()

sample obj1 objdisplay()

17

If you run the above program you will get the output as follows

Constructor invokedData=0Destructor invoked

When object of sample class object is created automatically the constructor is invoked and data is initialized to zero When the program ends the object is destroyed which invokes the destructor Please note that both the constructor and destructor is declared as public and they have no return value However constructors can have arguments and can be overloaded so that different constructors can be called depending upon the arguments that is passed Destructors on the other hand cannot be overloaded and cannot have any arguments

3 Describe the theory behind Virtual Functions and Polymorphism along with suitable programming examples for each

Virtual Functions Virtual means existing in effect but not in reality Virtual functions are primarily used in inheritance Let us suppose you have a class base as shown in the following program and two classes derv1 and derv2 are publicly derived from class base You would like to create a pointer that points to any of the derived class objects If you create a pointer of derv1 then it can point to derv1 object only Compiler will complain if you assign any other object is assigned to the pointer The solution is to create a pointer to Base class

Even though the address of derived classes is assigned to the pointer the compiler executes the base class function However if the base class function is made virtual we get the desired result In the following program we have made the base class function show() as virtual function by prefixing with the keyword virtual

virtualcpp include ltiostreamhgtclass base

publicvirtual void show() virtual functioncoutltltldquobaserdquoltltendl

class derv1public base

publicvoid show()

coutltltldquoderv1rdquoltltendl

class derv2 public base

publicvoid show()

18

coutltltldquoderv2rdquoltltendl

void main()derv1 dv1derv2 dv2base ptrptr=ampdv1ptr-gtshow()ptr=ampdv2ptr-gtshow()

By declaring the base class function as virtual we now get the output as

derv1derv2

In the above program depending on the contents in the pointer the compiler decides which class function to call during runtime This is known as late binding or dynamic binding

Polymorphism Polymorphism means same content but different forms In C++ polymorphism enables the same program code calling different functions of different classes For Example Letrsquos us suppose that we want to create a class shape and derive classes such as rectangle circle triangle etc Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen You would like to write a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime

Shape ptrarr[100]for (int j=0jltnj++)Ptrarr[j]-gtdraw()

This is an very desirable capability that completely different functions are executed by same function call If the ptrarr is pointing to a rectangle a rectangle is drawn If it is point int to circle circle is drawn

This is exactly what is polymorphism However to implement this approach several conditions should be met Firstly all the classes rectangle circle triangle should be derived from a single class (Here it is shape class) Secondly the draw() function must be declared as virtual in the base class (in the shape class)Operator overloading is a type of polymorphism which allows the same operators to behave differently with different datatypesoperands

4 Discuss the types of Inheritance with suitable example for each

Inheritance is a very powerful featu re of object oriented programming It allows reuse of code without modifying the original code It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort

19

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 17: (MCA) – Semester II MC0066 – OOPS using C++

Polymorphism and Overloading Operator overloading feature allows users to define how basic operators work with objects The operator + will be adding two numbers when used with integer variables However when used with user defined string class + operator may concatenate two strings Similarly same functions with same function name can perform different actions depending upon which object calls the function This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms) Operator overloading is a kind of polymorphism

2 Discuss the constructors and Destructors with suitable example

Constructors Constructors are member functions of a class which have same name as the class name Constructors are called automatically whenever an object of the class is created This feature makes it very useful to initialize the classdata members whenever a new object is created It also can perform any other function that needs to be performed for all the objects of the class without explicitly specifying it

Destructors Destructors on the other hand are also member functions with the same name as class but are prefixed with tilde (~) sign to differentiate it from theconstructor They are invoked automatically whenever the objectrsquos life expires or it is destroyed It can be used to return the memory back to the operating system if the memory was dynamically allocated

The following program implements the constructor and destructors for aclass

constdestcpp includeltiostreamhgtclass sample

privateint datapublicsample()

data=0 coutltltrdquoConstructor invokedrdquoltltendl

~sample()

coutltltrdquoDestructor invokedrdquovoid display()

coutltltrdquoData=rdquoltltdataltltendlvoid main()

sample obj1 objdisplay()

17

If you run the above program you will get the output as follows

Constructor invokedData=0Destructor invoked

When object of sample class object is created automatically the constructor is invoked and data is initialized to zero When the program ends the object is destroyed which invokes the destructor Please note that both the constructor and destructor is declared as public and they have no return value However constructors can have arguments and can be overloaded so that different constructors can be called depending upon the arguments that is passed Destructors on the other hand cannot be overloaded and cannot have any arguments

3 Describe the theory behind Virtual Functions and Polymorphism along with suitable programming examples for each

Virtual Functions Virtual means existing in effect but not in reality Virtual functions are primarily used in inheritance Let us suppose you have a class base as shown in the following program and two classes derv1 and derv2 are publicly derived from class base You would like to create a pointer that points to any of the derived class objects If you create a pointer of derv1 then it can point to derv1 object only Compiler will complain if you assign any other object is assigned to the pointer The solution is to create a pointer to Base class

Even though the address of derived classes is assigned to the pointer the compiler executes the base class function However if the base class function is made virtual we get the desired result In the following program we have made the base class function show() as virtual function by prefixing with the keyword virtual

virtualcpp include ltiostreamhgtclass base

publicvirtual void show() virtual functioncoutltltldquobaserdquoltltendl

class derv1public base

publicvoid show()

coutltltldquoderv1rdquoltltendl

class derv2 public base

publicvoid show()

18

coutltltldquoderv2rdquoltltendl

void main()derv1 dv1derv2 dv2base ptrptr=ampdv1ptr-gtshow()ptr=ampdv2ptr-gtshow()

By declaring the base class function as virtual we now get the output as

derv1derv2

In the above program depending on the contents in the pointer the compiler decides which class function to call during runtime This is known as late binding or dynamic binding

Polymorphism Polymorphism means same content but different forms In C++ polymorphism enables the same program code calling different functions of different classes For Example Letrsquos us suppose that we want to create a class shape and derive classes such as rectangle circle triangle etc Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen You would like to write a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime

Shape ptrarr[100]for (int j=0jltnj++)Ptrarr[j]-gtdraw()

This is an very desirable capability that completely different functions are executed by same function call If the ptrarr is pointing to a rectangle a rectangle is drawn If it is point int to circle circle is drawn

This is exactly what is polymorphism However to implement this approach several conditions should be met Firstly all the classes rectangle circle triangle should be derived from a single class (Here it is shape class) Secondly the draw() function must be declared as virtual in the base class (in the shape class)Operator overloading is a type of polymorphism which allows the same operators to behave differently with different datatypesoperands

4 Discuss the types of Inheritance with suitable example for each

Inheritance is a very powerful featu re of object oriented programming It allows reuse of code without modifying the original code It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort

19

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 18: (MCA) – Semester II MC0066 – OOPS using C++

If you run the above program you will get the output as follows

Constructor invokedData=0Destructor invoked

When object of sample class object is created automatically the constructor is invoked and data is initialized to zero When the program ends the object is destroyed which invokes the destructor Please note that both the constructor and destructor is declared as public and they have no return value However constructors can have arguments and can be overloaded so that different constructors can be called depending upon the arguments that is passed Destructors on the other hand cannot be overloaded and cannot have any arguments

3 Describe the theory behind Virtual Functions and Polymorphism along with suitable programming examples for each

Virtual Functions Virtual means existing in effect but not in reality Virtual functions are primarily used in inheritance Let us suppose you have a class base as shown in the following program and two classes derv1 and derv2 are publicly derived from class base You would like to create a pointer that points to any of the derived class objects If you create a pointer of derv1 then it can point to derv1 object only Compiler will complain if you assign any other object is assigned to the pointer The solution is to create a pointer to Base class

Even though the address of derived classes is assigned to the pointer the compiler executes the base class function However if the base class function is made virtual we get the desired result In the following program we have made the base class function show() as virtual function by prefixing with the keyword virtual

virtualcpp include ltiostreamhgtclass base

publicvirtual void show() virtual functioncoutltltldquobaserdquoltltendl

class derv1public base

publicvoid show()

coutltltldquoderv1rdquoltltendl

class derv2 public base

publicvoid show()

18

coutltltldquoderv2rdquoltltendl

void main()derv1 dv1derv2 dv2base ptrptr=ampdv1ptr-gtshow()ptr=ampdv2ptr-gtshow()

By declaring the base class function as virtual we now get the output as

derv1derv2

In the above program depending on the contents in the pointer the compiler decides which class function to call during runtime This is known as late binding or dynamic binding

Polymorphism Polymorphism means same content but different forms In C++ polymorphism enables the same program code calling different functions of different classes For Example Letrsquos us suppose that we want to create a class shape and derive classes such as rectangle circle triangle etc Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen You would like to write a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime

Shape ptrarr[100]for (int j=0jltnj++)Ptrarr[j]-gtdraw()

This is an very desirable capability that completely different functions are executed by same function call If the ptrarr is pointing to a rectangle a rectangle is drawn If it is point int to circle circle is drawn

This is exactly what is polymorphism However to implement this approach several conditions should be met Firstly all the classes rectangle circle triangle should be derived from a single class (Here it is shape class) Secondly the draw() function must be declared as virtual in the base class (in the shape class)Operator overloading is a type of polymorphism which allows the same operators to behave differently with different datatypesoperands

4 Discuss the types of Inheritance with suitable example for each

Inheritance is a very powerful featu re of object oriented programming It allows reuse of code without modifying the original code It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort

19

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 19: (MCA) – Semester II MC0066 – OOPS using C++

coutltltldquoderv2rdquoltltendl

void main()derv1 dv1derv2 dv2base ptrptr=ampdv1ptr-gtshow()ptr=ampdv2ptr-gtshow()

By declaring the base class function as virtual we now get the output as

derv1derv2

In the above program depending on the contents in the pointer the compiler decides which class function to call during runtime This is known as late binding or dynamic binding

Polymorphism Polymorphism means same content but different forms In C++ polymorphism enables the same program code calling different functions of different classes For Example Letrsquos us suppose that we want to create a class shape and derive classes such as rectangle circle triangle etc Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen You would like to write a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime

Shape ptrarr[100]for (int j=0jltnj++)Ptrarr[j]-gtdraw()

This is an very desirable capability that completely different functions are executed by same function call If the ptrarr is pointing to a rectangle a rectangle is drawn If it is point int to circle circle is drawn

This is exactly what is polymorphism However to implement this approach several conditions should be met Firstly all the classes rectangle circle triangle should be derived from a single class (Here it is shape class) Secondly the draw() function must be declared as virtual in the base class (in the shape class)Operator overloading is a type of polymorphism which allows the same operators to behave differently with different datatypesoperands

4 Discuss the types of Inheritance with suitable example for each

Inheritance is a very powerful featu re of object oriented programming It allows reuse of code without modifying the original code It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort

19

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 20: (MCA) – Semester II MC0066 – OOPS using C++

Types Of Inheritance

Hierarchical Inheritance We can have several classes derived from a single base class like shown below

Multi Level Inheritance Inheritance can also be multilevel inheritance where another class is derived from the derived class In such case the grand child class inherits all the properties of child and parent classes

Multiple Inheritance Multiple Inheritance is the process of inheriting a class from more than one parent class This would be required in several instances where you would like to have the functionalities of several classes This is also extensively used in class libraries

The syntax for implementing multiple inheritance is similar to single inheritance Let us suppose there are two classes A and B you want to derive a class C from A and B The syntax of class definition will be asfollows

class C public A public B

Book ID B0715

5 Write about the following with the help of suitable programming examples A) Throwing an Exception B) Catching an Exception

Throwing an Exception If you encounter an exceptional situation in your code ndash that is one where you donrsquot have enough information in the current context to decide what to do ndash you can send information about the error into a larger context by creating an object containing that information and ldquothrowingrdquo it out of yourcurrent context This is called throwing an exception Herersquos what it lookslike

throw myerror(ldquosomething bad happenedrdquo)

myerror is an ordinary class which takes a char as its argument You can use any type when you throw (including built-in types) but often yoursquoll use special types created just for throwing exceptions The keyword throw causes a number of relatively things to happen First it creates an object that isnrsquot there under normal program execution and of course the constructor is called for that object Then the object is in effect ldquoreturnedrdquo from the function even though that object type isnrsquot normally what the function is designed to return

Catching an Exception If a function throws an exception it must assume that exception is caught and dealt with The advantages of C++ exception handling is that it allows you to concentrate on the problem yoursquore actually trying to solve in one place and then deal with the errors from that code in another place

The try block If yoursquore inside a function and you throw an exception (or a called function throws an exception) that function will exit in the process of throwing If you donrsquot want a throw to leave a function you can set up a special block within the function where you try to solve your actual programming problem (and potentially generate exceptions) This is called the try block because you try your various function calls there The try block is an ordinary scope preceded by the keyword try

20

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 21: (MCA) – Semester II MC0066 – OOPS using C++

try Code that may generate exceptions

6 Write about the following with the help of suitable programming examples A) Uncaught Exceptions B) Standard Exceptions

Uncaught Exceptions If none of the exception handlers following a particular try block matches an exception that exception moves to the next-higher context that is the function or try block surrounding the try block that failed to catch the exception (The location of this higher-context try block is not always obvious at first glance) This process continues until at some level a handler matches the exception At that point the exception is considered ldquocaughtrdquo and no further searching occurs If no handler at any level catches the exception it is ldquouncaughtrdquo or ldquounhandledrdquo An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler ndash the most common reason for this is that the constructor for the exception object itself causes a new exception

Functions which can be used for Uncaught Exceptions

terminate() This is the special function which will be called automatically if an exception is uncaught It is actually pointer to a function and its default value is the Standard C library function abort() which immediately exits the program with no calls to the normal termination functions (which means that destructors for global and static objects might not be called)

set_terminate() You can install your own terminate() function using the standardset_terminate() function which returns a pointer to the terminate() function you are replacing so you can restore it later if you want Your custom terminate() must take no arguments and have a void return value In addition any terminate() handler you install must not return or throw an exception but instead must call some sort of program-termination function If terminate() is called it means the problem is unrecoverable

Standard Exceptions The set of exceptions used with the Standard C++ library are also available for your own use Generally itrsquos easier and faster to start with a standard exception class than to try to define your own If the standard class doesnrsquotdo what you need you can derive from it

The following are details for some of the standard exceptions

Exception The base class for all the exceptions thrown by the C++ standard library You can ask what() and get a result that can be displayed as a character representation

logic_error Derived from exception Reports program logic errors which could presumably be detected before the program executes

runtime_error Derived from exception Reports runtime errors which can presumably be detected only when the program executes

domain_error Reports violations of a precondition

invalid_argument Indicates an invalid argument to the function itrsquos thrown from

21

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 22: (MCA) – Semester II MC0066 – OOPS using C++

length_error Indicates an attempt to produce an object whose length is greater than or equal to NPOS (the largest representable value of type size_t)

7 Write the advantages of using exception handling with its basic models

There are two basic models in Exception-Handling theory

Termination In termination (which is what C++ supports) you assume the error is so critical therersquos no way to get back to where the exception occurred Whoever threw the exception decided there was no way to salvage the situation and they donrsquot want to come back

Resumption It means the exception handler is expected to do something to rectify the situation and then the faulting function is retried presuming success the second time If you want resumption you still hope to continue execution after the exception is handled so your exception is more like a function call ndash which is how you should set up situations in C++ where you want resumption-like behavior (that is donrsquot throw an exception call a function that fixes the problem) Alternatively place your try block inside a while loop that keeps reentering the try block until the result is satisfactory

Historically programmers using operating systems that supported resumptive exception handling eventually ended up using termination-like code and skipping resumption So although resumption sounds attractive at first it seems it isnrsquot quite so useful in practice One reason may be the distance that can occur between the exception and its handler itrsquos one thing to terminate to a handler thatrsquos far away but to jump to that handler and then back again may be too conceptually difficult for large systems where the exception can be generated from many points

8 Describe the time overhead of operations on sequence containers

Sequence Containers There are three types of sequence containers to store data in linear sequence These are the vector deque and list

vectorltTypegtdequeltTypegtlistltTypegt

To choose a container decide what sort of operations you will most frequently perform on your data and then use the following table to help in choosing

Time overhead of operations on sequence containers

Operation Vector Deque ListAccess first Element constant constant constantAccess last element constant constant constant

Access random element constant constant linearAdddelete at beginning linear constant constant

Adddelete at end constant constant constantAdddelete at random linear linear constant

22

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23

Page 23: (MCA) – Semester II MC0066 – OOPS using C++

Each container has attributes suited to particular applications

Vector The vector class is similar to an array and is able to access elements at any position with a constant time overhead O(1) Insertion and deletion at the end of vector is ldquocheaprdquo Memory overhead of a vector is very low and comparable to a normal array

Deque The double ended queue has similar properties to a vector but as the name suggests you can efficiently insert or delete elements at either end Deque like vector is not very good at inserting or deleting elements at random positions but it does allow random access to elements using the array like syntax

List Lists donrsquot provide access like an array or vector but are suited to applications where you want to add or remove elements to or from the middle They are implemented as double linked lists structures in order to support bidirectional iterators

23