Copyright 2005, The Ohio State University CSE – Introduction to C++ Name: Shirish Tatikonda Time: T 3:30 PM Room: BE Office: DL 674 Office Hours: TBD Webpage:
Copyright 2005, The Ohio State University Programming Paradigms Unstructured –goto Structured –Procedures and Functions Object Oriented –Classes, Entities Generic –No specific types and data structures
Copyright 2005, The Ohio State University 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 ); }
Copyright 2005, The Ohio State University 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#
Copyright 2005, The Ohio State University Example code class Rectangle { private: int width, length; public: Rectangle(int w, int l) { width = w; length = l; } main() { Rectangle rect(3,5); cout<<rect.area()<<endl; } int area() { return width*length; }
Copyright 2005, The Ohio State University 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
Copyright 2005, The Ohio State University OO Perspective of example Class Rectangle abstracts a specific shape Objectrect 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).
Copyright 2005, The Ohio State University 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
Copyright 2005, The Ohio State University 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
Copyright 2005, The Ohio State University 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 */
Copyright 2005, The Ohio State University 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 ; }
Copyright 2005, The Ohio State University 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++
Copyright 2005, The Ohio State University 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);
Copyright 2005, The Ohio State University Summary OOP is one of the many programming paradigms Classes & Objects Four Pillars of OOP References Function Overloading