1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.

17
1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che

Transcript of 1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.

Page 1: 1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.

1

Chapter 10Inheritance and Polymorphism

C/C++ Language Programming

Wanxiang Che

Page 2: 1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.

2

Array of ObjectsCircle circles[2] = {Circle(3), Circle(4)};

// Initialize sumdouble sum = 0;

// Add areas to sumfor (int i = 0; i < 2; i++) sum+=circles[i].get_area();

Page 3: 1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.

3

How to sum different Shape?

Circle circle(3);

Rectangle rect(4, 5);

double sum = 0;

sum += circle.get_area();

sum += rect.get_area();

Page 4: 1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.

4

How to sum different Shape?Shape shapes[2] = {Circle(3), Rectangle(4, 5)};

// Initialize sumdouble sum = 0;

// Add areas to sumfor (int i = 0; i < 2; i++) sum += shapes[i].get_area();

Page 5: 1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.

5

Inheritance Concept

• 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.

Page 6: 1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.

6

Shape class hierarchy

Shape

Rectangle

Square

Triangle Circle • • • •

inherits (isa)

Page 7: 1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.

7

Inheritance UML

Shape -color: string

-filled: bool

+Shape()

+Shape(color: string, filled: bool)

+getColor(): string

+setColor(color: string): void

+isFilled(): bool

+setFilled(filled: bool): void

+print(): string

The color of the object (default: white).

Indicates whether the object is filled with a color (default: false).

Creates a Shape.

Creates a Shape with the specified color and filled values.

Returns the color.

Sets a new color.

Returns the filled property.

Sets a new filled property.

Returns a string representation of this object.

Circle -radius: double

+Circle()

+Circle(radius: double)

+Circle(radius: double, color: string, filled: bool)

+getRadius(): double

+setRadius(radius: double): void

+getArea(): double

+getPerimeter(): double

+getDiameter(): double

Rectangle -width: double

-height: double

+Rectangle()

+Rectangle(width: double, height: double)

+Rectangle(width: double, height: double, color: string, filled: bool)

+getWidth(): double

+setWidth(width: double): void

+getHeight(): double

+setHeight(height: double): void

+getArea(): double

+getPerimeter(): double

Page 8: 1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.

8

Define a Class Hierarchy

• Syntax:

class DerivedClassName : access-level BaseClassName

access-level specifies the type of derivation• private by default, or public

E.g. class Circle: public Shape• Any class can serve as a base class

– Thus a derived class can also be a base class

Page 9: 1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.

9

Redefining Functions• A derived class can override methods defined in its parent

class. With overriding, – the method in the subclass has the identical signature to the method

in the base class. – a subclass implements its own version of a base class method.

class A {

protected:

int x, y;

public:

void print ()

{cout<<“From A”<<endl;}

}

class B : public A

{

public:

void print ()

{cout<<“From B”<<endl;}

}

Page 10: 1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.

10

redefining vs. overloading

• Overloading a function – A way to provide more than one function with

the same name but with different signatures to distinguish them.

• Redefining a function– The function must be defined in the derived

class using the same signature and same return type as in its base class

Page 11: 1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.

11

Static Binding

X print()

Classes

Y print()

Z print()

inherits (isa)

X x;Y y;Z z;X *px;

px = & ??;// can be x,y,or z

px->print(); // ??

Page 12: 1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.

12

Two Types of Binding

• Static Binding (the default in C++)– px->print() uses X’s print– this is known at compile time

• Dynamic Binding– px->print() uses the print() in the obj

ect pointed at– this is only known at run time– with virtual functions

Page 13: 1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.

13

Why “only known at run time”?

• Assume dynamic binding is being used:

X x;Y y;Z z;X *px;cin >> val;if (val == 1) px = &x;else px = &y;px->print();// which print() is used?

Page 14: 1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.

14

Define Virtual Functions

• To enable dynamic binding for a function, need to do two things:– The function must be declared virtual in the

base class.– The variable that references the object for the

function must contain the address of the object.

Page 15: 1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.

15

Virtual Functionclass Shape {

public:

Shape();

Shape(const Shape& orig);

virtual ~Shape();

void print();

virtual double get_area();};

Page 16: 1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.

16

Solution

int main() { Shape *shapes[2] = {new Circle(3), new Rectangle(4,

5)}; double sum = 0; for(int i = 0; i < 2; i++){ sum += shapes[i]->get_area(); } cout << sum << endl; return 0;}

Page 17: 1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.

17

Note

If a function is defined virtual in a base class, it is automatically virtual in all its derived classes. It is not necessary to add the keyword virtual in the function declaration in the derived class.