c++ chapter 1 introduction
DESCRIPTION
Chapter 1 to C++ IntroductionTRANSCRIPT
-
5/26/2018 C++ Chapter 1 Introduction
1/33
Chapter 1
INTRODUCTION
-
5/26/2018 C++ Chapter 1 Introduction
2/33
Welcome to C++!
-
5/26/2018 C++ Chapter 1 Introduction
3/33
A Simple C++ Program
// A first program in C++#include
int main()
{std::cout
-
5/26/2018 C++ Chapter 1 Introduction
4/33
What is C++?
Has 2 parts: Preprocessor Directives
Main function
-
5/26/2018 C++ Chapter 1 Introduction
5/33
Preprocessor Directives All directive begin with #
character E.g., #include , #define
Why preprocessor directives?
C++ defines only a small number ofoperation.
Most of the functions and symbols are
in Libraries.
-
5/26/2018 C++ Chapter 1 Introduction
6/33
Main Function
All C++ program must have main()
function. All function start with a { and end
with a } symbol.
It is the body of the function.
-
5/26/2018 C++ Chapter 1 Introduction
7/33
// Printing on one line with multiple statements
#include int main()
{
std::cout
-
5/26/2018 C++ Chapter 1 Introduction
8/33
//Printing multiple lines with a single statement
#include
int main()
{
std::cout
-
5/26/2018 C++ Chapter 1 Introduction
9/33
#include using namespace std;
int main(){
cout
-
5/26/2018 C++ Chapter 1 Introduction
10/33
#include
using namespace std;
int main()
{ int num;
cout > num;
cout
-
5/26/2018 C++ Chapter 1 Introduction
11/33
// Program that displays the sum of two numbers.#include using namespace std;
int main(){ int number1, number2,sum; // variable declarations
cout > number1; // read first integer into number1
cout > number2; // read second integer
sum = number1 + number2; // assignment of sum
cout
-
5/26/2018 C++ Chapter 1 Introduction
12/33
Memory Concepts Variable names such as number1,
number2, and sumis correspond tolocationsin the computers memory.
Every variable has a name, a type,
and avalue
-
5/26/2018 C++ Chapter 1 Introduction
13/33
Arithmetic in C++
C++ operation Arithmetic
operator
Algebraic
expression
C++
expressionAddition
Subtraction
MultiplicationDivision & integerdivision
Modulus
+-*
/
%
f + 7
p c
bm
x/y or x y
r mod s
f + 7
p c
b * m
x /y
r % s
-
5/26/2018 C++ Chapter 1 Introduction
14/33
Integer division /yields an integer result E.g. 7 / 4 = 1 17 / 5 = 3
0/4 = 0 4/0 is undefined Modulus operator %yields the remainder
after integer division.
The modulus operator is an integeroperator
Can be used only with integer operands.
E.g. 7 % 4 = 3 17 % 5 = 215%0 is undefined
-
5/26/2018 C++ Chapter 1 Introduction
15/33
Comments Are not part of the executable program
Comments are used for explaining difficult sections of code describe the program, author, date,
modification changes etc. documentation of variables and their
usage copyrighting
All comments must start with // Symbol /* and end with */ for multiplelines
-
5/26/2018 C++ Chapter 1 Introduction
16/33
E.g./*
* Programmer: William Bell* Date completed: May 9,2003* Instructor: Janet Smith
* Class: CIS61** Calculates and displays the area and* circumference of a circle*/
Use // for single line
-
5/26/2018 C++ Chapter 1 Introduction
17/33
Input Statement
Accepting data from the userthrough keyboard
Example
cin >> salary; //receive salary cin >> d >>f >>c; //receive 3 variable
-
5/26/2018 C++ Chapter 1 Introduction
18/33
Output Statement
Print result to the screen
Example cout
-
5/26/2018 C++ Chapter 1 Introduction
19/33
insert a line
cout
-
5/26/2018 C++ Chapter 1 Introduction
20/33
Escape sequence Description
\n Newline. Position the screen cursor tothe beginning of the next line
\t Horizontal tab. Move the screen cursorto the next tab stop
\r Carriage return. Position the screencursor to the beginning of the currentline
\\ Blackslash. Used to print a blackslashcharacter
\ Single quote. Used to print a single quotecharacter
\ Double quote. Used to print a doublequote character
\a Alert. Sound the s stem bell
-
5/26/2018 C++ Chapter 1 Introduction
21/33
Common Programming Errors
Debugging to find any error in the
program Types of Errors:
Syntax errors
Run-time errors
Logic errors
-
5/26/2018 C++ Chapter 1 Introduction
22/33
Syntax Errors
Code violation during compilation
Example:
Missing semicolon
Undefined variable Did not close /* with */
-
5/26/2018 C++ Chapter 1 Introduction
23/33
Run-time Errors
Illegal operation occur duringexecution
Example
Divide a number by zero Open an non existing file
Write to non existing printer
-
5/26/2018 C++ Chapter 1 Introduction
24/33
Logic Errors
Passed Syntax and Run-time Errorsbut produce false result
It is usually causes by the algorithm
of the program
-
5/26/2018 C++ Chapter 1 Introduction
25/33
I/O Manipulators Using library
setw(int n) sets width of next output valueonly to the argument.
setprecision(int n) set decimal places. Default is 6.
fixed output as decimal notation
scientific output as scientific notation
left left align . Use with setw()
right right align. Use with setw()
-
5/26/2018 C++ Chapter 1 Introduction
26/33
setw( )
E.g.,int n=1234
cout
-
5/26/2018 C++ Chapter 1 Introduction
27/33
output:
1 2 3 4
-
5/26/2018 C++ Chapter 1 Introduction
28/33
setprecision()
The precisiondetermines themaximum number of digits that shallbe output to express floating-pointvalues, counting both the digits
before and after the decimal point.
-
5/26/2018 C++ Chapter 1 Introduction
29/33
// setprecision example#include
#include using namespace std;
int main (){
double f =3.141596;cout
-
5/26/2018 C++ Chapter 1 Introduction
30/33
Output:
3.1416 // rounded up3.14
-
5/26/2018 C++ Chapter 1 Introduction
31/33
Fixed or Scientific
float n=12.345678;
cout
-
5/26/2018 C++ Chapter 1 Introduction
32/33
float n=123.2345;
cout
-
5/26/2018 C++ Chapter 1 Introduction
33/33
Left/Right
float n=123.456;
cout