0% found this document useful (0 votes)
349 views11 pages

Answers To Debugging Exercises Chap 14

This document provides the solutions to debugging exercises from Chapter 14: Templates in Thareja's book "Object Oriented Programming with C++". It gives the output for 7 code examples using templates and finds errors in 13 code examples involving templates.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
349 views11 pages

Answers To Debugging Exercises Chap 14

This document provides the solutions to debugging exercises from Chapter 14: Templates in Thareja's book "Object Oriented Programming with C++". It gives the output for 7 code examples using templates and finds errors in 13 code examples involving templates.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Thareja: Object Oriented Programming with C++

SOLUTIONS TO DEBUGGING EXERCISES (Find the output and


Find the error questions)

CHAPTER 14: TEMPLATES

Find the output of the following codes

1. #include <iostream.h>
template <class Type>
Type Max(Type Var1, Type Var2)
{
return Var1> Var2 ? Var1:Var2;
}
void main()
{ cout<<"\n Max(123, 456) = "<<Max(123,456);
cout<<"\n Max('z','B') = "<<Max('z','B');
cout<<"\n Max(12.34,3.45) = "<<Max(12.34,3.45);
}
Ans.
Max(123, 456) = 456

Max('z','B') = z

Max(12.34,3.45) = 12.34

2. #include<iostream.h>
template<class Type>
void print(Type data)
{ cout<<"\n"<<data;
}
void main()
{ int i=10;

©Oxford University Press India. All rights reserved.


Thareja: Object Oriented Programming with C++

SOLUTIONS TO DEBUGGING EXERCISES (Find the output and


Find the error questions)

float f= 123.456;
char c = 'A';
char *s = "Hello World";
print(i);
print(f);
print(s);
print(c);
}
Ans.
10

Hello World

123.456

3. #include <iostream.h>
template <class Type>
class Sample
{ public:
Sample();
~Sample();
Type Data(Type);
};

template <class Type>


Type Sample<Type>::Data(Type d)
{ return d;
}

©Oxford University Press India. All rights reserved.


Thareja: Object Oriented Programming with C++

SOLUTIONS TO DEBUGGING EXERCISES (Find the output and


Find the error questions)

template <class Type>


Sample<Type>::Sample()
{ cout<<"\n In Constructor";
}

template <class Type>


Sample<Type>::~Sample()
{ cout<<"\n In Destructor";
}
void main()
{ Sample<int> S1;
Sample<float> S2;
Sample<char> S3;
Sample<char*> S4;

cout<<"\n "<< S1.Data(11);


cout<<"\n "<<S2.Data(12.34);
cout<<"\n "<<S3.Data('A');
cout<<"\n "<<S4.Data("Hello World");
}
Ans.
In Constructor
In Constructor
In Constructor
In Constructor
11

©Oxford University Press India. All rights reserved.


Thareja: Object Oriented Programming with C++

SOLUTIONS TO DEBUGGING EXERCISES (Find the output and


Find the error questions)

12.34

Hello World

4. #include <iostream.h>
template<class T, class U>
void squareAndPrint(T x, U y)
{
cout<< x << " "<< x * x;
cout<< y <<" "<< y * y;
}
void main()
{
int ii = 2;
float jj = 2.1;
squareAndPrint<int, float>(ii, jj);
}
Ans. 2 4 2.1 4.41

5. #include <iostream.h>
template <class type>
class Test
{
public:
type Func(type Var)
{
return Var;

©Oxford University Press India. All rights reserved.


Thareja: Object Oriented Programming with C++

SOLUTIONS TO DEBUGGING EXERCISES (Find the output and


Find the error questions)

}
};
void main()
{
Test <int>T1;
Test <float>T2;
cout<< T1.Func(200);
cout<< T2.Func(3.123);
}
Ans. 2003.123

6. #include <iostream.h>
template<class Type>
Type square(Type x)
{
Type res = x * x;
return res;
};
void main()
{
int i = 2, ii;
float x = 2.2 , xx;
double y = 2.2, yy;
ii = square(i);
cout<< i <<" "<< ii;
yy = square(y);
cout<< y <<" "<< yy;
}

©Oxford University Press India. All rights reserved.


Thareja: Object Oriented Programming with C++

SOLUTIONS TO DEBUGGING EXERCISES (Find the output and


Find the error questions)

Ans. 2 4 2.2 4.84

7. #include <iostream.h>
class One
{
public:
One(int n)
{
cout<< n;
}
};
class Two : public One
{
public:
Two(int n, double d) : One(n)
{
cout<< d;
}
};
class Three : public Two
{
public:
Three(int n, double d, char ch) : Two(n, d)
{
cout<<ch;
}
};
void main()

©Oxford University Press India. All rights reserved.


Thareja: Object Oriented Programming with C++

SOLUTIONS TO DEBUGGING EXERCISES (Find the output and


Find the error questions)

{
Three three(5, 4.3, 'R');
}
Ans. 54.3R

Find the error in the following codes


1. template <class Type>
int square(int num)
{ return num*num;
}
Ans. Not a template function

2. template <class Type>


Type main()
{ Type a=3;
cout<<a;
}
Ans. Unresolved type of main()

3. template <class Type>


int square(int num)
{ return num*num;
}
void main()
{ void n = 2;
square(n);
}
Ans. Could not find a match for square(2)

©Oxford University Press India. All rights reserved.


Thareja: Object Oriented Programming with C++

SOLUTIONS TO DEBUGGING EXERCISES (Find the output and


Find the error questions)

4. template (class Type)


int square(int num)
{ return num*num;
}

int square(Type1 num)


{ Type 1 res = num * num;
return res;
}
Ans. There should be angular brackets not round brackets int
template line

5. template <class Type1, class Type2>


int square(Type1 num)
{ Type 2 res = num * num;
return res;
}
Ans. Return type is specified as int and the value being
returned is of Type2

6. template <class Type1, class Type2>


Type3 square(Type1 num)
{ Type 2 res = num * num;
return res;
}
Ans. Return type is specified as Type3 and the value being
returned is of Type2

©Oxford University Press India. All rights reserved.


Thareja: Object Oriented Programming with C++

SOLUTIONS TO DEBUGGING EXERCISES (Find the output and


Find the error questions)

7. template <class Type1, class Type2>


int square(Type1 num)
{ Type 2 res = num * num;
return res;
}
Ans. Return type is specified as int and the value being
returned is of Type2

8. template <class Type1, int size=10>


void add(Type1 arr)
{ for(inti=0;i<size;i++)
arr[i] += 2;
}
Ans. No error
9. template <class Type1, int Type2>
int square(Type1 num)
{ Type2 res = num * num;
return res;
}
Ans. Cannot specify int for second template argument

10. template <class Type1 = int, class Type2>


Type2 avg(Type1 arr)
{ int sum=0;
for(inti=0;i<size;i++)
sum += arr[i];
return (Type2)sum/size;
}

©Oxford University Press India. All rights reserved.


Thareja: Object Oriented Programming with C++

SOLUTIONS TO DEBUGGING EXERCISES (Find the output and


Find the error questions)

Ans. Cannot instantiate template argument at the time of


declaration

11. template <class Type1, class Type1>


void add(Type1 arr)
{ for(inti=0;i<size;i++)
arr[i] += 2;
}
Ans. Multiple definition of Type1 argument

12. template <class Type>


Type max(Type a, Type b)
{ if(a>b)
return a;
else
return b;
}
main()
{ int x;
max(x,20);
}
Ans. No error

13. #include<iostream.h>
template<class Type1, class Type2>
class Person
{ private:
Type1 mt1;

©Oxford University Press India. All rights reserved.


Thareja: Object Oriented Programming with C++

SOLUTIONS TO DEBUGGING EXERCISES (Find the output and


Find the error questions)

Type2 mt2;
public:
template<class Type1, class Type2>
Person(Type1 t1, Type2 t2)
{ mt1 = t1;
t2 = t2;
cout<<mt1<<mt2;
}
};
void main()
{ Person <int, float>P1(1, 3.4);
}
Ans. Template declaration error

©Oxford University Press India. All rights reserved.

You might also like