Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Ivor Horton, Beginning ANSI C++: The Complete Language, 3 rd.

Slides:



Advertisements
Similar presentations
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Advertisements

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. About the book Ivor Horton, Beginning ANSI C++: The Complete.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
計算機概論實習 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
Data types and variables
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
Chapter 2 Data Types, Declarations, and Displays
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Basic Data Type and calculations Data types in C++ Program.
First Program in C With Output. C Language Keywords.
L EC. 02: D ATA T YPES AND O PERATORS (1/2) Fall Java Programming.
Objectives You should be able to describe: Data Types
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
1 Chapter 9 Scope, Lifetime, and More on Functions.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
CS2311 Computer Programming Dr. Yang, Qingxiong (with slides borrowed from Dr. Xu, Henry) Lecture 2: Basic Syntax – Part I: Variables and Constants.
C-Language Keywords(C99)
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
CPS120: Introduction to Computer Science
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Definition Various stream manipulators can be used to specify the kinds of formatting to be performed during stream-I/O operations. Stream manipulators.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapters 1-5 Review C++ Class. Chapter 1 – the big picture Objects Class Inheritance Reusability Polymorphism and Overloading.
Chapter 2 Data types, declarations, and displays.
Simple Data Types Built-In and User Defined Chapter 10.
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.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
CPS120: Introduction to Computer Science Variables and Constants.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. More on Handling Basic Data Types Mixed types of data Data.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Overview of Previous Lesson(s) Over View  I nteractive development environment is a software application that provides comprehensive facilities to computer.
Chapter 15 - C++ As A "Better C"
C++ Lesson 1.
Introduction to C++ (Extensions to C)
The Machine Model Memory
Chap. 2. Types, Operators, and Expressions
Computing Fundamentals
Chapter 2.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
C++ Programming Lecture 3 C++ Basics – Part I
2. Second Step for Learning C++ Programming • Data Type • Char • Float
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
C++ Programming Basics
Module 2 Variables, Data Types and Arithmetic
Programming Languages and Paradigms
The Fundamentals of C++
Presentation transcript:

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Ivor Horton, Beginning ANSI C++: The Complete Language, 3 rd ed. ANSI C++ –The standard for C++ –American National Standards Institute –1998, International Standard for C++, ISO/IEC 14882, approved by ANSI and INCITS (International Committee for Information Technology Standards) About the book

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Programming Languages High level languages –1950s, FORTRAN, LISP, COBOL –1970s, BASIC, C, PASCAL –1980s, C++, Object PACAL/Delphi, Objective-C –1990s, Java, Python –2000s, C# Low level language –Assembly C++ language development environments –Borland C++ –Microsoft Visual C++, Microsoft Turbo C++ Programming Language rating, 2015,

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Interpreter and Compiler

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Advantages of C++ High level language with low level programming (programming down at hardware level) Various applications – word processing, scientific computation, operating system, computer games High efficient procedural programming inherits from C with powerful object-oriented programming Extensive standard library Many commercial libraries supporting a wide range of operating system environments and specialized for C++

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. C++ names Names are referred to as identifiers –Functions –Variables –Types –Labels –Namespaces Legal names Illegal names

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Program Structure

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Function Calls and Executions

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Compile and Link Processes

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Compilation Process

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Linking Process

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. C++ Source Characters Basic source character set ASCII (American Standard Code for Information Interchange) Unicode (UCS;Universal Character Set) –UCS-2 presents characters as 16-bit codes (65536 different codes) –UCS-4 presents characters as 32-bit codes (4 billion different codes)

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Escape Sequences Control characters “Problem” characters

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Simple Programs ASCII codes (app. A, p.1009) –A hexadecimal number: \x or \X –An octal number: \

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. White space When?

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Procedure and Object-oriented Programming Procedure programming –Also known as routines, subroutines, methods, or functions, simply contain a series of computational steps to be carried out –C, FORTRAN, PASCAL, COBOL, … Object-oriented programming(OOP) –Is a programming paradigm based on the concept of “object", which are data structures that contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. The Standard Library Standard library header –App. C

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Namespace std #include using namespace std; int main()// int main(void) { cout << “Who is the beginner \?” << endl; return 0; } #include void main()// void main(void) { std::cout << “Who is the beginner \?” << std::endl; } #include using std::cout; using std::endl; void main() {// void main(void) std::cout << “Who is the beginner \?” << std::endl; }

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Variable Types Integer variables –char (1byte) –short (2 bytes) –int (2 or 4 bytes) –long (4 or 8 bytes) Floating variables –float (4 bytes) –double (8 bytes) –long double (8 or 10 bytes)

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Integer Domain in VC++ TypeSize (bytes)Domain char1-128 ~ 127 unsigned char10U ~ 255U short ~ unsigned short20U ~ 65535U int ~ unsigned int40U ~ U long L~ L unsigned long40UL ~ UL

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Numeric functions for integers #include “math.h” or #include using namespace std;

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC.

Random numbers #include // standard library #include // timing function // random seed Program 2.5

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Floating Numbers In VC++ “ e003” represent the above number

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Floating domain in VC++ TypeSize (bytes) Decimal precision Domain float47≒ ~ double815≒ ~ long double 8 (10)15(19)≒ ~ ( ~ )

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Floating Operation The float.h file Program 2.7 –Floating point error –Never rely on an exact floating-point representation of a decimal value in your program code Program 2.8 –Floating point output manupilator

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Escape sequences

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Data Type Conversions Static cast –static_cast (expression) Constant cast –const_cast (expression) Reinterpret cast –reinterpret (expression) Dynamic cast –dynamic_cast (expression)

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Explicit Casts Static cast –Old-style casts (C) (object_type) (expression) int a = (int) 3.5; or int a = 3.5; –New-style casts (C++) static_cast (expression) int a = static_cast 3.5*23; Program 3.1

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Mixed expressions Mixed expression rule –Two floating operation  higher precision –Floating and Integer operation  floating

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. sizeof() Count the size of a type or a variable Program 3.2

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Numeric Limitation numeric_limits ::min() numeric_limits ::max() numeric_limits ::digits Program 3.3 Header file –limits.h

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Boolean Operators T: true; F: false PQP && QP || Q!P TTTTF TFFTF FTFTT FFFFT

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Bitwise operators

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Bitwise operations bit1bit2&|^~bit : true, 0: false

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Bitwise Operators Bitwise AND  & –0&0  0 0&1  0 1&1  1 Bitwise OR  | –0|0  0 0|1  1 1|1  1 Bitwise XOR  ^ –0^0  0 0^1  1 1^1  0 Left shift  << –( )<<3  ( ) Right shift  >> –( )>>2  ( ) Bitwise complement  ~ –~( )  ( )

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Output Manipulators Header file –iostream.h Behind cout << –dec, hex, oct, left, right, fixed, scientific, showpoint, noshowpoint, showbase, noshowbase, showpos, noshowpos, uppercase, nouppercase, boolalpha, noboolalpha

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC.

Output Manipulators Header file –iomanip.h Behind cout << –setprecision, setw, setbase, setfill

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Bitwise exclusive OR operations Program 3.4

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Lifetime of a Variable A variable can have three different kinds of storage duration –Automatic storage duration –Static storage duration –Dynamic storage duration

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Variable Scopes Automatic variables or local variables –Variables declared in block –Program 3.6 Global variables –Variables declared outside of blocks –::  scope resolution operator –Program 3.7 Static variables (local access, global exist) External variables

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Association for the operators OperatorAssociationOperatorAssociation Postfix ++LeftBinary + -Left Postfix --Left >Left Unary +Right& | ^ ~Left Unary -Right* / %Left Prefix ++Right= Prefix --Rightstatic_cast()Right

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Character Testing Functions Header file –#include –#include using namespace std; Character testing functions –isupper(), islower(), isalpha(), isdigit(), isxdigit(), isalnum(), isspace(), iscntrl(), isprint(), isgraph(), ispunct()

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Logical Operators Boolean operators Boolean header file –bool.h Return integer –false (0), true (non-zero) Program 4.4

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. More coffee and donuts

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Clearer version

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Logical Operators AND (&&), OR(||), NOT(!) boolean – false (0), true (otherwise) Programs 4.7, 4.8, 4.9

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. switch Statement Multiple choices Keywords –switch –case –default –break Program 4.8, 4.9

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Alternative Ways

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Decision Statement Blocks and Variable Scope