ch2part1
TRANSCRIPT
![Page 1: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/1.jpg)
1
Chapter 2:
C++ FUNDAMENTALS(Part 1)
![Page 2: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/2.jpg)
DCS5088 :: Chapter 2 (part 1) 2
Objectives
• Know the differences between C and C++
• Write simple C++ programs• Know how to use the cin and cout object• Implement selection structures
![Page 3: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/3.jpg)
DCS5088 :: Chapter 2 (part 1) 3
2.1 #include<iostream> • This is a pre-processor directive for C++• Contains definitions for functions or classes• Any other header files?
Yes. Example: cmath, iomanip• Notice that the header file no longer needs to
end with a ‘dot h’
![Page 4: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/4.jpg)
DCS5088 :: Chapter 2 (part 1) 4
2.2 using namespace std• Specified after including the iostream• Std = standard• A namespace is a collection of prewritten
classes and functions or names, such as cout and cin.
• The names you use in the program will have the meaning defined for them in the std namespace.
![Page 5: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/5.jpg)
DCS5088 :: Chapter 2 (part 1) 5
2.3 cout and cin
• cout standard output stream for displaying output to screen uses the stream insertion operator, <<
• cin standard input stream get user inputuses the stream extraction operator, >>
![Page 6: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/6.jpg)
DCS5088 :: Chapter 2 (part 1) 6
2.4 A simple C++ program#include<iostream>using namespace std;int main(){ int num; cout<<"Enter a number : "; cin>>num; cout<<"The number is …"<<endl; cout<<num<<"\n"; return 0;}
![Page 7: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/7.jpg)
DCS5088 :: Chapter 2 (part 1) 7
Output:
![Page 8: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/8.jpg)
DCS5088 :: Chapter 2 (part 1) 8
2.5 Create new line
• Use endl • Use \n (string constant)
![Page 9: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/9.jpg)
DCS5088 :: Chapter 2 (part 1) 9
2.5 Create new line
#include<iostream>using namespace std;int main(){ cout<<"hello …"<<endl; cout<<"\nMy name is Ali\n"; cout<<"I’m 21 years old";
}
![Page 10: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/10.jpg)
DCS5088 :: Chapter 2 (part 1) 10
2.6 Variables and Assignments
• C++ has rules you must follow in dealing with variables.
• For instance, you must declare all your variable names in your programs, which means to list your variables and indicate what type they are.
![Page 11: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/11.jpg)
DCS5088 :: Chapter 2 (part 1) 11
Example#include<iostream>using namespace std;int main(){ int student_id; float cgpa;
student_id=10401222; cgpa=3.96 ; cout<<student_id<<" "<<cgpa<<endl; student_id=10401223; cgpa=3.67 ; cout<<student_id<<" "<<cgpa<<endl; return 0;}
![Page 12: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/12.jpg)
DCS5088 :: Chapter 2 (part 1) 12
2.7 More on Variables
• Variables are implemented as memory locations.
• The compiler assigns memory location to each variable name in the program.
• The variable name is actually a label to memory locations allocated during the creation of the variable
![Page 13: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/13.jpg)
DCS5088 :: Chapter 2 (part 1) 13
Example
int student_id; float cgpa;
74 75 76 77 78 79 7A
0012FF74 4 bytes 4 bytes0012FF78
7B 7C
Type: float intName: cgpa student_id
![Page 14: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/14.jpg)
DCS5088 :: Chapter 2 (part 1) 14
Example
student_id=10401223; cgpa=3.67 ;
74 75 76 77 78 79 7A
0012FF74 4 bytes 4 bytes0012FF78
7B 7C
Type: float intName: cgpa student_id
3.67 10401223
![Page 15: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/15.jpg)
DCS5088 :: Chapter 2 (part 1) 15
2.8 Selection Statements
• if/else: general method for selecting an action for conditional execution, linearly checks conditions and chooses first one that evaluates to TRUE.
• switch: a potentially more efficient method of selecting actions, since it can use a “jump table."
![Page 16: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/16.jpg)
DCS5088 :: Chapter 2 (part 1) 16
2.8.1 If control structure- basics• Simple if statements if (expression) statement;
• Block if statements if (expression) { executable statement 1 executable statement 2 . . . }
![Page 17: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/17.jpg)
DCS5088 :: Chapter 2 (part 1) 17
2.8.2 Simple if-elseif (expression){executable statement 1aexecutable statement 1b. . .}else{executable statement 2aexecutable statement 2b. . .}
![Page 18: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/18.jpg)
DCS5088 :: Chapter 2 (part 1) 18
2.8.3 Nested if-elseif (outer){
if (inner){. . .}else{. . .}
}
else{. . .}
![Page 19: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/19.jpg)
DCS5088 :: Chapter 2 (part 1) 19
Example#include<iostream>using namespace std;int main(){ int num;
cout<<“Enter a number : “; cin>>num;
if (num < 10) { if (num > 5)
cout<<“\nThe number is between 5 – 10”; else cout<<“\nThe number is less than 10 }
![Page 20: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/20.jpg)
DCS5088 :: Chapter 2 (part 1) 20
Example (cont…)else cout<<“\nThe number is greater than 10”;
} //end of main()
![Page 21: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/21.jpg)
DCS5088 :: Chapter 2 (part 1) 21
2.8.4 If-else-if and switch• Provide similar capabilities• When one block is executed, the others
are bypassed
![Page 22: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/22.jpg)
DCS5088 :: Chapter 2 (part 1) 22
Example (using if-else-if)#include<iostream>using namespace std;int main(){ char input;
cout<<"Enter either A, B or C: "; cin>>input;
if (input == 'A') { cout<<"\nYou’ve entered A"; }
Continue…
![Page 23: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/23.jpg)
DCS5088 :: Chapter 2 (part 1) 23
Example (using if-else-if) –(cont…)
else if (input == 'B') { cout<<"\nYou’ve entered B"; } else if (input == 'C') { cout<<"\nYou’ve entered C"; } else cout<<"\nInvalid input"; return 0;} // end of main()
![Page 24: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/24.jpg)
DCS5088 :: Chapter 2 (part 1) 24
Example (using switch)#include<iostream>using namespace std;int main(){ char input;
cout<<"Enter either A, B or C: "; cin>>input;
switch(input) { case 'A' : cout<<"\nYou’ve entered A";
break;
![Page 25: Ch2part1](https://reader035.vdocuments.mx/reader035/viewer/2022062502/577cc5981a28aba7119ccfbc/html5/thumbnails/25.jpg)
DCS5088 :: Chapter 2 (part 1) 25
case 'B' : cout<<"\nYou’ve entered B"; break; case 'C' : cout<<"\nYou’ve entered C"; break;
default : cout<<"\nInvalid input"; } //end of switch return 0;} // end of main()