C++ Programming: Understanding Data Types and Input/Output Operations, Essays (high school) of Computer science

An excerpt from the fifth edition of 'C++ Programming: From Problem Analysis to Program Design'. It covers the basic elements of C++ programming, including data types, input and output operations. the concept of a C++ program, the role of whitespaces, and the difference between simple and complex data types. It also introduces the bool, char, floating-point, and string data types, as well as the process of allocating memory and putting data into variables.

Typology: Essays (high school)

2020/2021

Uploaded on 04/02/2022

lamis-mfk
lamis-mfk šŸ‡µšŸ‡ø

3 documents

1 / 78

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C++ Programming: From Problem Analysis to
Program Design, Fifth Edition
Chapter 2: Basic Elements of C++
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e

Partial preview of the text

Download C++ Programming: Understanding Data Types and Input/Output Operations and more Essays (high school) Computer science in PDF only on Docsity!

C++ Programming: From Problem Analysis to

Program Design, Fifth Edition

Chapter 2: Basic Elements of C++

Introduction

  • (^) Computer program
    • (^) Sequence of statements whose objective is to accomplish a task
  • (^) Programming
    • (^) Process of planning and creating a program

The Basics of a C++ Program

  • (^) Function: collection of statements; when executed, accomplishes something - (^) May be predefined or standard
  • (^) Syntax: rules that specify which statements (instructions) are legal
  • (^) Programming language: a set of rules, symbols, and special words
  • (^) Semantic rule: meaning of the instruction

Comments

  • (^) Comments are for the reader, not the compiler
  • (^) Two types:
    • (^) Single line // This is a C++ program. It prints the sentence: // Welcome to C++ Programming.
    • (^) Multiple line /* You can include comments that can occupy several lines. */

Reserved Words (Keywords)

  • (^) Reserved words, keywords, or word symbols - (^) Include: - (^) int - (^) float - (^) double - (^) char - (^) const - (^) void - (^) return

Identifiers

  • (^) Consist of letters, digits, and the underscore character (_)
  • (^) Must begin with a letter or underscore
  • (^) C++ is case sensitive
    • (^) NUMBER is not the same as number
  • (^) Two predefined identifiers are cout and cin
  • (^) Unlike reserved words, predefined identifiers may be redefined, but it is not a good idea

Whitespaces

  • (^) Every C++ program contains whitespaces
    • (^) Include blanks, tabs, and newline characters
  • (^) Used to separate special symbols, reserved words, and identifiers
  • (^) Proper utilization of whitespaces is important - (^) Can be used to make the program readable

Data Types

  • (^) Data type: set of values together with a set of operations
  • (^) C++ data types fall into three categories:

Simple Data Types (cont'd.)

  • (^) Integral data types are further classified into nine categories: - (^) char, short, int, long, bool - (^) unsigned char, unsigned short, unsigned int, unsigned long

Simple Data Types (cont'd.)

  • (^) Different compilers may allow different ranges of values

bool Data Type

  • (^) bool type
    • (^) Two values: true and false
    • (^) Manipulate logical (Boolean) expressions
  • (^) true and false
    • (^) Logical values
  • (^) bool, true, and false
    • (^) Reserved words

char Data Type

  • (^) The smallest integral data type
  • (^) Used for characters: letters, digits, and special symbols
  • (^) Each character is enclosed in single quotes
    • (^) 'A', 'a', '0', '*', '+', '$', '&'
  • (^) A blank space is a character
    • (^) Written ' ', with a space left between the single quotes

Floating-Point Data Types

(cont'd.)

  • (^) float: represents any real number
    • (^) Range: -3.4E+38 to 3.4E+38 (four bytes)
  • (^) double: represents any real number
    • (^) Range: -1.7E+308 to 1.7E+308 (eight bytes)

Floating-Point Data Types

(cont'd.)

  • (^) Maximum number of significant digits (decimal places) for float values is 6 or 7
  • (^) Maximum number of significant digits for double is 15
  • (^) Precision: maximum number of significant digits - (^) Float values are called single precision - (^) Double values are called double precision