lecture7

# lecture7 - 1 We sday, January 26 dne Polym orphism Virtual...

This preview shows pages 1–7. Sign up to view the full content.

1 Wednesday, January 26 th Polymorphism Virtual Functions Virtual Destructors Pure Virtual Functions Just a reminder! Daniel Yu is holding study sessions EVERY MONDAY from 4-6pm in 4760 Boelter Hall Did I mention that he’s holding them EVERY week? Take advantage of it!

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Polymorphism Consider a function that accepts a Person as an argument void LemonadeStand( Person &p ) { cout << “Hello “ << p .getName(); cout << “How many cups of ”; cout << “lemonade do you want?”; } Can we also pass a Student as a parameter to it? void main(void) { Person p; LemonadeStand(p); } We know we can do this: void main(void) { Student s; LemonadeStand(s); } But can we do this?
3 Polymorphism Consider a function that accepts a Person as an argument void LemonadeStand( Person &p ) { cout << “Hello “ << p .getName(); cout << “How many cups of ”; cout << “lemonade do you want?”; } Can we also pass a Student as a parameter to it? I’d like to buy some lemonade. We only serve people. Are you a person? Yes. I’m a person. I have a name and everything. class Person { public: string getName (void); ... private: string m_sName; int m_nAge; }; Ok. How many cups of lemonade would you like? Mom. I think that’s Lucy Lu! Shhhh.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Student Polymorphism Consider a function that accepts a Person as an argument void LemonadeStand( Person &p ) { cout << “Hello “ << p .getName(); cout << “How many cups of ”; cout << “lemonade do you want?”; } Can we also pass a as a parameter to it? I’d like to buy some lemonade. We only serve people. Are you a person? Prove it to us! class Student : public Person { public: // new stuff: int getStudentID(); private: // new stuff: int m_nStudentID; }; Hmm. I’m a student but as far as I know, all students are people! Well, you can see by my class declaration that all students are just a more specific sub-class of people . But do you have a name like a person? Since I’m based on a , I have every-thing a Person has… Including a name! Look! class Person { public: string getName (void); ... private: string m_sName; int m_nAge; Well, as long as you’re a person, we can serve you. Mom. It’s a NERD!!! Uh huh. getName
5 class Person { public: string getName(void) { return m_name; } ... private: string m_name; int m_age; }; Polymorphism The idea behind polymorphism is that once I define a function that accepts a (reference or pointer to a) Person void SayHi( Person &p ) { cout << “Hello “ << p.getName(); } main() { Person p (“Eric”,18); SayHi( p ); } float GPA = 1.6; Student s (“David”,19, GPA s class Student : public Person { public: // new stuff: int getGPA(); private: // new stuff: float m_gpa; Not only can I pass variables to that class… But I can also pass any variable that was derived from a Person!

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 Polymorphism Why is this? Well a Student IS a Person. Everything a Person can do, it can do. So if I can ask for a Person’s name with getName , I can ask for a Student’s getName too!
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 05/11/2011 for the course CS 33 taught by Professor Rohr during the Spring '08 term at UCLA.

### Page1 / 48

lecture7 - 1 We sday, January 26 dne Polym orphism Virtual...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online