Commit 994a9886 authored by Rachel Wil Sha Singh's avatar Rachel Wil Sha Singh 💬
Browse files

Operator Overloading lab

parent 50e12a4f
#include "CipherText.hpp"
CipherText::CipherText()
{
m_offset = 0;
m_text = "";
}
CipherText::CipherText( const CipherText& other )
{
m_offset = other.m_offset;
m_text = other.m_text;
}
CipherText::CipherText( const CipherText* other )
{
m_offset = other->m_offset;
m_text = other->m_text;
}
void CipherText::SetText( string text )
{
m_text = text;
m_offset = 0;
}
string CipherText::GetText() const
{
return m_text;
}
int CipherText::GetOffset() const
{
return m_offset;
}
void CipherText::Display() const
{
cout << m_text << endl;
}
void CipherText::OffsetText( int amount )
{
m_offset += amount;
for ( unsigned int i = 0; i < m_text.size(); i++ )
{
m_text[i] += amount;
}
}
CipherText CipherText::RemoveOffset()
{
CipherText temp( this );
temp = temp - temp.m_offset;
return temp;
}
CipherText& CipherText::operator=( const CipherText& other )
{
if ( this == &other ) return *this;
// Copy stuff over
return *this;
}
CipherText operator+( const CipherText& item, int amount )
{
return item; // PLACEHOLDER - REMOVE THIS
}
CipherText operator-( const CipherText& item, int amount )
{
return item; // PLACEHOLDER - REMOVE THIS
}
bool operator==( const CipherText& a, const CipherText& b )
{
return false; // PLACEHOLDER - REMOVE THIS
}
ostream& operator<<( ostream& out, CipherText& item )
{
return out;
}
istream& operator>>( istream& in, CipherText& item )
{
return in;
}
CipherText CipherText::operator[] ( const int index )
{
return CipherText(); // PLACEHOLDER - REMOVE THIS
}
#ifndef _CIPHER_TEXT
#define _CIPHER_TEXT
#include <iostream>
#include <string>
using namespace std;
class CipherText
{
public:
CipherText();
CipherText( const CipherText& other );
CipherText( const CipherText* other );
CipherText& operator=( const CipherText& other );
void SetText( string text );
string GetText() const;
int GetOffset() const;
void Display() const;
friend CipherText operator+( const CipherText& item, int amount );
friend CipherText operator-( const CipherText& item, int amount );
friend bool operator==( const CipherText& a, const CipherText& b );
friend ostream& operator<<( ostream& out, CipherText& item );
friend istream& operator>>( istream& in, CipherText& item );
CipherText operator[] ( const int index );
private:
int m_offset;
string m_text;
void OffsetText( int amount );
CipherText RemoveOffset();
};
#endif
#include "Program.hpp"
#include "utilities/Menu.hpp"
Program::Program()
{
m_cipherTexts[0].SetText( "Example text... 1 2 3" );
m_cipherTexts[1].SetText( "Example text... 1 2 3" );
m_done = false;
}
void Program::Run()
{
while ( !m_done )
{
Menu::ClearScreen();
Menu::Header( "CIPHER TEXT WIDGET" );
Header();
cout << endl << "OPERATIONS MENU" << endl;
Menu::ShowCallbackMenuWithPrompt( {
{ "Set text", bind( &Program::Menu_SetText, this ) },
{ "Get text", bind( &Program::Menu_GetText, this ) },
{ "Get offset", bind( &Program::Menu_GetOffset, this ) },
{ "Display", bind( &Program::Menu_Display, this ) },
{ "+ operator", bind( &Program::Menu_PlusOperator, this ) },
{ "- operator", bind( &Program::Menu_MinusOperator, this ) },
{ "a == b?", bind( &Program::Menu_EquivalentOperator, this ) },
{ "Output with <<", bind( &Program::Menu_OutputStreamOperator, this ) },
{ "Input with >>", bind( &Program::Menu_InputStreamOperator, this ) },
{ "Offset with []", bind( &Program::Menu_SubscriptOperator, this ) },
{ " QUIT", bind( &Program::Menu_Quit, this ) }
} )();
Menu::Pause();
}
}
void Program::Header()
{
cout << "m_cipherTexts[0]: " << m_cipherTexts[0].GetText() << " (" << m_cipherTexts[0].GetOffset() << ")" << endl;
cout << "m_cipherTexts[1]: " << m_cipherTexts[1].GetText() << " (" << m_cipherTexts[1].GetOffset() << ")" << endl;
cout << endl;
}
void Program::Menu_SetText()
{
Menu::ClearScreen();
Menu::Header( "Set Text" );
Header();
int index = Menu::GetValidChoice( 0, 1, "Enter which CipherText to modify (0, 1)" );
string str = Menu::GetStringChoice( "Enter a non-encrypted string to store." );
m_cipherTexts[index].SetText( str );
}
void Program::Menu_GetText()
{
Menu::ClearScreen();
Menu::Header( "Get Text" );
Header();
cout << "The m_cipherTexts[0] text currently is: " << m_cipherTexts[0].GetOffset() << endl;
cout << "The m_cipherTexts[1] text currently is: " << m_cipherTexts[1].GetOffset() << endl;
}
void Program::Menu_GetOffset()
{
Menu::ClearScreen();
Menu::Header( "Get Offset" );
Header();
cout << "The m_cipherTexts[0] offset currently is: " << m_cipherTexts[0].GetOffset() << endl;
cout << "The m_cipherTexts[1] offset currently is: " << m_cipherTexts[1].GetOffset() << endl;
}
void Program::Menu_Display()
{
Menu::ClearScreen();
Menu::Header( "Display" );
Header();
cout << "Calling m_cipherTexts[0].Display() ..." << endl;
m_cipherTexts[0].Display();
cout << "Calling m_cipherTexts[1].Display() ..." << endl;
m_cipherTexts[1].Display();
}
void Program::Menu_PlusOperator()
{
Menu::ClearScreen();
Menu::Header( "+ Operator" );
Header();
int index = Menu::GetValidChoice( 0, 1, "Enter which CipherText to modify (0, 1)" );
int offset = Menu::GetIntChoice( "Enter a number amount to add to the offset." );
m_cipherTexts[index] = m_cipherTexts[index] + offset;
cout << endl << "The CipherText string is now: " << m_cipherTexts[index].GetText() << endl;
}
void Program::Menu_MinusOperator()
{
Menu::ClearScreen();
Menu::Header( "- Operator" );
Header();
int index = Menu::GetValidChoice( 0, 1, "Enter which CipherText to modify (0, 1)" );
int offset = Menu::GetIntChoice( "Enter a number amount to subtract the offset." );
m_cipherTexts[index] = m_cipherTexts[index] - offset;
cout << endl << "The CipherText string is now: " << m_cipherTexts[index].GetText() << endl;
}
void Program::Menu_EquivalentOperator()
{
Menu::ClearScreen();
Menu::Header( "== Operator" );
Header();
cout << "Are m_cipherTexts[0] and m_cipherTexts[1] equvalent?" << endl;
if ( m_cipherTexts[0] == m_cipherTexts[1] )
{
cout << "They are equivalent." << endl;
}
else
{
cout << "They are NOT equivalent." << endl;
}
}
void Program::Menu_OutputStreamOperator()
{
Menu::ClearScreen();
Menu::Header( "<< Operator" );
Header();
cout << "m_cipherTexts[0] is " << m_cipherTexts[0] << endl;
cout << "m_cipherTexts[1] is " << m_cipherTexts[1] << endl;
}
void Program::Menu_InputStreamOperator()
{
Menu::ClearScreen();
Menu::Header( ">> Operator" );
Header();
int index = Menu::GetValidChoice( 0, 1, "Enter which CipherText to modify (0, 1)" );
cout << "Enter one word to store in the CipherText: ";
cin >> m_cipherTexts[index];
cout << endl << "The CipherText string is now: " << m_cipherTexts[index].GetText() << endl;
}
void Program::Menu_SubscriptOperator()
{
Menu::ClearScreen();
Menu::Header( "[] Operator" );
Header();
int index = Menu::GetValidChoice( 0, 1, "Enter which CipherText to modify (0, 1)" );
int offset = Menu::GetIntChoice( "Enter an offset amount to view (positive or negative is fine)." );
CipherText modified( m_cipherTexts[index][offset] );
cout << modified << endl;
cout << endl << "Do you want to save this string in m_cipherTexts[" << index << "]?" << endl;
string save = Menu::ShowStringMenuWithPrompt( { "yes", "no" } );
if ( save == "yes" )
{
m_cipherTexts[index] = modified;
}
}
void Program::Menu_Quit()
{
Menu::ClearScreen();
cout << "Goodbye." << endl;
m_done = true;
}
#ifndef _PROGRAM
#define _PROGRAM
#include "CipherText.hpp"
class Program
{
public:
Program();
void Run();
private:
CipherText m_cipherTexts[2];
bool m_done;
void Header();
void Menu_SetText();
void Menu_GetText();
void Menu_GetOffset();
void Menu_Display();
void Menu_PlusOperator();
void Menu_MinusOperator();
void Menu_EquivalentOperator();
void Menu_OutputStreamOperator();
void Menu_InputStreamOperator();
void Menu_SubscriptOperator();
void Menu_Quit();
};
#endif
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="OverloadedOperators_Lab" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/OverloadedOperators_Lab" 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/OverloadedOperators_Lab" 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="../CipherText.cpp" />
<Unit filename="../CipherText.hpp" />
<Unit filename="../Program.cpp" />
<Unit filename="../Program.hpp" />
<Unit filename="../main.cpp" />
<Unit filename="../utilities/Menu.cpp" />
<Unit filename="../utilities/Menu.hpp" />
<Extensions />
</Project>
</CodeBlocks_project_file>
# depslib dependency file v1.0
1600822939 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/OverloadedOperators/OverloadedOperators_Lab/main.cpp
<iostream>
<string>
"Program.hpp"
1600827887 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/OverloadedOperators/OverloadedOperators_Lab/CipherText.cpp
"CipherText.hpp"
1600827230 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/OverloadedOperators/OverloadedOperators_Lab/CipherText.hpp
<iostream>
<string>
1600823555 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/OverloadedOperators/OverloadedOperators_Lab/utilities/Menu.cpp
"Menu.hpp"
1600823555 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/OverloadedOperators/OverloadedOperators_Lab/utilities/Menu.hpp
<iostream>
<string>
<vector>
<cstdlib>
<map>
<limits>
<functional>
1600825158 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/OverloadedOperators/OverloadedOperators_Lab/Program.hpp
"CipherText.hpp"
1600825615 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/OverloadedOperators/OverloadedOperators_Lab/Program.cpp
"Program.hpp"
"utilities/Menu.hpp"
1600822939 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/OverloadedOperators/OverloadedOperators_Lab_Starter/main.cpp
<iostream>
<string>
"Program.hpp"
1600825158 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/OverloadedOperators/OverloadedOperators_Lab_Starter/Program.hpp
"CipherText.hpp"
1600827230 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/OverloadedOperators/OverloadedOperators_Lab_Starter/CipherText.hpp
<iostream>
<string>
1600825615 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/OverloadedOperators/OverloadedOperators_Lab_Starter/Program.cpp
"Program.hpp"
"utilities/Menu.hpp"
1600823555 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/OverloadedOperators/OverloadedOperators_Lab_Starter/utilities/Menu.hpp
<iostream>
<string>
<vector>
<cstdlib>
<map>
<limits>
<functional>
1600823555 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/OverloadedOperators/OverloadedOperators_Lab_Starter/utilities/Menu.cpp
"Menu.hpp"
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="../utilities/Menu.hpp" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="../CipherText.hpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="../Program.cpp" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="../utilities/Menu.cpp" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="../CipherText.cpp" open="1" top="1" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="1503" topLine="70" />
</Cursor>
</File>
<File name="../main.cpp" open="1" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="../Program.hpp" open="1" top="0" tabpos="7" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="268" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="../Program.hpp" open="1" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="../main.cpp" open="1" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="../utilities/Menu.hpp" open="1" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="../CipherText.cpp" open="1" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="../utilities/Menu.cpp" open="1" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="../Program.cpp" open="1" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="../CipherText.hpp" open="1" top="1" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>
#include <iostream>
#include <string>
using namespace std;
#include "Program.hpp"
int main()
{