Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the generic Container type using C++'s templating mechanism. Use the inclusion method for template programming by placing the member template definitions and the Container

Implement the generic Container type using C++'s templating mechanism. Use the inclusion method for template programming by placing the member template definitions and the Container template declaration in a single file called Container.hpp. The Container type will have the functionality as provided in the interface shown. Implement only the empty, add_item, get_item, remove_item methods.

_________________________________________

# ifndef _CONTAINER_HPP # define _CONTAINER_HPP # include  # include  template  class Container { public : using value_type = T; //T = Type_name void add_item (T item ); // output container full , if add_item cannot add T get_item (int index ); // throw a string if index out of bounds void remove_item (T item ); // remove first occurrence of item bool empty (); // check of Container is empty void clear (); // clear all contents , assign value_type constexpr int size (); // return current number of elements in container T* begin (); T* end (); private : int _size = 0; T container [N]; }; // Container interface /* Sample Template member function definitions */ template  constexpr int Container :: size (){ return _size ;} template  void Container :: clear (){ for (int i = 0; i < _size ; i++) container [i] = T(); // or value_type (); _size = 0; } template  T* Container :: begin (){ return _size ? & container [0] : nullptr ;} template  T* Container :: end (){ return begin () + _size ;} // // TODO : implement add_item , get_item , remove_item , empty // # endif

_____________________________________________________________

//Example tests of the Container type. Place in a file named A01.cpp # include  # include  # include "Container.hpp" int main () { std :: cout << " ******************* Test 1******************* "; std :: cout << " Testing Container  "; Container  container_of_strings ; std :: cout << " Testing Container :: add_items () {Green , Red , Black } "; container_of_strings.add_item ("Green "); container_of_strings.add_item ("Red"); container_of_strings.add_item ("Black "); std :: cout << " Testing Container :: range -based -for - loop () "; //Question: What is auto keyword in C++? //Answer: it is used for type deduction /* auto x = 2; auto y = 20.5; cout << typeid(x).name() << endl; //reads i(int) cout << typeid(y).name() << endl; //reads d(double) */ for ( auto & str : container_of_strings ) std :: cout << str << " "; std :: cout << " Testing Container :: remove_item (Red ) "; container_of_strings.remove_item ("Red"); std :: cout << " Testing Container :: range -based -for - loop () "; for ( auto & str : container_of_strings ) std :: cout << str << " "; std :: cout << " Testing Container :: size () "; std :: cout << " container_of_strings . size ()=" << container_of_strings.size() << " "; std :: cout << " ******************* Test 2******************* "; std :: cout << " Testing Container :: add_item (){0 ,2 ,4 ,... ,64 ,81} "; Container  container_of_ints ; for (int i = 0; i < 10; i++) container_of_ints.add_item(i * i); std :: cout << " Testing Container :: range -based -for - loop "; for ( auto num : container_of_ints ) std :: cout << num << " "; std :: cout << " Testing Container :: remove_item (16) "; container_of_ints.remove_item(16); std :: cout << " Testing Container :: range -based -for - loop "; for ( auto num : container_of_ints ) std :: cout << num << " "; std :: cout << " Testing Container :: clear () "; container_of_ints.clear(); std :: cout << " Testing Container :: empty () "; std :: cout << " container_of_ints . empty () is " << ( container_of_ints.empty()) ? " True " : "False"; std :: cout << " ***** Test completed , enter any key to exit ******* "; char s; std :: cin >> s; return 0; } /* Example output from tests: *******************Test 1******************* Testing Container Testing Container::add_items() {Green, Red, Black} Testing Container::range-based-for-loop() Green Red Black Testing Container::remove_item(Red) Testing Container::range-based-for-loop() Green Black Testing Container::size() container_of_strings.size()=2 *******************Test 2******************* Testing Container::add_item(){0,2,4,...,64,81} Testing Container::range-based-for-loop 0 1 4 9 16 25 36 49 64 81 Testing Container::remove_item(16) Testing Container::range-based-for-loop 0 1 4 9 25 36 49 64 81 Testing Container::clear() Testing Container::empty() container_of_ints.empty() is True */

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Relational Theory Normal Forms And All That Jazz

Authors: Chris Date

1st Edition

1449328016, 978-1449328016

More Books

Students also viewed these Databases questions