Starting Out with C++: Early Objects (9th Edition)
Starting Out with C++: Early Objects (9th Edition)
9th Edition
ISBN: 9780134400242
Author: Tony Gaddis, Judy Walters, Godfrey Muganda
Publisher: PEARSON
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 16.4, Problem 16.10CP

Explanation of Solution

Class template:

In C++, a class template is used to create a common version of a class and it does not have any additional code for handling multiple data types.

Defining class template objects:

Definition of class template object is little different from normal class object definition.

  • The difference is, to specify the data type that user want to pass as the type parameter.
  • This data type should be placed inside a pair of an angle brackets, which is followed by the class name is as follows:

  Class_name<int> object_name;

From the above declaration, “int” is a data type for the type parameter of the given class.

Example:

For example, consider the following program which is used to square the given value of any types, in which the class template “List” has been defined and its object has been defined in main() function:

//include the required header files

#include <iostream>

using namespace std;

//class template

template<class T>

//definition of the template class

class List

{

    //declare the class data member

    T x;

    //access specifier for the class member functions

    public:

    //default constructor

    List()

    {

        x=0;

    }

    //this member function is used to get the user input

    void getvalues()

    {

        //get the value from the user

       ;&#x...

Blurred answer
Students have asked these similar questions
member and non-member functions c++
TRUE OR FALSE: In C++, comments that begin with the "multi-line" comment symbol can't be nested.   A member function of a class has access to only the public data members of the class.   If a C++ function does not take any parameters, parenthesis around the empty parameter list are still required.   The size of a pointer variable depends on the type that it is pointing to.
Suppose your program uses a class template named List, which is defined as template<class T> class List{// Members are declared here… }; Give an example of how you would use int as the data type in the declaration of a List object. (Assume the class has a default constructor.)

Chapter 16 Solutions

Starting Out with C++: Early Objects (9th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education