//--------------------------------------------------------------------- // Sýnislausn á dæmi 2 á Vikublaði 4 í Tölvunarfræði 2, vor 2005 // // Hjálmtýr Hafsteinsson, janúar 2005 //--------------------------------------------------------------------- #include using namespace std; class Cat { public: Cat(int initialAge); ~Cat(); int GetAge(); void SetAge(int age); void Meow(); // Samanburðarvirkjar fyrir Cat bool operator< (Cat &rhs); bool operator<= (Cat &rhs); bool operator> (Cat &rhs); bool operator>= (Cat &rhs); bool operator== (Cat &rhs); bool operator!= (Cat &rhs); private: int itsAge; }; // Útfærsla á aðferðum Cat klasa Cat::Cat(int initialAge) { itsAge = initialAge; } Cat::~Cat() { } int Cat::GetAge() { return itsAge; } void Cat::SetAge(int age) { itsAge = age; } void Cat::Meow() { cout << "Meow.\n"; } bool Cat::operator< (Cat &rhs) { return itsAge < rhs.GetAge(); } bool Cat::operator<= (Cat &rhs) { return itsAge <= rhs.GetAge(); } bool Cat::operator> (Cat &rhs) { return itsAge > rhs.GetAge(); } bool Cat::operator>= (Cat &rhs) { return itsAge >= rhs.GetAge(); } bool Cat::operator== (Cat &rhs) { return itsAge == rhs.GetAge(); } bool Cat::operator!= (Cat &rhs) { return itsAge != rhs.GetAge(); } // Aðalforrit sem prófar virkjana int main() { Cat Mjasi(5); Cat Mosi(3); if( Mjasi < Mosi ) cout << "Mjasi er yngri" << endl; else if( Mjasi == Mosi ) cout << "Mjasi og Mosi eru jafngamlir" << endl; else cout << "Mjasi er eldri" << endl; return 0; }