0% found this document useful (0 votes)
4 views5 pages

Oops Unit 2

Uploaded by

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

Oops Unit 2

Uploaded by

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

OOPs – UNIT 2

Control flow (if-else etc) read from : PPS-UNIT 2 PART 2 OF 2


https://www.gecsheikhpura.org.in/wp-
content/uploads/sites/18/2024/11/file_6745f94452113.pdf

Functions read from : PPS- UNIT 4


https://www.gecsheikhpura.org.in/wp-
content/uploads/sites/18/2025/02/file_67a78c2f8c4a7.pdf

Function overloading:
It is a feature of C++ that allows multiple functions to have the
same name but with different parameter types, so the compiler chooses
the right one based on the arguments used.
• Define Multiple Functions with the Same Name : user can create two
or more functions with identical names.
• Different Parameters : These functions must differ in their:
o Number of parameters (e.g., sum(int a) vs. sum(int a, int b))
o Types of parameters (e.g., sum(int a) vs. sum(double a))
o Order of parameters (e.g., sum(int a, double b) vs. sum(double
a, int b))
• Same Return Type (or Different) :
Return type alone cannot distinguish overloaded functions.
• Compiler Decides Which to Call : Based on the arguments passed, the
correct function is automatically selected.

Example 1:
#include <iostream>
using namespace std;

void print(int a) {
cout << "Integer: " << a << endl;
}

void print(double b) {
cout << "Double: " << b << endl;
}

void print(string c) {
cout << "String: " << c << endl;
}

int main() {
print(5); // Calls print(int)
print(5.5); // Calls print(double)
print("Hello"); // Calls print(string)
return 0;
}
P a g e 1|5 OOPs. GEC_SHK
Example 2:
#include <iostream>
using namespace std;

// Function to add two numbers


int add(int a, int b) {
return a + b;
}

// Overloaded function to add three numbers


int add(int a, int b, int c) {
return a + b + c;
}

int main() {
cout << add(2, 3) << endl; // Calls add(int, int) → Output: 5
cout << add(1, 2, 3) << endl; // Calls add(int, int, int) → Output: 6
return 0;
}

Example 3:
#include <iostream>
using namespace std;

// Function to display (int, double)


void display(int a, double b) {
cout << "Int: " << a << ", Double: " << b << endl;
}

// Overloaded function to display (double, int)


void display(double a, int b) {
cout << "Double: " << a << ", Int: " << b << endl;
}

int main() {
display(5, 3.14); // Calls display(int, double)
display(2.71, 10); // Calls display(double, int)
return 0;
}

P a g e 2|5 OOPs. GEC_SHK


Default Argument:
Default arguments in C++ are values given to function parameters that
are used when no corresponding arguments are provided during the function
call.
• allow a function to be called with fewer arguments than it is
declared with.
• If an argument is missing, the compiler uses the default
value specified in the function declaration.
• Must be specified from right to left (trailing arguments first).
• Cannot skip arguments in the middle.

Syntax:
returnType functionName(type param1 = defaultValue1, type param2 =
defaultValue2, ...);

Example:
void greet(string name = "User", string message = "Hello") {
cout << message << ", " << name << "!" << endl;
}

Valid:
void func(int a, int b = 10, int c = 20);

Invalid:
void func(int a = 5, int b, int c = 20);

Example 1:
#include <iostream>
using namespace std;

void bookTicket(string destination, int seats = 1) {


cout << "Booked " << seats << " seat(s) to " << destination << endl;
}

int main() {
bookTicket("Bodh Gaya"); // Default: 1 seat
bookTicket("Rajgir", 4); // Override: 4 seats
return 0;
}

P a g e 3|5 OOPs. GEC_SHK


Example 2:
#include <iostream>
using namespace std;

// Function with default arguments (college & branch)


void printStudent(string name, string branch = "CSE", string college =
"GEC Sheikhpura") {
cout << "Name: " << name << endl;
cout << "Branch: " << branch << endl;
cout << "College: " << college << endl << endl;
}

int main() {
// Different ways to call the function
printStudent("Sunny"); // Uses defaults (CSE, GEC Sheikhpura)
printStudent("Shivam", "ECE"); // Overrides branch, keeps default college
printStudent("Rohit", "ME", "GEC Nawada"); // Overrides both defaults

return 0;
}

Cases Where Function Overloading Fails in C++


1. Overloading Based Only on Return Type
int func() { return 5; }
double func() { return 3.14; }

int main() {
func(); // Which one to call? int or double?
return 0;
}
// compiler cannot distinguish only if return type differs
2. Default Arguments Causing Ambiguity
void print(int a) { cout << "Int: " << a << endl; }
void print(int a, int b = 10) { cout << "Two Ints: " << a << ", "
<< b << endl; }

int main() {
print(5); // Ambiguity!
return 0;
}
// Both versions match due to default argument.
3. Type Conversion Leading to Ambiguity
void display(float x) { cout << "Float: " << x << endl; }
void display(double x) { cout << "Double: " << x << endl; }

int main() {
display(3.14); // Ambiguous! double or float ?
return 0;
}
// 3.14 is double so calling display(double x)
// but typecasted to float so calling display(float x)

P a g e 4|5 OOPs. GEC_SHK


Q1. Write an object oriented program in C++ using function overloading to
check whether the input data (either strings or integers) are palindrome
or not, and display the results accordingly.

#include <iostream>
#include <string>
#include <algorithm> // For reverse()
using namespace std;

class PalindromeChecker {
public:
// Method to check if a string is a palindrome
bool isPalindrome(const string &str) {
string reversed = str;
reverse(reversed.begin(), reversed.end());
return (str == reversed);
}
// Method to check if an integer is a palindrome
bool isPalindrome(int num) {
if (num < 0) return false; // Negative numbers can't be palindromes
int original = num;
int reversed = 0;

while (num > 0) {


reversed = reversed * 10 + num % 10;
num /= 10;
}
return (original == reversed);
}
};

int main() {
PalindromeChecker checker;
// Check for a string
string word;
cout << "Enter a word: ";
cin >> word;

if (checker.isPalindrome(word)) {
cout << word << " is a palindrome." << endl;
} else {
cout << word << " is NOT a palindrome." << endl;
}
// Check for an integer
int number;
cout << "Enter a number: ";
cin >> number;

if (checker.isPalindrome(number)) {
cout << number << " is a palindrome." << endl;
} else {
cout << number << " is NOT a palindrome." << endl;
}
return 0;
}

Q2. Can you use functions with default arguments as an alternative to


function overloading? Elaborate with suitable examples. (Answer yourself)
P a g e 5|5 OOPs. GEC_SHK

You might also like