13.1

Identify the error in the following program.

#include<iostream>
using namespace std;
class Test
{
      int intNumber;
      float floatNumber;
public:
      Test()
      {
           intNumber = 0;
           floatNumber = 0.0;
      }
      int getNumber()
      {
          return intNumber;
      }
      float getNumber()
      {
          return floatNumber;
      }
};
void main ()
     {
          Test objTest1;
          objTest1.getNumber();
     }
Answer

It show ambiguity error, because the compiler consider int getNumber() and float getNumber() as same function. It happened because you write objtest1.getNumber(); in the main() function

13.2

Identify the error in the following program.

#include<iostream>
using namespace std;
template <class R1, class T2>
class Person
{
      T1 m_t1;
      T2 m_t2;
public:
      Person(T1 t1, T2 t2)
      {
           m_t1 = t1;
           m_t2 = t2;
           cout << m_t1 << " " << m_t2 <<end1;
      }
      Person(T2 t2, T1 t1)
      {
           m_t2 = t2;
           m_t1 = t1;
           cout << m_t1 << " " << m_t2 <<end1;
      }
};
void main ()
     {
          Person< int, float> objPerson1(1, 2.345);
          Person<float, char> objPerson2(2.132, 'r');
     }

Answer

Here the two functions [‘person (T1 t1, T2, t2)’ and ‘person (T2 t2, T1, t1)’] are same. So you can write one of them.

13.3

Identify the error in the following program.

#include<iostream>
using namespace std;
template<class T1, class T2>
T1& MinMax(T1 t1, T1 t2)
{
    return t1 > t2 ? ta : t2;
    cout << " ";
}
void main()
{
     cout << ++MinMax(2, 3);
}
Answer

There is no error in this program. It will run successfully.

13.4

Find errors, if any, in the following code segment.

template<class T>
T max(T, T)
{..... };
unsigned int m;
int main()
{
    max(m, 100);
}
Answer

First you declared T as int type data and then declared as unsigned int type. So it will show error. If you write this as,
unsigned int n = 100; [N.B must be in main() function]
and max (m, n);
then it will not show any error.
Note: Write ‘return 0;’ in the main() function.


Next Previous