Presentation is loading. Please wait.

Presentation is loading. Please wait.

Informática II Prof. Dr. Gustavo Patiño MJ 16- 18 12-09-2013.

Similar presentations


Presentation on theme: "Informática II Prof. Dr. Gustavo Patiño MJ 16- 18 12-09-2013."— Presentation transcript:

1 Informática II Prof. Dr. Gustavo Patiño MJ 16- 18 12-09-2013

2  Programming Concept  Basic C++  C++ Extension from C Informática II. Facultad de Ingeniería. Universidad de Antioquia. 2013-2

3 A problem Find the area of a rectangle A set of data length width A set of functions area = length * width Then Applying functions to data to get answer Informática II. Facultad de Ingeniería. Universidad de Antioquia. 2013-2

4

5  The main program coordinates calls to procedures and hands over appropriate data as parameters. Informática II. Facultad de Ingeniería. Universidad de Antioquia. 2013-2

6  Familiar languages, as C, Pascal, Basic, Fortran, Assembly, all encapsulate functions and data in procedures.  In C, we also encapsulate procedures within procedures. We start with main() or a top level routine.  We then write subroutines that are called from main(). We introduce data into procedures using parameters or arguments and get information back using shared variables or return values. Informática II. Facultad de Ingeniería. Universidad de Antioquia. 2013-2

7 For the rectangle problem, mentally we form the model of what needs to be done => we develop a procedure: All such languages are called procedural languages. Informática II. Facultad de Ingeniería. Universidad de Antioquia. 2013-2 int compute_area (int l, int w) { return ( l * w ); } int compute_area (int l, int w) { return ( l * w ); }

8  Objects of the program interact by sending messages to each other

9 Informática II. Facultad de Ingeniería. Universidad de Antioquia. 2013-2 An object is an encapsulation of both functions and data (not one or the other individually)

10  Let's look at our earlier Rectangle through object oriented eyes: Informática II. Facultad de Ingeniería. Universidad de Antioquia. 2013-2 Object Rectangle data - encapsulated width length function-encapsulated area = length * width Object Rectangle data - encapsulated width length function-encapsulated area = length * width Called a method

11  In our object oriented program, we will have an instance of the class Rectangle.  If we wish to find the area of the rectangle, we send a request to the object instance telling the rectangle to return its area.  In C++, rather than writing a procedure, we define a class that encapsulates the knowledge necessary to answer the question here, what is the area of the rectangle. Informática II. Facultad de Ingeniería. Universidad de Antioquia. 2013-2

12  Let's look at our earlier Rectangle through object oriented eyes: class Rectangle { private: /* attributes */ public: /* operations */ } class Rectangle { private: /* attributes */ public: /* operations */ }

13 Informática II. Facultad de Ingeniería. Universidad de Antioquia. 2013-2 class Rectangle { private: int width, length; public: Rectangle(int w, int l) { width = w; length = l; } int area() { return width*length; } main() { Rectangle rect(3,5); cout<<rect.area()<<endl; }

14 Informática II. Facultad de Ingeniería. Universidad de Antioquia. 2013-2

15

16  Basic C++  Inherit all ANSI C directives  Inherit all C functions  Extensions Comments /* You can still use the old comment style, */ /* but you must be // very careful about mixing them */ // It's best to use this style for 1 line or partial lines /* And use this style when your comment consists of multiple lines */ Comments /* You can still use the old comment style, */ /* but you must be // very careful about mixing them */ // It's best to use this style for 1 line or partial lines /* And use this style when your comment consists of multiple lines */

17 cin and cout Example: cout << "hey"; char name[10]; cin >> name; cout << "Hey " << name << ", nice name." << endl; cout << endl; // print a blank line cin and cout Example: cout << "hey"; char name[10]; cin >> name; cout << "Hey " << name << ", nice name." << endl; cout << endl; // print a blank line

18 declaring variables almost anywhere Example: // declare a variable when you need it for (int k = 1; k < 5; k++) { cout << k; } declaring variables almost anywhere Example: // declare a variable when you need it for (int k = 1; k < 5; k++) { cout << k; }


Download ppt "Informática II Prof. Dr. Gustavo Patiño MJ 16- 18 12-09-2013."

Similar presentations


Ads by Google