Csc1100 lecture01 ch01-pt1
Transcript of Csc1100 lecture01 ch01-pt1
![Page 1: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/1.jpg)
A First Book of C++ 4th Edition 1
Du`a for StudyDu`a for Study
![Page 2: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/2.jpg)
A First Book of C++A First Book of C++Chapter 1Chapter 1
Getting StartedGetting Started
![Page 3: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/3.jpg)
In this chapter, you will learn about: Introduction to Programming Function and Class Names The cout Object Programming Style Common Programming Errors
A First Book of C++ 4th Edition 3
ObjectivesObjectives
![Page 4: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/4.jpg)
Computer program (software) Data and instructions used to operate a computer
Programming Writing computer program in a language that
the computer can respond to and that other programmers can understand
Programming language Set of instructions, data, and rules used to
construct a programA First Book of C++ 4th Edition 4
Introduction to Introduction to ProgrammingProgramming
![Page 5: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/5.jpg)
Categories of Programming Language Level
High-level languages (e.g., C, C++, C#, Visual Basic, Java, Python)
Low-level languages (e.g., Assembly language)
A First Book of C++ 4th Edition 5
Introduction to Programming Introduction to Programming (cont’d.)(cont’d.)
![Page 6: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/6.jpg)
Assembly vs Machine Assembly vs Machine LanguageLanguage
A First Book of C++ 4th Edition 6
• Early computers were programmed in assembly language• To calculate wages = rates * hours in assembly language:
1) Load 2) Multiply 3) Store
010001
010011
010010
![Page 7: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/7.jpg)
Categories of Programming Language (cont) Orientation
Procedural language Instructions are used to create self-contained units
(procedures) Procedures accept data as input and transform data to
produce a specific result as an output Initially, high-level programming languages were
predominately procedural
A First Book of C++ 4th Edition 7
Introduction to Programming Introduction to Programming (cont’d.)(cont’d.)
![Page 8: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/8.jpg)
Introduction to Programming Introduction to Programming (cont’d.)(cont’d.)
A First Book of C++ 4th Edition 8
a + b = ?
a = 2 , b =3 5
![Page 9: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/9.jpg)
Object-oriented languages Program must define objects that it is manipulating Such definitions include:
The general characteristics of objects (attribute/state) Specific operations to manipulate objects
(behavior/method) C++ is an object-oriented language
Has procedures and objects Supports code reuse
A First Book of C++ 4th Edition 9
Introduction to Programming Introduction to Programming (cont’d.)(cont’d.)
![Page 10: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/10.jpg)
C++ began as extension to C C is a procedural language developed in the
1970s at AT&T Bell Laboratories In early 1980s, Bjarne Stroustrup (“Bee-yarn-
eh Strow-strup”, also at AT&T) used his background in simulation languages to develop C++
Object-orientation and other procedural improvements were combined with existing C language features to form C++
A First Book of C++ 4th Edition 10
Introduction to Programming Introduction to Programming (cont’d.)(cont’d.)
![Page 11: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/11.jpg)
Before writing a program, a programmer must clearly understand: What data is to be used The expected result The procedure needed to produce this result
The procedure is referred to as an algorithm Step-by-step sequence of instructions
describing how to perform a computation
A First Book of C++ 4th Edition 11
Algorithms and ProceduresAlgorithms and Procedures
![Page 12: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/12.jpg)
Computers think algorithmically (step by step, use rules) NOT heuristically (based on experience, no rules)!
Assume that a program must calculate the sum of all whole numbers from 1 through 100 (1+2+3+4+5+…+100)
A computer: Cannot respond to heuristic command: “Add the numbers
from 1 - 100” Is an algorithm-responding machine and not a heuristic-
responding machine Several methods or algorithms can be used to find the
required sum
A First Book of C++ 4th Edition 12
Algorithms and Procedures (cont’d.)
![Page 13: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/13.jpg)
A First Book of C++ 4th Edition 13
![Page 14: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/14.jpg)
Algorithms and Procedures (cont’d.)
A First Book of C++ 4th Edition 14
Calculate the result for 3 x 2
![Page 15: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/15.jpg)
A First Book of C++ 4th Edition 15
Flow Chart
![Page 16: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/16.jpg)
A First Book of C++ 4th Edition 16
![Page 17: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/17.jpg)
C++ source program Set of instructions written in C++ language
Machine language Internal computer language Consists of a series of 1s and 0s (binaries)
Source program cannot be executed until it is translated into machine language Interpreted language translates one statement at a
time Compiled language translates all statements together
A First Book of C++ 4th Edition 17
Program Translation
![Page 18: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/18.jpg)
Program Translation (cont'd.)
A First Book of C++ 4th Edition 18
100100 010010int x = 8
![Page 19: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/19.jpg)
Processing a Program
A First Book of C++ 4th Edition 19
![Page 20: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/20.jpg)
Modular programs Segments arranged in logical order to form an
integrated unit Module
Segments (small parts) of modular program Function: Name of a C++ procedure
Composed of sequence of C++ instructions Function interface is its inputs and results (output) Method of converting input to results is encapsulated
and hidden within function
A First Book of C++ 4th Edition 20
Function and Class Names
![Page 21: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/21.jpg)
A First Book of C++ 4th Edition 21
Function and Class Names (cont'd.)
![Page 22: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/22.jpg)
Function and Class Names (cont'd.)
A First Book of C++ 4th Edition 22
Accepts 2 numbers as input, produce a result
3 5
15
Input
![Page 23: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/23.jpg)
Identifiers (or variables) Names that convey an idea of the purpose of function or
class Identifier composition rules
First character must be a letter or underscore (A1, _1) Only letter, digit, or underscore may follow (AA, A2, A_) Blank spaces aren’t allowed (A B) Identify component words with initial capitalization (numPos) Cannot be C++ keyword (e.g., auto, break, int, switch …) Should be a mnemonic (short, easy to remember,
meaningful)
A First Book of C++ 4th Edition 23
Function and Class Names (cont'd.)
![Page 24: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/24.jpg)
Function and Class Names (cont'd.)
A First Book of C++ 4th Edition 24
![Page 25: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/25.jpg)
Examples of valid identifiers:grosspay $cost (valid in C++)addNums degToRadmultByTwo salesTaxnetPay bessel_name my$
**Note: When we combine two or more words to form an identifier (or variables), it is a good practice to capitalize each of the new word.
A First Book of C++ 4th Edition 25
Function and Class Names (cont'd.)
![Page 26: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/26.jpg)
Examples of invalid identifiers:
4ab3 (begins with a number)e*6 (contains a special character)while (is a keyword)#amount (begins with special character)
A First Book of C++ 4th Edition 26
Function and Class Names (cont'd.)
![Page 27: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/27.jpg)
Each C++ program must have one and only one key function named main()
Called a driver function because it drives the other modules
A First Book of C++ 4th Edition 27
The main() Function
![Page 28: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/28.jpg)
The main() Function (cont'd.)
A First Book of C++ 4th Edition 28
![Page 29: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/29.jpg)
First line of function is called header line What type of data, if any, is returned from function The name of function What type of data, if any, is sent into function
Data transmitted into function at runtime are referred to as arguments of function arguments (input to function)
Example: header line void sum(int a, int b) { }
A First Book of C++ 4th Edition 29
The main() Function (cont'd.)
![Page 30: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/30.jpg)
Starting to Program with Dev C++
A First Book of C++ 4th Edition 30
![Page 31: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/31.jpg)
Starting to Program with Dev C++
A First Book of C++ 4th Edition 31
![Page 32: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/32.jpg)
The main() Function (cont'd.)
A First Book of C++ 4th Edition 32
beginning of program
end of program
![Page 33: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/33.jpg)
The cout object sends data to the standard output display device The display device is usually a video screen Name derived from console output and
pronounced “see out” Data is passed to cout by the insertion
symbol cout << “Hello there, World!”;
A First Book of C++ 4th Edition 33
The cout Object
![Page 34: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/34.jpg)
The cout Object (cont’d.)
A First Book of C++ 4th Edition 34
//Pre-processor command <header file>//File for pre-written classes/classes (a containet //that provides contexts/scope for variables)
system(“pause”);
![Page 35: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/35.jpg)
The cout Object (cont’d.)
A First Book of C++ 4th Edition 35
//Pre-processor command <header file>
system(“pause”);std::cout << “Hello there world!”;
name for namespace
![Page 36: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/36.jpg)
Preprocessor command Performs an action before the compiler translates
source code to machine code Example: #include <iostream> Causes the iostream file to be inserted wherever
the #include command appears iostream is part of the C++ standard library
Included in iostream are two important classes: istream: Declarations and methods for data input ostream: Declarations and methods for data output
A First Book of C++ 4th Edition 36
The cout Object (cont’d.)
![Page 37: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/37.jpg)
Namespace File accessed by compiler when looking for
prewritten classes or functions Sample namespace statement:
using namespace std; iostream contained in a namespace called std Compiler uses iostream’s cout object from std
whenever cout is referenced
A First Book of C++ 4th Edition 37
The cout Object (cont’d.)
![Page 38: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/38.jpg)
The cout Object (cont’d.)
A First Book of C++ 4th Edition 38
![Page 39: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/39.jpg)
Newline escape sequence (“\n”) Instructs the display device to move to a new line Caused when the characters backslash \ and n
are used together Backslash provides an “escape” from the normal
interpretation of the character that follows Newline escape sequences can be placed
anywhere within a message to cout
A First Book of C++ 4th Edition 39
The cout Object (cont’d.)
![Page 40: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/40.jpg)
The cout Object (cont’d.)
A First Book of C++ 4th Edition 40
![Page 41: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/41.jpg)
Every C++ program must contain one and only one main() function Statements included within braces { }
C++ allows flexibility in format for the word main, the parentheses ( ), and braces { } More than one statement can be put on line One statement can be written across lines
Use formatting for clarity and ease of program reading
A First Book of C++ 4th Edition 41
Programming StyleProgramming Style
![Page 42: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/42.jpg)
Function name starts in column 1 Name and parentheses on their own line
Opening brace of function body on next line Aligned with first letter of function name
Closing brace is last line of function Aligned with opening brace
Standard form highlights the function as a unit
A First Book of C++ 4th Edition 42
Programming Style (cont’d.)Programming Style (cont’d.)
![Page 43: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/43.jpg)
Within function, indent statements 2-3 spaces Creates uniform look for similar statement
groups Good programming practice
Final program form should be consistent Proper format improves program readability
and understandability
A First Book of C++ 4th Edition 43
Programming Style (cont’d.)Programming Style (cont’d.)
![Page 44: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/44.jpg)
Explanatory remarks written within program (//) Clarify purpose of the program Describe objective of a group of statements Explain function of a single line of code
Computer ignores all comments Comments exist only for convenience of reader
A well-constructed program should be readable and understandable Comments help explain unclear components
A First Book of C++ 4th Edition 44
CommentsComments
![Page 45: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/45.jpg)
Line comment Begins with two slashes(//) and continues to the end of the
line Can be written on line by itself or at the end of line that
contains program code// this is a line comment
Block comment Multiple-line comment begins with the symbols /* and ends
with the symbols *//* This is a block comment that spans three lines */
A First Book of C++ 4th Edition 45
Comments (cont’d.)Comments (cont’d.)
![Page 46: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/46.jpg)
Omitting parentheses after main() Omitting or incorrectly typing the opening brace {
Opening brace signifies start of function body Omitting or incorrectly typing the closing brace }
Closing brace signifies end of function Omitting the semicolon at the end of each
statement ; Adding a semicolon after the #include <iostream> preprocessor command
#include <iostream> ; X (wrong, no need)A First Book of C++ 4th Edition 46
Common Programming Common Programming ErrorsErrors
![Page 47: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/47.jpg)
Misspelling the name of an object or function Example: Typing “cot” instead of “cout”
Forgetting to close a string sent to cout with a double-quote symbol
cout << “Hello world Forgetting \n to indicate a new line
A First Book of C++ 4th Edition 47
Common Programming Errors Common Programming Errors (cont'd.)(cont'd.)
![Page 48: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/48.jpg)
A C++ program consists of one or more modules One module must be the function main() main() is starting point of C++ program
The simplest C++ program has the form:
#include <iostream>using namespaces std;int main(){ program statements; return 0;}
A First Book of C++ 4th Edition 48
SummarySummary
![Page 49: Csc1100 lecture01 ch01-pt1](https://reader031.fdocument.pub/reader031/viewer/2022030120/58a19fe71a28abe6468b477f/html5/thumbnails/49.jpg)
C++ statements are terminated by a semicolon ; Standard library contains many functions and
classes Standard Library provided with C++ compiler Includes <iostream> for input and output
cout object displays text or numeric results Stream of characters is sent to cout by:
Enclosing characters in double quotes “ ” Using the insertion (“put to”) operator, <<
A First Book of C++ 4th Edition 49
Summary (cont'd.)Summary (cont'd.)