Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Module 9

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 11

1, the program outputs 7

#include <iostream>

using namespace std;

template < class T > class Pocket


{
public:
T value;
Pocket (T value);
};
template <class T > Pocket < T >:: Pocket (T value):value (value)
{
} //line i
int main()
{
Pocket < double >a (7); //lineii
cout <<a.value <<endl;

return 0;
}

2, the program outputs 2.5

#include <iostream>

using namespace std;

template < class A > void


f (A & a) //linei
{
cout <<1 + a <<endl;
}
void f (double &a) //line ii
{
cout << 2 + a <<endl;
}
int main()
{
float a = 1.5;
f < float &> (a); //lineii
return 0;
}

3, the program outputs Hello World!

#include <iostream>

using namespace std;

template < class T > class Pocket


{
T _v;
public:
Pocket ()
{
}
Pocket (T v):_v (v)
{
}
T getV ()
{
return _v;
}
void add (T & a)
{
_v +=a;
}
};
int main()
{
Pocket < string >a ("Hello");
string s (" world!");
a.add (s);
cout <<a.getV () <<endl;

return 0;
}

4, compilation error in line ii

#include <iostream>

using namespace std;

template < class T > class Pocket


{
T value; //line i
public:
Pocket (T value):value (value)
{
}
};
int main()
{
Pocket < double >a (7);
cout << a.value << endl; //line ii

return 0;
}

5, compilation error in line ii

#include <iostream>

using namespace std;

template < class T > void


f (T & a) //linei
{
cout <<1 + a <<endl;
}
void f (double &a)
{
cout << 2 + a <<endl;
}
int main()
{
double a = 1.5;
f < float &> (a); //lineii
}
return 0;

6, output HiTech, HiTech (not in the choices)


compilation in line i is incorrect

#include <iostream>
#include <string>
using namespace std;

template < typename T > class Pocket


{
T value;
public:
Pocket ()
{
}
Pocket (T value);

T getValue()
{
return value;
}
void add (T _Right)
{
value += _Right;
}
friend ostream & operator<< (ostream & _os, const Pocket < T > &value)
{
_os << value.value;
return _os;
}
};
template <class T > Pocket < T >:: Pocket (T value):value (value)
{
}
int main ()
{
Pocket < string > a ("Hi");
string n ("Tech");
a.add (n); //line i
cout<< a <<", "; //line ii
cout<< a;
return 0;
}

7, the program outputs 7

#include <iostream>
#include <string>
using namespace std;

template < class T > class Pocket


{

public:
T value; //linei
Pocket (T value):value (value)
{
}
};

int main ()
{
Pocket < double > a (7);
cout<< a.value <<endl; //lineii
return 0;
}

8, the program outputs 3.5

#include <iostream>
#include <string>
using namespace std;

template < class T > void


f ( T & a) //line i
{
cout << 1 + a << endl;
}

void f (double &a) //line ii


{
cout << 2 + a << endl;
}
int main ()
{
double a = 1.5;
f (a); //lineii
return 0;
}

9, compilation error in line i

#include <iostream>
#include <string>
using namespace std;

template < typename T > class Pocket


{
T value;
public:
Pocket ()
{
}
Pocket (T value);
T getValue ()
{
return value;
}
void add(T _Right)
{
value += _Right;
}
void add (string & _Right)
{
value.insert (0, _Right);
}
};
template <class T > Pocket < T >:: Pocket (T value):value (value)
{
}
int main ()
{
Pocket < string > a ("Hi");
string n ("Tech");
a.add (n); //line i
cout<< a.getValue () <<", "; //line ii
cout<< a.getValue ();
return 0;

10, the program outputs 7,9

#include <iostream>
#include <string>
using namespace std;

template < class Ty > class Pocket


{
Ty value;
public:
Pocket ()
{
}
Pocket (Ty value);

Ty getValue()
{
return value;
}
void add (Ty _Right)
{
value += _Right;
}
template < class Tx > Tx get (Tx _Right)
{
return (Tx) (value) + _Right; //linei
}
friend ostream & operator<< (ostream & _os, const Pocket < Ty > &value)
{
_os << value.value;
return _os;
}
};
template <class T > Pocket < T >:: Pocket (T value):value (value)
{
}
int main ()
{
Pocket < int > a (7);
cout<< a <<", "; //line ii
cout<< a.get <double> (2);
return 0;
}

11, compilation error in line i (not sure)

#include <iostream>
using namespace std;

template < class T > class Pocket


{
public:
T value;
Pocket (T value);
};
template < class T > Pocket::Pocket (T value):value (value)
{
} //linei
int main ()
{
Pocket < double > a (7); //lineii
cout<< a.value <<endl;
return 0;
}

12, compilation error in line i

#include <iostream>
using namespace std;

template <typedef T> //linei


class Pocket
{
public:
T value;
Pocket (T value);
};
template < class T > Pocket <T> ::Pocket (T value):value (value)
{
} //lineii
int main ()
{
Pocket < double > a (7);
cout<< a.value <<endl;
return 0;
}

13, the program outputs 2

#include <iostream>
using namespace std;
template < class T> void
f ( T &a)//linei
{
cout << 1 + a << endl;
}
void f (double &a) //line ii
{
cout << 2 + &a << endl;
}
//lineii
int main ()
{
int a = 1.5;
f (a); //lineii
return 0;
}

14, compilation error in line ii

#include <iostream>
using namespace std;

class SomethingSpecial
{
};
template < typename T > class Pocket
{
T value;

public:

Pocket ()
{
}
Pocket (T value);

T getValue()
{
return value;
}
void add (T _Right)
{
value += _Right;
}
};
template < class T > Pocket < T >:: Pocket (T value):value (value)
{
}

int main ()
{
Pocket < double > a (7); //line i
Pocket < SomethingSpecial > n;
n.add (SomethingSpecial ()); //line ii
cout<< a.getValue () <<", ";
a.add (3);
cout<< a.getValue ();
return 0;

15, compilation error in line ii

#include <iostream>
using namespace std;

class SomethingSpecial
{
};
template < typename T > class Pocket
{
T value;

public:

Pocket ()
{
}
Pocket (T value);

T getValue()
{
return value;
}
void add (T _Right)
{
value += _Right;
}
};
template < class T > Pocket < T >:: Pocket (T value):value (value)
{
}

int main ()
{
Pocket < string > a ("Hi");
string n ("Maker");
a.add(n); //line i
cout<< a.getValue () <<", ";
a.add (3); //line ii
cout<< a.getValue ();
return 0;

16, the program outputs 10,13

#include <iostream>
using namespace std;

class NothingSpecial
{
};
template < typename T > class Pocket
{
T value; //line i

public:

Pocket ()
{
}
Pocket (T value);

T getValue()
{
return value;
}
void add (T _Right)
{
value += _Right;
}
};
template < class T > Pocket < T >:: Pocket (T value):value (value)
{
}

int main ()
{
Pocket < double > a (7);
Pocket <NothingSpecial>n; //line ii
a.add (3); //line i
cout<< a.getValue () <<", ";
a.add (3); //line ii
cout<< a.getValue ();
return 0;

17, compilation error in line ii (INCORRECT)

#include <iostream>
using namespace std;

class SomethingSpecial
{
double value;

SomethingSpecial (): value (0)


{
}

SomethingSpecial (double value): value (value)


{
}
SomethingSpecial operator+= (SomethingSpecial & _Right)
{
SomethingSpecial result;
result.value = value + _Right.value;
return result;
}
};

template < typename T > class Pocket


{
T value;

public:

Pocket ()
{
}
Pocket (T value);

T getValue()
{
return value;
}
void add (T _Right)
{
value += _Right;
}
};
template < class T > Pocket < T >:: Pocket (T value):value (value)
{
}

int main ()
{
Pocket < double > a (7); //line i
Pocket < SomethingSpecial > n;
n.add (SomethingSpecial ()); //line ii
cout<< a.getValue () <<", ";
a.add (3);
cout<< a.getValue ();
return 0;

18, the program outputs 7

#include <iostream>

using namespace std;

template < typename T > //linei


class Pocket
{
public:
T value;
Pocket (T value);
};
template <class T > Pocket < T >:: Pocket (T value):value (value)
{
} //line ii
int main()
{
Pocket < double >a (7);
cout <<a.value <<endl;
return 0;
}

19, class & typename

to define template type parameters

20, the program outputs 2

#include <iostream>

using namespace std;

template < class T > void


f (T & a) //linei
{
cout <<1 + a <<endl;
}
int main()
{
int a = 1;
f (a); //lineii
return 0;
}

You might also like