Upload
sorn-chanratha
View
259
Download
1
Embed Size (px)
DESCRIPTION
មេរៀន C++/ C plus plus
Citation preview
Chapter I
1
Lecturer: Hong Mom
Objec&ve
Create by: Hong Mom 2
A9er comple;ng this chapter, you will be able to: • Demonstrate structure of C++ • Use Input & output stream • Use comment, Data types , Operators, and Func;on Manipula;ons
Agenda
• History of C++ • Structure of C++ language • Variable Declara;on • Data types • Input & Output stream • Comment • Operators • Func;on Manipula;ons
Create by: Hong Mom 3
History of C++ • Evolved from two other language : BCPL and B • Extension of C • C++ developed by Bjarne Stroustrup at AT&T Bell Labs in the 1980s.
• Provides capabili;es for object-‐oriented programming
• Objects: reusable so9ware components – Model items in real world
• Object-‐oriented programs – Easy to understand, correct and modify
Create by: Hong Mom 4
General form of C++ program
General Form Header File Global Declara;on; int main(){ Local Declara;on; statement(s); return 0; }
Create by: Hong Mom 5
Variable • C++ variable can hold value as
– Number – Data (of other type )
• Variable are memory loca;on • Name of variable is called iden;fier Example: int age;
double num_dollars; num_dollars = 50.0;
Create by: Hong Mom 6
Variable Declara;on
A variable can be given any name, called an iden;fier in C++, provided it is within the following rules:
Create by: Hong Mom 7
S can only be constructed using leeers, numerals or underscored(_)
S Must start with a leeer or an underscore
S Cannot be a C++ keyword
S Can contain any number of characters, but only the first thirty-‐one characters are significant to the C++ compiler
Variable Declara;on
Examples
– valid identifiers: first_name, age, y1000,salary
– Invalid identifiers: 100y,*Recle,int
Create by: Hong Mom 8
Data types
Type Mem. Used Size Range Precision
int 2 bytes -‐32,768 to 32767 N/A
long (long int) 4 bytes -‐2,147,483,647 to 2,147,483,647
N/A
float 4 bytes ~ 10-‐38 to 1038 7 digits
double 8 bytes ~ 10-‐308 to 10308 15 digits
long double 10 bytes ~ 10 -‐4932 to 104932 19 digits
Create by: Hong Mom 9
Some number of types
Data type • char : holds any single character on the keyboard. • String : holds more than one character (will learn later) Example: //define variable a as integer int a; //define variable b as boolean bool b; //define variable f as double double f;
Create by: Hong Mom 10
// Program to display the memory required by C++ data types. #include<iostream.h> void main() { cout << ” Data type Size (bytes)” <<endl; cout <<” -‐ -‐ -‐ -‐ -‐ -‐ -‐ -‐-‐ -‐ -‐-‐ -‐-‐ -‐ -‐ -‐ -‐ -‐ -‐ -‐“ <<endl; cout <<” char “ << sizeof ( char ) <<endl; cout <<” int “ << sizeof (int ) <<endl; cout <<” long int “ << sizeof ( long ) <<endl; cout <<” bool “ << sizeof ( bool ) <<endl; cout <<” float “ << sizeof (float ) <<endl; cout <<” double “ << sizeof (double ) <<endl; }
Create by: Hong Mom 11
Input & Output Stream
• stream : is a transfer of informa;on in the form of a sequence of bytes
• iostream library: <iostream.h>: contains cin, cout, cerr, and clog objects • << and >> : inser;on and extrac;on operators • cin
• Standard input (keyboard) • istream object
Create by: Hong Mom 12
Example: cin>>age;
Input & Output Stream • cout
• Standard output(display screen) • ostream object
• cerr – Outputs immediately – Unbuffered
• clog – buffered-‐ prinr when buffer full or flushed
Create by: Hong Mom 13
Example: cerr<< something;
Example: clog<< something;
Example: cout<<“CIEDC”;
Input & Output Stream
Example: //test01.cpp // Output a string using cout using std::cout; void main() {
cout << “Welcome to C++ Programming!”; }
Create by: Hong Mom 14
// test02.cpp //calcula;ng the sum of two numbers //with using cin and cout #include <iostream> using std::cout; using std::cin; int main(){
int a, b; cout << “Enter two in numbers:”; cin >> a >> b; cout << “sum of “ << a “ and “ << b <<“ is:” <<(a+b) << endl; return 0;
}
Create by: Hong Mom 15
Example #include <iostream> using namespace std; int main( ) { char str[] = "Unable to read....";
cerr << "Error message : " << str << endl; return 0;
} When the above code is compiled and executed, it produces following result: Error message : Unable to read....
Create by: Hong Mom 16
Example
#include <iostream> using namespace std; int main( ) { char str[] = "Unable to read...."; clog << "Error message : " << str << endl;
return 0; } When the above code is compiled and executed, it produces following result: Error message : Unable to read....
Create by: Hong Mom 17
Comments
• Comment appear in green in C++ • Comments are explanatory notes; they are ignored by the compiler.
• Document programs • Improve program readability.
Create by: Hong Mom 18
Comments • There are two ways to include comments in a program:
– Single-‐line comment Begin with //
– Mul;-‐lines comment /*…. */ // A double slash marks the start of a //single line comment.
/* A slash followed by an asterisk marks the start of a multiple line comment. It ends with an asterisk followed by a slash. */
Create by: Hong Mom 19
Operator
Binary Operators Symbol Example
Addi;on + c = a+b
Subtrac;on -‐ c = a – b;
Mul;plica;on * c = a * b;
Division / c = a / b;
Create by: Hong Mom 20
• Arithme&c Operator
Operator
Operator Meaning a++ a = a+1; //add by 1 a9er a is used ++a a = a+1; //add by 1 before a is used a-‐-‐ a = a – 1; //sub by 1 a9er a is used -‐-‐a a = a-‐1; //sub by 1 before is used
Create by: Hong Mom 21
• Unary Operator
Operator
Example: cout << “20 divided by 3 is “ << (20/3) << endl; cout <<“With a remainder of “ << (20%3) << endl; Output:
20 divided by 3 is 6 With a remainder of 2
Create by: Hong Mom 22
• The % Operator
Combined Operators
Operators Example
+= c += a ó c = c + a
-‐= c -‐= a ó c = c -‐ a
*= c *= a ó c = c*a
/= c /= a ó c = c / a
%= c %= a ó c = c%a
Create by: Hong Mom 23
A statement such as var = var + 3 may also writen as var += 3;
Precedence Rule and Parentheses • Consider the following statement
var = 2 + 6 * 9 ; Does this mean (a) that 2 is added to 6, giving 8, which is mul;plied by 9, Giving var a value of 72 (b) that 6 is mul;plied by 9, which is added to 2, giving var a value of 56 (a) and (b) ,which one is right answer?
Create by: Hong Mom 24
Precedence Rule and Parentheses
Operator Precedence Meaning Associa&vity -‐ Highest Unary minus Right to le9 * / %
Lower Mul;plica;on, Division, and modulus
Le9 to right
+ -‐
Lowest Addi;on and subtrac;on
Le9 to right
Create by: Hong Mom 25
Operator precedence provide these rules.
Notes: you can use parentheses to change the order of evalua;on Ex: var = ( 2 + 6 ) * 9;
Manipulators
• Manipulators are used to modify input and output data stream.
• #include <iomanip> • Manipulators
-‐ endl: used in previous program to skip to the start of a new line On the screen. -‐ setw: used to set the width of a data field.
Create by: Hong Mom 26
Manipulators Example: #include<iostream> #include<iomanip> void main(){
int num1 = 111, num2 = 2345; cout << “Without setw:” << endl; cout << num1 << num2 << endl; cout << “With setw:” << endl; cout << setw(4) << num1
<< setw(7) << num2 << endl; } Create by: Hong Mom 27
Output:
Without setw:
1112345
With setw:
111 2345
Manipulators -‐ sePill: used to change the “padding” character from a space to any other character. Example: long num = 123456; cout << setw(9) << sePill(‘*’) << num << endl; cout << setw(10) << sePill(‘0’) << num << endl;
Output: ***123456 0000123456
Create by: Hong Mom 28
Manipulators
-‐ setprecision: used to specify the number of digits of a number to display. Example: double num = 123.4567; cout << num << endl; cout << setprecision(7) << num << endl; cout << fixed << setprecision (2) << num << endl;
Create by: Hong Mom 29
Output: 123.456 123.4567 123.45