Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */...
Transcript of Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */...
![Page 1: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/1.jpg)
Basics of C++
![Page 2: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/2.jpg)
Hello World Program in C++ /* * Multiple line * comment */ #include<iostream> //Single line comment using namespace std; //This is where the execution of program begins int main() { // displays Hello World! on screen cout<<"Hello World!"; return 0; } Output: Hello World!
![Page 3: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/3.jpg)
Data types in C++ are categorised in three groups: Built-in, user-defined and Derived
![Page 4: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/4.jpg)
Built in data types
![Page 5: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/5.jpg)
Declaration of variables
Int a;
Float mynumber;
Int a, b, c;
![Page 6: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/6.jpg)
// operating with variables #include <iostream> using namespace std; int main () { // declaring variables: int a, b; int result; // process: a = 5; b = 2; a = a + 1; result = a - b; // print out the result: cout << result; // terminate the program: return 0; }
![Page 7: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/7.jpg)
![Page 8: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/8.jpg)
User-defined data types
• three types of user-defined data types in C++ 1. struct 2. union 3. enum
![Page 9: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/9.jpg)
Derived data types in C++
• three types of derived-defined data types in C++ 1. Array 2. Function 3. Pointer
![Page 10: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/10.jpg)
• Types of Operators in C++
![Page 11: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/11.jpg)
Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=,^=, |=)
• Assignments operators in C++ are: =, +=, -=, *=, /=, %=
• num2 = num1 would assign value of variable num1 to the variable.
• num2+=num1 is equal to num2 = num2+num1
• num2-=num1 is equal to num2 = num2-num1
• num2*=num1 is equal to num2 = num2*num1
• num2/=num1 is equal to num2 = num2/num1
• num2%=num1 is equal to num2 = num2%num1
![Page 12: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/12.jpg)
#include <iostream> using namespace std; int main(){ int num1 = 240; int num2 = 40; num2 = num1; cout<<"= Output: "<<num2<<endl; num2 += num1; cout<<"+= Output: "<<num2<<endl; num2 -= num1; cout<<"-= Output: "<<num2<<endl; num2 *= num1; cout<<"*= Output: "<<num2<<endl; num2 /= num1; cout<<"/= Output: "<<num2<<endl; num2 %= num1; cout<<"%= Output: "<<num2<<endl; return 0; }
![Page 13: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/13.jpg)
• Output:
• = Output: 240
• += Output: 480
• -= Output: 240
• *= Output: 57600
• /= Output: 240
• %= Output: 0
![Page 14: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/14.jpg)
• Increase and decrease (++, --)
• Shortening even more some expressions, the increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Thus:
• c++;
• c+=1;
• c=c+1;
![Page 15: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/15.jpg)
Relational and equality operators ( ==, !=, >, <, >=, <= )
![Page 16: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/16.jpg)
• Conditional operator ( ? )
• 7==5 ? 4 : 3 // returns 3, since 7 is not equal to 5.
• 7==5+2 ? 4 : 3 // returns 4, since 7 is equal to 5+2.
• 5>3 ? a : b // returns the value of a, since 5 is greater than 3.
• a>b ? a : b // returns whichever is greater, a or b.
![Page 17: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/17.jpg)
• Bitwise Operators ( &, |, ^, ~, <<, >> )
• Logical operators ( !, &&, || )
![Page 18: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/18.jpg)
Introduction to strings • The C++ language library provides support for strings through the standard string
class. // my first string #include <iostream> #include <string> using namespace std; int main () { string mystring; mystring = "This is the initial string content"; cout << mystring << endl; mystring = "This is a different string content"; cout << mystring << endl; return 0; } • This is the initial string content • This is a different string content
![Page 19: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/19.jpg)
• string mystring = "This is a string";
• string mystring ("This is a string");
![Page 20: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/20.jpg)
Escape sequences
Escape sequence Meaning
\\ \ character
\' ' character
\" " character
\? ? character
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\ooo Octal number of one to three digits
\xhh . . . Hexadecimal number of one or more
digits
20
![Page 21: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/21.jpg)
Defined constants (#define)
• #define preprocessor directive
• Format : #define identifier value
For example:
• #define PI 3.14159
• #define NEWLINE '\n'
![Page 22: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/22.jpg)
Declared constants (const)
• With the const prefix you can declare constants with a specific type in the same way as you would do with a variable:
• const int pathwidth = 100;
• const char tabulator = '\t';
![Page 23: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/23.jpg)
Standard Input (cin).
• standard input in C++ is done by applying the
overloaded operator of extraction (>>) on the cin stream
Ex:
• int age;
• cin >> age;
• cin >> a >> b;
![Page 24: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/24.jpg)
// i/o example #include <iostream> using namespace std; int main () { int i; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is " << i; cout << " and its double is " << i*2 << ".\n"; return 0; }
![Page 25: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/25.jpg)
• Output
• Please enter an integer value: 702
• The value you entered is 702 and its double is
1404.
![Page 26: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/26.jpg)
Control Structures
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
Iteration structures (loops)
![Page 27: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/27.jpg)
// custom countdown using while #include <iostream> using namespace std; int main () { int n; cout << "Enter the starting number > "; cin >> n; while (n>0) { cout << n << ", "; --n; } cout << "FIRE!\n"; return 0; }
![Page 28: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/28.jpg)
• Enter the starting number > 8
8, 7, 6, 5, 4, 3, 2, 1, FIRE!
![Page 29: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/29.jpg)
// number echoer #include <iostream> using namespace std; int main () { unsigned long n; do { cout << "Enter number (0 to end): "; cin >> n; cout << "You entered: " << n << "\n"; } while (n != 0); return 0; }
![Page 30: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/30.jpg)
• Enter number (0 to end): 12345
• You entered: 12345
• Enter number (0 to end): 160277
• You entered: 160277
• Enter number (0 to end): 0
• You entered: 0
![Page 31: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/31.jpg)
// countdown using a for loop #include <iostream> using namespace std; int main () { for (int n=10; n>0; n--) { cout << n << ", "; } cout << "FIRE!\n"; return 0; }
![Page 32: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/32.jpg)
• 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
![Page 33: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/33.jpg)
Jump statements. The break statement // break loop example #include <iostream> using namespace std; int main () { int n; for (n=10; n>0; n--) { cout << n << ", "; if (n==3) { cout << "countdown aborted!"; break; } } return 0; }
![Page 34: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/34.jpg)
• 10, 9, 8, 7, 6, 5, 4, 3, countdown aborted!
![Page 35: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/35.jpg)
The continue statement
// continue loop example #include <iostream> using namespace std; int main () { for (int n=10; n>0; n--) { if (n==5) continue; cout << n << ", "; } cout << "FIRE!\n"; return 0; }
![Page 36: Basics of C++ · Basics of C++ . Hello World Program in C++ /* * Multiple line * comment */ #include //Single line comment ... • The C++ language library provides](https://reader034.fdocuments.in/reader034/viewer/2022052005/6018d5227a2faa25142f9427/html5/thumbnails/36.jpg)
• 10, 9, 8, 7, 6, 4, 3, 2, 1, FIRE!