private color Color Polymorphism and Virtual Functions And we need that a

Private color color polymorphism and virtual

This preview shows page 29 - 34 out of 34 pages.

private : color Color; };
Image of page 29
Polymorphism and Virtual Functions And we need that a pointer array of base class Shape to hold the address of the heterogeneous objects SquareShape S(W, P, Blue , 1); TriangleShape T(W, P, Red, 1); RectangleShape R(W, P, Yellow, 3, 2); CircleShape C(W, P, Yellow, 4); Shape *A[4] = {&T, &R, &C, &S}; for ( int i = 0; i < 4; ++i) { A[i]->Draw(); } When i is 2, a CircleShape’s Draw() is used
Image of page 30
Virtual Functions Can be invoked via either a dereferenced pointer or a reference object Actual function to be invoked is determined from the type of object that is stored at the memory location being accessed Definition of the derived function overrides the definition of the base class version Determination of which virtual function to use cannot always be made at compile time Decision is deferred by the compiler to run time
Image of page 31
Pure Virtual Function Has no implementation A pure virtual function is specified in C++ by assigning the function the null address within its class definition A class with a pure virtual function is an abstract base class Convenient for defining interfaces Base class cannot be directly instantiated
Image of page 32
A Shape Abstract Base Class class Shape : public WindowObject { public : Shape(SimpleWindow &w, const Position &p, const color &c = Red); color GetColor() const ; void SetColor( const color &c); virtual void Draw() = 0; // pure virtual // function! private : color Color; };
Image of page 33
Home works Home works Exercises 14.41 Exercises Read the section 14.5 of textbook (Page779~801) carefully and complete the class SeqList End of Chapter 14 End of Chapter 14
Image of page 34

You've reached the end of your free preview.

Want to read all 34 pages?

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes