CPSC 102-001 Computer Science II
(Advanced C Programming)

Fall 2010
09:05-09:55MW Daniel 415
Lab 11

Objectives:
To implement and exercise the object_t and plane_t objects

What to hand in:
A tar.gz archive of your lab11/ directory, including:
  1. A README file containing
    1. Course id--section no
    2. Name
    3. Assignment description
    4. Brief solution description (e.g., program design, description of algorithm, etc., however appropriate).
    5. Lessons learned, identified interesting features of your program
    6. Any special usage instructions
  2. Makefile
  3. source code (.h headers and .cpp source)
  4. object code (do a make clean before tar)
How to hand in:
See sendlab notes

Description:
  1. Following the class notes, define the object_t class in the object.h interface, providing the following class member functions:
      // constructors (overloaded)
      object_t(std::string itype = std::string("unknown"));
    
      // copy constructor
      object_t(const object_t& rhs);
    
      // destructor (default ok)
      ~object_t();
    
      // assignment operator
      const object_t& operator=(const object_t& rhs);
    
      // friends (i/o)
      friend std::ostream& operator<<(std::ostream& s, const object_t& rhs)
            { return rhs.put(s); }
      friend std::ostream& operator<<(std::ostream& s, object_t *rhs)
            { return(s << (*rhs)); }
    
      friend std::istream& operator>>(std::istream& s, object_t& rhs)
            { return rhs.get(s); }
      friend std::istream& operator>>(std::istream& s, object_t *rhs)
            { return(s >> (*rhs)); }
    
    
      // methods
      int           getcookie();
      std::string   gettype();
      std::string   getname();
    
      virtual std::ostream& put(std::ostream& s) const;
      virtual std::istream& get(std::istream& s);
    
      // protected data members
      protected:
      int           cookie;         // magic number
      std::string   type;           // e.g., plane, sphere, etc.
      std::string   name;           // e.g., left_wall, center_sphere, etc.
      std::string   material;       // material
    		

  2. The two i/o virtual member functions (a.k.a. methods) put() and get() must be implemented in the object_t implementation object.cpp
  3. Following the class notes, define the plane_t class in the plane_t.h interface, providing the following class member functions:
      // constructors (overloaded)
      plane_t(std::string token);
    
      // copy constructor
      plane_t(const plane_t& rhs);
    
      // destructors (default ok)
      ~plane_t();
    
      // assignment operator
      const plane_t& operator=(const plane_t& rhs);
    
      // friends
      std::ostream& put(std::ostream& s) const;
      std::istream& get(std::istream& s);
    
      private:
      vec_t normal;
      vec_t point;
      double ndotq;
    		
  4. The two i/o virtual member functions (a.k.a. methods) put() and get() must be implemented in the plane_t implementation plane.cpp
  5. Your code should be such that the following main() routine works unaltered:
    int main(int argc, char *argv[])
    {
            list_t          mats;
            list_t          objs;
            material_t      *mat;
            object_t        *obj;
            std::string     token,name;
    
      // input should consist only of material definitions
      // but there can be any number of material defs in the file
      while(!std::cin.eof()) {
    
        if((std::cin >> token).good()) {
    
          if(token == "material") {
            std::cin >> (mat = new material_t());
            std::cerr << "loaded " << mat->getname() << std::endl;
            mats.add((void *)mat);
          }
    
          if(token == "plane") {
            std::cin >> (obj = new plane_t(token));
            std::cerr << "loaded " << obj->getname() << std::endl;
            objs.add((void *)obj);
          }
        }
      }
    
      // verify that we have all materials
      material_list_print(mats);
    
      // verify that we have all materials
      object_list_print(objs);
    }
    		
  6. Don't forget to provide the following to functions in main.cpp
    
    void        object_list_print(list_t& objs);
    void        material_list_print(list_t& mats);
    material_t* material_getbyname(list_t& mats, std::string name);
    		
  7. Sample input:
    material green
    {
       ambient 0 5 0
    }
    
    material brown
    {
       ambient 3 3 0
    }
    
    plane wall
    {
       material green
       normal 0 0 1
       point  0 0 -7
    }
    
    plane floor
    {
       material brown
       normal 0 1 1
       point  0 0 -7
    }
    		
  8. Sample output
    loaded green
    loaded brown
    loaded wall
    loaded floor
    material green
    {
       ambient 0 5 0
    }
    
    material brown
    {
       ambient 3 3 0
    }
    
    plane wall
    {
      material green
      normal 0 0 1
      point  0 0 -7
    }
    
    plane floor
    {
      material brown
      normal 0 1 1
      point  0 0 -7
    }