1 / 14

CSE 459.22 – Introduction to C++

CSE 459.22 – Introduction to C++. Name: Shirish Tatikonda Time: T 3:30 PM Room: BE 394 E-mail: tatikond@cse.ohio-state.edu Office: DL 674 Office Hours: TBD Webpage: http://www.cse.ohio-state.edu/~tatikond/459.22/. Programming Paradigms. Unstructured goto Structured

kmartin
Download Presentation

CSE 459.22 – Introduction to 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. CSE 459.22 – Introduction to C++ Name: Shirish Tatikonda Time: T 3:30 PM Room: BE 394 E-mail: tatikond@cse.ohio-state.edu Office: DL 674 Office Hours: TBD Webpage: http://www.cse.ohio-state.edu/~tatikond/459.22/

  2. Programming Paradigms • Unstructured • goto • Structured • Procedures and Functions • Object Oriented • Classes, Entities • Generic • No specific types and data structures

  3. Procedural Paradigm • Decide which procedures you want • Use the best algorithms you can find • Example: C, Pascal int compute_area (int l, int w) { return ( l * w ); }

  4. Object Oriented Paradigm (OOP) • OOP is a decomposition paradigm for program code, not a model for computation. • Object interaction is through messages • Examples: SIMULA, Smalltalk, C++, Java, Python and C#

  5. Example code 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; }

  6. OOP – Key Concepts • Class • basis for modularity and structure of the program • represents real-world entity (type), say Dog. • Object • an instance of a class (or type), say Doberman • Can be characterized by • Identity • State • Behavior • Objects of same class has same data type

  7. OO Perspective of example Class Rectangle abstracts a specific shape Object rect data - encapsulated width length function ( called a method )- encapsulated area = length * width • Call a method (message) on object, rect to find area. • In C++, rather than writing a procedure, we define a class that encapsulates the knowledge necessary to solve the problem (finding the area of the rectangle).

  8. OOP – Key Concepts • Four Pillars • Abstraction: Selective Ignorance • Encapsulation ~ Information Hiding • Inheritance: Hierarchy • Polymorphism: Multiple forms • Abstraction: ability of a program to ignore the details of an object's (sub) class and work at desired level • Doberman: Dog – Canidae – Carnivora

  9. Encapsulation (Information Hiding) • Encapsulating object with resources, data & code • Data can be accessed only through code/methods • Code: object’s interface • Inheritance (Hierarchy) • Mechanism for creating subclasses (is-a relation) • Subclass acquires data and methods from super class • Polymorphism • Multiple forms of behavior for the same method • Type of behavior depends on the place from which method is called

  10. Basic C++ • Inherits all ANSI C features • You don’t have to do OOP using C++ • Comments • /* Single line comment */ • // New style for single line comment • /* Multiple Line Comment */

  11. Use objects to read and write (instead of printf, scanf) cout << "hey"; // no newline charatcter ‘\n’ char name[10]; cin >> name; cout << "Hey " << name << ", nice name." << endl; cout << endl; // print a blank line • Declare variables anywhere // declare a variable when you need it for (int k = 1; k < 5; k++) { cout << k ; }

  12. Const • Replacement for #define in C • Interpreted by the compiler • Type checking is applied • Should be initialized. (const int i; // error) • References • Alternate names for an object • Initialization should be done along with declaration int i =1; int& r = i; // r is reference to i int& e = 1; // error: should be an lvalue rr++; // equivalent of i++

  13. Function Overloading • Functions with same name (within same scope) can exist as long as signature differs • Signature: number, type, and order (??) of arguments (return type is not included) • Name + Signature uniquely identifies a function (within one scope)` • int foo(int p1, char p2 ); Vs int foo(char p1, char p2); • char foo(char p1, char p2 ); Vs int foo(char p1, char p2);

  14. Summary • OOP is one of the many programming paradigms • Classes & Objects • Four Pillars of OOP • References • Function Overloading

More Related