0% found this document useful (0 votes)
51 views14 pages

Constructor Overloading: Week-4 (Part-1)

This document discusses function overloading, constructor overloading, and destructors in C++. It explains that functions can be overloaded based on their signature (name and parameter types). Constructors can also be overloaded by defining multiple constructors with different parameter lists in a class. Destructors are automatically called when an object is destroyed, but destructors cannot be overloaded or take parameters. The document provides examples of function, constructor, and destructor overloading in C++.

Uploaded by

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

Constructor Overloading: Week-4 (Part-1)

This document discusses function overloading, constructor overloading, and destructors in C++. It explains that functions can be overloaded based on their signature (name and parameter types). Constructors can also be overloaded by defining multiple constructors with different parameter lists in a class. Destructors are automatically called when an object is destroyed, but destructors cannot be overloaded or take parameters. The document provides examples of function, constructor, and destructor overloading in C++.

Uploaded by

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

Constructor

Overloading
Week-4 (Part-1)

CSSE-2133 Object Oriented Programming


Overloading
Overloading

Function Operator
Overloading Overloading

Constructor
Overloading

CSSE-2133 Object Oriented Programming


Function Overloading
• A function name having several definitions that are
differentiable by the number or types of their arguments
• Example
float divide (int a, int b);
float divide (float x, float y);
• The key to function overloading is a function’s
name and argument list which is also known as
the function signature
o It is the signature, not the function type that enables
function overloading

CSSE-2133 Object Oriented Programming


Cont…
• If two functions are having same number and types of
arguments in the same order, they are said to have the same
signature
• Even if they are using distinct variable names, it doesn’t
matter
• For instance, following two functions have same signature

void squar (int a, float b);


void squar (int x, float y);

CSSE-2133 Object Oriented Programming


Steps to Overloading
Functions
• Declare and define all the functions with the same name but
different signatures, separately
• After declaring overloading functions, you must define them
separately

CSSE-2133 Object Oriented Programming


Example

CSSE-2133 Object Oriented Programming


Important Note !
• When a function name is declared more than once in a program, the
compiler will interpret the second (and subsequent) declaration(s) as
follows:
o If the signatures of subsequent functions match the previous
function’s, then the second is treated as a re-declaration of the
first
o If the signatures of two functions match exactly but the return
type differ, the second declaration is treated as an erroneous re-
declaration of the first and is flagged at compile time as an error

For example,
float square (float f);
Error
double square (float x);

CSSE-2133 Object Oriented Programming


Constructor Overloading
• In C++ a class may have more than 1 constructor
• Constructor overloading is the process of defining more than 1
constructor in the same class
• Just like overloaded functions, overloaded constructors in a
class must have different parameter lists
o Rectangle ();
o Rectangle (int);
o Rectange(int,float,string);

CSSE-2133 Object Oriented Programming


Example

CSSE-2133 Object Oriented Programming


class Calculator Calculator(int a, int b, int c)
{ {
int val1; val1 = a;
int val2; val2 = b;
int val3; val3 = c;
public: }
Calculator() int sum1()
{ {
return (val1 + val2);
val1 = 10;
}
val2 = 20;
int sum2()
val3 = 30;
{
}
return (val1 + val2 + val3);
Calculator(int a, int b)
}
{
};
val1 = a;
val2 = b;
}
void main()
{
Calculator obj1;
Calculator obj2(22, 33);
Calculator obj3(50, 80, 110);
cout << "Sum of number of object # 1 :: " << obj1.sum1() << endl;
cout << "Sum of number of object # 1 :: " << obj2.sum1() << endl;
cout << "Sum of number of object # 1 :: " << obj3.sum2() << endl;
}

CSSE-2133 Object Oriented Programming


Progress Check !
TURE/FALSE
• A class may have more than one destructor
• When an object is destroyed, destructor is automatically called
• Presence of many destructor in a class is called destructor
overloading
• A destructor can have a return type
• A destructor can have arguments like destructor
• We can pass arguments to a constructor
• Destructors cannot take arguments
• Destructors can be overloaded.

CSSE-2133 Object Oriented Programming


Progress Check
• Return type of a destructor is 
(a) int (b) tilde
(c) void (d) nothing, destructor has no return value
• A class may have __________ destructor 
(a) two (b) only one
(c) many (d) none of these

CSSE-2133 Object Oriented Programming


Assignment # 1
• Assignment will be uploaded by Saturday evening
• Due date = 18th March 2019
• Soft Copies are to be submitted by the due date on edmodo,
class code =
• Plagiarism of any sort is intolerable
• Assignment will be marked based on effort

CSSE-2133 Object Oriented Programming

You might also like