Commit 47f05727 authored by Rachel Wil Sha Singh's avatar Rachel Wil Sha Singh 💬
Browse files

Templated function and class

parent 0a09a212
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="Templated Array" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/Templated Array" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/Templated Array" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="main.cpp" />
<Extensions>
<code_completion />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>
# depslib dependency file v1.0
1599586304 source:/home/wilsha/TEACHING/PUBLIC/cs250-basic-data-structures-with-cpp/example-code/Templates/Templated Array/main.cpp
"TemplatedArray.hpp"
<iostream>
1599586340 /home/wilsha/TEACHING/PUBLIC/cs250-basic-data-structures-with-cpp/example-code/Templates/Templated Array/TemplatedArray.hpp
<iostream>
<stdexcept>
1599586201 source:/home/wilsha/TEACHING/PUBLIC/cs250-basic-data-structures-with-cpp/example-code/Templates/Templated Array/TemplatedArray.cpp
#ifndef _TEMPLATED_ARRAY
#define _TEMPLATED_ARRAY
#include <iostream>
#include <stdexcept>
using namespace std;
template <typename T>
class TemplatedArray
{
public:
TemplatedArray();
TemplatedArray( int size );
~TemplatedArray();
void PushToBack( T item );
void RemoveFromBack();
bool IsFull();
bool IsEmpty();
void Display();
int Size();
private:
void AllocateMemory( int size );
void DeallocateMemory();
int m_arraySize;
int m_storedItems;
T* m_array;
};
template <typename T>
TemplatedArray<T>::TemplatedArray()
{
cout << endl << "Function: Default constructor" << endl;
m_arraySize = 0;
m_storedItems = 0;
m_array = nullptr;
}
template <typename T>
TemplatedArray<T>::TemplatedArray( int size )
{
cout << endl << "Function: Constructor with size " << size << endl;
m_array = nullptr;
AllocateMemory( size );
}
template <typename T>
TemplatedArray<T>::~TemplatedArray()
{
cout << endl << "Function: Destructor" << endl;
DeallocateMemory();
}
template <typename T>
void TemplatedArray<T>::PushToBack( T item )
{
cout << endl << "Function: PushToBack item " << item << endl;
if ( IsFull() )
{
throw runtime_error( "Array is full!" );
}
if ( m_array == nullptr )
{
AllocateMemory( 10 );
}
m_array[ m_storedItems ] = item;
m_storedItems++;
}
template <typename T>
void TemplatedArray<T>::RemoveFromBack()
{
cout << endl << "Function: RemoveFromBack" << endl;
if ( IsEmpty() )
{
throw runtime_error( "Array is empty!" );
}
// Lazy deletion
m_storedItems--;
}
template <typename T>
bool TemplatedArray<T>::IsFull()
{
cout << endl << "Function: IsFull?" << endl;
return ( m_arraySize == m_storedItems );
}
template <typename T>
bool TemplatedArray<T>::IsEmpty()
{
cout << endl << "Function: IsEmpty?" << endl;
return ( m_storedItems == 0 );
}
template <typename T>
void TemplatedArray<T>::Display()
{
cout << endl << "Function: Display" << endl;
for ( int i = 0; i < m_storedItems; i++ )
{
cout << i << ". " << m_array[i] << endl;
}
}
template <typename T>
int TemplatedArray<T>::Size()
{
cout << endl << "Function: Size" << endl;
return m_storedItems;
}
template <typename T>
void TemplatedArray<T>::AllocateMemory( int size )
{
cout << endl << "Function: AllocateMemory with size " << size << endl;
// Clear out any memory currently stored
DeallocateMemory();
m_array = new T[ size ];
m_arraySize = size;
m_storedItems = 0;
}
template <typename T>
void TemplatedArray<T>::DeallocateMemory()
{
cout << endl << "Function: DeallocateMemory" << endl;
// Free the memory allocated
if ( m_array != nullptr )
{
delete [] m_array;
m_array = nullptr;
m_arraySize = 0;
m_storedItems = 0;
}
}
#endif
#include "TemplatedArray.hpp"
#include <iostream>
using namespace std;
int main()
{
TemplatedArray<string> myList( 10 );
try
{
myList.RemoveFromBack();
}
catch( runtime_error ex )
{
cout << "ERROR: " << ex.what() << endl;
}
myList.PushToBack( "cat" );
myList.PushToBack( "rat" );
myList.PushToBack( "bat" );
myList.Display();
myList.RemoveFromBack();
myList.Display();
return 0;
}
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="Templated Function" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/Templated Function" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/Templated Function" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="templatedFunction.cpp" />
<Extensions>
<code_completion />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>
# depslib dependency file v1.0
1599584376 source:/home/wilsha/TEACHING/PUBLIC/cs250-basic-data-structures-with-cpp/example-code/Templates/Templated Function/templatedFunction.cpp
<iostream>
#include <iostream>
using namespace std;
template <typename T>
T Sum( T numA, T numB )
{
return numA + numB;
}
int main()
{
int intA = 4, intB = 6;
float floatA = 3.9, floatB = 2.5;
string strA = "alpha", strB = "bet";
cout << intA << " + " << intB
<< " = " << Sum( intA, intB ) << endl;
cout << floatA << " + " << floatB
<< " = " << Sum( floatA, floatB ) << endl;
cout << strA << " + " << strB
<< " = " << Sum( strA, strB ) << endl;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment