Programming Language Principles Lecture 30 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Object-Oriented Programming.

Slides:



Advertisements
Similar presentations
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Advertisements

C++ Programming Languages
Programming Language Concepts Lecture 19 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Object-Oriented Programming (Part.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles C/C++ Emery Berger and Mark Corner University of Massachusetts.
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
C++ fundamentals.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Review of C++ Programming Part II Sheng-Fang Huang.
COMPUTER PROGRAMMING. Introduction to C++ History Merges notions from Smalltalk and notions from C The class concept was borrowed from Simular67 Developed.
Introduction to C++ Systems Programming.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Chapter 12: Adding Functionality to Your Classes.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
C++ Programming. Table of Contents History What is C++? Development of C++ Standardized C++ What are the features of C++? What is Object Orientation?
Introduction to Object-oriented programming and software development Lecture 1.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
C++ language first designed or implemented In 1980 by Bjarne Stroustrup, from Bell labs. that would receive formally this name at the end of 1983.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Prepared by: Elsy Torres Shajida Berry Siobhan Westby.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
Object-Oriented Programming Chapter Chapter
ISBN Object-Oriented Programming Chapter Chapter
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Overview of C++ Polymorphism
Object-Oriented Programming (OOP) and C++
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
C++ Programming Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Chapter 15 - C++ As A "Better C"
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Principles of programming languages 10: Object oriented languages
Introduction to C++ Systems Programming.
Polymorphism.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Object-Oriented Programming Part 1
Object-Oriented Programming
CS212: Object Oriented Analysis and Design
Overview of C++ Polymorphism
Object-Oriented Programming (Part 2)
Lecture 6: Polymorphism
Presentation transcript:

Programming Language Principles Lecture 30 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Object-Oriented Programming

Object Oriented Programming Over time, data abstraction has become essential as programs became complicated. Benefits: 1. Reduce conceptual load (minimum detail). 2. Fault containment. 3. Independent program components. (difficult in practice). Code reuse possible by extending and refining abstractions.

Object Oriented Programming A methodology of programming Four (Five ?) major principles: 1.Data Abstraction. 2.Encapsulation. 3.Information Hiding. 4.Polymorphism (dynamic binding). 5.Inheritance. (particular case of polymorphism ?) Will describe these using C++, because...

The C++ language An object-oriented, general-purpose programming language, derived from C (C++ = C plus classes). C++ adds the following to C: 1.Inlining and overloading of functions. 2.Default argument values. 3.Argument pass-by-reference. 4.Free store operators new and delete, instead of malloc() and free(). 5.Support for object-oriented programming, through classes, information hiding, public interfaces, operator overloading, inheritance, and templates.

Design Objectives in C++ Compatibility. Existing code in C can be used. Even existing, pre-compiled libraries can be linked with new C++ code. Efficiency. No additional cost for using C++. Overheadof function calls is eliminated where possible. Strict type checking. Aids debugging, allows generation of efficient code. C++ designed by Bjarne Stroustrup of Bell Labs (now at UT Austin). Standardization: ANSI, ISO.

Non Object-Oriented Extensions to C Major improvements over C. 1.Stream I/O. 2.Strong typing. 3.Parameter passing by reference. 4.Default argument values. 5.Inlining. We’ve discussed some of these already.

Stream I/O in C++ Input and output in C++ is handled by streams. The directive #include declares 2 streams: cin and cout. cin is associated with standard input. Extraction: operator>>. cout is associated with standard output. Insertion: operator<<. In C++, input is line buffered, i.e. the user must press before any characters are processed.

Example of Stream I/O in C++ A function that returns the sum of the numbers in the file Number.in int fileSum(); { ifstream infile("Number.in"); int sum = 0; int value; //read until non-integer or while(infile >> value) sum = sum + value; return sum; }

Example of Stream I/O in C++ Example 2: A function to copy myfile into copy.myfile void copyfile() { ifstream source("myfile"); ofstream destin("copy.myfile"); char ch; while (source.get(ch)) destin<<ch; }

Line-by-line textfile concatenation int ch; // Name1, Name2, Name3 are strings ifstream f1 (Name1); ifstream f2 (Name2); ofstream f3 (Name3); while ((ch = f1.get())!=-1 ) if (ch =='\n') while ((ch = f2.get())!=-1) { f3.put(ch); if (ch == '\n') break; } else f3.put(ch); }

Why use I/O streams ? Streams are type safe -- the type of object being I/O'd is known statically by the compiler rather than via dynamically tested '%' fields. Streams are less error prone: – Difficult to make robust code using printf. Streams are faster: printf interprets the language of '%' specs, and chooses (at runtime) the proper low-level routine. C++ picks these routines statically based on the actual types of the arguments.

Why use I/O streams ? (cont’d) Streams are extensible -- the C++ I/O mechanism is extensible to new user-defined data types. Streams are subclassable -- ostream and istream (C++ replacements for FILE*) are real classes, and hence subclassable. Can define types that look and act like streams, yet operate on other objects. Examples: –A stream that writes to a memory area. –A stream that listens to external port.

C++ Strong Typing There are 6 principal situations in which C++ has stronger typing than C. 1.The empty list of formal parameters means "no arguments" in C++. In C, it means "zero or more arguments", with no type checking at all. Example: char * malloc();

C++ Strong Typing (cont’d) 2.In C, it's OK to use an undefined function; no type checking will be performed. In C++, undefined functions are not allowed. Example: main() f( ); // C++: error, f not defined // C: OK, taken to mean int f()

C++ Strong Typing (cont’d) 3.A C function, declared to be value- returning, can fail to return a value. Not in C++. Example: double foo() { /*... */ return; } main() { if ( foo() ) {... }... } // C : OK // C++: error, no return value.

C++ Strong Typing (cont’d) 4.In C, assigning a pointer of type void* to a pointer of another type is OK. Not in C++. Example: int i = 1024; void *pv = &i; // C++: error, // explicit cast required. // C : OK. char *pc = pv; int len = strlen(pc);

C++ Strong Typing (cont’d) 5.C++ is more careful about initializing arrays: Example: char A[2]="hi"; // C++: error, // not enough space for '\0' // C : OK, but no '\0' is stored. It's best to stick with char A[] = "hi“;

C++ Strong Typing (cont’d) 6.Free store (heap) management. In C++, we use new and delete, instead of malloc and free. malloc() doesn't call constructors, and free doesn't call destructors. new and delete are type safe.

Object-Oriented Programming Object-oriented programming is a programming methodology characterized by the following concepts: 1.Data Abstraction: problem solving via the formulation of abstract data types (ADT's). 2.Encapsulation: the proximity of data definitions and operation definitions. 3.Information hiding: the ability to selectively hide implementation details of a given ADT. 4.Polymorphism: the ability to manipulate different kinds of objects, with only one operation. 5.Inheritance: the ability of objects of one data type, to inherit operations and data from another data type. Embodies the "is a" notion: a horse is a mammal, a mammal is a vertebrate, a vertebrate is a lifeform.

O-O Principles and C++ Constructs O-O ConceptC++ Construct(s) AbstractionClasses EncapsulationClasses Information HidingPublic and Private Members PolymorphismOperator overloading, templates, virtual functions InheritanceDerived Classes

O-O is a different Paradigm Central questions when programming. –Imperative Paradigm: –What to do next ? –Object-Oriented Programming –What does the object do ? (vs. how) Central activity of programming: –Imperative Paradigm: –Get the computer to do something. –Object-Oriented Programming –Get the object to do something.

C vs. C++, side-by-side

C vs. C++, side-by-side (cont’d) In C++, methods can appear inside the class definition (better encapsulation)

C vs. C++, side-by-side (cont’d) In C++, no explicit referencing. Could have overloaded > for Stacks: s > i;

Structures and Classes in C++ Structures in C++ differ from those in C in that members can be functions. A special member function is the “constructor”, whose name is the same as the structure. It is used to initialize the object: struct buffer { buffer() {size=MAXBUF+1; front=rear=0;} char buf[MAXBUF+1]; int size, front, rear; }

Structures and Classes in C++ The idea is to add some operations on objects of type buffer : struct buffer { buffer() {size=MAXBUF+1;front=rear=0;} char buf[MAXBUF+1]; int size, front, rear; int succ(int i) {return (i+1)%size;} int enter(char); char leave(); }

Structures and Classes in C++ The definition (body) of a member function can be included in the structure's declaration, or may appear later. If so, use the name resolution operator (::) int buffer::enter(char x) { // body of enter } char buffer::leave() { // body of leave }

Public and Private Members Structures and classes are closely related in C++: struct x { }; is equivalent to class x { public: }; Difference: by default, members of a structure are public; members of a class are private. So, class x { }; is the same as struct x { private: };

Header File Partitioning

Header File Partitioning (cont’d)

Programming Language Principles Lecture 30 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Object-Oriented Programming