Function Overloading
#include<iostream.h>
#include<conio.h>
class Addition
{
public:
void sum(int a, int b)
{
cout<<a+b;
}
void sum(int a, int b, int c)
{
cout<<a+b+c;
}
};
void main()
{
clrscr();
Addition obj;
obj.sum(10, 20);
cout<<endl;
obj.sum(10, 20, 30);
}
#include<iostream.h> #include<conio.h> class Addition { public: void sum(int a, int b) { cout<<a+b; } void sum(int a, int b, int c) { cout<<a+b+c; } }; void main() { clrscr(); Addition obj; obj.sum(10, 20); cout<<endl; obj.sum(10, 20, 30); }
Output
30
60
30 60
Unary minus (-) operator overloading program in c++.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*C++ program for unary minus (-) operator overloading.*/
#include<iostream>
using
namespace
std;
class
NUM
{
private
:
int
n;
public
:
//function to get number
void
getNum(
int
x)
{
n=x;
}
//function to display number
void
dispNum(
void
)
{
cout <<
"value of n is: "
<< n;
}
//unary - operator overloading
void
operator - (
void
)
{
n=-n;
}
};
int
main()
{
NUM num;
num.getNum(10);
-num;
num.dispNum();
cout << endl;
return
0;
}
value of n is: -10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| /*C++ program for unary minus (-) operator overloading.*/ #include<iostream> using namespace std; class NUM { private : int n; public : //function to get number void getNum( int x) { n=x; } //function to display number void dispNum( void ) { cout << "value of n is: " << n; } //unary - operator overloading void operator - ( void ) { n=-n; } }; int main() { NUM num; num.getNum(10); -num; num.dispNum(); cout << endl; return 0; } |