STARTING OUT WITH C++ MPL
STARTING OUT WITH C++ MPL
9th Edition
ISBN: 9780136673989
Author: GADDIS
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++
Following is the member function clear() in the class template List, please complete this function with a single looping statement. template void List::clear() { }
#include #include using namespace std; class Staff{ public: int code; string name; public: Staff(){} Staff(int code, string name){ this->code = code; this->name = name; } Staff(const Staff &s){ this->code=s.code; this->name = s.name; } int getCode(){ return code; } void setCode(int c){ code = c; } string getName(){ return name; } void setName(string n){ name = n; }; class Faculty: public Staff{ public: int department; string subjectTaken; string researchArea; Faculty(int code,string name,int department, string subjectTaken, string researchArea):Staff(code,name) { this->department = department; this->subjectTaken = subjectTaken; this->researchArea = researchArea; } Faculty() { } Faculty(const Faculty &f){ code = f.code; name = f.name; department = f.department; subjectTaken = f.subjectTaken; researchArea = f.researchArea; } int getDepartment() { return department; } void setDepartment(int department) {…

Chapter 16 Solutions

STARTING OUT WITH C++ MPL

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