You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include<iostream>usingnamespacestd;classAddition{
public:voidadd(int a, int b) {
cout << "The sum = " << (a + b) << endl;
}
voidadd(int a, int b, int c) {
cout << "The sum = " << (a + b + c) << endl;
}
};
intmain() {
Addition ad;
ad.add(10, 29);
ad.add(20, 30, 59);
return0;
}
by using different sequence of parameters
#include<iostream>usingnamespacestd;classAddition{
public:voidadd(int a, double b) {
cout << "The sum = " << (a + b) << endl;
}
voidadd(double a, int b) {
cout << "The sum = " << (a + b) << endl;
}
};
intmain() {
Addition ad;
ad.add(10, 29.32);
ad.add(20.23, 30);
return0;
}
Default Arguments
voidadd(int a, double b) {};
cout << add(); //No Parameter
cout << add(5); //Single Parameter
cout << add(5, 2.3); //Dual Parameter with correct Data types
cout << add(3.5, 3); //Dual Parameter with incorrect Data types
Example
1. Use of default argument in C++
#include<iostream>usingnamespacestd;intsum(int a, int b, int c = 0, int d = 0) {
return (a + b + c + d);
}
intmain() {
cout << sum(12, 34) << endl;
cout << sum(12, 34, 24) << endl;
cout << sum(12, 34, 12, 42);
return0;
}