logo Use CA10RAM to get 10%* Discount.
Order Nowlogo
(5/5)

we will look at some examples of Template Specialization.

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

Template Lab

In this lab, we will look at some examples of Template Specialization. The files are in ~cs253/Lab/Template. Create a file called results.cc, and turn it in for credit. You must compile it with

g++ -std=c++17 -Wall, and that must not produce any warnings or errors. A video introduction is available.

A Bar Graph Template

In 1.cc, we have a program that defines the BarGraph

template. Here's how to use it:

BarGraph alpha;

alpha += 12;

Oh, you're special?

alpha += 6;

alpha += 4;

imgflip.com

alpha += 6; alpha += 6; alpha.dump();

Tell me about it.

This create a Bargraph of ints, uses it to count four ints, and then produce this bar graph:

4 * 6 ***

12 *

This template class has several features worth discussion:

It uses a std::map to count the elements as they come in. The key of the map is the templated type T, an int, in this case.

The value of the map is an unsigned int. It's unsigned because it's a counter, and counters can't be negative. We may as well use an unsigned int, which has no negative values, but can hold twice as many positive values.

The map is sorted by the key (an int, in this case). Therefore, the output is in the order 4, 6, 12.

It overloads += to "add" items to the bar graph. The return type of this operator+= is void. += usually returns a reference to this, because all standard assignment operators do that, but it's only a convention, • Batrruleh uses a std::string temporary to print a number of stars. That is, string(23,'*') calls the string ctor to creates a temporary unnamed string that contains 23 asterisks.

• It uses an iterator to traverse (walk through) the sorted map.

Advantages of templates

The benefit of a template is its generality. BarGraph should work for any type, as long as it is (think "Duck Typing"):

⚫ copyable (copy ctor & operator=)

• comparable via <

• printable via

 

(5/5)
Attachments:

Related Questions

. Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

. The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

. SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

. Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Husnain SaeedComputer science

837 Answers

Hire Me
expert
Atharva PatilComputer science

632 Answers

Hire Me
expert
Chrisantus MakokhaComputer science

547 Answers

Hire Me
expert
AyooluwaEducation

734 Answers

Hire Me

Get Free Quote!

374 Experts Online