lecture7 - polymorphism and virtual functions

# lecture7 - polymorphism and virtual functions - Monday...

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

Monday, January 26 th   Polymorphism Virtual Functions Virtual Destructors Pure Virtual Functions  Don’t forget!   Thursday – Jan 29 th  at  4pm  in  3400 Boelter Hall UCLA CS Alumni Panel Are you startup material? Four UCLA alums talk about their software startups!

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

View Full Document
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 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
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? 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  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 you’re  a person, we can serve  you. Mom. It’s a  NERD!!! Uh huh. getName
class Person { public: string getName(void); ... private: string m_sName; int m_nAge; }; Polymorphism Any time you have a function that takes a  (reference to) a   superclass  as an  argument... void SayHi( Person &p ) { cout << “Hello “ << p.getName(); } main() { } Person c; SayHi( c ); You may also pass in a  subclass  to the function. Student s; SayHi( s ); class Student : public Person { public: // new stuff: int getStudentID(); private: // new stuff: int m_nStudentID; };   Person  Student

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

View Full Document
Polymorphism Why is this?   Well a Student  IS a  Person.   Everything a Person can do, it  can do.   void SayHi( Person &p ) { cout << “Hello “ << p.getName(); } main() { } Person c; SayHi( c ); Student s; SayHi( s );
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern