30
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. 1

MC0066

Embed Size (px)

Citation preview

Page 1: MC0066

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

Page 2: MC0066

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 1;statement 2;}else{ statement 3;statement 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.

// evenodd.cpp# include <iostream.h># include <conio.h>void main(){

int num;cout<<”Please enter a number”<<endl;cin>>num;

2

Page 3: MC0066

If ((num%2) == 0)cout<<num <<” is a even number”;elsecout<<num <<” is a odd number”;getch();

}

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 multipleconditions.If (condition1){ if (condition 2)

{ statement1;Statement2;

}else if (condition3)

{statement3;

}}else statement4;

Example :

//Large.cpp# include <iostream.h>void main(){

int a,b,c;cout<<”Please enter three numbers”;cin>>a>>b>>c;If ((a>b) && (b>c))

cout<<a<< “ is the largest number”;else if ((b>a) && (b>c))

cout<<b<< “ is the largest number”;else if ((c>a) && (c>b))

cout<<c<< “ is the largest number”;}

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

Page 4: MC0066

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: statement1;break;case value2: statement2;break;case value3: statement3;break;default: statement4;

}

Example :

# include<iostream.h>void main(){

char pos;int x=15, y=15;cout << “ you are currently located at” <<x<<” “<<y<<endl;cout>>”please choose the letter to move l for left, r for right, u for up and d for down” <<endl;cin>>pos;switch (pos){ case ‘l’: x--;break;case ‘r’: x++;break;case ‘u’: y++;break;case ‘d’: y--;break;default: cout<<”You selected a wrong option”;}cout<<“ you are now located at” <<x<<” “<<y;

}

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

Page 5: MC0066

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 way:if (a>b)large=a;elselarge=b;

The above can be done using conditional operator in the following way: large= (a>b) ? 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 "matrixMultiply.h"#include <iostream>#include <stdlib.h>

using namespace std;

int main(){

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

for(int i = 0; i < 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 < rows2; i++){

matrixTwo[i] = (float*)malloc(columns2 * sizeof(float));}

matrixTwo[0][0] = 1;

5

Page 6: MC0066

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

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

for(int i = 0; i < rows1; i++){

result[i] = (float*)malloc(columns2 * sizeof(float));}

matrixMultiply(3,1,matrixOne,1,3,matrixTwo, result);

return 0;

}

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

if( cols1 != rows2 ){

cout << "Can't Multiply These Matricies!";

} else{

float tempResult;

for (int i=0;i<rows1;i++) {

for(int j=0;j<cols2;j++) {

tempResult = 0;

for(int k=0;k<rows2;k++) { tempResult += mat1[i][k]*mat2[k][j];

}

result[i][j] = tempResult;

}

6

Page 7: MC0066

}

}

}

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 d3.add(d1,d2). 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 :

//unary.cpp# include <iostream.h>class counter{

unsigned int count;public:counter(){count=0;}int getcount()

7

Page 8: MC0066

{return count;

}void operator ++(){ count++;}

};void main(){

counter c1;c1++;++c1;cout<<c1.getcount();

}

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 <iostream>using namespace std;struct A {

void f() {

cout << "Class A" << endl; }

};struct B: A {

void f() {

cout << "Class B" << endl; }

};

8

Page 9: MC0066

void g(A& arg) {

arg.f();}

int main() {

B x;g(x);

}

The following is the output of the above example:

Class A

When function g() is called, function A::f() 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 A::f() is declared with the virtual keyword:

#include <iostream>using namespace std;struct A {

virtual void f() {

cout << "Class A" << endl; }

};struct B: A {

void f() {

cout << "Class B" << endl; }

};void g(A& arg ) {

arg.f();}

int main() {

B x;g(x);

}

9

Page 10: MC0066

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 functions#include <iostream>using namespace std;class CRectangle {

int width, height;public:void set_values (int, int);int area () {return (width * height);}friend CRectangle duplicate (CRectangle);

};void CRectangle::set_values (int a, int b) {

width = a;height = b;

}CRectangle duplicate (CRectangle rectparam) {

CRectangle rectres;rectres.width = rectparam.width*2;rectres.height = rectparam.height*2;return (rectres);

}int main () {CRectangle rect, rectb;rect.set_values (2,3);rectb = duplicate (rect);cout << rectb.area();

return 0;}

10

Page 11: MC0066

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 isn't! 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 class.class CPointCollection;// Point class.class CPoint {friend CPointCollection;private:double m_x;double m_y;public:CPoint(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 {private:vector<CPoint> m_vecPoints;public:CPointCollection(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

Page 12: MC0066

void CPointCollection::set(const double x, const double y) {// Get the number of elements in the collection.const int nElements = m_vecPoints.size();// Set each element.for(int i=0; i<nElements; i++) {m_vecPoints[i].m_x = x;m_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 I/O classes simplify such file read/write operations for the programmer by providing easier to use classes.

C++ File I/O Classes and Functions:

There are 3 File I/O classes in C++ which are used for File Read/Write operations. They areifstream         -  Can be used for File read/input operationsofstream        -  Can be used for File write/output operationsfstream          -  Can be used for both read/write c++ file I/O operations

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

fstream::open method - to open the filefstream::Operator >> and fstream::Operator <<  - For reading from or writing to the file.fstream::close  - 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 i/o stream operators to read data from a file. In all cases the header file fstream.h must be included.

#include<fstream.h>    int main()    {        char str[2000];

12

Page 13: MC0066

        fstream file_op("c:\\test_file.txt",ios::in);        while(file_op >> str)        cout << str ;         file_op.close();

        return 0;    }

The class fstream, which is used above is the one which is commonly used for c++ file i/o 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 fstream::get() or fstream::getline() methods. Here is the example for using fstream getline method.

#include <fstream.h>    int main()    {        char str[2000];        fstream file_op("c:\\test_file.txt",ios::in);        while(!file_op.eof())         {              file_op.getline(str,2000);              cout <<str;        }         file_op.close();        cout <<endl;

        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 <fstream.h>    int main()    {        fstream file_op("c:\\CoderSource_file.txt",ios::out);

        file_op<<"Test Write to file";        file_op.close();

13

Page 14: MC0066

        return 0;    }

To modify the data or to seek to a different position inside the file, the c++ file i/o 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 I/O operations we do a fstream::close(), 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 <iostream>using namespace std;template <class T>class stream {public:void f() { cout << "stream<T>::f()"<< endl; }};template <>class stream<char> {public:void f() { cout << "stream<char>::f()"<< endl; }};int main() {

stream<int> si;stream<char> sc;si.f();sc.f();return 0;}

Output:

stream<T>::f()stream<char>::f()

In the above example, stream<char> is used as the definition of streams of chars; other streams will be handled by the template class generated from the class template

14

Page 15: MC0066

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

//base template classtemplate<typename T1, typename T2>class X {};//partial specializationtemplate<typename T1>class X<T1, int> {};int main() {// generates an instantiation from the base templateX<char, char> xcc ;//generates an instantiation from the partial specializationX<char, int> 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

Page 16: MC0066

December 2010

Master of Computer Application (MCA) – Semester 2

MC0066 – OOPS using C++ – 4 Credits

(Book ID: B0681 & B0715)

Assignment Set – 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

Page 17: MC0066

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 object’s 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

// constdest.cpp# include<iostream.h>class sample{

private:int data;public:sample() {

data=0; cout<<”Constructor invoked”<<endl;

}~sample() {

cout<<”Destructor invoked”;}void display(){

cout<<”Data=”<<data<<endl;}};void main(){

sample obj1; obj.display();

}

17

Page 18: MC0066

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.

//virtual.cpp# include <iostream.h>class base{

public:virtual void show() // virtual function{cout<<“base”<<endl;}

};class derv1:public base{

public:void show(){

cout<<“derv1”<<endl;}

};class derv2: public base{

public:void show(){

18

Page 19: MC0066

cout<<“derv2”<<endl;}

};void main(){derv1 dv1;derv2 dv2;base *ptr;ptr=&dv1;ptr->show();ptr=&dv2;ptr->show();}

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 :Let’s 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=0;j<n;j++)Ptrarr[j]->draw();

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 datatypes/operands.

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

Page 20: MC0066

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 – that is, one where you don’t have enough information in the current context to decide what to do – you can send information about the error into a larger context by creating an object containing that information and “throwing” it out of yourcurrent context. This is called throwing an exception. Here’s what it lookslike:

throw myerror(“something bad happened”);

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 you’ll 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 isn’t there under normal program execution, and of course the constructor is called for that object. Then the object is, in effect, “returned” from the function, even though that object type isn’t 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 you’re actually trying to solve in one place, and then deal with the errors from that code in another place.

The try block : If you’re 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 don’t 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

Page 21: MC0066

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 “caught,” and no further searching occurs. If no handler at any level catches the exception, it is “uncaught” or “unhandled.” An uncaught exception also occurs if a new exception is thrown before an existing exception reaches its handler – 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 it’s easier and faster to start with a standard exception class than to try to define your own. If the standard class doesn’tdo 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 it’s thrown from.

21

Page 22: MC0066

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 there’s 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 don’t 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 – which is how you should set up situations in C++ where you want resumption-like behavior (that is, don’t 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 isn’t quite so useful in practice. One reason may be the distance that can occur between the exception and its handler; it’s one thing to terminate to a handler that’s 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 :

vector<Type>deque<Type>list<Type>

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 linearAdd/delete at beginning linear constant constant

Add/delete at end constant constant constantAdd/delete at random linear linear constant

22

Page 23: MC0066

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 “cheap” . 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 don’t 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