lecture7

lecture7 - Wednesday, January 27 th Polymorphism Virtual...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Wednesday, January 27 th Polymorphism Virtual Functions Virtual Destructors Pure Virtual Functions Office hours on Wed, Feb 3 rd will be from 9-10am Carey will be attending the Society of Women Engineers Luncheon from 12-1pm on Wed, Feb 3 rd Everyones invited to join me for lunch (first come, first served) Contact Ashley Jin for more details: ashpjin@ucla.edu 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? 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? Person Id like to buy some lemonade. We only serve people. Are you a person? Yes. Im 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 thats Lucy Lu! Shhhh. 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 Student as a parameter to it? Id 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. Im 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 . public Person But do you have a name like a person? Since Im based on a Person , 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 youre a person, we can serve you. Mom. Its a NERD!!! Uh huh. getName 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(...
View Full Document

This note was uploaded on 04/10/2012 for the course CS 32 taught by Professor Davidsmallberg during the Winter '08 term at UCLA.

Page1 / 48

lecture7 - Wednesday, January 27 th Polymorphism Virtual...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online