1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.
-
Upload
lauren-mosley -
Category
Documents
-
view
216 -
download
2
Transcript of 1 Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che.
1
Chapter 10Inheritance 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();
3
How to sum different Shape?
Circle circle(3);
Rectangle rect(4, 5);
double sum = 0;
sum += circle.get_area();
sum += rect.get_area();
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();
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.
6
Shape class hierarchy
Shape
Rectangle
Square
Triangle Circle • • • •
inherits (isa)
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
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
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;}
}
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
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(); // ??
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
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?
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.
15
Virtual Functionclass Shape {
public:
Shape();
Shape(const Shape& orig);
virtual ~Shape();
void print();
virtual double get_area();};
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;}
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.