virtual bool equal(const Object* other_object) { return (id == other_object->id); } //at objects.h
//at object.cpp:
bool String::equal(const Object* other_object)
{
cout <<"the_string: " << endl;
//this->print();
cout << this->get_string() << endl;
cout <<"other_object the_string: " << endl;
//cout << dynamic_cast<String*>(other_object)->get_string() << endl;
//cout << ((String*)other_object)->get_string() << endl;
//cout << ((String*)other_object)->the_string << endl; --> also this line prints garbage
//((PassengerCompartment*)other_object)->get_description().print();
//check if strings are the same
if ( the_string == ((String*)other_object)->the_string)
{
//check id
if ( Object::equal(other_object) )
return true;
else
return false;
}
else
return false;
}
////////////////////////////////////////////
bool PlaneComponent::equal(const Object* other_object)
{
cout << "------------------" << endl;
cout << "------------------" << endl;
cout << "------------------" << endl;
cout << "------------------" << endl;
cout << "--------PlaneComponent::equal----------" << endl;
this->get_description().print();
((PassengerCompartment*) other_object)->get_description().print(); //is printed correctly!
cout << "--------PlaneComponent::equal----------" << endl;
cout << "------------------" << endl;
cout << "------------------" << endl;
cout << "------------------" << endl;
cout << "------------------" << endl;
//check if description is the same
if ( description.equal(other_object) )
{
//check id
if ( Object::equal(other_object) )
return true;
else
return false;
}
}
bool PassengerCompartment::equal(const Object* other_object)
{
bool result = false;
//if there is an inner compartment check if equal
if ( inner_PassengerCompartment != NULL ) ////to simplify the problem I create a
////NULL inner_PassengerCompartment
////so this block is not "active"
{ //manually check for it's description and ID to avoid recursion
if ( get_description() == ((PlaneComponent*)other_object)->get_description() )
{ //check IDs
if ( get_id() == other_object->get_id() )
result = true;
else result = false;
}
else result = false;
if ( inner_PassengerCompartment->PlaneComponent::equal( ((PassengerCompartment*)other_object)->inner_PassengerCompartment ) )
result = true;
else result = false;
}
//all the printing done below is correct!!
this->get_description().print();
((PassengerCompartment*) other_object)->get_description().print();
//check for the passenger compartment itself
if ( PlaneComponent::equal(other_object) )
result = true;
else result = false;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////COPY CONSTRUCTORS///////////////////////////////
///////////////////////////////COPY CONSTRUCTORS///////////////////////////////
///////////////////////////////////////////////////////////////////////////////
Object::Object(const Object& object_)
{
id = object_.id;
cout << "Copy of Object just created!" << endl;
}
String::String(const String& given) : Object(given)
{
the_string = given.the_string;
cout << "Copy of String just created!" << endl;
}
PlaneComponent::PlaneComponent(const PlaneComponent& given) : Object(given), description(given.description)
{
cout << description.get_string() << endl;
cout << "PlaneComponent just created!" << endl;
}
PassengerCompartment::PassengerCompartment(const PassengerCompartment& given) : PlaneComponent(given)
{
if (given.inner_PassengerCompartment != NULL )
{//if the given PassengerCompartment has an inner PassengerCompartment, copy that, too
inner_PassengerCompartment = new PassengerCompartment(*(given.inner_PassengerCompartment), true);
std::cout << "Inner PassengerCompartment just created!" << std::endl;
}
else
inner_PassengerCompartment = NULL;
std::cout << "PassengerCompartment just created!" << std::endl;
}
PassengerCompartment::PassengerCompartment(const PassengerCompartment& given, bool for_innerPC) : PlaneComponent(given)
{ //////this is done for the simplification of the problem
inner_PassengerCompartment = NULL;
}