Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this part of C++ lab make a template out of the myMax function and test it on different data types. Using maxTemplate.cpp Compile and

For this part of C++ lab make a template out of the myMax function and test it on different data types.

  • Using maxTemplate.cpp
  • Compile and run the program to see how it works.
  • Make a template out of myMax. Dont forget the return type.
  • Modify the prototype appropriately.
  • Test your myMax template on int, double, and string types.

When you are done your output should resemble this:

The max of 3 and 5 is 5

The max of 5.6 and 7.3 is 7.3

The max of donkey and apple is donkey

maxTemplate.cpp

#include

#include

using namespace std;

//Make a template out of the prototype

int myMax(int, int);

int main()

{

int i_one = 3, i_two = 5;

cout << "The max of " << i_one << " and " << i_two << " is " << myMax(i_one, i_two) << endl;

//Test your template on float and string types

return 0;

}

//Make a template out of this function. Don't forget the return type.

int myMax(int one, int two)

{

int bigger;

if (one < two)

{

bigger = two;

}

else

{

bigger = one;

}

return bigger;

}

Class Template

For this part of the lab you will make a template out a linked list class and a forward iterator.

list.h

#ifndef LIST_H_

#define LIST_H_

#include

#include

#include "node_dll.h"

namespace DS {

template<typename NT, typename VT>

class node_iterator : public std::iterator

{

private:

NT* itr;

public:

node_iterator(NT* nd = nullptr) : itr(nd) {}

node_iterator& operator++ () // Pre-increment

{

}

node_iterator operator++ (int) // Post-increment

{

}

// two-way comparison: v.begin() == v.cbegin() and vice versa

template<class TT>

bool operator == (const node_iterator& rhs)

{

}

template<class TT>

bool operator != (const node_iterator& rhs)

{

}

VT& operator* () const

{

}

VT& operator-> () const

{

}

// Hack to allow us to create one iterator for both const and mutator

operator node_iterator() const

{

return node_iterator(itr);

}

};

class list {

public:

typedef int value_type;

typedef DSDLL::node node;

typedef node_iterator iterator;

typedef node_iterator const_iterator;

iterator begin() { return iterator(head); }

iterator end() { return iterator(); }

const_iterator begin() const { return const_iterator(head); }

const_iterator end() const { return const_iterator(); }

const_iterator cbegin() const { return const_iterator(head); }

const_iterator cend() const { return const_iterator(); }

list() : head(nullptr), tail(nullptr) {}

virtual ~list();

void insert(const value_type&, node* = nullptr);

private:

node * head;

node * tail;

};

} /* namespace DS */

#endif /* LIST_H_ */

list.cpp

#include "list.h"

namespace DS {

list::~list() {

while ( head != nullptr ) {

node * deleteMe = head;

head = head->next();

delete deleteMe;

}

}

//Insert at cursor or at head when cursor is null

void list::insert(const value_type& value, node* cursor) {

if ( head == nullptr || cursor == nullptr || cursor == head) {

head = new node(value, head);

if ( tail == nullptr )

tail = head;

} else {

cursor->prev() = cursor->prev()->next() = new node(value, cursor, cursor->prev());

if ( tail->next() != nullptr )

tail = tail->next();

}

}

} /* namespace DS */

Driver

//============================================================================

// Name : lablisttplitr.cpp

// Author :

// Version :

// Copyright : Your copyright notice

// Description : Hello World in C++, Ansi-style

//============================================================================

#include

#include

#include "list-solution.h"

using namespace DS;

template <typename T>

std::ostream& operator<<(std::ostream& out, const list&);

template <typename T>

void addItem(list&, const T&);

int main() {

list numbers;

numbers.insert(12);

numbers.insert(34);

std::cout << numbers << std::endl;

addItem(numbers, 10);

std::cout << numbers << std::endl;

list names;

std::string last = " Foo";

names.insert("Wendy");

names.insert("Marcus");

std::cout << names << std::endl;

addItem(names, last);

std::cout << names << std::endl;

return 0;

}

template <typename T>

void addItem(list& container, const T& item) {

for ( auto it = container.begin() ; it != container.end() ; ++it ) {

*it += item;

}

}

template <typename T>

std::ostream& operator<<(std::ostream& out, const list& container) {

for ( auto it = container.cbegin() ; it != container.cend() ; ++it ) {

out << "->[" << *it << "]";

}

out << "--X";

return out;

}

Submit only maxTemplate.cpp, list.h

Templating will require the class implementation moved to list.h, once done make sure you delete list.cpp so that your IDE does not try to compile it. Do not create inline functions.

  1. Memory leaks will lead to grade leaks on this assignment. No heap memory should be in use when the main function reaches the return 0.
  2. Absolutely no #includes in your implementation other than ostream and includes already included in provided files
  3. No recursion!

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

Students also viewed these Databases questions