BSc Sem 1 Computer Paper 2017 Answers of 5 B, C, D

Question 5(B) answer in brief (Any one)                                                                            2 marks
1.      What is Exception?

An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.
Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is built upon three keywords: try, catch,and throw.
·        throw − A program throws an exception when a problem shows up. This is done using a throw keyword.
·         catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.
·         try − A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.
2.      Constructor vs. Destructor

BASIS FOR COMPARISON
CONSTRUCTOR
DESTRUCTOR
Purpose

It allocates the memory to an object.
It deallocates the memory of an object.
Declaration
class_name( arguments if any ){ };
~ class_name( no arguments ){ };
Arguments
Constructor accepts argument
Destructor does not accepts any argument.
Calling
Constructor is called automatically, while the object is created.
Destructor is called automatically, as block is exited or program terminates.
Working
Constructor allows object to initialize some of its value before, it is used.
Destructor allows object to execute some code at the time of its destruction.
Order of execution

Constructor are called in successive order.
Destructor are called in reverse order of constructor.
In numbers
There can be multiple constructor in the class.
But there is always a single destructor in the class.
Copy Constructor
Copy constructor allows a constructor to declare and initialize a object from another object.
No such concept.
Over loading
Constructors can be overloaded .
Destructor can not be overloaded.


Question 5(C) answer in detail (Any one)                                                                           3 marks
1.      Explain Constructor Overloading.

Just like other member functions, constructors can also be overloaded. Infact when you have both default and parameterized constructors defined in your class you are having Overloaded Constructors, one with no parameter and other with parameter.
You can have any number of Constructors in a class that differ in parameter list.
class Student
{
 int rollno;
 string name;
 public:
 Student(int x)
 {
  rollno=x;
  name="None";
 }
 Student(int x, string str)
 {
  rollno=x ;
  name=str ;
 }
};

int main()
{
 Student A(10);
 Student B(11,"Ram");
}

In above case we have defined two constructors with different parameters, hence overloading the constructors.
One more important thing, if you define any constructor explicitly, then the compiler will not provide default constructor and you will have to define it yourself.
In the above case if we write Student S; in main(), it will lead to a compile time error, because we haven't defined default constructor, and compiler will not provide its default constructor because we have defined other parameterized constructors.

2.      Write a note on Template Class.

Just as we can define function templates, we can also define class templates. The general form of a generic class declaration is shown here −
template <class type> class class-name {
   .
   .
   .
}
Here, type is the placeholder type name, which will be specified when a class is instantiated. You can define more than one generic data type by using a comma-separated list.
Following is the example to define class Stack<> and implement generic methods to push and pop the elements from the stack −
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <stdexcept>

using namespace std;

template <class T>
class Stack {
   private:
      vector<T> elems;    // elements

   public:
      void push(T const&);  // push element
      void pop();               // pop element
      T top() const;            // return top element
     
      bool empty() const {      // return true if empty.
         return elems.empty();
      }
};

template <class T>
void Stack<T>::push (T const& elem) {
   // append copy of passed element
   elems.push_back(elem);   
}

template <class T>
void Stack<T>::pop () {
   if (elems.empty()) {
      throw out_of_range("Stack<>::pop(): empty stack");
   }
  
   // remove last element
   elems.pop_back();        
}

template <class T>
T Stack<T>::top () const {
   if (elems.empty()) {
      throw out_of_range("Stack<>::top(): empty stack");
   }
  
   // return copy of last element
   return elems.back();     
}

int main() {
   try {
      Stack<int>         intStack;  // stack of ints
      Stack<string> stringStack;    // stack of strings

      // manipulate int stack
      intStack.push(7);
      cout << intStack.top() <<endl;

      // manipulate string stack
      stringStack.push("hello");
      cout << stringStack.top() << std::endl;
      stringStack.pop();
      stringStack.pop();
   } catch (exception const& ex) {
      cerr << "Exception: " << ex.what() <<endl;
      return -1;
   }
}
If we compile and run above code, this would produce the following result −
7
hello
Exception: Stack<>::pop(): empty stack


Question 5 (D) write a note on (Any one)                                                                            5 marks
1.      What is inheritance? Explain types of inheritance.

Inheritance in Object Oriented Programming can be described as a process of creating new classes from existing classes.
New classes inherit some of the properties and behavior of the existing classes. An existing class that is "parent" of a new class is called a base class. New class that inherits properties of the base class is called a derived class.
Inheritance is a technique of code reuse. It also provides possibility to extend existing classes by creating derived classes.

Types of Inheritance

In C++, we have 5 different types of Inheritance. Namely,
1.    Single Inheritance
2.    Multiple Inheritance
3.    Hierarchical Inheritance
4.    Multilevel Inheritance
5.    Hybrid Inheritance 

draw diagrams for different inheritance

2.      Explain friend function with example.

Friend functions are actually not class member function. Friend functions are made to give privateaccess to non-class functions. You can declare a global function as friend, or a member function of other class as friend.
Example :
class WithFriend
{
 int i;
 public:
 friend void fun(); // Global function as friend
};

void fun()
{
 WithFriend wf;
 wf.i=10;  // Access to private data member
 cout << wf.i;
}

int main()
{
fun(); //Can be called directly
}


Hence, friend functions can access private data members by creating object of the class. Similarly we can also make function of other class as friend, or we can also make an entire class as friend class.


class Other
{
 void fun();
};

class WithFriend
{
 private:
 int i;
 public:
 void getdata();  // Member function of class WithFriend
 friend void Other::fun();   // making function of class Other as friend here
 friend class Other;  // making the complete class as friend
};


When we make a class as friend, all its member functions automatically become friend functions.
Friend Functions is a reason, why C++ is not called as a pure Object Oriented language. Because it violates the concept of Encapsulation.


Comments

Popular posts from this blog

પટેલ સમાજનો ઈતિહાસ જાણો : કોણ અને ક્યાંથી આવ્યા હતા પાટીદારો

Python HTML Generator using Yattag Part 1

Python HTML Generator using Yattag Part 2