1 / 30

Inheritance

Inheritance. Derived Class. Given a particular class, it is easy to create a another class (derived class) which is only a little bit different. E.g., given Insect class, you can create derived classes named BumbleBee class and Grasshopper class.

azuka
Télécharger la présentation

Inheritance

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

  2. Derived Class • Given a particular class, it is easy to create a another class (derived class) which is only a little bit different. • E.g., given Insect class, you can create derived classes named BumbleBee class and Grasshopper class. • A class derived from another class (parent) inherits all the public methods of the parent class.

  3. Derived Class • Insect Class (6 legs, 4 wings, can fly) • Bumble Bee Class (derived class) • Grasshopper Class(derived class) • Bumble Bee has all Insect characteristics + others—e.g., ability to sting • Grasshopper has all Insect characteristics + others—e.g., ability to jump

  4. Derived Class • Account class (ID No., owner, deposit, withdraw) • Savings class (derived class) • Checking class (derived class) • Saving has all Account characteristics + interest rate • Checking has all Account characteristics + minimum balance

  5. Derived Class • class Person(ID No., name, address, phone) • derived class Faculty (+ department) • derived class Student (+ major, advisor) • derived class Staff (+ department, supervisor) Person Student Staff Faculty

  6. Derived Class • class Shape (position, outlined) • derived class Circle (+ radius) • derived class Rectangle (+ width, height) • derived class Triangle (+ sideA, sideB, sideC) Shape Rectangle Triangle Circle

  7. Derived Class • Derived class is a more specialized version of a base class. • Inherits from parent class all data and functions in its public section

  8. Advantages of Derived Class • Developing more complicated classes from existing class, which has been tested • Reuse of class resource • Organization of related classes

  9. Shape Class (base class) class Shape {private: int topLeftX; // top-left position int topLeftY; bool outline; // bordered or not public: Shape(); int getX(); int getY(); bool getOutline(); void display(); };

  10. Circle Class (derived class) class Circle: public Shape {private: double radius; public: Circle(); Circle(double r); Circle(int x, int y, bool ol, double r); double getRadius(); double area(); double circumference(); void display(); // overwriting // getX(), getY(), and getOutline() are // inherited from Shape };

  11. Rectangle Class (derived class) class Rectangle: public Shape {private: double width, double height; public: Rectangle(); Rectangle(double w, double h); Rectangle(int x, int y, bool ol, double w, double h); double getWidth(); double getHeight(); double area(); double perimeter(); void display(); // overwriting // getX(), getY(), and getOutline() are // inherited from Shape };

  12. Using Derived Classes #include <iostream> #include “shape.h”#include “circle.h” #include “recangle.h” using namespace std; int main(){ Circle c1(1.0); cout << “Circle\n”; cout << “ x-pos: “ << c1.getX() << endl; cout << “ y-pos: “ << c1.getY() << endl; cout << “ area: “ << c1.area() << endl; cout << “ circumference: “ << c2.circumference() << endl;

  13. Using Derived Classes Circle c2(10, 20, true, 1.0); Rectangle r2(30, 40, false, 2.0, 3.0); cout << “Circle: “;c2.display() << endl; cout << “Rectangle: “; r2.display() << endl;

  14. Implementation for Shape #include “shape.h” void Shape::display(){ cout << “X: “ << topleftX << endl; cout << “Y: “ << topleftY << endl; }

  15. Implementation for Circle #include “circle.” Circle::Circle(int x, int y, int ol, double r): Shape(x, y, ol){ // Shape(x, y, ol) has been invoked // at this point radius = r; }

  16. Implementation for Circle void Circle::display(){ Shape::display(); cout << “Radius: “ << radius << endl; }

  17. Implementation for Rectangle void Rectangle::display(){ Shape::display(); cout << “Width: “ << width << endl << “Height: “ << height << endl; }

  18. Utility Function int2str() // converts int to string #include <sstream> using std::stringstream; string int2str(int num) { stringstream ss; ss << num; return ss.str(); }

  19. Using Function int2str() // converts int n = 11; int nsqr = n * n; string message; message = “Square of “ + int2str(n) + “ is “ + int2str(nsqr);

  20. Polymorphism

  21. What Is Polymorphism? • Given:void display(Shape x){x.print();} • In Main():Shape s;Circle c;Rectangle r;display (s);display (c);display(r); Is there a way to let the function decide which print() to use?

  22. What Is Polymorphism? • A Circle is a Shape. • A Rectangle is a Shape. • (But Shape is not a Circle. Shape is not a Rectangle.) • Polymorphism – or Dynamic Binding • Automatically choosing appropriate version of a function depending on the type of object.

  23. Example of Polymorphism • Suppose an array of Shape object is created. • Array elements must of same type. • We can have an array of pointers to Shape objects.Shape *list[5]; 1 2 3 4 5 list

  24. Example of Polymorphism #include <iostream> #include “shape.h” #include “circle.h” #include “rectangle.h” using namespace std; int main(){ Shape *list[5] = {new Shape(10, 11, true), new Circle(20, 21, true, 1.0), new Rectangle(30, 31, false, 2.0, 3.0) }; for (int i = 0; i < 3; i++){ cout << list[i].toString() << endl; }

  25. Example of Polymorphism The output is: Each call to toString() function is from the Shape class.

  26. Example of Polymorphism To allow polymorphism (dynamic binding): class Shape {private: int topLeftX; // top-left position int topLeftY; bool outline; // bordered or not public: Shape(); int getX(); int getY(); bool getOutline(); virtual string toString(); };

  27. Example of Polymorphism class Circle: public Shape {private: double radius; public: Circle(); Circle(double r); Circle(int x, int y, boolol, double r); double getRadius(); double area(); double circumference(); string toString(); // This is also virtual };

  28. Example of Polymorphism #include <iostream> #include “shape.h” #include “circle.h” #include “rectangle.h” using namespace std; int main(){ Shape *list[5] = {new Shape(10, 11, true), new Circle(20, 21, true, 1.0), new Rectangle(30, 31, false, 2.0, 3.0) }; for (int i = 0; i < 3; i++){ cout << list[i].toString() << endl; }

  29. Example of Polymorphism The output is: From toString() in Shape class From toString() in Circle class From toString() in Rectangle class

  30. Example of Polymorphism • Polymorphism – dynamic binding • When an object’s function is called, the version which is appropriate to the object’s class is dynamically bound.

More Related