function definition

can a single class have the same function defined twice.plz xplain
Is this for homework?
Yes you can. You can override the first function with a second function. Granted that their parameters aren't the same.
Last edited on
if d same function with same parameters can be defined inside the class and outside the class in a single program
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
Why not simply try?
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.