--%>

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 : Explain Aggregation Aggregation : It is

    Aggregation: It is a relationship in which an object has one or more other subordinate objects as portion of its state. The subordinate objects usually have no self-governing existence separate from their containing object. Whenever the containing obj

  • Q : Does TestComplete maintain testing of

    Does TestComplete maintain testing of Flex applications?

  • Q : Explain Method overriding Method

    Method overriding: It is a method stated in a super class might be overridden by a method of similar name stated in a sub class. The two methods should have similar name and number and types of formal arguments. Any checked exception thrown by sub-cla

  • Q : Explain why java is so important for

    The internet aided java to the forefront of programming. And java consequently has had a deep effect on the internet. The reason for this is highly simple: java uses the universe of objects that can travel freely in cyber space. In a network, two broad categories of

  • Q : Features of WordPress Normal 0 false

    Normal 0 false false

  • Q : How would you extract an exact

    How would you extract an exact attribute by using XSLT, from an element into an XML document?

  • Q : Homework Assignment How much would it

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

  • Q : Define the reasons of Process Handle

    Define the reasons of Process Handle Table.

  • Q : Write a program that initializes an

    Write a program that initializes an integer array a[20] with values {0, 1, 2, 3, ...18, 19 - once each} then scrambles them up and prints the values in random order. For example: 19 2 3 8 11 1 4 17 7 15 9 0 16 12 18 13 5 6 10 14    

  • Q : Reducing state space of code What is

    What is the way to reduce the state space of the code during model checking?