CS 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

Slides:



Advertisements
Similar presentations
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Advertisements

Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Pointer Data Type and Pointer Variables
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Implications of Inheritance COMP204, Bernhard Pfahringer.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Pointers OVERVIEW.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
CS 153: Concepts of Compiler Design October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CS 152: Programming Language Paradigms March 5 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 152: Programming Language Paradigms March 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CS 152: Programming Language Paradigms April 16 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 152: Programming Language Paradigms May 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Memory Management in Java Mr. Gerb Computer Science 4.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
C11, Implications of Inheritance
Object Lifetime and Pointers
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CMPE Data Structures and Algorithms in C++ September 14 Class Meeting
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CS 153: Concepts of Compiler Design November 28 Class Meeting
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Java Programming Language
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
9-10 Classes: A Deeper Look.
Java Programming Language
COP 3330 Object-oriented Programming in C++
CS 144 Advanced C++ Programming March 21 Class Meeting
CS 144 Advanced C++ Programming April 30 Class Meeting
9-10 Classes: A Deeper Look.
CS 144 Advanced C++ Programming April 30 Class Meeting
CMPE 152: Compiler Design May 2 Class Meeting
Presentation transcript:

CS 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 2 Midterm Solution: Question 1a  Write a recursive Scheme procedure call that has three parameters, a math library list, a function name, and a numeric argument. It returns the result of evaluating the named function expression using the argument. You may write any necessary helper procedures.  You want to write procedure call so that it will work with any similarly structured “math library”. (define math-library '((double (+ x x)) (triple (+ x x x)) (square (* x x)) (cube (* x x x)) ))

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 3 Midterm Solution: Question 1a, cont’d  To make use of a function expression expr such as '(+ x x), you can turn it into a lambda expression... ... which can be eval ’d into a procedure:  Then apply the procedure to an argument arg : (define math-library '((double (+ x x)) (triple (+ x x x)) (square (* x x)) (cube (* x x x)) )) (list 'lambda (list 'x) expr)  (lambda (x) (+ x x))) ((eval (list 'lambda (list 'x) expr)) arg) (eval (list 'lambda (list 'x) expr))  #

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 4 Midterm Solution: Question 1a, cont’d  So the problem becomes a recursive search for the correct function expression to apply to the argument: (define math-library '((double (+ x x)) (triple (+ x x x)) (square (* x x)) (cube (* x x x)) )) (define call (lambda (library name arg) (cond ((null? library) '()) ((eq? name (caar library)) ((eval (list 'lambda (list 'x) (cadar library))) arg)) (else (call (cdr library) name arg)) ))) Function name Function expression

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 5 Midterm Solution: Question 1b  Suppose Scheme didn’t have the if special form and therefore you decided to invent your own:  Describe what happens when you use my-if in the factorial procedure, and explain why that happens. (define my-if (lambda (test true-part false-part) (cond (test true-part) (else false-part) ))) (define fact (lambda (n) (my-if (= n 0) 1 (* n (fact (sub1 n))) )))

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 6 Midterm Solution: Question 1b, cont’d  Procedure fact goes into an infinite loop.  Note that my-if is a procedure. Therefore, is a procedure call with three arguments. All three arguments are always evaluated during a call, even when n is 0.  Infinite looping when n goes negative. (define my-if (lambda (test true-part false-part) (cond (test true-part) (else false-part) ))) (define fact (lambda (n) (my-if (= n 0) 1 (* n (fact (sub1 n))) ))) (my-if (= n 0) 1 (* n (fact (sub1 n))))

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 7 Midterm Solution: Question 2a  What does predicate guzzinta do?  guzzinta instantiates Q (quotient) to be the integer result of N (numerator) divided by D (denominator). How many times does D “guzzinta” (goes into) N ? _ /*1*/ is_integer(0). /*2*/ is_integer(N) :- is_integer(M), N is M+1. /*3*/ guzzinta(D, N, Q) :- is_integer(Q), /*4*/ P1 is Q * D, /*5*/ P2 is (Q + 1) * D, /*6*/ P1 =< N, /*7*/ P2 > N, /*8*/ !. ?- guzzinta(3, 14, R). R = 4. ?- guzzinta(2, 3, R). R = 1.

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 8 Midterm Solution: Question 2b  Explain how the second query above is satisfied in terms of resolution, unification, backtracking, and the cut operation. _ /*1*/ is_integer(0). /*2*/ is_integer(N) :- is_integer(M), N is M+1. /*3*/ guzzinta(D, N, Q) :- is_integer(Q), /*4*/ P1 is Q * D, /*5*/ P2 is (Q + 1) * D, /*6*/ P1 =< N, /*7*/ P2 > N, /*8*/ !. ?- guzzinta(3, 14, R). R = 4. ?- guzzinta(2, 3, R). R = 1.

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 9 Midterm Solution: Question 2b, cont’d /*1*/ is_integer(0). /*2*/ is_integer(N) :- is_integer(M), N is M+1. /*3*/ guzzinta(D, N, Q) :- is_integer(Q), /*4*/ P1 is Q * D, /*5*/ P2 is (Q + 1) * D, /*6*/ P1 =< N, /*7*/ P2 > N, /*8*/ !. [trace] 3 ?- guzzinta(2, 3, R). Call: (6) guzzinta(2, 3, _G2166) ? creep Call: (7) is_integer(_G2166) ? creep Exit: (7) is_integer(0) ? creep Call: (7) _G2242 is 0*2 ? creep Exit: (7) 0 is 0*2 ? creep Call: (7) _G2248 is (0+1)*2 ? creep Exit: (7) 2 is (0+1)*2 ? creep Call: (7) 0=<3 ? creep Exit: (7) 0=<3 ? creep Call: (7) 2>3 ? creep Fail: (7) 2>3 ? creep Redo: (7) is_integer(_G2166) ? creep Call: (8) is_integer(_G2239) ? creep Exit: (8) is_integer(0) ? creep Call: (8) _G2166 is 0+1 ? creep Exit: (8) 1 is 0+1 ? creep Exit: (7) is_integer(1) ? creep Call: (7) _G2245 is 1*2 ? creep Exit: (7) 2 is 1*2 ? creep Call: (7) _G2251 is (1+1)*2 ? creep Exit: (7) 4 is (1+1)*2 ? creep Call: (7) 2=<3 ? creep Exit: (7) 2=<3 ? creep Call: (7) 4>3 ? creep Exit: (7) 4>3 ? creep Exit: (6) guzzinta(2, 3, 1) ? creep R = 1.  Match Rule 3 (resolution).  Instantiate D to 2 and N to 3 (unification).  First subgoal: Match Fact 1.  Fail the fifth subgoal (2 > 3).  Backtrack from Fact 1 to Rule 2.  Cut prevents a final backtrack. _

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 10 Midterm Solution: Question 2c /*1*/ is_integer(0). /*2*/ is_integer(N) :- is_integer(M), N is M+1. /*3*/ guzzinta(D, N, Q) :- is_integer(Q), /*4*/ P1 is Q * D, /*5*/ P2 is (Q + 1) * D, /*6*/ P1 =< N, /*7*/ P2 > N, /*8*/ !. [trace] 3 ?- guzzinta(2, 3, R). Call: (6) guzzinta(2, 3, _G2166) ? creep Call: (7) is_integer(_G2166) ? creep Exit: (7) is_integer(0) ? creep Call: (7) _G2242 is 0*2 ? creep Exit: (7) 0 is 0*2 ? creep Call: (7) _G2248 is (0+1)*2 ? creep Exit: (7) 2 is (0+1)*2 ? creep Call: (7) 0=<3 ? creep Exit: (7) 0=<3 ? creep Call: (7) 2>3 ? creep Fail: (7) 2>3 ? creep Redo: (7) is_integer(_G2166) ? creep Call: (8) is_integer(_G2239) ? creep Exit: (8) is_integer(0) ? creep Call: (8) _G2166 is 0+1 ? creep Exit: (8) 1 is 0+1 ? creep Exit: (7) is_integer(1) ? creep Call: (7) _G2245 is 1*2 ? creep Exit: (7) 2 is 1*2 ? creep Call: (7) _G2251 is (1+1)*2 ? creep Exit: (7) 4 is (1+1)*2 ? creep Call: (7) 2=<3 ? creep Exit: (7) 2=<3 ? creep Call: (7) 4>3 ? creep Exit: (7) 4>3 ? creep Exit: (6) guzzinta(2, 3, 1) ? creep R = 1.  Why is the cut necessary?  Cut prevents a final backtrack after a solution is found. Q is bound to the quotient.  Without the cut, if you press ; after the solution, the program goes into an infinite loop with ever increasing values of N.

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 11 Midterm Solution: Question 3  Classes and objects that you create for a Smalltalk program become part of the entire “ecosystem” of previously defined Smalltalk classes and objects.  A typical Java program is standalone. It may use previously defined classes and packages. But unless you “jar” the program into the previously defined packages, it does not become part of the Java ecosystem. _

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 12 Eclipse C/C++ Development Tool (CDT)  Install CDT as a plug-in to an existing Eclipse installation: to/EclipseCpp_HowTo.html You will also need to install a C/C++ compiler separately.  Now you can develop, debug, and run Java, C, and C++ programs within a single IDE. _

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 13 Allocation of Java Objects  When created, Java objects are always allocated in the runtime heap. heap: An area of memory reserved at run time to store dynamically allocated objects.  Java objects are always explicitly dynamically allocated with new. _

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 14 Allocation of Java Objects, cont’d  A Java object is automatically garbage-collected when there are no longer any references to it.  The class’s finalize method is called by the garbage collector. Java provides a do-nothing finalize method for every class. You can override the class. There is no guarantee which thread will invoke the finalize method. _

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 15 Allocation of Java Objects, cont’d public class MyClass { private int field; public MyClass(int f) { this.field = f; System.out.println("MyClass constructor called: field = " + field); } protected void finalize() { System.out.println("MyClass finalize called: field = " + field); } public class AllocateTest { public static void main(String[] args) { MyClass mc = new MyClass(12); } MyClass constructor called: field = 12 Demo CONSTRUCTOR

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 16 Allocation of C++ Objects  Automatically allocated on the runtime stack via a declaration. Automatically deallocated when going out of scope.  Explicitly dynamically allocated in the runtime heap with new. Must be explicitly deallocated with delete. _

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 17 Allocation of C++ Objects, cont’d  C++ class MyClass : class MyClass { private: int field; public: MyClass(int f) : field(f) { cout << "MyClass constructor called: field = " << field << endl; } ~MyClass() { cout << "MyClass destructor called: field = " << field << endl; } }; CONSTRUCTOR DESTRUCTOR

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 18 Allocation of C++ Objects, cont’d void test() { cout << endl << "Automatic allocation on the runtime stack:" << endl; MyClass mc(11); cout << endl << "Dynamic allocation:" << endl; MyClass *mcp = new MyClass(12); cout << endl << "Explicit deletion of dynamically allocated object:" << endl; delete(mcp); cout << endl << "Automatic deallocation when going out of scope:" << endl; } int main() { test(); } Automatic allocation on the runtime stack: MyClass constructor called: field = 11 Dynamic allocation: MyClass constructor called: field = 12 Explicit deletion of dynamically allocated object: MyClass destructor called: field = 12 Automatic deallocation when going out of scope: MyClass destructor called: field = 11 Demo

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 19 Virtual vs. Static Method Binding  Some object-oriented languages do virtual binding of methods at run time. Example: Java  Other object-oriented languages can do either static binding or virtual binding. Example: C++ _

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 20 Java Virtual Methods  Java class Animal : public class Animal { public void what() { System.out.println("I am Animal."); } public void sound() { System.out.println("I make animal noises."); } public void identify() { what(); sound(); } }

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 21 Java Virtual Methods, cont’d  Class Animal has a subclass Cat :  The subclass overrides methods what() and sound(). The ensures proper overriding. public class Cat extends Animal public void what() { System.out.println("I am Cat."); public void sound() { System.out.println("I purr."); } }

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 22 Java Virtual Methods, cont’d  The main class VirtualTest1. public class VirtualTest1 { public static void main(String[] args) { Animal animal = new Animal(); Cat cat = new Cat(); System.out.println("\nAfter dynamic allocation:"); animal.identify(); cat.identify(); System.out.println("\nAfter animal = cat:"); animal = cat; animal.identify(); } }

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 23 Java Virtual Methods, cont’d public class Animal { public void what() { System.out.println("I am Animal."); } public void sound() { System.out.println("I make animal noises."); } public void identify() { what(); sound(); } public class VirtualTest1 { public static void main(String[] args) { Animal animal = new Animal(); Cat cat = new Cat();... animal.identify(); cat.identify();... } } I am Animal. I make animal noises. I am Cat. I purr.  Java methods are virtual. The correct methods are determined at run time according to the types of the objects. Variable animal refers to an Animal object, and variable cat refers to a Cat object.

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 24 Java Virtual Methods, cont’d  Now variable animal also refers to a Cat object. Virtual methods enable the right methods to be called at run time. public class Animal { public void what() { System.out.println("I am Animal."); } public void sound() { System.out.println("I make animal noises."); } public void identify() { what(); sound(); } public class VirtualTest1 { public static void main(String[] args) { Animal animal = new Animal(); Cat cat = new Cat();... animal = cat; animal.identify();... } } I am Cat. I purr.

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 25 Java Virtual Methods, cont’d  It doesn’t matter if we change the type of variable cat to Animal. Variable cat still refers to a Cat object. Demo public class Animal { public void what() { System.out.println("I am Animal."); } public void sound() { System.out.println("I make animal noises."); } public void identify() { what(); sound(); } public class VirtualTest2 { public static void main(String[] args) { Animal animal = new Animal(); Animal cat = new Cat();... animal.identify(); cat.identify();... } } I am Animal. I make animal noises. I am Cat. I purr.

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 26 C++ Static and Virtual Methods  C++ class Animal : class Animal { public: void what() { cout << "I am Animal." << endl; } virtual void sound() { cout << "I make animal noises." << endl; } void identify() { what(); sound(); } };  C++ methods are static by default. Specify virtual to make a method virtual instead. STATIC VIRTUAL

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 27 C++ Static and Virtual Methods, cont’d  Class Animal has a subclass Cat :  The subclass overrides methods what() and sound(). _ class Cat : public Animal { public: void what() { cout << "I am Cat." << endl; } void sound() { cout << "I purr." << endl; } };

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 28 C++ Static and Virtual Methods, cont’d  Main: int main() { Animal animal; Cat cat; cout << endl << "After automatic allocation:" << endl; animal.identify(); cat.identify(); cout << endl << "After animal = cat:" << endl; animal = cat; animal.identify(); Animal *animalp = new Animal(); Cat *catp = new Cat(); cout << endl << "After dynamic allocation:" << endl; animalp->identify(); catp->identify(); cout << endl << "After animalp = catp:" << endl; animalp = catp; animalp->identify(); return 0; }

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 29 C++ Static and Virtual Methods, cont’d  Method what() is static. Animal method identify() will always call static Animal method what() regardless of the object type. class Animal { public: void what() { cout << "I am Animal." << endl; } virtual void sound() { cout << "I make animal noises." << endl; } void identify() { what(); sound(); } }; int main() { Animal animal; Cat cat; animal.identify(); cat.identify();... } I am Animal. I make animal noises. I am Animal. I purr.

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 30 C++ Static and Virtual Methods, cont’d  Variable animal gets a copy of variable cat ’s contents. But its type is still an Animal object. int main() { Animal animal; Cat cat;... animal = cat; animal.identify(); } I am Animal. I make animal noises. class Animal { public: void what() { cout << "I am Animal." << endl; } virtual void sound() { cout << "I make animal noises." << endl; } void identify() { what(); sound(); } };

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 31 C++ Static and Virtual Methods, cont’d  Dynamically allocating objects. Animal method what() is still static. class Animal { public: void what() { cout << "I am Animal." << endl; } virtual void sound() { cout << "I make animal noises." << endl; } void identify() { what(); sound(); } }; int main() { Animal *animalp = new Animal(); Cat *catp = new Cat(); animalp->identify(); catp->identify();... } I am Animal. I make animal noises. I am Animal. I purr.

SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 32 C++ Static and Virtual Methods, cont’d  Now pointer variable animalp also refers to a Cat object. int main() { Animal *animalp = new Animal(); Cat *catp = new Cat();... animalp = catp; animalp->identify(); } I am Animal. I purr. Demo class Animal { public: void what() { cout << "I am Animal." << endl; } virtual void sound() { cout << "I make animal noises." << endl; } void identify() { what(); sound(); } };