Question
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
{
return node_iterator
}
};
class list {
public:
typedef int value_type;
typedef DSDLL::node
typedef node_iterator
typedef node_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
int main() {
list
numbers.insert(12);
numbers.insert(34);
std::cout << numbers << std::endl;
addItem(numbers, 10);
std::cout << numbers << std::endl;
list
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
for ( auto it = container.begin() ; it != container.end() ; ++it ) {
*it += item;
}
}
template <typename T>
std::ostream& operator<<(std::ostream& out, const list
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.
- 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.
- Absolutely no #includes in your implementation other than ostream and includes already included in provided files
- No recursion!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started