CS 225 Lecture Notes - Lecture 10: Virtual Function

20 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+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents