Top Banner
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 I - Introductory Elements and Concepts {michel.mas}@gmail.com February 14, 2013 Minimal Introduction to C++ Language - Michel Alves A Approach Directed to Resolution of Pratical Problems
15

Minimal Introduction to C++ - Part I

Aug 07, 2015

Download

Education

Michel Alves
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Minimal Introduction to C++ - Part I

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-970

Part I - Introductory Elements and Concepts{michel.mas}@gmail.com

February 14, 2013

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

Page 2: Minimal Introduction to C++ - Part I

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

Introductory Elements and Concepts

C++

I A Programming LanguageI Multi-platformI Multi-paradigmI Support multiple libraries(OpenGL, boost, CGAL, etc.)

I GUI: FLTK, QT, GTKmm, etc.I Fast, Clean, Elegant, Powerful, etc.

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

Page 3: Minimal Introduction to C++ - Part I

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

Anatomy of Programs in C++

A Simple Program in C++� �1 /∗∗2 ∗ I n c l u s i o n o f l i b r a r i e s .3 ∗/4 #i n c l u d e <ios t ream> /∗ De f au l t l i b r a r y f o r man i pu l a t i o n o f f l ow s or s t r eams ∗/5 #i n c l u d e <c s t d l i b > /∗ De f au l t l i b r a r y f o r man i pu l a t i o n o f system d i r e c t i v e s ∗/67 /∗∗8 ∗ Main Func t i on . Th i s i s our e n t r y p o i n t f u n c t i o n .9 ∗/10 i n t main ( i n t argc , cha r ∗ a rgv [ ] )11 {12 s t d : : cout << "He l l o World ! " << std : : e nd l ;13 r e t u r n EXIT_SUCCESS ;14 }� �

Compilationg++ filename.cxx -o myapplication

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

Page 4: Minimal Introduction to C++ - Part I

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

Behaviour of Functions in C++

Behaviour of Functional Units

I For each functionalunit we have an entry(valid or void).

I This entry will betransformed into thefunction (valid entry).

I The function willreturn a transformedvalue (valid output).

When the function does not return a value we

call procedure!

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

Page 5: Minimal Introduction to C++ - Part I

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

The Inclusion System

Headers and Inclusion System

HeadersI Legacy: cstdio, cstdlib, cmath, climits, etc.I Native: iostream, fstream, iomanip, etc.I STL: vector, list, set, map, algorithm, etc.

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

Page 6: Minimal Introduction to C++ - Part I

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

A Universal makefile

Importance of the makefile

I Automate operations of compilationI Avoid compilation of unneeded filesI Used with several tools (gcc, latex, R, etc.)

Essential Clausules - makefile

CXX = ‘which g++‘SRC = $(wildcard * .cxx)OBJ = $(SRC:.cxx=.o)

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

Page 7: Minimal Introduction to C++ - Part I

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

Solved Problems

A List of Solved Problems

I Distance between points.I Reading content of a file.I Calculating area of cosine function.I Sorting numbers from a file.I Constructor’s and destructor’s flow.I Classifying triangles.

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

Page 8: Minimal Introduction to C++ - Part I

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

Solved Problems: Distance between points.

Figure: Can you write another solution using adifferent approach? Think in classes!

I For this exercise we willbuild a minimal systemthat calculates thedistance betweenpoints on R2 .

I We will use structs torepresent theabstraction Point.

I We will introduce theheader cmath.

I In the end we will showthe value of distance.

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

Page 9: Minimal Introduction to C++ - Part I

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

Solved Problems: Reading content of a file.

I For this exercise we willbuild a minimal systemthat reads the contentof a file.

I We will use the defaultentry: std::cin.

I We will perform thesum of read values.

I The content of filemust be numerical.

I In the end we will showthe sum of values.

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

Page 10: Minimal Introduction to C++ - Part I

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

Solved Problems: Area of cosine function.

0 1 2 3 4 5

−1.

5−

1.0

−0.

50.

00.

51.

01.

5

f (x ) = cos (x )

x

y=

f(x

)

0.5 1.5 2.5 3.5 4.5

0

π2

π

3π2

A

cosine curvesome notable pointsarea under the curve

Area of the Region:

A = ∫0

π 2cos(x)dx

Figure: Can you write a funcion that calculates thearea of any curve?

I For this exercise we willbuild a minimal systemthat calculates the areaof cosine function.

I We will use therectangle method andthe trapezium methodfor this calculation.

I We will use a range forthe x axis.

I In the end we will do acomparison betweenthe methods.

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

Page 11: Minimal Introduction to C++ - Part I

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

Solved Problems: Sorting numbers from a file.

I For this exercise we willbuild a minimal systemthat sorts a set ofnumbers.

I We will see how to usea STL Conteiner calledvector and how to readvalues from a file (again).

I We will see also how topass parameters perreference, and theimportance of this kindof passage.

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

Page 12: Minimal Introduction to C++ - Part I

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

Solved Problems: Constructor’s and destructor’s flow.

I For this exercise we willbuild a minimal systemthat tracks thebehaviour of objects.

I We will see howconstructors anddestructors areperformed and the flowof a program.

I In the end we will seewhat happens when weallocate memory for anobject (and we don’t desallocate).

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

Page 13: Minimal Introduction to C++ - Part I

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

Solved Problems: Classifying triangles

A

B

C

−4 −2 0 2 4

−4

−2

02

4

Tr i ang l e

x

y

−5 −4 −3 −2 −1 0 1 2 3 4 5

−5

−4

−3

−2

−1

01

23

45

Tr i ang l e

Coordinates:

A: ( −4 , 1 )B: ( 2 , −4 )C: ( 4 , 4 )

Build a minimalsystem that classifytriangles using yourside information.The system must willhave classes thatrepresents pointsand triangles:

I Class Point.

I Class Triangle.

Test the system withthe triangle of theimage.

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

Page 14: Minimal Introduction to C++ - Part I

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

Exercises to Home

I Attribute: valueI Operators: + - * /I Methods: cos, sin, sqrt,pow, exp, is_prime, log.

I Attribute: edgeI Operators: ==, !=, >, <I Methods: area, volume,diagonal, diagonal_face.

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

Page 15: Minimal Introduction to C++ - Part I

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