Next February 2001 Slide #25

Lab 6


  1. Implement a Person class where the Person objects may each have a
    name, age, and spouse. The class should support at least the following methods:
            $person = Person->new();
            $person = Person->new('Name');
            $person = Person->new('Name', 23);  # specify name and age
    
            $person->name('Name');              # set name
            $name = $person->name;              # get name
    
            $person->age(23);                   # set age 
            $age = $person->age;                # get age
    
            # This should also set $person2's spouse to $person1
            $person1->spouse($person2);         # set spouse
            $spouse = $person1->spouse;         # get spouse
            $spouse_name = $spouse->name;       # get spouse's name
    
            # This should return a string of the form:
            # "Person Claudius is 23 years old
            #   or
            # "Person Claudius is 23 years old and is married to Gertrude
            $person->as_string      
    

    Use the person-test.pl program that I supplied to test the class.


  2. If you have time, change the ->new method to increment a counter every time an object is created. Add a ->census class method that returns the value of the counter.


  3. If you have time, change the ->new method so that it may be called either as a class method or as an object method. That is,
            $person = Person->new(...);
    

    should continue to work as before, and

            $person2 = $person->new;
    

    should also work. If called as an object method, the constructor should return a copy of the object on whose behalf it is called.


  4. If you have time, change the implementation of the class so that the data for each person is backed on the disk somehow.



Next Copyright © 2001 M-J. Dominus