1 / 18

Introduction to Object Oriented Programming in C++

Introduction to Object Oriented Programming in C++. Informática II Prof. Dr. Gustavo Patiño MJ 16- 18 12-09-2013. Introduction to C++. Programming Concept Basic C++ C++ Extension from C. What programming is?. A problem Find the area of a rectangle A set of data length

chiko
Download Presentation

Introduction to Object Oriented Programming in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Object Oriented Programming in C++ Informática II Prof. Dr. Gustavo Patiño MJ 16- 18 12-09-2013

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

  3. What programming is? 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. Programming Concept Evolution Informática II. Facultad de Ingeniería. Universidad de Antioquia. 2013-2

  5. Procedural Concept • 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. Procedural Concept (II) • 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. Procedural Concept (III) 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. intcompute_area (int l, int w) { return ( l * w ); } Informática II. Facultad de Ingeniería. Universidad de Antioquia. 2013-2

  8. Object-Oriented Concept • Objects of the program interact by sending messages to each other

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

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

  11. OO Perspective (2) • 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. Class definition in C++ • Let's look at our earlier Rectangle through object oriented eyes: class Rectangle { private: /* attributes */ public: /* operations */ }

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

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

  15. Characteristics of OOPL Informática II. Facultad de Ingeniería. Universidad de Antioquia. 2013-2

  16. Basic C++ and Extensions from C • 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 */

  17. Basic C++ Extension from C (2) • cinand cout • Example: • cout << "hey"; • char name[10]; • cin>> name; • cout<< "Hey " << name << ", nice name." << endl; • cout<< endl; // print a blank line

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

More Related