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 : Software Quality Assurance and Software

    What real-life experience have you gained along with Software Quality Assurance as well as Software Quality Control?

  • Q : Features of WordPress Normal 0 false

    Normal 0 false false

  • Q : What is validating parser What is

    What is validating parser? Answer: A parser makes sure that an XML document is valid additionally to being well formed.

  • Q : What is an Iterator pattern Iterator

    Iterator pattern: It is a common pattern in which the contents of a collection are iterated above in order. The Iterator pattern frees a client of data from requiring details of how the data is stored. This pattern is maintained by the Iterator and Li

  • Q : Explain Window manager Window manager :

    Window manager: This is a window manager which provides a computer user with a virtual desktop having one or more windows and working regions in which individual programs might be run. Window managers permit the contents of a user's desktop to be arra

  • Q : Maximum clock frequency of the circuit

    What do you mean by the term set up time and hold time constraints? Explain what do they mean? Which one is vital for estimating the maximum clock frequency of the circuit?

  • Q : Explain Message passing Message passing

    Message passing: We characterize the object interactions as message passing. The client object sends a message to a server object by invoking a technique from the server's class. Arguments might be passed with the message, and a outcome is returned by

  • Q : Examples of applications which can

    Give some examples of applications which can benefit from using XML?

  • Q : Define the term Top level class Define

    Define the term Top level class: It is a class defined either at outermost level of a package or the static nested class.

  • Q : State Precedence rules Precedence rules

    Precedence rules: The rules which determine the order of computation of an expression comprising more than one operator. The operators of higher precedence are computed before those of lower precedence. For example, in the expression x+y*z, the multip

©TutorsGlobe All rights reserved 2022-2023.