Object Oriented Programming 2075

Tribhuwan University
Institute of Science and Technology
2075
Bachelor Level / Second Semester / Science
Computer Science and Information Technology ( CSC161 )
( Object Oriented Programming )
Full Marks: 60
Pass Marks: 24
Time: 3 hours
Candidates are required to give their answers in their own words as far as practicable.
The figures in the margin indicate full marks.

Long answer question:

Group A

Attempt any two questions:(2 x 10 = 20)

1. Explain the concept of user-defined to user-defined data conversion rotine located in the destination class.

10 marks view

2. Depict the difference between private and public derivation. Explain derived class constructor with suitable program.

10 marks view

3. Briefly explain the hierarchy of stream classes. Write a program that overloads extraction and insertion operators.

10 marks view

Short answer questions:

Group B

Attempt any eight questions:(8 x 5 = 40)

4. Write a member function called reverseit() that reverses a string ( an array of character). Use a for loop that swaps the first and last characters, then the second and next-to last characters and so on. The string should be passed to reverseit() as an argument.

5 marks view

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<string.h>

void reverseit(char *p)

{

    int j;

    int len=strlen(p);

    j=len-1;

    for(int i=0;i<len/2;i++)

    {

      char a=p[i];

      p[i]=p[j];

      p[j]=a;

      j--;

    }

    cout<<"\\n\\nReverse Of String :";

    puts(p);

}

void main()

{

    char str[50];

    char ch;

    cout<<"\\nEnter The String : ";

    gets(str);

    reverseit(str);

    getch();

}

5. What is the principle reason for using default arguments in the function? Explain how missing arguments and default arguments are handled by the function simultaneously?

5 marks view

A function can be called without specifying all its arguments. But it does not work on any general function. The function declaration must provide default values for those arguments that are not specified. When the arguments are missing from function call , default value will be used for calculation.

Example:

#include <iostream>

using namespace std;

int sum(int a, int b=10, int c=20);

int main(){

   cout<<sum(11)<<endl;   /* In this case a value is passed as 11 and b and c values are taken from default arguments.*/

   cout<<sum(11, 12)<<endl;   /* In this case a value is passed as 11 and b value as 12, value of c values is taken from default arguments.*/

   cout<<sum(11, 12, 13)<<endl;  /* In this case all the three values are passed during function call, hence no default arguments have been used.*/

   return 0;

}

int sum(int a, int b, int c){

   int z;

   z = a+b+c;

   return z;

}

6."An overloaded function appears to perform different activities depending the kind of data send to it" Justify the statement with appropriate example.

5 marks view

7. Explain the default action of the copy constructor. Write a suitable program that demonstrates the technique of overloading the copy constructor. 

5 marks view

8. Briefly explain types of inheritance used in object oriented Programming.

5 marks view

9. Create a real scenario where static data members are useful. Explain with suitable example.

5 marks view

10. Create a function called swaps() that interchanges the values of the two arguments sent to it (pass these arguments by reference).  Make the function into a template, so it can be used with all numerical data types (char, int, float, and so on). Write a main() program to exercise the function with several types.

5 marks view

#include<iostream>

using namespace std;

template <class T>

void swaps(T &a,T &b)      //Function Template

{

    T temp=a;

    a=b;

    b=temp;

}

int main()

{

    int x1=5,y1=9;

    float x2=3.5,y2=6.5;

    cout<<"Before Swap:"<<endl;

    cout<<"x1="<<x1<<"y1="<<y1<<endl;

    cout<<"x2="<<x2<<"y2="<<y2<<endl;

    swaps(x1,y1);

    swaps(x2,y2);

    cout<<"After Swap:"<<endl;

    cout<<"x1="<<x1<<"y1="<<y1<<endl;

    cout<<"x2="<<x2<<"y2="<<y2<<endl;

    return 0;

}

11. Explain how exceptions are used for handling C++ error in a systematic and OOP-oriented way with the design that includes multiple exceptions.

5 marks view

Exceptions are run-time anomalies or abnormal conditions that a program encounters during its execution. Exception Handling in C++ is a process to handle runtime errors. We perform exception handling so the normal flow of the application can be maintained even after runtime errors. In C++, we use 3 keywords to perform exception handling: try, catch, and throw

  • The try statement allows us to define a block of code to be tested for errors while it is being executed.
  • The throw keyword throws an exception when a problem is detected, which lets us create a custom error.
  • The catch statement allows us to define a block of code to be executed, if an error occurs in the try block.

Syntax to handle multiple exceptions:

try

{

    //protected code

} catch(exceptionName e1)

{

    // catch block

} catch(exceptionName e2)

{

    // catch block

} catch(exceptionName en)

{

    // catch block

}

Example:

#include<iostream.h>
       #include<conio.h>
       void main()
       {
            int a=2;
              try
              {

                  if(a==1)
                      throw a;                  //throwing integer exception

                  else if(a==2)
                      throw 'A';                //throwing character exception

                  else if(a==3)
                      throw 4.5;                //throwing float exception

              }
              catch(int a)
              {
                  cout<<"\\nInteger exception caught.";
              }
              catch(char ch)
              {
                  cout<<"\\nCharacter exception caught.";
              }
              catch(double d)
              {
                  cout<<"\\nDouble exception caught.";
              }
              cout<<"\\nEnd of program.";
       }
   Output :
              Character exception caught.
              End of program.

12. How is character I/O different from Binary I/O? Explain with examples.

5 marks view