Tuesday, March 19, 2013

Programming Basics #3

Classes

Getting a hang of classes, like a redditor...

1:  #include <iostream>  
2:  #include <string>  
3:    
4:  using namespace std;  
5:  int pauser;  
6:  #define pause cin >> pauser;  
7:    
8:  class cat  
9:  {  
10:  public:  
11:       string color;  
12:       string name;  
13:       float weight;  
14:       int happylevel;  
15:       void scare();  
16:       void play();  
17:       cat();  
18:       cat(string, string);  
19:  };  
20:    
21:  cat::cat()  
22:  {  
23:       color="unknown";  
24:       name="unknown";  
25:       happylevel=0;  
26:  }  
27:    
28:  cat::cat(string colorIn, string nameIn)  
29:  {  
30:       color=colorIn;  
31:       name=nameIn;  
32:       happylevel=0;  
33:  }  
34:    
35:  void cat::scare()  
36:  {  
37:       cout << name << " is terrified! Happylevel - 1!" << endl;  
38:       happylevel-=1;  
39:  }  
40:    
41:  void cat::play()  
42:  {  
43:       cout << name << " chases the laser! Happylevel + 1!" << endl;  
44:       happylevel+=1;  
45:  }  
46:    
47:  int main()  
48:  {  
49:       int choiceCat, choiceAction;  
50:       cat goosey("white", "Goosey");  
51:       cat lucy;  
52:       lucy.color="black";  
53:       lucy.name="Lucy";  
54:       lucy.weight=10.0;  
55:       goosey.weight=5.0;  
56:    
57:       while(choiceCat!=3)  
58:       {  
59:            cout << "Select cat:" << endl;  
60:            cout << "1. Lucy   (Happy Level: " << lucy.happylevel << ")" << endl;  
61:            cout << "2. Goosey  (Happy Level: " << goosey.happylevel << ")" << endl;  
62:            cout << "3. Exit" << endl;  
63:            cin >> choiceCat;  
64:            if (choiceCat==3) break;  
65:            else  
66:            {  
67:                 cout << "\nSelect action:" << endl;  
68:                 cout << "1. Play" << endl;  
69:                 cout << "2. Scare" << endl;  
70:                 cout << "3. Exit" << endl;  
71:                 cin >> choiceAction;  
72:                 if (choiceAction==3) break;  
73:                 if (choiceCat==1)  
74:                 {  
75:                      if (choiceAction==1) lucy.play();  
76:                      if (choiceAction==2) lucy.scare();  
77:                 }  
78:                 if (choiceCat==2)  
79:                 {  
80:                      if (choiceAction==1) goosey.play();  
81:                      if (choiceAction==2) goosey.scare();  
82:                 }  
83:            }  
84:       }  
85:            return 0;  
86:  }  

Output:
 Select cat:  
 1. Lucy   (Happy Level: 0)  
 2. Goosey  (Happy Level: 0)  
 3. Exit  
 1  
   
 Select action:  
 1. Play  
 2. Scare  
 3. Exit  
 1  
 Lucy chases the laser! Happylevel + 1!  
 Select cat:  
 1. Lucy   (Happy Level: 1)  
 2. Goosey  (Happy Level: 0)  
 3. Exit  
 2  
   
 Select action:  
 1. Play  
 2. Scare  
 3. Exit  
 2  
 Goosey is terrified! Happylevel - 1!  
 Select cat:  
 1. Lucy   (Happy Level: 1)  
 2. Goosey  (Happy Level: -1)  
 3. Exit  

No comments:

Post a Comment