Chapter 2 Introduction to C++
description
Transcript of Chapter 2 Introduction to C++
![Page 1: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/1.jpg)
Chapter 2 Introduction to C++
Department of Computer ScienceMissouri State University
![Page 2: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/2.jpg)
Outline
The structure of C++ programs Cout Objects The #include directive Variables and constants Data Types Operations Comments Programming Style
![Page 3: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/3.jpg)
An example
// This program calculates a pay check.
#include<iostream>using namespace std;
void main( ){
int workerPayRate; float workHours, pay;
workerPayRate=12; workHours=10.5; pay=workHours workerPayRate; cout<<“A worker earns $”<<pay<<endl;
}
![Page 4: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/4.jpg)
Comments
Used to document parts of the program Intended for persons reading the source
code of the program: Indicate the purpose of the program Describe the use of variables Explain complex sections of code
Are ignored by the compiler
![Page 5: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/5.jpg)
CommentsMy:
// all students grades statistics
void main( ){
}
Linda:
// freshman grades statistics
void main( ){
}
David:
// sophomore grades statistics
void main( ){
}
Kevins:
// junior grades statistics
void main( ){
}
![Page 6: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/6.jpg)
C++ Style Comments
Begin with // through to the end of line:int length = 12; // length in inchesint width = 15; // width in inchesint area; // calculated area
// calculate rectangle areaarea = length * width;
![Page 7: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/7.jpg)
C-Style Comments
Begin with /*, end with */ Can span multiple lines:
/* this is a multi-line C-style comment*/
Can be used like C++ style comments:int area; /* calculated area */
![Page 8: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/8.jpg)
An example
// This program calculates a pay check.
#include<iostream>using namespace std;
void main( ){
int workerPayRate; float workHours, pay;
workerPayRate=12; workHours=10.5; pay=workHours workerPayRate; cout<<“A worker earns $”<<pay<<endl;
}
![Page 9: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/9.jpg)
Preprocessor Directive Part
# include <aFileName>
![Page 10: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/10.jpg)
An example
// This program calculates a pay check.
#include<iostream>using namespace std;
void main( ){
int workerPayRate; float workHours, pay;
workerPayRate=12; workHours=10.5; pay=workHours workerPayRate; cout<<“A worker earns $”<<pay<<endl;
}
![Page 11: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/11.jpg)
Namespaces
Cheek
Glass
Temple
Missouri State Campus
cout
ostream
……
C++
![Page 12: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/12.jpg)
An example
// This program calculates a pay check.
#include<iostream>using namespace std;
void main( ){
int workerPayRate; float workHours, pay;
workerPayRate=12; workHours=10.5; pay=workHours workerPayRate; cout<<“A worker earns $”<<pay<<endl;
}
![Page 13: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/13.jpg)
Procedure Part
General form of a procedure or a function
Return _Type functioName ( parameter list){
Function_body
}
![Page 14: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/14.jpg)
An example
// This program calculates a pay check.
#include<iostream>using namespace std;
void main( ){
int workerPayRate; float workHours, pay;
workerPayRate=12; workHours=10.5; pay=workHours workerPayRate; cout<<“A worker earns $”<<pay<<endl;
}
![Page 15: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/15.jpg)
Operations : Assignment
float currentTemperature;double pi;int Price;
currentTemperature =pi =Price =
Outputcout<<“Current temperature is “ <<currentTemperature<<endl;cout<<“pi=”<<pi<<endl;
![Page 16: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/16.jpg)
Arithmetic Operators
Used for performing numeric calculations C++ has unary, binary, and ternary
operators: unary (1 operand) -5 binary (2 operands) 13 - 7 ternary (3 operands) exp1 ? exp2 : exp3
![Page 17: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/17.jpg)
Arithmetic Operators
Ternary operator: conditional operatorX<0 ? Y=10 : z=20;
![Page 18: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/18.jpg)
Binary Arithmetic Operators
SYMBOL OPERATION EXAMPLE VALUE OF ans
+ addition ans = 7 + 3; 10
- subtraction ans = 7 - 3; 4
* multiplication ans = 7 * 3; 21
/ division ans = 7 / 3; 2
% modulus ans = 7 % 3; 1
![Page 19: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/19.jpg)
/ Operator
/ (division) operator performs integer division if both operands are integerscout << 13 / 5; // displays 2cout << 91 / 7; // displays 13
If either operand is floating point, the result is floating pointcout << 13 / 5.0; // displays 2.6cout << 91.0 / 7; // displays 13.0
![Page 20: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/20.jpg)
% Operator
% (modulus) operator computes the remainder resulting from integer divisioncout << 13 % 5; // displays 3
% requires integers for both operandscout << 13 % 5.0; // error
![Page 21: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/21.jpg)
An example
// This program calculates a pay check.
#include<iostream>using namespace std;
void main( ){
int workerPayRate; float workHours, pay;
workerPayRate=12; workHours=10.5; pay=workHours workerPayRate; cout<<“A worker earns $”<<pay<<endl;
}
![Page 22: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/22.jpg)
Cout ObjectExercise:
1) cout<<“You got 98 points. Cheers!”;
2) cout<<“You got ”; cout<<“98”; cout<<“ points. ”; cout<<“Cheers!”;
3) cout<<“You got ”<<“98” <<“ ponts. ”<<“Cheers!”;
4) cout<<“You got ”<<98<<“ points. Cheers!”;
5) cout<<“You got ”<<98<<“ points.”<<endl; cout<<“Cheers!”;
![Page 23: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/23.jpg)
Common escape sequences
Exercise 1
He says “I’m genius!”
Exercise 2
My hw is in the directory \upload\csc125\hl
![Page 24: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/24.jpg)
Programming Style
The visual organization of the source code Includes the use of spaces, tabs, and blank
lines Does not affect the syntax of the program Affects the readability of the source code
![Page 25: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/25.jpg)
Programming Style
// This program calculates a pay check.#include<iostream> using namespace std; void
main ( ){ int workerPayRate; float workHours, pay;workerPayRate=12;workHours=10.5; pay=workHours workerPayRate; cout<< “A worker earns $”<<pay<<endl; }
![Page 26: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/26.jpg)
Programming Style
void main( ){ float height=4, width=3.5, radius=12.1, base=9, top=5.2;
float area1, area2, area3, area4;
area1=height width;area2=3.14 radius radius;area3=height base/2;area4=(top+base) width/2;cout<<“Areas:”<<area1<<“, ”<<area2<<“, ”
<<area3<<“, ”<<area4<<endl;}
![Page 27: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/27.jpg)
Programming Style
Common elements to improve readability: Braces { } aligned vertically Indentation of statements within a set of
braces Blank lines between declaration and other
statements Long statements wrapped over multiple lines
with aligned operators
![Page 28: Chapter 2 Introduction to C++](https://reader036.fdocuments.net/reader036/viewer/2022081420/56815e43550346895dccb2cd/html5/thumbnails/28.jpg)
Standard and Prestandard C++
Older-style C++ programs:• Use .h at end of header files:#include <iostream.h>
• Do not use using namespace convention
• May not compile with a standard C++ compiler