2. Method Overloading Flashcards

1
Q

void Compute(int x, double y, char z)
Compute(true, 1.5, ‘A’)

A

promotie
Compute(1, 1.5, ‘A’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

‘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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

class Math
{
public:
int Inc(char v1) {
return v1 + 1;
}
};
void main()
{
Math m;
m.Inc(1.0);
}

A

warning C4244: ‘argument’:
conversion from ‘double’ to
‘char’, possible loss of data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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);
}

A

float Math::Inc(float v1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

void Compute(int x, double y, char z)
Compute(100, 1.5, ‘A’)

A

Perfect

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

void Compute(int x, const void* y, char z)
Compute(NULL, “C++”, ‘A’)

A

Cast
Compute(0, (const void*)”C++”, ‘A’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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);
}

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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 );
}

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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);
}

A

error C2664: ‘int Math::Inc(char *)’:
cannot convert argument 1 from

‘double *’ to ‘char *’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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;
}

A

Eroare la compilare
Nu putem face MO deoarece Add are aceeasi signatura si tipuri de return diferite

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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’);
}

A

int Math::Inc(int v1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

void Compute(int x, const void* y, char z)
Compute(NULL, “C++”, ‘A’)

A

promotie

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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;
}

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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);
}

A

Ambiguu
1.0 este double value poate fi promovat atat de double, cat si la int

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

class Math
{
public:
int Inc(char* v1) { return 1; }
};
void main()
{
Math m;
m.Inc(1.0);
}

A

error C2664: ‘int Math::Inc(char *)’:
cannot convert argument 1 from

‘double’ to ‘char *’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

void Compute(int x, double y, char z)
Compute(3, 1.5, ‘A’)

A

Conversie

17
Q

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(100);
}

A

int Math::Inc(int v1)