Upload
azuka
View
24
Download
0
Tags:
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
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.
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
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
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
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
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
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
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();};
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};
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};
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;
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;
Implementation for Shape#include “shape.h”void Shape::display(){ cout << “X: “ << topleftX << endl; cout << “Y: “ << topleftY << endl; }
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;}
Implementation for Circlevoid Circle::display(){ Shape::display(); cout << “Radius: “ << radius << endl;}
Implementation for Rectanglevoid Rectangle::display(){ Shape::display(); cout << “Width: “ << width << endl << “Height: “ << height << endl;}
Utility Function int2str()// converts int to string
#include <sstream>using std::stringstream;
string int2str(int num) { stringstream ss; ss << num; return ss.str();}
Using Function int2str()// converts
int n = 11;int nsqr = n * n;
string message;message = “Square of “ + int2str(n) + “ is “ + int2str(nsqr);
Polymorphism
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?
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.
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
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; }
Example of PolymorphismThe output is:
Each call to toString() function is from the Shape class.
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();};
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};
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; }
Example of PolymorphismThe output is:
From toString() in Shape class
From toString() in Rectangle class
From toString() in Circle class
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.