140 likes | 155 Views
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
E N D
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 • Procedures and Functions • Object Oriented • Classes, Entities • Generic • No specific types and data structures
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 ); }
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#
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; }
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
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).
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
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
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 */
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 ; }
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++
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);
Summary • OOP is one of the many programming paradigms • Classes & Objects • Four Pillars of OOP • References • Function Overloading