Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
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
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 Inheritance
Inheritance is a mechanism of reusing and extending
existing classes without modifying them, thus
producing 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 Rectangle : 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 Polymorphism
Polymorphism is the ability of objects belonging to
different types to respond to method, field, or
property 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++∗/
2
3 4 + 5 // i n t e g e r a d d i t i o n
4 3.14 + 2.0 // f l o a t i n g p o i n t a d d i t i o n
5 " foo " + " bar " // s t r i n g c o n c a t e n a t i o n !
6
7 /∗ In C++, that type of polymorphism 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 different
contexts.
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 Example
Code 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 ; };
3
4 /∗ S u b 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 Tiger : 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 Ocelot : 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 template <c l a s s T> T max(T a , T b ) { r e t u r n a > b ? a : b ;}
2
3 i n t main ( )
4 {
5 std : : cout << max (9 , 5) << " t " << max( " foo " , " bar " ) << std : : e ndl ; // 9 foo
6 r e t u r n 0 ;
7 }
¦ ¥
§ ¤
1 3.14 + 2.0 // f l o a t i n g p o i n t a d d i t i o n
2 " foo " + " bar " // s t r i n g c o n c a t e n a t i o n !
¦ ¥
§ ¤
1 f l o a t b = 6 ; // i n t g et s promoted ( c a s t ) to f l o a t i m p l i c i t l y
2 i n t a = 9.99 // f l o a t g et 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 Problems
1 The Hierarchy of Polygon Class.
• Build a system that can represent the hierarchy of
polygons. The base action of this hierarchy must
be 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 rectangle
and 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 ours
complexity.
• 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 - michel.mas@gmail.com
Minimal Introduction to C++ Language - Michel Alves A Approach Directed to Resolution of Pratical Problems

More Related Content

Minimal Introduction to C++ - Part III - Final

  • 1. 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
  • 2. 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
  • 3. Universidade Federal de Alagoas :: Campus A. C. Simões Geometric Modeling and Computer Vision Laboratory What is Inheritance? The Concept of Inheritance Inheritance is a mechanism of reusing and extending existing classes without modifying them, thus producing 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 Rectangle : p u b l i c Polygon { /∗Some code . . . ∗/ }; ¦ ¥ Minimal Introduction to C++ Language - Michel Alves A Approach Directed to Resolution of Pratical Problems
  • 4. Universidade Federal de Alagoas :: Campus A. C. Simões Geometric Modeling and Computer Vision Laboratory What is Polymorphism? The Concept of Polymorphism Polymorphism is the ability of objects belonging to different types to respond to method, field, or property 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++∗/ 2 3 4 + 5 // i n t e g e r a d d i t i o n 4 3.14 + 2.0 // f l o a t i n g p o i n t a d d i t i o n 5 " foo " + " bar " // s t r i n g c o n c a t e n a t i o n ! 6 7 /∗ In C++, that type of polymorphism 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 different contexts. Minimal Introduction to C++ Language - Michel Alves A Approach Directed to Resolution of Pratical Problems
  • 5. 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
  • 6. 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
  • 7. Universidade Federal de Alagoas :: Campus A. C. Simões Geometric Modeling and Computer Vision Laboratory Polymorphism in C++: Code Example Code 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 ; }; 3 4 /∗ S u b 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 Tiger : 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 Ocelot : 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 template <c l a s s T> T max(T a , T b ) { r e t u r n a > b ? a : b ;} 2 3 i n t main ( ) 4 { 5 std : : cout << max (9 , 5) << " t " << max( " foo " , " bar " ) << std : : e ndl ; // 9 foo 6 r e t u r n 0 ; 7 } ¦ ¥ § ¤ 1 3.14 + 2.0 // f l o a t i n g p o i n t a d d i t i o n 2 " foo " + " bar " // s t r i n g c o n c a t e n a t i o n ! ¦ ¥ § ¤ 1 f l o a t b = 6 ; // i n t g et s promoted ( c a s t ) to f l o a t i m p l i c i t l y 2 i n t a = 9.99 // f l o a t g et 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
  • 8. Universidade Federal de Alagoas :: Campus A. C. Simões Geometric Modeling and Computer Vision Laboratory Solved Problems A List of Solved Problems 1 The Hierarchy of Polygon Class. • Build a system that can represent the hierarchy of polygons. The base action of this hierarchy must be 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 rectangle and trapezium. Minimal Introduction to C++ Language - Michel Alves A Approach Directed to Resolution of Pratical Problems
  • 9. 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 ours complexity. • 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
  • 10. Universidade Federal de Alagoas :: Campus A. C. Simões Geometric Modeling and Computer Vision Laboratory Thanks Thanks for your attention! Michel Alves dos Santos - michel.mas@gmail.com Minimal Introduction to C++ Language - Michel Alves A Approach Directed to Resolution of Pratical Problems