Search This Blog

Tuesday 22 February 2011

C_PLUS_PLUS_TWO_MARKS_PART_II


                                      OBJECT ORIENTED PROGRAMMING

       1. What do you mean by function overloading?
Defining multiple functions with the same name is called function overloading. These functions must differ in their number, order or types of arguments.

       2. What is an inline function?
                    The function whose code gets inserted, instead of a jump to the function,
            at the place where there is a function call is known as the inline function.

          By making the function inline we request the compiler to insert the code at the
            place where there is a function call, so that, time is saved in calling the function
            and returning from the function.

      3. In how many ways can we pass variables to a function?
          There are three ways in which we can pass variables to a function.
  1. By value
void fun(int,int);
fun(a,b);

  1. by reference
void fun(int&,int&);
fun(a,b);

  1. By address
void fun(int*,int*);
fun(&a,&b);

  1. What is operator overloading?
Operator overloading means giving capability to the operator to work on different types of operands. The operator +,* etc. work on operands of type int,float etc. We can overload these operators by giving them the capability to work on user-defined data types.

  1. What are the advantages of operator overloading?
By making the operators to work on user defined data types just like they work on built-in-types, we can have a consistent approach.

Also, it is more intuitive to overload an operator rather than calling a function to perform the same job. This provides clarity to the program. For example, if a and b are variables of some user-defined data type, then writing a+b is more intuitive than calling a function add(a,b).

  1. What are the two major components of an object?
Data members and the member functions that operate upon the data are the major components of an object.


  1. What is an anonymous class?
Whenever a class is defined without any name (tag) then that class is said to be an anonymous class.
For eg.
Class
{
Int I;
Int j;
}obj;

  1. When does a this pointer get created?
The this pointer gets created when a member function (non-static) of a class is called.

  1. What is a constructor?
A constructor is a special type of member function for automatic initialization of object. Whenever an object is created constructor is automatically called. The name of the constructor must be same as that of its class.

  1.  What is a destructor?
A destructor is used to destroy the objects that have been created by constructor. The name of the destructor is same as constructor but is preceded by a tilde(~).

  1. What is static binding and dynamic binding?
When a function call gets resolved at compile time it is called static binding or early binding.

When the call gets resolved at run time it is called dynamic binding or late binding.
  1. What are pure virtual functions?
A pure virtual function is a virtual function with the expression=0 added to the declaration.

      13. What is an abstract class?
          A class that contains at least one pure virtual function is called an abstract class.
An abstract class cannot be instantiated.

  1. Compile time polymorphism can be done by
i) function overloading and
ii) operator overloading.

  1.  Run time polymorphism is done by
 virtual functions.



  1. Can we declare a static function as virtual?
No. The virtual function mechanism is used on the specific object that determines which virtual function to call. Since the static functions are not any way related to objects, they cannot be declared as virtual.

  1. What are templates?
Templates allow us to write one function or class that works for different data types. By using templates , we can design a single class/ functions that operates on many data types, instead of having to create a separate class/functions for each type. When used with functions they are known as function templates,  whereas when used with classes they are called class templates.

  1. What are exceptions? Give reason that cause exceptions?
Exceptions are errors that occur at run time. Following are the few reasons that can cause exceptions.
-         inability to open a file.
-         Exceeding the bounds of an array
-         Attempting to initialize an object to an impossible values
-         Falling short of memory

  1. How can we handle exceptions?
The exceptions are handled by using try, throw and catch blocks.  
There are four parts involved in the exception handling mechanism. These are as follows.
-         specifying the exception class
-         throwing an exception
-         the try block
-         the exception handler (catch block)

  1. What is an exception handler?
The catch block that handles the run-time error, is known as exception handler. The catch block must appear just after the try block.

  1. What is a friend function?
A global function that can access all the data members of a class is called friend function of that class. To make any function a friend of some class it must be declared with a keyword friend in that class. A friend function acts as a bridge between two classes.

     21. What are the operators that cannot be overloaded?
          ?: (conditional operator)
          :: scope resolution operator
          Sizeof  (size of operator)
          .  (Membership operator)
          .* (Pointer to member operator)

  1. What are manipulators?
Manipulators are the instructions to the output stream to modify the output in various ways.

  1. What is the purpose of istream class?
The istream class performs activities specific to input. It is derived from the ios class. The most commonly used member function of this class is the overloaded >> operator which can extract values of all basic types. We can extract even a string using this operator.

  1. What is an ostream class?
The ostream class handles output or insertion activities. This class is derived from ios class. The most commonly used member function of this class is the overloaded << operator function. Two more useful functions of this class are put() and flush(). The put() function is used to put a character in the stream, whereas flush() function flushes the buffer and inserts a newline.

  1. What is an iostream class?
The iostream class is derived from both iostream and ostream by multiple inheritance. It acts only as a base class from which other classes can be derived. Other than the constructors and destructors, it doesn’t contain any other member functions. The classes derived from iostream can perform both input and output.

  1. What is a stream?
Stream is a general name given to the flow of data. To represent different kinds of data, different streams are used. For example, the standard output stream flows to the screen display, the standard input stream flows from the keyboard. In c++, a stream is represented by an object of a particular class.

  1. What is single inheritance?
If a class is derived from only one base class then that is known as single inheritance.

  1. What is multiple inheritance?
If a class is derived from more than one base class then inheritance is known as multiple inheritance.

  1. What is multilevel inheritance?
If a class is derived from another derived class is called multilevel inheritance.

  1. What is hierarchical inheritance?
When from one base class more than one classes are derived that is called hierarchical inheritance.

  1. What is hybrid inheritance?
It is the combination of both multilevel inheritance and multiple inheritance.
      31.

No comments: