function definition

May 2, 2011 at 6:08pm
can a single class have the same function defined twice.plz xplain
May 2, 2011 at 6:13pm
Is this for homework?
May 2, 2011 at 9:31pm
Yes you can. You can override the first function with a second function. Granted that their parameters aren't the same.
Last edited on May 2, 2011 at 9:31pm
May 3, 2011 at 5:25pm
if d same function with same parameters can be defined inside the class and outside the class in a single program
May 3, 2011 at 5:34pm
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
#include <iostream>

// Setup the same function name 3 times
// with 3 different parameters
void MyFunction();
void MyFunction(int);
void MyFunction(int, int);

int main()
{
      MyFunction(); // This will call the 1st one
      MyFunction(2); // This will call the 2nd one
      MyFunction(2, 3); // This will call the 3rd one
   return 0;
}

void MyFunction() // First
{
      std::cout << "You called the first version of MyFunction()" << std::endl;
}

void MyFunction(int i) // Second
{
      std::cout << "You called the second version of MyFunction()" << std::endl;
}

void MyFunction(int i, int j) // Third
{
      std::cout << "You called the third version of MyFunction()" << std::endl;
}
Last edited on May 3, 2011 at 5:56pm
May 3, 2011 at 5:54pm
Why not simply try?
May 3, 2011 at 7:10pm
As a virtual function sort of. That's not really what happens since the virtual gets ignored if another function redefines it.
Topic archived. No new replies allowed.