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++: Early Objects
- In C++ The base class Pet has private data members petName, and petAge. The derived class Dog extends the Pet class and includes a private data member for dogBreed. Complete main() to: create a generic pet and print information using PrintInfo(). create a Dog pet, use PrintInfo() to print information, and add a statement to print the dog's breed using the GetBreed() function. Ex. If the input is: Dobby2Kreacher3German Schnauzer the output is: Pet Information: Name: Dobby Age: 2Pet Information: Name: Kreacher Age: 3 Breed: German Schnauzerarrow_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_forwardWhat must you be sure of when passing a class object to a function template that uses an operator, such as * or >?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_forwardC++ Q2: Write a program that defines a (value) as a base class with a set_values () function that give value to integer (val). Then define sub-class square, that calculate the square of the value squar (). In the main, define object of square (sq) and then call the functions in this object.arrow_forwardStatic analyzer write code c++ create your static analyzer tool thatreads your code as a text. Then, it analyzes it based on the below checklist. The checklist: - Do the attributes (e.g., data type and size) of each parameter match theattributes of each corresponding argument?arrow_forward
- Language C#arrow_forwardcse c++ coding questionarrow_forwardC++ You may work on a single class. The parameter and return type s of each function and class member function should be decided in advance. The program will be best implemented as a multi-file program, (header file, the main program,..) Design a generic class to hold the following information about a bank account: Balance Number of deposits this month Number of withdrawals Annual Interest Rate Monthly service charges The class should have the following member function: Constructors Accepts arguments for the balance and annual interest rate. deposit a virtual function that accepts an argument for the amount of the deposit. The function should add the argument to the account balance. It should also increment the variable holding the number of deposits. withdraw a virtual function that accepts an argument for the amount of the withdrawal. The function should subtract the argument from the account balance. It should also increment the variable…arrow_forward
- MindTap: In C#, Write a program named Averages that includes a method named Average that accepts any number of numeric parameters, displays them, and displays their average. Test your function in your Main(). Tests will be run against Average() to determine that it works correctly when passed one, two, or three numbers, or an array of numbers.arrow_forwardTopical Information Use C++. The purpose of this project is to test your ability to use templates, dynamic memory (mixed with classes), operator overloading, and libraries effectively in program design. Program Information Create a template class for a dynamic 1D array. You can model your class off of our String class or the dynamic list class. Changes from String class: You won't need an end-of-string element in the array. The element type is now templated. Translation will no longer be necessary. operators - and -= 'might' now make sense (erase element(s)?). ... Show how useful your template array class is by creating arrays of short integers doubles String class objects (static) Point class objects pointers to Point class objects (each allocated on the heap) — all in one test application. Make sure your test application is a good/thorough test of your class. (Your test application might utilize the applyand accumulatefunctions from lecture to facilitate testing. Also note how a…arrow_forwardC++ Define an "Expression" class that manages expression info: operand1 (integer), operand2 (integer), and the expression operator (char). It must provide at least the following method:- toString() to return a string containing all the expression info such as 50 + 50 = 100 Write a command-driven program named "ListExpressions" that will accept the following commands:add, listall, listbyoperator, listsummary, exit "add" command will read the expression and save it away in the list "listall" command will display all the expressions "listbyoperator" command will read in the operator and display only expressions with that given operator "listsummary" command will display the total number of expressions, the number of expressions for each operator, the largest and smallest expression values "exit" command will exit the program Requirements:- It must be using the array of pointers (not vector) to Expression objects to manage the list of Expression…arrow_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