What is a virtual function?

Slides:



Advertisements
Similar presentations
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Advertisements

1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
PARTS OF THE BODY.
The Frightening FER-DE-LANCE By: Logan Madson Blending In With Branches Some FER-DE-LANCE’S live in the woods on the ground. In the day they live in.
 an organism that makes its own food  an organism that eats other living things to get energy.
Food Chains and Ecosystems
ALLERGIES AND SCHOOL. EATING OUT CAN BE HAZZARDOUS TO YOUR HEALTH  WHAT ?  CAN YOU GO OUT TO DINNER SAFELY?
Purpose and Concepts The animal shelter was having to put down some dogs because they didn’t have enough money to pay for the necessities. The shelter.
Scientific Method Vocabulary. Biology: The study of life and living things.
I.
Slobcat is our cat. He does nothing but Lie about and sleep.
Cause & Effect. A cause and effect relationship  There is always something going on. This is your event.  The reason it happened is your cause.  What.
Morgan White. Me neither, but click that thing. Click Insert, Shapes, and it’s the very bottom choice.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
Tree python By Steven Romano 12/10/08 5-H I got this image from Google image. I choose the Tree python because snakes are one of my favorite animals.
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
The cat.
The cat is in the room eat lunch on the cosh. I love my cat so much it eat’s my food of my stuff.
What wild animals do you know?
How are living and nonliving things alike and different?
Making coffee Start or stop Process Decision Input/output Start
Food Chain.
My New Year’s Resolution
Ecosystems and How They Work BACK NEXT.
QUESTIONS ABOUT YOU SHOULD ASK.
PARTS OF THE BODY.
高中第八册 Module 3.
CLEAR IDEAS Based on workshop by Dr Kamal Birdi.
Raccoon! Hi! This is a raccoon. We explain you what a raccoon is.
Polymorphism.
Inheritance II CMSC 202.
A new beginning.
Cellular Respiration Harvesting Chemical Energy
Story of Maui-Maui.
Ecosystems Study Guide
PRG 218 Week 5 Individual Assignment Coding: Derived Classes Please click here to buy ( (
Expected Learning: Explore what is causing polar bears to struggle to survive. 3 Activity 3: Trouble.
Extra.
Array of objects.
A Presentation by Poppy Lally
Why is Soil So Important!
Natural Selection.
Present perfect progressive Past perfect progressive
Polymorphism 1 CMSC 202.
Private.
Array of objects.
مديريت موثر جلسات Running a Meeting that Works
Jeff West - Quiz Section 12
Getting Hooked on Hopium
Inheritance: Polymorphism and Virtual Functions
Vocabulary Intro.
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
Enough-too.
冀教版 五年级上册 Lesson 24 Year Animals.
Inheritance: Polymorphism and Virtual Functions
Do you think you got Skittles?
Cat.
My Animal Report Cover Page Title of the report Author
Denise Hunt Japan.
P1 Q2) Generation of electricity (pt1)
Cause and Effect.
What is the "animal " doing ?.
1. What animal 2. Male or Female ? 4. Male or Female? Why? Why?
PARTS OF THE BODY.
Fluency 30 words Zebras.
Plenary
Lecture 6: Polymorphism
Similes, metaphors, & idioms
Module 10Unit 2 I’m in New York now..
Getting Hooked on Hopium
Presentation transcript:

What is a virtual function?

This makes sense, right? class Animal { public: void eat() { cout << "I'm eating generic food."; } } class Cat : public Animal void eat() { cout << "I'm eating a rat."; } Animal *animal = new Animal; Cat *cat = new Cat; animal->eat(); // outputs: "I'm eating generic food." cat->eat(); // outputs: "I'm eating a rat."

Whoa, what happened? void dinner(Animal *xyz) { xyz->eat(); } Animal *animal = new Animal; Cat *cat = new Cat; dinner(animal); // outputs: "I'm eating generic food." dinner(cat); // outputs: "I'm eating generic food."

The compiler made a compile time decision. At compile time, dinner didn’t know what was being passed. It did the only thing it could. We want to ask for a RUN TIME decision. We want to say, if dinner got an animal, call animal eat. BUT if dinner got a cat, call cat’s eat. This is why we need virtual.

Now a run time decision is made. class Animal { public: virtual void eat() { cout << "I'm eating generic food."; } } dinner(animal); // outputs: "I'm eating generic food." dinner(cat); // outputs: "I'm eating a rat."