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

©TutorsGlobe All rights reserved 2022-2023.