Programming Abstractions

Slides:



Advertisements
Similar presentations
CSE202: Lecture 1The Ohio State University1 Introduction to C++
Advertisements

1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
1 Lecture 19 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Computer Science 1620 Function Scope & Global Variables.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 1 Introduction to C++: Name space: Defines an area in which names have scope. Can use the same name in different name spaces. Header file (file.
Creating your first C++ program
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Objective Write simple computer program in C++ Use simple Output statements Become familiar with fundamental data types.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Programming Abstractions Cynthia Lee CS106B. Today’s Topics Introducing C++ from the Java Programmer’s Perspective  C++ strings and streams, continued.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Week 13 - Friday.  What did we talk about last time?  Server communications on a socket  Function pointers.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Lecture 3: Getting Started & Input / Output (I/O)
Programming Abstractions
Bill Tucker Austin Community College COSC 1315
Computer Programming Your First Java Program: HelloWorld.java.
EECS 183.
Chapter 1.2 Introduction to C++ Programming
Great way to learn is by example so fire up Visual Studios C (at home make sure you custom install with C++ - no longer default) by Deborah R. Fowler.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Topic Pre-processor cout To output a message.
Chapter 1.2 Introduction to C++ Programming
LESSON 2 Basic of C++.
Programming Abstractions
CMSC 202 Lesson 2 C++ Primer.
Computing and Statistical Data Analysis Lecture 2
Completing the Problem-Solving Process
Introduction to C++ Systems Programming.
Bill Tucker Austin Community College COSC 1315
Programming Abstractions
Functions in C++ Eric Roberts CS 106B January 7, 2015.
Lab 7: File Input/Output Graham Northup
CompSci 230 Software Construction
COMP 2710 Software Construction File I/O
Global & Local Identifiers
Going from C++ to Java Jayden Navarro.
Programming Abstractions
Programming Abstractions
Chapter 2 – Getting Started
Starting Out with C++: From Control Structures through Objects
Computing Fundamentals
Namespaces How Shall I Name Thee?.
C++ Compilation Model C++ is a compiled language
CS150 Introduction to Computer Science 1
From C to C++: Summary of weeks 1 - 4
CS1201: Programming Language 2
Arithmetic Operations
A First Program.
Reading from and Writing to Files
Using string type variables
CMSC 202 Lesson 6 Functions II.
Subject:Object oriented programming
CS 144 Advanced C++ Programming February 21 Class Meeting
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
CS31 Discussion 1D Winter19: week 4
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
CS31 Discussion 1D Fall18: week 2
Introduction to Programming - 1
Chapter 1 c++ structure C++ Input / Output
Reading from and Writing to Files
Presentation transcript:

Programming Abstractions CS106B Cynthia Lee

Today’s Topics Introducing C++ from the Java Programmer’s Perspective firstprogram.cpp Function prototypes <iostream> and cout "simpio.h" and getLine() Hamilton example C++ strings and streams

C++ from the Java Programmer’s Perspective (But it’s ok if you don’t know java!)

A first C++ program (Error) firstprogram.cpp #include <iostream> #include "console.h" using namespace std; int main(){ cout << "|-5| = " << absoluteValue(-5) << endl; return 0; } int absoluteValue(int n) { if (n<0){ return -n; return n; You don’t know what automata is, do you? SOON! -- firstprogram.cpp : unlike java, filename doesn’t have to match class! In fact, don’t need a class! -- instead of "import" we have "#include" – note some have "" some have <> -- .h extension means "header" interface information as opposed to .cpp for implementation code -- "using namespace" just don’t worry about why and use it (saves having to do ::) -- main returns type int, just return zero if everything went ok (ignore why) -- we use cout instead of System.out.println(), endl puts a newline

A first C++ program (Fixed #1) firstprogram.cpp #include <iostream> #include "console.h" using namespace std; int absoluteValue(int n) { if (n<0){ return -n; } return n; int main(){ cout << "|-5| = " << absoluteValue(-5) << endl; return 0; You don’t know what automata is, do you? SOON! -- firstprogram.cpp : unlike java, filename doesn’t have to match class! In fact, don’t need a class! -- instead of "import" we have "#include" – note some have "" some have <> -- .h extension means "header" interface information as opposed to .cpp for implementation code -- "using namespace" just don’t worry about why and use it (saves having to do ::) -- main returns type int, just return zero if everything went ok (ignore why) -- we use cout instead of System.out.println(), endl puts a newline

A first C++ program (Fixed #2) firstprogram.cpp #include <iostream> #include "console.h" using namespace std; int absoluteValue(int n); int main(){ cout << "|-5| = " << absoluteValue(-5) << endl; return 0; } int absoluteValue(int n) { if (n<0){ return -n; return n; You don’t know what automata is, do you? SOON! -- firstprogram.cpp : unlike java, filename doesn’t have to match class! In fact, don’t need a class! -- instead of "import" we have "#include" – note some have "" some have <> -- .h extension means "header" interface information as opposed to .cpp for implementation code -- "using namespace" just don’t worry about why and use it (saves having to do ::) -- main returns type int, just return zero if everything went ok (ignore why) -- we use cout instead of System.out.println(), endl puts a newline

Design Question: Why does C++ have the function prototype syntax? In other words, why not just have a rule that you must set up the ordering so you define your functions before using them, as in the "FIXED 1" example? C++ could have done that, but such a rule would be too cumbersome for programmers to follow. C++ could have done that, but good programming style dictates "top-down" approach that logically puts main() first and helper functions it calls to follow. C++ could not have done that, because sometimes there is no way to order the functions so that all functions are defined before being used. Other/none/more than one of the above All of the above!

Which code comes first, the chicken or the egg Which code comes first, the chicken or the egg? (this code is just for fun, for now—we’ll cover recursion in depth in a few weeks!) #include<iostream> #include "console.h" using namespace std; void go(); void stanford(); int main(){ go(); return 0; } void go() { cout << "Go!" << endl; stanford(); void stanford() { cout << "Stanford!" << endl; What does this code do??

Streams in C++ Hamilton Example iostream (C++ Standard) simpio (Stanford)

HamDaDaDa Code Demo: What essential skills did we just see? You can read and write input/output with: cout, cin (<iostream>) getInteger(), getLine(), etc ("simpio.h") print a message before waiting for input cin and cout use the >> and << operators, respectively Remember: the arrows point in the way the data is “flowing” These aren’t like HTML tags <b> or Java/C++ parentheses () or curly braces {} in that they don’t need to “match” Good style: “static const int” to make int constants No “magic numbers”! Works for other types too ("static const double")