CSE 143 Introduction to C++ [Appendix B] 4/11/98.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Chapter 5 Functions.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Basic Elements of C++ Chapter 2.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
References types and Value types With a very brief introduction to struct and enum Reference types and Value types1.
CSE 332: C++ execution control statements Overview of C++ Execution Control Expressions vs. statements Arithmetic operators and expressions * / % + - Relational.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Chapter 6: Programmer- defined Functions Development of simple functions using value and reference parameters JPC and JWD © 2002 McGraw-Hill, Inc. Modified.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction 15.2C A Simple Program: Adding Two Integers.
Introduction to Programming
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
1 Scope Lifetime Functions (the Sequel) Chapter 8.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Chapter 15 - C++ As A "Better C"
Bill Tucker Austin Community College COSC 1315
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
User-Written Functions
Chapter 1.2 Introduction to C++ Programming
Computing and Statistical Data Analysis Lecture 2
Chapter 1.2 Introduction to C++ Programming
Objectives In this chapter, you will:
Computing and Statistical Data Analysis Lecture 2
Introduction to C++ Systems Programming.
Predefined Functions Revisited
Introduction to Computer Science / Procedural – 67130
Basic Elements of C++.
Review: Two Programming Paradigms
Student Book An Introduction
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Basic Elements of C++ Chapter 2.
Multiple Files Revisited
Object Oriented Programming COP3330 / CGS5409
Introduction to C++ Programming
Chapter 6: Functions Starting Out with C++ Early Objects Ninth Edition
Pointers, Dynamic Data, and Reference Types
Built-In (a.k.a. Native) Types in C++
6 Chapter Functions.
Testing and Repetition
Chapter 2: Introduction to C++.
Pointers Pointers point to memory locations
Programming Introduction to C++.
Chapter 15 - C++ As A "Better C"
COP 3330 Object-oriented Programming in C++
Predefined Functions Revisited
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
Lecture 3 More on Flow Control, More on Functions,
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

CSE 143 Introduction to C++ [Appendix B] 4/11/98

Introduction to C++ C++ is a superset of C (Almost) any legal program in C is also a legal C++ program The biggest changes are those to support "object-oriented" programming We'll introduce these gradually 4/11/98

A Few Simple Extensions cout for Output to screen Comments Declarations Symbolic constants Reference parameters Enumerated types 4/11/98 CSE 143

cout for output to screen #include <iostream.h> cout << "Hello out there"; puts a message on the screen It's part of the stream I/O package We'll study in much more detail soon Meanwhile -- try it out! 4/11/98

Two Styles of Comments Old C-style comments /* This is a comment */ Double-slash comments (comment extends from the // to the end of the line) int id; // student ID number Which form is better? 4/11/98

Declarations Are Statements C++ declarations are statements, and can appear anywhere a normal statement can void something (int x) { if (x == 10) x = x / 2; int y; // Declaration can occur here ... } Common usage: for-loop index variables for (int k = 0; k < 100; k++) { // do something 4/11/98

Symbolic Constants Do not use #define … #define PI 3.14159 Why not? Because no type checking is performed. Use this form instead... const double PI = 3.14159; … because the compiler will perform type checking and scope analysis 4/11/98

Parameter Passing Pass by value (as in C): Pass by reference: Passes a copy of the actual parameter (except arrays) Assignments to formal don’t affect actual parameters Pass by reference: Makes formal an alias for actual parameter Like passing address of actual, without messy & and * Assignments to formal do change actual 4/11/98

Pass by Reference Place & between type and parameter name Invocation void increment (int &x) { x=x+1; }name Invocation int someVal = 0; increment(someVal); // no & needed here!! What if someVal were passed by value? 4/11/98

Enumerated Types User-defined type whose constants are meaningful identifiers, not just numbers enum Color { RED, GREEN, BLUE }; Declare like other types; use like other integer types Color skyColor; ... switch (skyColor) { case RED: ... case GREEN: ... case BLUE: ... } 4/11/98

Type Casting Don’t use it unless really necessary. C style casts (prefix notation): someInt = (int) someFloat; C++ style casts (functional notation): someInt = int(someFloat); 4/11/98

New bool type Review: C convention is 0 is false, non-0 is true C++ bool has two legal values: true and false bool, true and false are all reserved words now Direction implementation of the "Boolean" concept bool checkBigNumber (double d) { if (d > 30E6) return true; else return false; } Not supported in earlier C++ compilers Not mentioned in earlier textbooks 4/11/98

int vs. bool Under the hood, bool is an int type Use bool where Boolean values are natural int i; bool b=true; b = (mass >= 10.8); //value is true or false if (b) ... //OK while (b && !(i < 15)) ... //OK Avoid: i = b; //marginally OK: value is 0 or 1 i = true; //OK, but bad style b = i; //ill-advised (warning) cout << displays 0 or 1 for bool values 4/11/98