1 / 0

Inheritance Concepts

Inheritance Concepts. Derive a new class ( subclass ) from an existing class ( base class or superclass ). Inheritance creates a hierarchy of related classes (types) which share code and interface. Inheritance Examples. Tiger. Dog. Lion. Cat. Bear. horse. Animal class hierarchy.

thai
Download Presentation

Inheritance Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance Concepts Derive a new class (subclass) from an existing class (base class or superclass). Inheritance creates a hierarchy of related classes (types) which share code and interface.
  2. Inheritance Examples
  3. Tiger Dog Lion Cat Bear horse Animalclass hierarchy Animals Domestic Wild
  4. logo card owner’s name inheritsfrom (isa) mastercard visacard americanexpress hologram category pin Credit cards
  5. Implementing Inheritance in C++ Develop a base class called student Use it to define a derived class called grad_student
  6. The Student Class Hierarchy student print() student_id,year, name inherits (isa) grad_student Inherits data and methods from base class dept,thesis
  7. The Student Class Hierarchy student print() student_id,year, name inherits (isa) grad_student dept,thesis print() Override print()
  8. Student Class class student {public: student(string nm, int id, int y); void print();private: int student_id; int year; string name;};
  9. Member functions student::student(string nm, int id, int y){ student_id = id; year = y; name = nm;}void student::print(){ cout << "\n STUDENT " << name;}
  10. Graduate Student Class grad_student.h class grad_student: public student {public: grad_student(string nm, int id, int y, string d, string th); void print();private:string dept;string thesis;};
  11. grad_student.cpp grad_student::grad_student(string nm, int id, int y, string d, string th) :student(nm, id, y){ dept = d; thesis = th;}void grad_student::print(){ cout << “GRAD STUDENT “ << name << dept << ", " << thesis << endl;} Calls base class constructor
  12. Examples of Use grad_student * g = new grad_student(…); g->print(); // grad student print student * s = new student(…); s->print(); // student print No concern about which print is called
  13. Examples of Use Base class pointer can store any descendant class student * joe; if (…) joe = new grad_student(); else joe = new student(); joe->print(); Dilemma - which print do you want? joe->print(); //student print if NOT virtual joe->print(); //correct print if virtual “virtual” means “Make a run time decision”
  14. Examples of Use Base class pointer can store any descendant class student * all[SIZE]; all[0] = new student(…); all[1] = new grad_student(); all[2] = new student(…); all[3] = new grad_student(); … for (i=0; I < SIZE;i++) all[i]->print(); Dilemma - which print do you want?
  15. Two Types of Binding Static Binding (the default in C++) y->print() uses y’s print this is known at compile time Dynamic Binding all[i]->print() uses the print() in the object pointed at this is only known at run time coded in C++ with virtual functions
  16. Student Class class student {public: student(string nm, int id, int y); void virtualprint();private: int student_id; int year; string name;};
  17. shape inherits (isa) rectangle • • • • circle triangle square Representing Shapes: abstract base class shape is only a category and cannot be instantiated
  18. Why do we want an abstract base class? Helpful for organization Can use a pointer to a base class to store actual instantiations of derived classes
  19. C++ Shape Classes class shape {public: virtual double area() = 0;//abstract // shape will NOT define area // but forces all derived classes to define area};class rectangle: public shape {public: double area() const {return (height*width);} :private: double height, width;};
  20. Every descendant of shape MUST have an area method. class circle: public shape {public: double area() {return (PI*radius*radius);} :private: double radius;};// etc
  21. Use: shape* p[N];circle c1,...;rectangle r1,...; :// fill in p with pointers to // circles, squares, etcp[0] = &c1; p[1] = &r1; ... : :// calculate total areafor (i = 0; i < N; ++i) tot_area = tot_area + p[i]->area();
More Related