Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1


Ratings: (0)|Views: 17 |Likes:
Published by api-26548538
PDF Copy of ADS Unit II notes
PDF Copy of ADS Unit II notes

More info:

Published by: api-26548538 on Oct 18, 2008
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





Provides support for compile-time polymorphism.
Adds flexibility and convenience.
Most commonly overloaded functions are constructors.
o Most important form of an overloaded constructor is the
copy constructor.
Function Overloading
Is the process of using the same name for 2 or more functions.
Each redefinition of the function must use either different
types of parameters or a different number of parameters.
2 functions differing in only their return types cannot be

Also 2 functions whose only difference is that one takes
reference parameter and the other call-by-value parameter cannot
be overloaded.

Overloading Constructors
3 main reasons why constructors are overloaded:
1. To Gain Flexibility:

By providing a constructor for each way that a user of
the class may plausibly want to construct an object, the
flexibility of the class is increased.

2. Allowing Both Initialized & UnInitialized Objects:

Important if one wants to be able to create dynamic
arrays of object of the class, since it is not possible
to initialize a dynamically allocated array.

The default constructor is used to construct the
uninitialized array and the dynamically created array.
The parameterized constructor is called to create the
objects for the initialized array version.
#include <iostream.h>
class powers
int x;

// overload constructor two ways
powers() { x = 0; } // no initializer
powers(int n) { x = n; } // initializer

int getx() { return x; }
void setx(int i) { x = i; }
int main()
powers ofTwo[] = {1, 2, 4, 8, 16}; // initialized
powers ofThree[5];
// uninitialized
powers *p;
int i;
cout << "Powers of two: ";
for(i=0; i<5; i++)
// show powers of two
cout << ofTwo[i].getx() << " ";
cout << "\n\n";
for(i=1; i<6; i++)
// set powers of three
cout << "Powers of three: ";
// show powers of three
for(i=0; i<5; i++)
cout << ofThree[i].getx() << " ";
cout << "\n\n";
p = new powers[5];
// dynamically allocate an array
for(i=0; i<5; i++)
cout << "Powers of two: ";
for(i=0; i<5; i++)
// show powers of two
cout << p[i].getx() << " ";
cout << "\n\n";
delete [] p;
return 0;
3. Copy Constructors:
Helps prevent problems that might occur
When one object is used to initialize another.
oMyclass B = A;
o By default, when one object is used to initialize
another, C++ performs bit-wise copy.
o This should not be done, since the new object shares
the allocated memory as well.
o Hence a destructor frees the same memory twice once
for both objects.
When a copy of an object is made when it is passed as an
argument to a function.
When a temporary object is created as a return value from
a function.
\ue000C++ allows creation of copy constructor, which the compiler uses
when one object initializes another.
\ue000When a copy constructor exists, the default, bit wise copy is
\ue000General for :
classname(const classname &ob)
// body
\ue000It is permissible for a copy constructor to have additional
parameters as long as they have default arguments defined for
\ue000In all cases, the first parameter must be a reference to the
object doing the initializing.
\ue000C++ defines 2 distinct types of situations in which the value of
one object is given to another.
1. Assignment
2.Initialization, which can occur in 3 ways \u2013
a. One object explicitly initializes another
b. A copy of an object is made to be passed to a
c. A temporary object is generated (a return value ).
\ue000The copy constructor applies only toinitializations.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->