0% found this document useful (0 votes)
3 views11 pages

L5 Overloading, Constructor and Destructor

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

L5 Overloading, Constructor and Destructor

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Overloading

• The C++ language allows the programmer to specify more than one
definition for a function name or operator.
• It is one of the many useful features that object-oriented languages provide,
which increases the power and flexibility of C++.
• Overloading is a technique of using a single identifier to define different
methods of a class that differs in their input and output parameters.
• Overloading is generally used when a program block executes the same task
but is slightly distinct in a set of parameters.
• Overloading is a concept used to avoid redundant code where the
same method name or operator is used multiple times but with a
different set of parameters or number of operands.

• Overloading provides code clarity, reduces complexity, and increases


the runtime presentation of code.
There are two types of overloading provided by C++. These are:

 Operator Overloading
 Function Overloading

Operator Overloading
Operator overloading is a type of polymorphism in which a
single operator is overloaded to give a user-defined meaning.
 Operator overloading provides a flexible option for creating
new definitions of C++ operators.
There are some C++ operators which cannot be overloaded

The lists of such operators are


oClass member access operator (. , .* )
oScope resolution operator (::)
oConditional Operator (?:)
oSize Operator (sizeof)
• An overloaded operator is used to operate on the user-defined data type.
• Take an example of the addition operator (+) operator that has been overloaded to perform
addition on various variable types, like integer, floating point, String (concatenation), etc.

Syntax for C++ Operator Overloading

To overload an operator, we use a special operator function.


We define the function inside the class or structure whose objects/variables we want the overloaded
operator to work with.

class classname
{
---
public
returntype operator symbol (arguments) {
... .. ...
}
... .. ...
};
Operator Overloading in Unary Operators

• Unary operators operate on only one operand.

• The increment operator ++ and decrement operator -- are examples of


unary operators.

• Overloading program with ++ unary operator=>


class Count
{
private:
int value;

public:
// Constructor to initialize count to 100
Count() : value(100)
{}
// Overload ++ when used as prefix
void operator ++ ( )
{
++value;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;

// Call the "void operator ++ ()" function


++count1;
count1.display();
return 0;
}
Constructor
• C++ provides a particular member function called the Constructor,
which enables an object to initialize itself at the time of its creation.
It is known as the automatic initialization of objects.

• (Basic programs)
CONSTRUCTOR DESTRUCTOR

Destructor is used to destroy the


Constructor helps to initialize the object of a
instances.
class.

Declared as Declared as
classname( arguments if any ) ~ classname( no arguments )
{Constructor’s Body }. { }.
Constructor can either accept arguments or
While it can’t have any arguments.
not.

A constructor is called when an instance or It is called while object of the class


object of a class is created. is freed or deleted.
Constructor is used to allocate the While it is used to deallocate the
memory to an instance or object. memory of an object of a class.
Constructor can be overloaded. While it can’t be overloaded.

The constructor’s name is same as the Here, its name is also same as the class
class name. name preceded by the tiled (~) operator.
In a class, there can be multiple
There is always a single destructor.
constructors.
• There is a concept of copy constructor which is used to initialize
an object from another object.
• Conversely, there is no copy destructor concept.

You might also like