30
Inheritance

Inheritance

  • Upload
    azuka

  • View
    24

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Inheritance

Inheritance

Page 2: Inheritance

Derived Class

• Given a particular class, it is easy to create a Given a particular class, it is easy to create a another class (derived class) which is only a another class (derived class) which is only a little bit different.little bit different.

• E.g., given E.g., given InsectInsect class, you can create class, you can create derived classes named derived classes named BumbleBeeBumbleBee class and class and GrasshopperGrasshopper class. class.

• A class derived from another class (parent) A class derived from another class (parent) inherits all the public methods of the parent inherits all the public methods of the parent class.class.

Page 3: Inheritance

Derived Class

• Insect Class (6 legs, 4 wings, can fly)Insect Class (6 legs, 4 wings, can fly)– Bumble Bee Class (derived class)Bumble Bee Class (derived class)– Grasshopper Class(derived class)Grasshopper Class(derived class)

• Bumble Bee has all Insect characteristics + Bumble Bee has all Insect characteristics + others—e.g., ability to stingothers—e.g., ability to sting

• Grasshopper has all Insect characteristics + Grasshopper has all Insect characteristics + others—e.g., ability to jumpothers—e.g., ability to jump

Page 4: Inheritance

Derived Class

• Account class (ID No., owner, deposit, Account class (ID No., owner, deposit, withdraw)withdraw)– Savings class (derived class)Savings class (derived class)– Checking class (derived class)Checking class (derived class)

• Saving has all Account characteristics + Saving has all Account characteristics + interest rateinterest rate

• Checking has all Account characteristics + Checking has all Account characteristics + minimum balanceminimum balance

Page 5: Inheritance

Derived Class

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

Person

StudentFaculty Staff

Page 6: Inheritance

Derived Class• class Shape (position, outlined)class Shape (position, outlined)

– derived class Circle (+ radius)derived class Circle (+ radius)– derived class Rectangle (+ width, height)derived class Rectangle (+ width, height)– derived class Triangle (+ sideA, sideB, sideC)derived class Triangle (+ sideA, sideB, sideC)

Shape

RectangleCircle Triangle

Page 7: Inheritance

Derived Class

• Derived class is a more specialized version Derived class is a more specialized version of a base class.of a base class.

• Inherits from parent class all data and Inherits from parent class all data and functions in its public sectionfunctions in its public section

Page 8: Inheritance

Advantages of Derived Class

• Developing more complicated classes from Developing more complicated classes from existing class, which has been testedexisting class, which has been tested

• Reuse of class resourceReuse of class resource

• Organization of related classesOrganization of related classes

Page 9: Inheritance

Shape Class (base class)

class Shape {private: int topLeftX; // top-left position int topLeftY; bool outline; // bordered or notpublic: Shape(); int getX(); int getY(); bool getOutline(); void display();};

Page 10: Inheritance

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};

Page 11: Inheritance

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};

Page 12: Inheritance

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;

Page 13: Inheritance

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;

Page 14: Inheritance

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

Page 15: Inheritance

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;}

Page 16: Inheritance

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

Page 17: Inheritance

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

Page 18: Inheritance

Utility Function int2str()// converts int to string

#include <sstream>using std::stringstream;

string int2str(int num) { stringstream ss; ss << num; return ss.str();}

Page 19: Inheritance

Using Function int2str()// converts

int n = 11;int nsqr = n * n;

string message;message = “Square of “ + int2str(n) + “ is “ + int2str(nsqr);

Page 20: Inheritance

Polymorphism

Page 21: Inheritance

What Is Polymorphism?• Given:Given:void display(Shape x){void display(Shape x){ x.print(); x.print();}}

• In Main():In Main():Shape s;Shape s;Circle c;Circle c;Rectangle r;Rectangle r;

display (s);display (s);display (c);display (c);display(r);display(r);

Is there a way to let the function decide which print() to use?

Page 22: Inheritance

What Is Polymorphism?

• A Circle A Circle is a is a Shape.Shape.

• A Rectangle A Rectangle is a is a Shape.Shape.

• (But Shape is (But Shape is notnot a Circle. Shape is a Circle. Shape is notnot a a Rectangle.)Rectangle.)

• Polymorphism – or Dynamic BindingPolymorphism – or Dynamic Binding– Automatically choosing appropriate version of Automatically choosing appropriate version of

a function depending on the type of object.a function depending on the type of object.

Page 23: Inheritance

Example of Polymorphism

• Suppose an array of Shape object is created.Suppose an array of Shape object is created.

• Array elements must of same type.Array elements must of same type.

• We can have an array of We can have an array of pointerspointers to Shape to Shape objects.objects.

Shape *list[5];Shape *list[5];1 2 3 4 5

list

Page 24: Inheritance

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; }

Page 25: Inheritance

Example of PolymorphismThe output is:

Each call to toString() function is from the Shape class.

Page 26: Inheritance

Example of PolymorphismTo allow polymorphism (dynamic binding):

class Shape {private: int topLeftX; // top-left position int topLeftY; bool outline; // bordered or notpublic: Shape(); int getX(); int getY(); bool getOutline(); virtual string toString();};

Page 27: Inheritance

Example of Polymorphismclass 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(); string toString(); // This is also virtual};

Page 28: Inheritance

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; }

Page 29: Inheritance

Example of PolymorphismThe output is:

From toString() in Shape class

From toString() in Rectangle class

From toString() in Circle class

Page 30: Inheritance

Example of Polymorphism

• Polymorphism – dynamic bindingPolymorphism – dynamic binding

• When an object’s function is called, the When an object’s function is called, the version which is appropriate to the object’s version which is appropriate to the object’s class is dynamically bound.class is dynamically bound.