Object Oriented Programming 2075( Old Course)

Tribhuwan University
Institute of Science and Technology
2075( Old Course)
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.

Section A

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

1. Discuss the feature of Object-Oriented Programming. Differentiate between Object Oriented   Programming and any other programming language that you know.

10 marks view

2. How can you convert the user defined data type into primitive data type and vice versa? Explain both conversion routine with suitable example.

10 marks view

3. Create a class Stack with suitable data members and member functions to push and pop the elements of the stack. Add the exception when user tries to add item while the stack is full and when user tries to delete item while the stack is empty. Throw exception in both of the cases and handle these exception.

10 marks view

# include<iostream>

using namespace std;

class Stack

{

  int top;

   public:

    int a[5];     //Maximum size of Stack

    Stack()     //constructor

    {

        top = -1;

    }

    void push(int x)

    {

      if(top<5)

      {

        a[++top] = x;

        cout << "Element Inserted \\n";

      }

      else

      {

        throw "Stack is full. You cannot insert the item!!!";

      }

    }  

    int pop()

    {

      if(top>=0)

      {

        int d = a[top--];

        return d;

      }

      else

      {

        throw "Stack is empty. You cannot pop the item!!!";

      }

    } 

};

int main() 

{

  int item;

  Stack s1;

  int ch=1;

  cout<<"\\n1.PUSH\\n2.POP\\n3.EXIT";

  cout<<"\\nEnter your choice:";

  cin>>ch;

  do

  {

    switch(ch)

    {

      case 1:

        cout<<"\\nEnter the item to push:";

        cin>>item;

        try

        {

          s1.push(item);

        }

        catch(const char* e)  

        {

          cout<<e<<endl;

        }

        break; 

      case 2:

        try

        {

          cout<<"\\nPoped Item is:"<<s1.pop();

        }

        catch( const char* e) 

        {

          cout<<e<<endl;

        }

        break;

      case 3:

        exit(0);

    }

    cout<<"\\nEnter your choice:";

    cin>>ch;

  }while(ch<4);

return 0;

}

Section B

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

4. "Concept of friend in against the philosophy of Object Oriented Programming". Explain.

5 marks view

A friend function is a function that can access private members of a class even though it is not a member of that class.

A class can also be declared as friend of some other class. When a class ABC declares another class XYZ as its friend, then friend class XYZ can access to all data and function members of class ABC.

The philosophy of object oriented programming is that we can't access private members of a class from outside the class. But in C++, we can access private members of a class even from non-member function using concept of friend function and friend class. So, it is against the philosophy of Object Oriented Programming.

5. Explain about the importance of constructors and destructors with their execution sequence.

5 marks view

Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object.

Destructor is a special class function which destroys the class object as soon as the scope of object ends. The destructor is called automatically by the compiler when the object goes out of scope.

Importance of Constructor:

  • Constructors are used to initialize the objects of the class with initial values.
  • Constructors are invoked automatically when the objects are created.
  • Constructors can have default parameters.
  • If constructor is not declared for a class , the C++ compiler generates a default constructor.
  • A constructor can be used explicitly to create new objects of its class type.
  • The constructor is executed automatically.
  • Constructor function can be overloaded.

Importance of Destructor:

  • Destructors are invoked automatically when the objects are destroyed.
  • Destructors can not have any parameter and will not return any value.
  • Destructors are declared in a program to release memory space for future utilization.

Example

#include <iostream>

using namespace std;

class MyClass

{

   private:

      int m, n;

   public:

      MyClass( )   // constructor 

      {

         m = 10;

         n = 5;

      }

      ~MyClass( )   //destructor

      {

          cout<<"Destructor called!!";

      }

      void display()

      {

          cout<<"The sum is:"<<(m+n)<<endl;

      }

};

int main()

{

    MyClass obj;

    obj.display();

    return 0;

}

6. What is template? How can you differentiate a function template from a class template? Explain.

5 marks view

Templates is defined as a blueprint or formula for creating a generic class or a function. To simply put, we can create a single function or single class to work with different data types using templates. 

Function Template

It is used to define a function to handle arguments of different types in different times. Using function template, we can use same function for arguments of one data type in some cases and arguments of other types in other cases.

Syntax:

template<class type>
ret_type func_name(parameter list)
{
//body of the function
}

Example:

#include<iostream.h>
using namespace std;
template<class X>
X func( X a, X b)
{
 return a;
}
int main()
count<<func(15,8);      //func(int,int);
count,,func('p','q');   //func(char,char);
count<<func(7.5,9.2);   //func(double,double)
return();
}

Class Template

Class Templates Like function templates, class templates are useful when a class defines something that is independent of the data type. 

Syntax:

template<class Ttype>
class class_name
{
//class body;
}

Example:

#include <iostream>
using namespace std;

template <class T>
class Calculator
{
private:
	T num1, num2;
	
public:
	Calculator(T n1, T n2)
	{
		num1 = n1;
		num2 = n2;
	}
	void displayResult()
	{
		cout << "Numbers are: " << num1 << " and " << num2 << "." << endl;
		cout << "Addition is: " << add() << endl;
		cout << "Subtraction is: " << subtract() << endl;
		cout << "Product is: " << multiply() << endl;
		cout << "Division is: " << divide() << endl;
	}
	T add() { return num1 + num2; }
	T subtract() { return num1 - num2; }
	T multiply() { return num1 * num2; }
	T divide() { return num1 / num2; }
};

int main()
{
	Calculator<int> intCalc(2, 1);
	Calculator<float> floatCalc(2.4, 1.2);
	
	cout << "Int results:" << endl;
	intCalc.displayResult();
	
	cout << endl << "Float results:" << endl;
	floatCalc.displayResult();
	
	return 0;
}

7. Explain about this pointer with suitable example.

5 marks view

8. Write a C++ program containing a possible exception. Use a try block to throw it and a catch block to handle it.


5 marks view

9. Differentiate between compile time polymorphism and run time polymorphism.

5 marks view

10. What is container class? Differentiate container class from inheritance.

5 marks view

11. Write a program to demonstrate the use of default argument  in functions.

5 marks view

12. How can you differentiate a macro with an inline function? Are they same or different? Justify.

5 marks view
13. Define the various ambiguity situations that may occur during the process of inheritance. How can you resolve that ambiguity situation?
5 marks view