COMP1008 Other OO Languages C++ and Ruby

Similar documents
Comp151. Definitions & Declarations

An Incomplete C++ Primer. University of Wyoming MA 5310

C++ Programming Language

C++FA 5.1 PRACTICE MID-TERM EXAM

CORBA Programming with TAOX11. The C++11 CORBA Implementation

C++ Overloading, Constructors, Assignment operator

C++ INTERVIEW QUESTIONS

CSI33 Data Structures

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Delegating Constructors (revision 3)

Semester Review. CSC 301, Fall 2015

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Evolution of the Major Programming Languages

Chapter 3.2 C++, Java, and Scripting Languages. The major programming languages used in game development.

The C Programming Language course syllabus associate level

Semantic Analysis: Types and Type Checking

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

CISC 181 Project 3 Designing Classes for Bank Accounts

C# and Other Languages

Computer Programming C++ Classes and Objects 15 th Lecture

Object Oriented Software Design II

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Visual Studio 2008 Express Editions

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Cours de C++ Utilisations des conteneurs

McGraw-Hill The McGraw-Hill Companies, Inc.,

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

El Dorado Union High School District Educational Services

Chapter 5 Names, Bindings, Type Checking, and Scopes

Crash Course in Java

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Database Toolkit: Portable and Cost Effective Software

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Compiler Construction

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

CSC230 Getting Starting in C. Tyler Bletsch

Copyright 2001, Bill Trudell. Permission is granted to copy for the PLoP 2001 conference. All other rights reserved.

Introduction to Java

A Comparison of Programming Languages for Graphical User Interface Programming

Note: Syntactically, a ; is needed at the end of a struct definition.

Habanero Extreme Scale Software Research Project

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

C++FA 3.1 OPTIMIZING C++

CS 106 Introduction to Computer Science I

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

Sources: On the Web: Slides will be available on:

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Appendix K Introduction to Microsoft Visual C++ 6.0

Functions and Parameter Passing

Chapter 1 Fundamentals of Java Programming

Passing 1D arrays to functions.

Brent A. Perdue. July 15, 2009

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

Characteristics of Java (Optional) Y. Daniel Liang Supplement for Introduction to Java Programming

How to: Use Basic C++, Syntax and Operators

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Coding conventions and C++-style

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Moving from CS 61A Scheme to CS 61B Java

sqlpp11 - An SQL Library Worthy of Modern C++

Object-Oriented Programming

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Konzepte objektorientierter Programmierung

Object Oriented Software Design II

Basics of I/O Streams and File I/O

Introduction to Object-Oriented Programming

Why you shouldn't use set (and what you should use instead) Matt Austern

Inheritance in Programming Languages

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Software Engineering Concepts: Testing. Pointers & Dynamic Allocation. CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009

Glossary of Object Oriented Terms

Java Application Developer Certificate Program Competencies

Informatica e Sistemi in Tempo Reale

Basics of C++ and object orientation in OpenFOAM

URI and UUID. Identifying things on the Web.

Embedded SQL. Unit 5.1. Dr Gordon Russell, Napier University

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

C++ program structure Variable Declarations

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

CSCI E 98: Managed Environments for the Execution of Programs

Programming Languages

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

The programming language C. sws1 1

1 Abstract Data Types Information Hiding

Chapter 1 Java Program Design and Development

Jonathan Worthington Scarborough Linux User Group

Binary storage of graphs and related data

Tutorial on C Language Programming

Java Interview Questions and Answers

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

CSE 452: Programming Languages. Acknowledgements. Contents. Java and its Evolution

iphone Objective-C Exercises

Software documentation systems

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Transcription:

COMP1008 Other OO Languages C++ and Ruby Agenda Categories of Object-Oriented Languages Type Checking C++ Ruby 2 Other Object-Oriented Languages Many OO languages exist. Only a minority are in widespread use. See http://en.wikipedia.org/wiki/object-oriented_programming#history and similar websites. Java is one of the most popular and actively used. All share the same underlying ideas and concepts. Try some other languages yourself! 3

Categories of OO Languages Fully compiled to machine code run directly by processor: C++, Eiffel, (Ada95) Compiled to bytecode run by virtual machine: Smalltalk, Java, (C#) Interpreted and scripting languages: Ruby, Python, JavaScript 4 Type Checking Check that code follows language syntax and grammar specification. Check that variables and values have correct type. Check that values of different types are not mixed up. e.g. void d = true + 10; Check that methods are called with correct kind of parameters. And so on. 5 Static v. Dynamic Type Checking Static type checking is done by the compiler. Compiler checks and enforces the type rules. Dynamic type checking is done while a program is run. Runtime code checks types of values/objects are correct. Compiled languages typically use static type checking but need some dynamic checking as well. C++, Java have extensive static checking. But, Java does make significant use of dynamic checking. 6

Static v. Dynamic Type Checking (2) Static Advantages: An entire category of errors can be detected before a program is ever run. Errors are reported early during compilation. Type safety. Disadvantages Can be complex. Program code longer (due to type declarations, etc.) and takes more time to write. Limits, or makes more complicated, what can be expressed with the language. More complicated and slower compilers. 7 Static v. Dynamic Type Checking (3) Dynamic Advantages: Flexibility, greater ease of expression. Allows more dynamic code (e.g., reflection, self-modifying code). Don t need to declare variables, types, etc. before use. Coding speed. Disadvantages: Type checking delayed until code is run. Type errors may not be found for some time. Less information in source code for understanding what it does. 8 Static v. Dynamic Type Checking (4) Which is best? Long running and contentious debate! Static checking seen as good for larger, more complex programs. Dynamic checking seen as good for rapid development, prototyping and agile development. Static checking has been fairly dominant for several decades but dynamic checking on rise again. 9

Strong v. Weak Typing With a strongly typed language all type errors will be detected. Either by the compiler or at runtime. With a weakly typed language type errors are ignored. Binary representation just used without checking e.g., Use an binary may represent an int but used as float. Defaults used, e.g., treat everything as a string. Or errors just happen. C++, Java, Ruby, Python, Smalltalk are all strongly typed. Weakly typed languages are typically used for scripting, e.g., bash shell script. 10 C++ Well established, heavy-weight OO language. Has sunk a bit under its own weight, though. Compiles direct to machine code, so seen as good for performance. Derived from C, which was developed with Unix. Syntax and many features inherited by Java. Widely used for systems programming and application development. 11 C++ Language Big and complex. Fully supports OO programming. Methods called functions (& member functions). Also supports procedural programming (no classes) Upwardly compatible with the C programming language. Standard libraries, including STL (standard template library). No virtual machine so gives direct access to memory. Efficient compilers can generate high-performance code. 12

C++ Compilation Source File Source File Source File.o file contains binary code compiled from source file. Compiler Object File Object File Object File Library Code Library Code Linker Linking combines object files into single executable program. Executable Program 13 C++ Compiler Many available Lots of programmers use GCC, the GNU Compiler Collection Major Free Software Foundation project. See http://gcc.gnu.org/ Supports C, C++, Objective-C, Objective-C++, Java, Fortran and Ada. Operating Systems like GNU/Linux and OS X are written in C/C++ and compiled by GCC. g++ command used for C++ compilation (gcc for C). 14 Declaration v. Definition A declaration introduces a name and its type. Declarations are put into a header file, a.h file. A definition defines what a name is. e.g., provides a function body. Definitions are put in a.cpp (or.cc) file. Compiling a.cpp file typically requires one or more hearder files to be included. Linking a complete program requires all the declarations and definitions to be consistent. 15

C++ Class Person Header File #include <string> #include <vector> using std::string; using std::vector; class Person { private: string firstname; string familyname; vector<string> emailaddresses; public: Person(string firstname, string familyname); string getfullname(); void addemailaddress(string email); vector<string> getemailaddresses(); ; Include header files for string and vector library classes. Vector is like ArrayList. std::string means string in standard namespace. Using allows string to be used instead of std::string. 16 C++ Class Person Member Functions #include "Person.h" Person::Person(string firstname, string familyname) : firstname(firstname), familyname(familyname) { Include Person header file in order to declare class. string Person::getFullName() { return firstname + " " + familyname; void Person::addEmailAddress(string email) { emailaddresses.push_back(email); vector<string> Person::getEmailAddresses() { return emailaddresses; Declare member functions using Person:: to identify which class function belongs to. Class can be compiled using: g++ -c Person.cpp Creates Person.o, unlinked binary code representation. 17 Using Class Person #include <iostream> #include "Person.h" using std::cout; using std::endl; void show(vector<string> v) { cout << "---" << endl; vector<string>::iterator iter; for (iter = v.begin(); iter!= v.end(); iter++) { cout << *iter << endl; cout << "---" << endl; cout << message is like System.out.println << is the inserter operator. void useperson() { Person p("arthur","dent"); cout << "Person fullname: " << p.getfullname() << endl; p.addemailaddress("dent@earth.com"); p.addemailaddress("dent@earth.co.uk"); show(p.getemailaddresses()); int main (int argc, char* const argv[]) { useperson(); return 0; main function where execution starts. 18

Running Program From the command line: > g++ -c Person.cpp > g++ -c main.cpp > g++ -o Person Person.o main.o > Person Creates an executable program called Person. For simple programs can do this in one step: > g++ *.cpp > a.out This creates an executable program called a.out. 19 Objects and Memory The C++ Person class looks like it works the same way as the Java version. Object references pointing to objects. But it is not... C++ has a more complex memory model. Gives programmer control but programmer must understand how memory is used. 20 Variables and Objects Person p("arthur","dent"); Person q("ford","prefect");... q = p; Variable p actually holds the Person object, not a reference to it. The assignment copies the object. Hence in the Person example: objects passed as parameters objects returned from methods and objects used in assignment expressions are all copied. No references, changing copy doesn't change original. Memory allocation is handled automatically. C++ allows a copy constructor to control copying. 21

Using Pointers We have to rewrite C++ Person to use pointers, to make it behave like the Java version. A pointer is a memory address. A pointer variable stores the address in memory where the object is located. Like a reference but a pointer is a real memory address. 22 C++ Person Header with Pointers #include <string> #include <vector> using std::string; using std::vector; class Person2 { private: string* firstname; string* familyname; vector<string*>* emailaddresses; public: Person2(string* firstname, string* familyname); ~Person2(); string* getfullname(); void addemailaddress(string* email); vector<string*>* getemailaddresses(); ; * is used to denote a pointer type. Person* is pointer to Person (object). p: arthur : Person firstname = "Arthur" familyname = "Dent" emailaddresses = empty list Objects are allocated on the Heap (heap object). Also have to introduce a destructor. Note - this is an example, not necessarily normal practice. 23 C++ Person Member Functions with Pointers #include "Person2.h" Person2::Person2(string* firstname, string* familyname) : firstname(firstname), familyname(familyname) { emailaddresses = new vector<string*>(); Person2::~Person2() { delete firstname; delete familyname; vector<string*>::iterator iter; for (iter = emailaddresses->begin(); iter!= emailaddresses->end(); iter++) { delete *iter; delete emailaddresses; string* Person2::getFullName() { return new string(*firstname + " " + *familyname); void Person2::addEmailAddress(string* email) { emailaddresses->push_back(email); vector<string*>* Person2::getEmailAddresses() { return emailaddresses; Destructor must be declared to delete object and anything it points at. No garbage collection. * also used as dereference operator to follow pointer and access object. -> is also a dereference operator to call function. 24

Using Person with Pointers #include <iostream> #include "Person2.h" using std::cout; using std::endl; void show2(vector<string*>* v) { cout << "---" << endl; vector<string*>::iterator iter; for (iter = v->begin(); iter!= v->end(); iter++) { cout << **iter << endl; new is used to create cout << "---" << endl; objects void useperson2() { Person2* p2 = new Person2(new string("ford"), new string("prefect")); std::cout << "Person fullname: " << *(p2->getfullname()) << endl; p2->addemailaddress(new string("dent@earth.com")); p2->addemailaddress(new string("dent@earth.co.uk")); show2(p2->getemailaddresses()); int main (int argc, char * const argv[]) { useperson2(); 25 Pointers and Memory person: Person object string object string object vector object string object string object... Local variable in stack memory Objects in heap memory 26 delete C++ no garbage collection. Heap Objects must be deleted explicitly using the delete operator. If an object has pointers to other objects, then code must be written to delete all the objects. Hence, the need to declare a destructor function. If a heap object is not deleted it remains in memory. A memory leak occurs when heap objects are not deleted (a bug). If the program is run long enough, memory can run out even though the objects cannot be used. 27

Pointer Arithmetic A reference in Java can only be used in a controlled way. A pointer in C++ can be changed by addition and substraction. Any location in data memory can potentially be accessed. Any piece of memory could be treated as an object, whether it holds an object or not. 28 Pointer Errors Person* p = new Person("Arthur","Dent"); Memory Location of object Location accessed by pointer p = p + 2; // pointer arithmetic p->getfullname(); // Runs and tries to use whatever is in memory as a Person object. // May or may not fail, depending on memory content. 29 No Pointers in Java Pointers give a lot of power, but: Programmer must get use correct. Programmer must manage memory, new/delete. Can easily be abused. Allows encapsulation to be completely bypassed. Cause of many bugs in C++ programs. Pointers can lead to very efficient code. Java deliberately replaced pointers with references and memory management. To eliminate a large source of errors. 30

Questions? 31 Ruby An interpreted language. No compiler. An interpreter reads program text line by line and carries out each statement. This can be optimised. Trade-off performance for flexibility and rapid programming (no time compiling). Small, light-weight language. Very strongly typed, dynamically type checked. 32 Ruby Class Person Person.rb class Person def initialize(firstname,familyname) @firstname = firstname @familyname = familyname @emailaddresses = [] end def getfullname @firstname + " " + @familyname end def addemailaddress(address) @emailaddresses << address end Append to array @name used to denote an instance variable. Variable is added to object when used. An empty array Constructor method No type declarations needed def getemailaddresses end Array.new(@emailAddresses) 33

Using Ruby Class Person person = Person.new("Arthur","Dent") puts person.getfullname person.addemailaddress "arthur@earth.com" emails = person.getemailaddresses puts emails.length puts emails[0] person.addemailaddress "arthur@hog.com" emails = person.getemailaddresses puts emails.length puts emails[0..-1] Displays: Arthur Dent 1 arthur@earth.com 2 arthur@earth.com arthur@hog.com This code can simply be appended to the source file after the class declaration. Note that no class, method or main has to be declared. Run using the command: ruby Person.rb Ruby interpreter reads file and interprets code line by line. puts (put string) is used to display output. 34 Ruby, objects and memory Like Java, Ruby uses object references. Provides garbage collection. Strong dynamic typing means that only methods declared by object s class can be called. But Ruby language provides more ways of declaring methods than Java. Everything is an object, no primitive types. e.g., 1.next => 2 (call method next on 1) 35 Summary Strong v. Weak Type Checking Static v. Dynamic Type Checking C++, compiled OO language Ruby, interpreted OO language 36