Week 5

Objects and Inheritance

Derivation
  1. What objects would you make for a video game? How would they relate with each other?
  2. Construct a derivation tree for the objects.

Polymorphism

1:	 class Simpson {
2:	 public:
3:	   virtual void greeting();
4:	   virtual void catchPhrase() = 0;
5:	   void parting();
6:	 }
7:	 
8:	 void Simpson::greeting() {
9:	   cout << "hello!";
10:	 }
11:	 
12:	 void Simpson::parting() {
13:	   cout << "goodbye!";
14:	 }
15:	 
16:	 class Bart : public Simpson {
17:	   void greeting();
18:	   void catchPhrase();
19:	   void parting();
20:	 }
21:	 
22:	 void Bart::greeting() {
23:	   cout << "Hi, I'm Bart!";
24:	 }
25:	 
26:	 void Bart::catchPhrase() {
27:	   cout << "Don't have a cow!";
28:	 }
29:	 
30:	 void Bart::parting() {
31:	   cout << "Later!";
32:	 }
33:	 
34:	 class Lisa : public Simpson {
35:	   void greeting();
36:	   void catchPhrase();
37:	 }
38:	 
39:	 void Lisa::greeting() {
40:	   cout << "Hi, my name is Lisa.";
41:	 }
42:	 
43:	 void Lisa::catchPhrase() {
44:	   cout << "I like tai-chi and chai tea";
45:	 }
46:	 
47:	 int main() {
48:	   Simpson s1;
49:	   Simpson* sp1, sp2, sp3;
50:	   Bart b1;  
51:	   Lisa l1;
52:	 
53:	   s1.greeting();
54:	   s1.catchPhrase();
55:	   s1.parting();
56:	 
57:	   b1.greeting();
58:	   l1.catchPhrase();
59:	   b1.parting();
60:	 
61:	   sp1 = &s1;
62:	   sp1->greeting();
63:	   sp1->parting();
64:	 
65:	   sp2 = &l1;
66:	   sp2->greeting();
67:	   sp2->catchPhrase();
68:	   sp2->parting();
69:	 
70:	   sp3 = &b1;
71:	   sp3->greeting();
72:	   sp3->catchPhrase();
73:	   sp3->parting();  
74:	 }
  1. What lines are printed out by main()?
  2. Which are the inherited functions, virtual functions and pure virutal functions? What's the difference?
  3. How would you make Simpson into an abstract class?

Programming Assignment 2

Make sure your program compiles with make. No compile, no credit.
Create a README file containing Submit your assignment with the following commands:
make clean
handin cs40at pa2 *