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

ADS_Unit_II

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

Availability:

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

05/09/2014

pdf

text

original

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

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:
\u2022

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:
\u2022

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.

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

public:
// 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
ofThree[i-1].setx(i*i*i);
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++)
p[i].setx(ofTwo[i].getx());
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;
}
UNIT II
3
3. Copy Constructors:
Helps prevent problems that might occur
\u2022
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.
\u2022
When a copy of an object is made when it is passed as an
argument to a function.
\u2022
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
bypassed.
\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
them.
\ue000In all cases, the first parameter must be a reference to the
object doing the initializing.
NOTE:
\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
(declaration).
b. A copy of an object is made to be passed to a
function.
c. A temporary object is generated (a return value ).
\ue000The copy constructor applies only toinitializations.

You're Reading a Free Preview

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