Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ fundamentals Lecture 1, Chapter 2 – pp49 -98 11/22/2018 Y K Choi.

Similar presentations


Presentation on theme: "C++ fundamentals Lecture 1, Chapter 2 – pp49 -98 11/22/2018 Y K Choi."— Presentation transcript:

1 C++ fundamentals Lecture 1, Chapter 2 – pp49 -98 11/22/2018 Y K Choi

2 Topics to Be Covered A simple C++ program Comments in program
Assigning a value in C++ Constants Names Definitions Expressions Output statements A simple application Summary 11/22/2018 Y K Choi

3 A simple program – hello world
include: instructs preprocessor to copy contents of isotream.h and string.h into the program, you can use search to find where they are. // This is the hello world #include <iostream> #include <string> using namespace std; void main() { cout << "hello world" <<endl; } Says the program will be using objects that are named in a region called std. cout: character out, note “<<“ and endl 11/22/2018 Y K Choi

4 Sample Output program Output from DOS 11/22/2018 Y K Choi

5 Comments It is a good programming practice to add comments for maintenance purpose
In c++, there are types of comments: // and /*… */ // for a single line /*….. */ for a block of lines Example: //this is the first program a++; // increment the variable a Example /* This is the first program. It involves comments */ For a block of lines 11/22/2018 Y K Choi

6 Example block Single line 11/22/2018 Y K Choi

7 Assigning a value Assigning a value to x
// This program accepts and output a value #include <iostream> #include <string> using namespace std; int main() { //display the following cout <<"Enter a value (integer)?"; int x; //defines as integer cin >> x; //input a value cout << "The ouput of three times your input is" << 3*x <<endl; return 0; } Assigning a value to x 11/22/2018 Y K Choi

8 Example Name of file output 11/22/2018 Y K Choi

9 Constants – a string constant is a sequence of zero or more characters enclosed
Use “ “ to quote a string If the string consists of “, use backslash \” Newline \n Tab \t Backspace \b Bell \a Single quote \” 11/22/2018 Y K Choi

10 Example 11/22/2018 Y K Choi

11 Constants – integer, octal, hexadecimal and long
integer: C++ reserves 4 bytes Example: Octal: with a leading zero (also 4 bytes) Example: Hexadecimal: prefix with 0x or 0X Example: 0x24 0x1F Long: with L (reserves 8 bytes) Example: 12234L 11/22/2018 Y K Choi

12 Example 11/22/2018 Y K Choi

13 Floating Point 1234 The general format is Mantissa x 10 ^ exponent 67
11/22/2018 Y K Choi

14 Names – some words are reserved as part of the language and cannot be used by programmer
Definition are: short, int, long, float, double, char Identifiers are: main, cout, endl, cin, switch, this, if, else, for, do , while, class, template, asm, auto You cannot int main //define main as the integer variable 11/22/2018 Y K Choi

15 Definitions General format Type ID, ID,,,,,; Example int x, y, z;
float speed, weight, height; With initialisation int x = 3, y = 4, z= 5; 11/22/2018 Y K Choi

16 Expressions Simple expressions int weight = 123.5;
Binary arithmetic expressions a = b + c; // add two together a = 3*6 //18 a = 12/4; //equal to 3 A = 10%3 // remainder, value is 1 Unary expression int i = 1; ~i; //unary, means 0 – 1 11/22/2018 Y K Choi

17 Output statements cout (pronounced as “C out” )
cout << (here << is the insertion operator) cout << endl; (endl is a special class of object called a manipulator which means to insert a new line and output the stream to the display (VDU) cout << “CityU”; cout << endl; Can be merged into one as follows cout <<“CItyU” <<endl; 11/22/2018 Y K Choi

18 Summary Comments // or /*….. */ Assigning a value, use cin >> m
Constants, 234 (integer), 45.6 (float) Definition, int a; float f; char c; Names: cannot use defined words such as main Expressions, + - * / and % Output statements, cout 11/22/2018 Y K Choi

19 11/22/2018 Y K Choi


Download ppt "C++ fundamentals Lecture 1, Chapter 2 – pp49 -98 11/22/2018 Y K Choi."

Similar presentations


Ads by Google