CS 225 Lecture Notes - Lecture 10: Virtual Function

19 views4 pages

Document Summary

= d; // base class pointer can point at a derived class, d is still pointing. // this is fine, no slicing d = b; // compiler error. // whatever class builds the object, is responsible of destructing it. Cs 225 - lecture page 1 cout << ball << endl; void display(); private: string name; Examples1 sphere s; ball b; s. display(); // sphere b. dispaly(); // ball. Example2 sphere * sptr; sptr = &s; sptr->display(); // virtual is slower, so don"t just randomly use it virtual may be overwritten in a derived class class sphere { public: sphere(); sphere(double r); void sphere::display() { cout << sphere << endl; virtual void display(); Cs 225 - lecture page 2 private: double theradius; class ball:public sphere { public: ball(); ball(double r, string n); string getname(); void ball::display() { cout << ball << endl; virtual void display(); // but kept for communication purposes private: string name;

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents