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

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

Objectives:
To implement and exercise the material_t object

What to hand in:
A tar.gz archive of your lab10/ 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 material_t class in the material.h interface, providing the following class member functions:
      // constructors (overloaded)
      material_t();
    
      // copy constructor
      material_t(const material_t& rhs);
    
      // destructor (default ok)
      ~material_t();
    
      // assignment operator
      const material_t& operator=(const material_t& rhs);
    
      // friends (i/o)
      friend std::ostream& operator<<(std::ostream& s, const material_t& rhs);
      friend std::ostream& operator<<(std::ostream& s, material_t *rhs);
    
      // methods
      int           getcookie();
      std::string   getname(;
      drgb_t        getamb();
      drgb_t        getdiff();
      drgb_t        getspec();
    
      // private data members
      private:
      int           cookie;         // magic number
      std::string   name;           // material name
      drgb_t        ambient;        // ambient color
      drgb_t        diffuse;        // ambient color
      drgb_t        specular;       // ambient color
    		

  2. The two i/o friend member functions (a.k.a. methods) operator<<() and operator>>() must be implemented in the material_t implementation material.cpp
  3. Your code should be such that the following main() routine works unaltered:
    int main()
    {
            list_t          mats;
            material_t      *mat;
            std::string     entity,name;
    
      // input should consist only of material definitions
      // but there can be any number of material defs in the file
      while(!std::cin.eof()) {
        std::cin >> entity;
        if(entity == "material" && std::cin.good()) {
          std::cin >> (mat = new material_t());
          std::cerr << mat->getname() << std::endl;
          mats.add((void *)mat);
        }
      }
    
      // read them all in...now try to print them
      material_list_print(mats);
    
      // see if we can find the first in the list, get its ambient data
      if((mat = material_getbyname(mats,"blue")) != NULL) {
        assert(mat->getcookie() == MAT_COOKIE);
        std::cerr << "found " << mat->getname() << std::endl;
        std::cerr << "ambient is: " << mat->getamb() << std::endl;
      }
      
      // see if we can find the last one, get its ambient data
      if((mat = material_getbyname(mats,"yellow")) != NULL) {
        assert(mat->getcookie() == MAT_COOKIE);
        std::cerr << "found " << mat->getname() << std::endl;
        std::cerr << "ambient is: " << mat->getamb() << std::endl;
      }
    
      // see what happens if we try to find a non-existent element
      if((mat = material_getbyname(mats,"chartreuse")) != NULL) {
        assert(mat->getcookie() == MAT_COOKIE);
        std::cerr << "found " << mat->getname() << std::endl;
        std::cerr << "ambient is: " << mat->getamb() << std::endl;
      }
    }
    		
  4. Don't forget to provide the following to functions in main.cpp
    
    void        material_list_print(list_t& mats);
    material_t* material_getbyname(list_t& mats, std::string name);
    		
  5. Sample input:
    material blue
    {
       ambient 0 0 5
       diffuse 0 0 5
    }
    
    material green
    {
       ambient 0 6 0
       diffuse 0 7 0
      specular 1 1 1
    }
    
    material yellow
    {
       ambient 7 6 0
       diffuse 7 7 0
    		
  6. Sample output
    blue
    green
    yellow
    material blue
    {
       ambient 0 0 5
       diffuse 0 0 5
    }
    
    material green
    {
       ambient 0 6 0
       diffuse 0 7 0
      specular 1 1 1
    }
    
    material yellow
    {
       ambient 7 6 0
       diffuse 7 7 0
    }
    
    found blue
    ambient is: 0 0 5
    found yellow
    ambient is: 7 6 0