Software Design 8.1 A Rose by any other name…C or Java? l Why do we use Java in our courses (royal we?)  Object oriented  Large collection of libraries.

Slides:



Advertisements
Similar presentations
C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
Advertisements

C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
CS201 – Introduction to Computing – Sabancı University 1 First Midterm Exam l November 22, 2008, Saturday, 10:40 – 12:20, max 100 minutes l One A4 size.
1 CSC 222: Computer Programming II Spring 2004  vectors  library, templated  advantages over arrays:  const-reference parameters  growable vectors.
CSE 374 Programming Concepts & Tools Hal Perkins Winter 2012 Lecture 19 – Introduction to C++
Variables Names Bindings Type Scope. L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address.
Three types of computer languages
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles C/C++ Emery Berger and Mark Corner University of Massachusetts.
C++ fundamentals.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
CPS 108 : Spring From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea  Not really a standard language like C++  Arguably.
Classes: From Use to Implementation
High-Level Programming Languages: C++
11 Getting Started with C# Chapter Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a.
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Compsci 100, Fall ’.1 Views of programming l Writing code from the method/function view is pretty similar across languages  Organizing methods.
Working with Strings Lecture 2 Hartmut Kaiser
By – Tanvir Alam.  This tutorial offers several things.  You’ll see some neat features of the language.  You’ll learn the right things to google. 
 200 Total Points ◦ 74 Points Writing Programs ◦ 60 Points Tracing Algorithms and determining results ◦ 36 Points Short Answer ◦ 30 Points Multiple Choice.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
CIT 590 Intro to Programming First lecture on Java.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
A Computer Science Tapestry 12.1 Tools for programming l Syntatic details of control and variables  If, while, switch, function calls  Scope and lifetime.
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.
C++ Memory Overview 4 major memory segments Key differences from Java
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first.
CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account sysadmin/account_request.cgi Need to know UNIX (Linux)
CompSci 100e 11.1 Sorting: From Theory to Practice l Why do we study sorting?  Because we have to  Because sorting is beautiful  Example of algorithm.
CompSci Problem: finding subsets l See CodeBloat APT, requires finding sums of all subsets  Given {72, 33, 41, 57, 25} what is sum closest (not.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Object Oriented Software Development 4. C# data types, objects and references.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Compsci 101, Fall LWoC l Review Recommender, dictionaries, files  How to create recommendations in order? food.txt  Toward a Duke eatery-recommender.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
CompSci 100e 12.1 Sorting: In 2 Slides l Why do people study sorting?  Because we have to  Because sorting is beautiful  Example of algorithm analysis.
Object-Oriented Programming (OOP) and C++
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
C++ Functions A bit of review (things we’ve covered so far)
Week 13 - Friday.  What did we talk about last time?  Server communications on a socket  Function pointers.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Chapter Topics The Basics of a C++ Program Data Types
Bottom up meets Top down
Eugene Hsu.
Computing and Statistical Data Analysis Lecture 2
Basic Elements of C++.
Working with Strings Lecture 2 Hartmut Kaiser
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.
Basic Elements of C++ Chapter 2.
(5 - 1) Object-Oriented Programming (OOP) and C++
Reference parameters (6.2.3)
Brought to you by C++ Tutorial Brought to you by
Working with Strings Lecture 2 Hartmut Kaiser
From C++ to Java Java history: Oak, toaster-ovens, internet language, panacea What it is O-O language, not a hybrid (cf. C++) compiled to byte-code, executed.
(5 - 1) Object-Oriented Programming (OOP) and C++
SPL – PS1 Introduction to C++.
From bit to byte to char to int to long
Presentation transcript:

Software Design 8.1 A Rose by any other name…C or Java? l Why do we use Java in our courses (royal we?)  Object oriented  Large collection of libraries  Safe for advanced programming and beginners  Harder to shoot ourselves in the foot l Why don't we use C++ (or C)?  Standard libraries weak or non-existant (comparatively)  Easy to make mistakes when beginning  No GUIs, complicated compilation model

Software Design 8.2 Why do we learn other languages? l Perl, Python, PHP, mySQL, C, C++, Java, Scheme, ML, …  Can we do something different in one language? Depends on what different means. In theory: no; in practice: yes  What languages do you know? All of them.  In what languages are you fluent? None of them l In later courses why do we use C or C++?  Closer to the machine, we want to understand the machine at many levels, from the abstract to the ridiculous Or at all levels of hardware and software  Some problems are better suited to one langauge What about writing an operating system? Linux?

Software Design 8.3 C++ on three slides l Classes are similar to Java, compilation model is different  Classes have public and private sections/areas  Typically declaration in.h file and implementation in.cpp Separate interface from actual implementation Good in theory, hard to get right in practice  One.cpp file compiles to one.o file To create an executable, we link.o files with libraries Hopefully someone else takes care of the details (Makefile) l We #include rather than import, this is a preprocessing step  Literally sucks in an entire header file, can take a while for standard libraries like iostream, string, etc.  No abbreviation similar to java.util.*;

Software Design 8.4 C++ on a second slide l We don't have to call new to create objects, they can be created "on the stack"  Using new creates memory "on the heap"  In C++ we need to do our own garbage collection, or avoid and run out of memory (is this an issue?) vector similar to ArrayList, pointers are similar to arrays  Unfortunately, C/C++ equate array with memory allocation  To access via a pointer, we don't use. we use -> l Streams are used for IO, iterators are used to access begin/end of collection  ifstream, cout correspond to Readers and System.out

Software Design 8.5 C++ idioms/general concepts l Genericity  Templates, STL, containers, algorithms l Copy/Assignment/Memory  Deep copy model, memory management “required” l Low-level structures  C-style arrays and strings compared to STL, Tapestry l const  Good for clients, bad for designers/coders? l From C to C++ to Java  function pointers, function objects, inheritance

Software Design 8.6 Language Design Goals l Java  New, pure O-O language  Compiled to byte-code, write once run “anywhere” simple, object-oriented, portable, interpreted, robust, secure, architecture-neutral, distributed, dynamic, multithreaded, high performance l C++  Compatible, O-O language features added on to C  No feature was included that would cause a serious incompatibility with C cause run-time or space overheads for a program that didn’t use it increase run-time or space requirements for a C program. significantly increase the compile time compared not be easy and efficient to implement in a traditional C programming environment.

Software Design 8.7 How do we read a file? (SearchDemo) Java: TreeSet unique = new TreeSet (); int total = 0; while (s.hasNext()){ String str = s.next(); unique.add(str.toLowerCase()); total++; } myWordsAsList = new ArrayList(set); C++:set unique; int total = 0; string word; while (input >> word){ transform(word.begin(), word.end(), word.begin(), ::tolower); unique.insert(word); total++; } myWords = vector (unique.begin(), unique.end());

Software Design 8.8 Toward an Understanding of C++ l Traditional first program, doesn’t convey power of computing but it illustrates basic components of a simple program #include using namespace std; // traditional first program int main() { cout << " Hello world " << endl; return 0; } l This program must be edited/typed, compiled, linked and executed. l Other languages don’t use compile/link phase, examples?

Software Design 8.9 What’s a namespace? l In “standard” C++, objects and types are classified as to what namespace they’re in. Hierarchy is good. #include // traditional first program int main() { std::cout << " Hello world " << std::endl; return 0; } l It’s much simpler to “use” a namespace, in small programs there won’t be any conflicts (and small is fairly big)

Software Design 8.10 Compiling and linking, differences // string.cpp // stuff we can’t // understand #include int main() { string s = “hi”; } hello.cpp … hello.o … string.o hello Link hello.exe

Software Design 8.11 Quadratic Equation Example void Roots(double a, double b, double c, double& root1, double& root2); // post: root1 and root2 set to roots of // quadratic ax^2 + bx + c // values undefined if no roots exist int main() { double a, b, c, r1, r2; cout << "enter coefficients "; cin >> a >> b >> c; Roots(a, b, c, r1, r2); cout << "roots are " << r1 << " " << r2 << endl; return 0; }

Software Design 8.12 Who supplies memory, where’s copy? void Roots(double a, double b, double c, double& root1, double& root2); // post: root1 and root2 set to roots of // quadratic ax^2 + bx + c // values undefined if no roots exist l For value parameter, the argument value is copied into memory that “belongs” to parameter l For reference parameter, the argument is the memory, the parameter is an alias for argument memory double x, y, w, z; Roots(1.0, 5.0, 6.0, x, y); Roots(1.0, w, z, 2.0, x); // no good, why?

Software Design 8.13 Parameter Passing: const-reference l When parameters pass information into a function, but the object passed doesn’t change, it’s ok to pass a copy  Pass by value means pass a copy  Memory belongs to parameter, argument is copied l When parameter is altered, information goes out from the function via a parameter, a reference parameter is used  No copy is made when passing by reference  Memory belongs to argument, parameter is alias l Sometimes we want to avoid the overhead of making the copy, but we don’t want to allow the argument to be changed (by a malicious function, for example)  const-reference parameters avoid copies, but cannot be changed in the function

Software Design 8.14 Count # occurrences of “e” l Look at every character in the string, avoid copying the string int letterCount(const string& s, const string& letter) // post: return number of occurrences of letter in s { int count = 0; for(int k = 0; k < s.length(); k++) { if (s.substr(k, 1) == letter) { count++; } return count; } l Calls below are legal (but won’t be if just reference parameters) int ec = letterCount("elephant", "e"); string s = "hello"; cout << letterCount(s, "a");

Software Design 8.15 General rules for Parameters l Don’t worry too much about efficiency at this stage of learning to program  You don’t really know where efficiency bottlenecks are  You have time to develop expertise l However, start good habits early in C++ programming  Built-in types: int, double, bool, char, pass by value unless returning/changing in a function  All other types, pass by const-reference unless returning/changing in a function  When returning/changing, use reference parameters l Const-reference parameters allow constants to be passed, “hello” cannot be passed with reference, but ok const- reference