300 likes | 403 Vues
In object-oriented programming, inheritance allows a class (derived class) to inherit characteristics and behaviors from another class (parent class). For example, a derived class such as 'BumbleBee' or 'Grasshopper' can extend from a base class like 'Insect', inheriting its properties while also adding new features. This enables the creation of specialized classes that maintain the structure of the parent class while enhancing functionality. Benefits of using derived classes include code reuse, easier class organization, and improved maintainability.
E N D
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.
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
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
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
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
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
Advantages of Derived Class • Developing more complicated classes from existing class, which has been tested • Reuse of class resource • Organization of related classes
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(); };
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 Circle void Circle::display(){ Shape::display(); cout << “Radius: “ << radius << endl; }
Implementation for Rectangle void 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);
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?
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.
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
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 Polymorphism The output is: Each call to toString() function is from the Shape class.
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(); };
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 };
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 Polymorphism The output is: From toString() in Shape class From toString() in Circle class From toString() in Rectangle class
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.