Advertisement
Guest User

Struct item

a guest
Mar 28th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <iostream>
  3. using namespace std;
  4. struct item
  5. {
  6.     double height;
  7.     double width;
  8.     double weight;
  9.  
  10.     string name;
  11.     string stateOfMatter;
  12.  
  13.     bool flammable;
  14.  
  15.     /**
  16.     *Item is the base structure for all subclasses.
  17.     *Every item should have these properties.
  18.     *@param height - The height of the object (feet).
  19.     *@param width  - The width of the object (feet).
  20.     *@param weight - The weight of the object. (Pounds)
  21.     *@param name   - The GENERIC name of the item. Birch wood, for example, would be wood.
  22.     *@param state  - The state of matter. The common states of matter are liquid, solid, and gas.
  23.     *@param flame  - Whether or not the item is flammable.
  24.     */
  25.     item(double h, double w,double weight, string n,
  26.          string state, bool flame)
  27.     {
  28.         this->height=h;
  29.         this->width=w;
  30.         this->weight=weight;
  31.         this->name=name;
  32.         this->flammable=flame;
  33.         this->stateOfMatter=state;
  34.     }
  35.  
  36. };
  37. struct wood:item
  38. {
  39.  
  40.     string type;
  41.     double age;
  42.     wood(string type, double age, double h, double w,double weight, string n,
  43.          string state, bool flame):item(h,w,weight,n,state,flame)
  44.     {
  45.         this->type=type;
  46.         this->age=age;
  47.     }
  48.     /**
  49.     *This will set the wood on fire, destroying it.
  50.     *Only flammable items can be burned, but all wood should be declared as flammable.
  51.     *@param the wood to burn.
  52.     *@return 1 if the wood is burnt, 0 if it was not burnt.
  53.     */
  54.     short burn(wood toBurn) {
  55.         if(toBurn.flammable==true)
  56.         {
  57.             this->type="Charcoal";
  58.  
  59.             height/=5.;
  60.             width/=5.;
  61.             weight/=5;
  62.             name="Burnt wood";
  63.             stateOfMatter="Solid";
  64.             flammable=true;
  65.             return 1;
  66.         }
  67.         else return 0;
  68.     }
  69.     /**
  70.     *Returns information regarding the status of the wood.
  71.     *@param the wood to retrieve.
  72.     *@return A string representing the stats.
  73.     */
  74.     string getStats(wood toGet)
  75.     {
  76.         string toReturn;
  77.         //Substruct specific variables.
  78.         toReturn+="Type: ";
  79.         toReturn+=toGet.type;
  80.         toReturn+="\nAge: ";
  81.         toReturn+=toGet.age;
  82.         //Superstruct variables.
  83.         toReturn+="\nHeight: ";
  84.         toReturn+=toGet.height;
  85.         toReturn+="\nWidth: ";
  86.         toReturn+=toGet.width;
  87.         toReturn+="\nWeight: ";
  88.         toReturn+=toGet.weight;
  89.         toReturn+="\nGeneric name: ";
  90.         toReturn+=toGet.name;
  91.         toReturn+="\nState of Matter: ";
  92.         toReturn+=toGet.stateOfMatter;
  93.         toReturn+="\nFlammable: ";
  94.         toReturn+=toGet.flammable;
  95.         toReturn+="\n";
  96.  
  97.         return toReturn;
  98.     }
  99. };
  100. int main()
  101. {
  102.     wood w("Birch", 15,5,2,100,"wood","solid",true);
  103.     cout << endl<<w.getStats(w)<<endl;
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement