A.hpp
#include <string>
#include <vector>
class A
{
private:
std::vector <std::string> m_member;
public:
A(std::vector <std::string> input);
const std::string GetValue() const;
bool operator==(const A& rhs) const;
};
A.cpp
#include <string>
#include <vector>
#include "A.hpp"
A::A(std::vector <std::string> input) : m_member(input)
{
}
const std::string A::GetValue() const
{
std::string returnString;
for(std::vector <std::string>::const_iterator iter = m_member.begin(); iter != m_member.end(); ++iter)
{
returnString = returnString + *iter;
}
return returnString;
}
bool A::operator==(const A& rhs) const
{
std::string rhsValue = rhs.GetValue();
std::string lhsValue = this->GetValue();
//Equality is true if lhsValue is equal to rhsValue
return (lhsValue == rhsValue);
}
main.cpp
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include "A.hpp"
using namespace std;
int main(int argc, char *argv[])
{
vector <string> input1;
input1.push_back("Hello");
input1.push_back("World");
vector <string> input2;
input2.push_back("Fellow");
input2.push_back("World");
vector <string> input3;
input3.push_back("Hollow");
input3.push_back("World");
vector <string> input4;
input4.push_back("Hello");
input4.push_back("World");
A obj_A1(input1);
A obj_A2(input2);
A obj_A3(input3);
A obj_A4(input4);
vector <A> A_vec;
A_vec.push_back(obj_A1);
A_vec.push_back(obj_A2);
A_vec.push_back(obj_A3);
A* obj_p_A1 = new A(input1);
A* obj_p_A2 = new A(input2);
A* obj_p_A3 = new A(input3);
A* obj_p_A4 = new A(input4);
cout << obj_p_A2->GetValue() << endl;
cout << obj_p_A4->GetValue() << endl;
vector <A*> A_p_vec;
A_p_vec.push_back(obj_p_A1);
A_p_vec.push_back(obj_p_A2);
A_p_vec.push_back(obj_p_A3);
if(obj_A2 == obj_A4)
{
cout << "objects are equal" << endl;
}
else
{
cout << "objects are NOT equal" << endl;
}
if(find(A_vec.begin(), A_vec.end(), obj_A4) != A_vec.end())
{
cout << "obj_A4 found in A_vec" << endl;
}
if(*obj_p_A2 == *obj_p_A4)
{
cout << "objects pointers are equal" << endl;
}
else
{
cout << "objects pointers are NOT equal" << endl;
}
//if(find(A_p_vec.begin(), A_p_vec.end(), obj_p_A4) != A_p_vec.end()) // Overloaded comparison operator not
// called because pointer obj_p_A4
// has been used.
//{
// cout << "*obj_p_A4 found in A_p_vec" << endl;
//}
system("PAUSE");
return EXIT_SUCCESS;
}
No comments:
Post a Comment