2. Method Overloading Flashcards
void Compute(int x, double y, char z)
Compute(true, 1.5, ‘A’)
promotie
Compute(1, 1.5, ‘A’)
class Math
{
public:
int Inc(short v1);
float Inc(float v1);
};
int Math::Inc(short v1)
{
return v1 + 1;
}
float Math::Inc(float v1)
{
return v1+1.0f;
}
void main()
{
Math m;
m.Inc(‘a’);
}
‘a’ is a “char” type value. As there is no method with the name Inc that has
one parameter of type “char”, the compiler promotes the char value to int
and tries again. Since there is no Inc method that has an int parameter (but
there are two Inc methods, an ambiguity case will be declared, and the code
will not compile. Event if, a char can fully be converted (without any value
lost) into a short , promotion only works for int and double types.
class Math
{
public:
int Inc(char v1) {
return v1 + 1;
}
};
void main()
{
Math m;
m.Inc(1.0);
}
warning C4244: ‘argument’:
conversion from ‘double’ to
‘char’, possible loss of data
class Math
{
public:
int Inc(int v1);
float Inc(float v1);
};
int Math::Inc(int v1)
{
return v1 + 1;
}
float Math::Inc(float v1)
{
return v1+1.0f;
}
void main()
{
Math m;
m.Inc(1.0f);
}
float Math::Inc(float v1)
void Compute(int x, double y, char z)
Compute(100, 1.5, ‘A’)
Perfect
void Compute(int x, const void* y, char z)
Compute(NULL, “C++”, ‘A’)
Cast
Compute(0, (const void*)”C++”, ‘A’)
class Math
{
public:
int Inc(void* v1);
};
int Math::Inc(void* v1)
{
return 1;
}
void main()
{
Math m;
double d = 1.0;
m.Inc(&d);
}
Pointer conversions are also impossible. “&d” is a “double *” that can not be
converted to “char *”.
However, using an explicit cast will solve this problem. In this case, the code
will compile.
class Math
{
public:
int Inc(char* v1);
};
int Math::Inc(char* v1)
{
return 1;
}
void main()
{
Math m;
double d = 1.0;
m.Inc( (char *)&d );
}
Pointer conversions are also impossible. “&d” is a “double *” that can not be
converted to “char *”.
However, using an explicit cast will solve this problem. In this case, the code
will compile.
class Math
{
public:
int Inc(char* v1);
};
int Math::Inc(char* v1)
{
return 1;
}
void main()
{
Math m;
double d = 1.0;
m.Inc(&d);
}
error C2664: ‘int Math::Inc(char *)’:
cannot convert argument 1 from
‘double *’ to ‘char *’
class Math
{
public:
int Add(int v1, int v2);
long Add(int v1, int v2);
};
int Math::Add(int v1, int v2)
{
return v1 + v2;
}
long Math::Add(int v1, int v2)
{
return v1 + v2;
}
Eroare la compilare
Nu putem face MO deoarece Add are aceeasi signatura si tipuri de return diferite
class Math
{
public:
int Inc(int v1);
float Inc(float v1);
};
int Math::Inc(int v1)
{
return v1 + 1;
}
float Math::Inc(float v1)
{
return v1+1.0f;
}
void main()
{
Math m;
m.Inc(‘a’);
}
int Math::Inc(int v1)
void Compute(int x, const void* y, char z)
Compute(NULL, “C++”, ‘A’)
promotie
class Math
{
public:
int Add(int v1, int v2);
long Add(int v1, int v2 = 0);
};
int Math::Add(int v1, int v2)
{
return v1 + v2;
}
long Math::Add(int v1, int v2)
{
return v1+v2;
}
Eroare la compilare
Nu putem face MO deoarece Add are aceeasi signatura si tipuri de return diferite
Nu conteaza ca un parametru este dat by default
class Math
{
public:
int Inc(int v1);
float Inc(float v1);
};
int Math::Inc(int v1)
{
return v1 + 1;
}
float Math::Inc(float v1)
{
return v1+1.0f;
}
void main()
{
Math m;
m.Inc(1.0);
}
Ambiguu
1.0 este double value poate fi promovat atat de double, cat si la int
class Math
{
public:
int Inc(char* v1) { return 1; }
};
void main()
{
Math m;
m.Inc(1.0);
}
error C2664: ‘int Math::Inc(char *)’:
cannot convert argument 1 from
‘double’ to ‘char *’