Concept explainers
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...
Want to see the full answer?
Check out a sample textbook solutionChapter 16 Solutions
STARTING OUT WITH C++ MPL
- 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.)arrow_forwardAssume the class NumList has overloaded the [] operator. In the expression below, list1 is an instance of the NumList class: list1[25]Rewrite this expression to explicitly call the function that overloads the [] operator.arrow_forward#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) {…arrow_forward
- Problem Attachedarrow_forwardPYTHON: Given a base Plant class and a derived Flower class, write a program to create a list called my_garden. Store objects that belong to the Plant class or the Flower class in the list. Create a function called print_list(), that uses the print_info() instance methods defined in the respective classes and prints each element in my_garden. The program should read plants or flowers from input (ending with -1), add each Plant or Flower to the my_garden list, and output each element in my_garden using the print_info() function.arrow_forwardC++ Please explain the code below. It doesn't have to be long, as long as you explain what the important parts of the code do. You can also explain it line by line for best ratings. Thank you so much! #include "list.h"#include <cstdlib>#include <iostream>#include <cmath>using namespace std;class ArrayList : public List { int* array; int index; int capacity; void dyn_all_add(){ int cap = ceil(capacity * 1.5); array = (int*)realloc(array,cap * sizeof(int)); capacity = cap; } void dyn_all_rem(){ int cap = capacity - (capacity/3); array = (int*)realloc(array,cap * sizeof(int)); capacity = cap; } public: // CONSTRUCTOR ArrayList() { capacity = 4; array = (int*)malloc(capacity); index = 0; } int add(int num) { if (index == capacity){ dyn_all_add(); } *(array + index) = num; index++; return…arrow_forward
- C++ Please explain the code below. It doesn't have to be long, as long as you explain what the important parts of the code do. You can also explain it line by line for best ratings. Thank you so much! #include "list.h"#include <cstdlib>#include <iostream>#include <cmath>using namespace std;class ArrayList : public List { int* array; int index; int capacity; void dyn_all_add(){ int cap = ceil(capacity * 1.5); array = (int*)realloc(array,cap * sizeof(int)); capacity = cap; } void dyn_all_rem(){ int cap = capacity - (capacity/3); array = (int*)realloc(array,cap * sizeof(int)); capacity = cap; } public: // CONSTRUCTOR ArrayList() { capacity = 4; array = (int*)malloc(capacity); index = 0; } int add(int num) { if (index == capacity){ dyn_all_add(); } *(array + index) = num; index++; return…arrow_forwardLanguage C#arrow_forwardComputer Science //iterator() creates a new Iterator over this list. It will//initially be referring to the first value in the list, unless the//list is empty, in which case it will be considered both "past start"//and "past end". template <typename ValueType>typename DoublyLinkedList<ValueType>::Iterator DoublyLinkedList<ValueType>::iterator(){//return iterator(head);} //constIterator() creates a new ConstIterator over this list. It will//initially be referring to the first value in the list, unless the//list is empty, in which case it will be considered both "past start"//and "past end". template <typename ValueType>typename DoublyLinkedList<ValueType>::ConstIterator DoublyLinkedList<ValueType>::constIterator() const{//return constIterator(head);} //Initializes a newly-constructed IteratorBase to operate on//the given list. It will initially be referring to the first//value in the list, unless the list is empty, in which case//it will be…arrow_forward
- C++ Programming Requirments: Please submit just one file for the classes and main to test the classes. Just create the classes before main and submit only one cpp file. Do not create separate header files. Please note: the deleteNode needs to initialize the *nodePtr and the *previousNode. The code to do this can be copied from here:ListNode *nodePtr, *previousNode = nullptr; Part 1: Your own Linked ListDesign your own linked list class to hold a series of integers. The class should have member functions for appending, inserting, and deleting nodes. Don't forget to add a destructor that destroys the list. Demonstrate the class with a driver program.Part 2: List PrintModify the linked list class you created in part 1 to add a print member function. The function should display all the values in the linked list. Test the class by starting with an empty list, adding some elements, and then printing the resulting list out.arrow_forwardJAVA CODEarrow_forwardcse c++ coding questionarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education