Minimal Introduction to C++ - Part III - Final

10
Universidade Federal de Alagoas :: Campus A. C. Simões Geometric Modeling and Computer Vision Laboratory Minimal Introduction to C++ A Approach Directed to Resolution of Practical Problems Michel Alves dos Santos Universidade Federal de Alagoas, Campus A. C. Simões Tabuleiro do Martins - Maceió - AL, CEP: 57072-970 Part III - Mechanisms of Inheritance and Polymorphism {michel.mas}@gmail.com February 14, 2013 Minimal Introduction to C++ Language - Michel Alves A Approach Directed to Resolution of Pratical Problems

Transcript of Minimal Introduction to C++ - Part III - Final

Universidade Federal de Alagoas :: Campus A. C. Simões Geometric Modeling and Computer Vision Laboratory

Minimal Introduction to C++A Approach Directed to Resolution of Practical Problems

Michel Alves dos Santos

Universidade Federal de Alagoas, Campus A. C. SimõesTabuleiro do Martins - Maceió - AL, CEP: 57072-970Part III - Mechanisms of Inheritance and Polymorphism

{michel.mas}@gmail.com

February 14, 2013

Minimal Introduction to C++ Language - Michel Alves A Approach Directed to Resolution of Pratical Problems

Universidade Federal de Alagoas :: Campus A. C. Simões Geometric Modeling and Computer Vision Laboratory

Inheritance and Polymorphism

Mechanisms of Inheritance and Polymorphism

What happens in this figure?Minimal Introduction to C++ Language - Michel Alves A Approach Directed to Resolution of Pratical Problems

Universidade Federal de Alagoas :: Campus A. C. Simões Geometric Modeling and Computer Vision Laboratory

What is Inheritance?

The Concept of InheritanceInheritance is a mechanism of reusing and extending

existing classes without modifying them, thusproducing hierarchical relationships between them.

� �1 c l a s s Polygon{ /∗Some code . . . ∗/ } ;2 c l a s s T r i a n g l e : p u b l i c Polygon { /∗Some code . . . ∗/ } ;3 c l a s s Rec tang l e : p u b l i c Polygon { /∗Some code . . . ∗/ } ;� �Minimal Introduction to C++ Language - Michel Alves A Approach Directed to Resolution of Pratical Problems

Universidade Federal de Alagoas :: Campus A. C. Simões Geometric Modeling and Computer Vision Laboratory

What is Polymorphism?

The Concept of PolymorphismPolymorphism is the ability of objects belonging to

different types to respond to method, field, orproperty calls of the same name, each one according

to an appropriate type-specific behavior.

Entities that have more than one form.� �1 /∗Example : the + ( p l u s ) o p e r a t o r i n C++∗/23 4 + 5 // i n t e g e r a d d i t i o n4 3 .14 + 2 .0 // f l o a t i n g po i n t a d d i t i o n5 " foo " + "bar " // s t r i n g c on c a t ena t i o n !67 /∗ I n C++, tha t type o f po lymorph ism i s c a l l e d o v e r l o a d i n g ∗/� �

Polymorphism means that some code or operations or objects behave differently in differentcontexts.

Minimal Introduction to C++ Language - Michel Alves A Approach Directed to Resolution of Pratical Problems

Universidade Federal de Alagoas :: Campus A. C. Simões Geometric Modeling and Computer Vision Laboratory

Inheritance and Polymorphism: Examples

Some Examples of Inheritance and Polymorphism

What is the importance of abstract classes?Minimal Introduction to C++ Language - Michel Alves A Approach Directed to Resolution of Pratical Problems

Universidade Federal de Alagoas :: Campus A. C. Simões Geometric Modeling and Computer Vision Laboratory

Polymorphism in C++

Types of Polymorphism in C++

• Subtype Polymorphism (Runtime Polymorphism).• Parametric Polymorphism (Compile-Time Polymorphism).• Ad-hoc Polymorphism (Overloading).• Coercion Polymorphism (Casting).

Minimal Introduction to C++ Language - Michel Alves A Approach Directed to Resolution of Pratical Problems

Universidade Federal de Alagoas :: Campus A. C. Simões Geometric Modeling and Computer Vision Laboratory

Polymorphism in C++: Code ExampleCode Example� �

1 /∗Base c l a s s ∗/2 c l a s s F e l i d { p u b l i c : v i r t u a l v o i d meow( ) = 0 ; } ;34 /∗ Sub c l a s s e s ∗/5 c l a s s Cat : p u b l i c F e l i d { p u b l i c : v o i d meow( ) { cout << "meow!\ n" ; }} ;6 c l a s s T i ge r : p u b l i c F e l i d { p u b l i c : v o i d meow( ) { cout << "MREOW!\ n" ; }} ;7 c l a s s Oce lo t : p u b l i c F e l i d { p u b l i c : v o i d meow( ) { cout << "mews !\ n" ; }} ;� �� �1 t emp la te <c l a s s T> T max(T a , T b ) { r e t u r n a > b ? a : b ;}23 i n t main ( )4 {5 s t d : : cout << max (9 , 5) << "\ t " << max(" foo " , " bar " ) << std : : e nd l ; // 9 foo6 r e t u r n 0 ;7 }� �� �1 3 .14 + 2 .0 // f l o a t i n g po i n t a d d i t i o n2 " foo " + "bar " // s t r i n g c on c a t ena t i o n !� �� �1 f l o a t b = 6 ; // i n t g e t s promoted ( c a s t ) to f l o a t i m p l i c i t l y2 i n t a = 9 .99 // f l o a t ge t s demoted to i n t i m p l i c i t l y� �Minimal Introduction to C++ Language - Michel Alves A Approach Directed to Resolution of Pratical Problems

Universidade Federal de Alagoas :: Campus A. C. Simões Geometric Modeling and Computer Vision Laboratory

Solved Problems

A List of Solved Problems1 The Hierarchy of Polygon Class.

• Build a system that can represent the hierarchy ofpolygons. The base action of this hierarchy mustbe area.

2 The Area of Any Function or Curve j.• Build a program that can calculate the area of any

function or curve using the methods of rectangleand trapezium.

Minimal Introduction to C++ Language - Michel Alves A Approach Directed to Resolution of Pratical Problems

Universidade Federal de Alagoas :: Campus A. C. Simões Geometric Modeling and Computer Vision Laboratory

Exercises to Home

• Problem: The Hierarchy of Sorting Algorithms• Build a minimal system that can represent the

hierarchy of sorting algorithms considering ourscomplexity.

• Algorithms that must be implemented: bubble, insertion, quick and merge.

• Polyhedron Representation• Build a minimal system that can represent

regular polyhedrons. The base class

Polyhedron must has the follow methods:

• is_regular, area, volume, edge_length.

Minimal Introduction to C++ Language - Michel Alves A Approach Directed to Resolution of Pratical Problems

Universidade Federal de Alagoas :: Campus A. C. Simões Geometric Modeling and Computer Vision Laboratory

Thanks

Thanks for your attention!Michel Alves dos Santos - [email protected]

Minimal Introduction to C++ Language - Michel Alves A Approach Directed to Resolution of Pratical Problems