--%>

Operator overloading and Constructor

Q. Explain operator overloading? State the rules to be followed for overloading operators.

Ans. 

C++ owns rich set of operators. All C operators are valid in C++ also. In addition, C++ introduce some new operators, like

: : scope resolution operator

: * pointer to member declaratory

*pointer to member operator

Delete memory release operator

End l line feed operator

New memory allocation operator

Set W field width operator

In addition, C++ also allows us to provide new definitions to some of the build in operators. That is why we may provide various meanings to an operator. On the basis of the types of arguments used. This process is known as the operator overloading. The input or output operators << and >> are good examples of operator overloading. Although the built in definition of the << operator is for shifting of bits, it is also for displaying the values of various data types. This has been made possible by the header file iostream where a number of overloading definitions for << are included. Thus the statement

Cout << 75.86;

Invokes the definition for displaying a double type value and

Cout << "well done";

Invokes the definition for displaying a char value.

Rules for overloading operators: Although it looks simple to redefine the operators, there are certain restrictions and limitations in overloading them. Some of them are listed below:

Only existing operators can be overloaded. New operators cannot be created. The overloaded operator must have at least one operand that is of user defined type. We cannot change the basic meaning of an operator. This is to say, we neither can nor define the plus (+) operator to subtract one value from the other. Overloaded operators follow the syntax rules of the original operators. They cannot be over hidden. There are some operators that cannot be overloaded. We cannot use friend function to certain operators. However member functions cannot be using to over load them. Unary operators, over loaded by means of a member functions, take on explicit arguments and return no explicit values, but those over loaded by means of friend function, take one reference argument (the object of the relevant class). Binary operators over loaded through a member function take one explicit argument and those which are over loaded through a friend function take two explicit arguments. Binary arithmetic operators such as +, -, * and must explicitly return a value. They must not attempt to change their own arguments.

Ans. (b) Constructor: A constructor is a special member function whose task is to initialize the objects of its class. It also provides memory to class member variables inside the objects. It is special because its name is same as the class name. This function is called whenever an object is created. It is called constructor because it constructs the values of data members of the class.

Types of constructors: A constructor that accepts no parameters is called the default constructor. A constructor that accepts default arguments is called as parameterized constructors. A constructor that accepts default arguments is called as default argument constructor. A constructor allocating dynamic memory using new operator is called as dynamic constructor. A constructor used to declare and initialize an object from another object is called copy constructor.

  • Constructors must be defined in public section.
  • They do not have return types, not even void and therefore, they cannot return values.
  • They cannot be inherited.
  • They can have default arguments. They cannot be virtual. We cannot refer to their addresses.
  • They make implicit calls to the operator now. When memory allocation is required.
  • A class can have more than one constructor if their arguments are different. This is called as constructor overloading.
  • Every class contains a default zero arguments constructor if no explicit constructor is defined.

Copy constructor: As stated earlier, a copy constructor is used to declare and initialize an object from another object. For example, the statement

Integer I 2 (11);

Would define the object I 2 (11) and at the same time initialize it to the values of I 1. Another form of this statement is the process of initializing through a copy constructor is known as copy initialization. Remember the statement:

I 2 = 11;

# include iostream.h >

 Class code

{

Int id;

Public:

Code () {  }       // constructor

Code (Int a) { id = a; }      // constructor again

Code (code & x)      // copy constructor

{

Id = x. Id;

Void display (void)     // copy in the value

{

Cout << id;

}

};

Int main ();

Code A (100);   // object A is created and initialized

Code B (A);      // copy constructor called

Code C = A;    // copy constructor called again

Code D;   // D is created not initialized

D = A;                    // copy constructor not called

Cout << "\n" id "of A": A. Display ();

Cout << "\n" id "of B": B. Display ();

Cout << "\n" id "of C": C. Display ();

Cout << "\n" id "of D": D. Display ();

Return 0;

   Related Questions in Programming Languages

  • Q : Describe object-oriented programming

    Briefly describe object-oriented programming (OOP)?

  • Q : Homework Assignment How much would it

    How much would it cost to do a basic program within the given requirements?

  • Q : Define Double buffering Double

    Double buffering: A graphics method employed to smooth animation. The later version of an image is drawn `at the back the scenes' and then exhibited in its totality whenever the drawing is finished. The supposition is that it will be relatively fast t

  • Q : What is Boundary error Boundary error :

    Boundary error: The errors which arise from programming mistakes prepared at the edges of a problem- indexing off the edge of an array, commencing with no items of data, loop termination and so forth. Boundary errors are a very general type of logical

  • Q : Create an applet of bounces in JAVA

    Create an applet that bounces a blue ball inside an applet using Thread.  The ball (diameter is 10) will start at position (0,0).  When the ball hits the edge of the applet, the ball should bounce off the edge at a randomly selected angle between 20 and 60 d

  • Q : Define Finite State Machines Finite

    Finite State Machines : A Finite State Machine (FSM) is one of the most suitable models for formal checks, especially for concurrent systems. However, FSMs can have problems with inheritance (the state model can change in derived classes) if state asp

  • Q : What is u-area Explain what is meant by

    Explain what is meant by the term u-area (user area)?

  • Q : Difference between the choice and list

    Illustrate the difference between the choice and list?

  • Q : What is Boolean expression Boolean

    Boolean expression: It is an expression whose outcome is of type Boolean, that is, gives a value of either true or false. The operators like && and || take Boolean operands and generate a Boolean outcome. The relational operators obtain operan

  • Q : Illustrates the difference between

    Illustrates the difference between Property Get, Let and Set?