--%>

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 Real number Real number : It

    Real number: It is a number with an integer and a fractional portion. The primitive types double and float are employed to symbolize real numbers.

  • Q : Explain way to open one resistance of

    Explain the way to open just one resistance of vb application (.exe).

  • Q : Describe File system File system : The

    File system: The operating system makes it possible to utilize space on a computer's disk drives by imposing a structured file system on disk storage. Each and every file system contains its own conventions for the manner in which the files are named,

  • Q : Functions of states of the

    Mention the different states of the XMLHttpRequest an dalso describe their functions.

  • Q : Define the term Marking interface

    Define the term Marking interface: It is an interface with no methods.

  • Q : Describe Overriding for restriction

    Overriding for restriction: It is a form of method overriding in which the sub-class version of a method calls the super-class version first of all and then employs or manipulates the outcome or consequences of that call in some manner.

  • Q : Computer science 1. Here is a short

    1. Here is a short program. It prints out the value of a variable "x". Ernie and Bert disagree about what will be printed: Ernie says, the value gets changed in "changeX" so it will print "7", and Bert says, no, when the function exits the changes get reversed and the value goes back to "5". Explain

  • Q : Microsoft Access I have attached a

    I have attached a database and a PDF with 5 questions. I need the 5 questions answered with 5 queries in the database. Question 3 needs a crosstab query. All the questions require the proper join.

  • Q : Explain Constructor Constructor : A

    Constructor: A constructor is automatically called whenever an instance of its class is formed. A constructor always has similar name as its class, and encompass no return type. For example: public

  • Q : Write a program that prints out 20

    Write a program that prints out 20 random cards form a standard deck of 52 cards. Make sure the program prints out different sets of cards from one run to the next.