12.1

What is generic programming? How is it implemented in C++?

Answer

Generic programming is an approach where generic types are used as parameters in algorithms so that they work for variety of suitable data types and data structures.

It is implemented in C++ using class templates.

12.2

A template can be considered as a kind of macro. Then, what is the difference between them?

Answer

Temples are a feature of the C++ programming language at allows functions and classes to be operated with generic types. This allows a functions or class to work on many different data types without being written for each one. Temples are of great utility programmers in C++, especially when combined with multiple inheritance and operator overloading. The C++ STL provides many useful functions within a framework of connected templates.

On the other hand the # define directive is typically used to associate meaningful identifiers with constants, keywords and commonly used statements or expressions. Identifiers that represent constants are sometimes called “symbolic constant” or “manifest constant”.

Identifiers that represents statements or expressions are called “macros”. In this preprocessor documentation only the term ‘macro’ is used.

12.3

Distinguish between overloaded functions and junction templates.

Answer

Basic difference is function body will not differ in function overloading.

12.4

Distinguish between the terms class template and template class.

Answer

Class template is a generic class. The class template definition is very similar to an ordinary class definition except the prefix template and the use of type T.

On the other hand a class created from a class template is called a template class. The process of creating a specific class from a class template is called instantiation.

12.5

A class (or function) template is known as a parameterized class (or function). Comment.

Answer

When an object of specific type is defined for actual use, the template definition for that class is substituted with the required data type. Since a template is defined with a parameter that would be replaced by a specified data type at the time of actual use of the class or function, the templates are called parameterized classes or functions.

12.6

State which of the following definitions are illegal.

  • (a) temnplate
    class city
    {…… };
  • (b) template
    class city
    {….. }
  • (c) template
    class city
    {…… };
  • (d) template
    class city
    {….. };
  • (e) class
    class list
    {….. };
  • (f) class
    class list
    {….. };
Answer
  • (a) Ok
  • (b) Illegal (type name expected)
    template < class P, class R, class s>
    class city
    {……}
  • (c) Ok
  • (d) Ok
  • (e) Ok
  • (f) Ok
12.7

Identify which of the following junction template definitions are illegal.

  • (a) template
    void fun(A, B)
    {…. };
  • (b) template
    void fun(A, A)
    {….. };
  • (c) template
    void fun(A, A)
    {….. };
  • (d) template
    T fun(T, R)
    {….. };
  • (c) template
    A fun(int A)
    {….. };
Answer
  • (a) Illegal
  • (b) Ok
  • (c) Ok
  • (d) Ok

 


Next Previous