introduction to c++ lecture 1 - suleyman demirel...
TRANSCRIPT
![Page 1: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/1.jpg)
Instructor: Bakhyt Bakiyev
Basic things
![Page 2: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/2.jpg)
content
Introduction: history of C and C++, and differences.
Data types
if, if – else, conditional operators,
switch
loops
![Page 3: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/3.jpg)
C is a general-purpose computer programming
language developed in 1972 by Dennis Ritchie at the Bell
Telephone Laboratories for use with the Unix operating
system.
C++ was written by Bjarne Stroustrup at Bell Labs
during 1979-1985. C++ is an extension of C. Bjarne
Stroustrup added features to C and he called it as "C with
Classes". An then the term C++ was first used in 1983.
History of C and C++
http://www.cs.bell-labs.com/who/dmr/
http://www2.research.att.com/~bs/homepage.html
![Page 4: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/4.jpg)
C and C++ difference
C++ is a general–purpose programming language with a bias
towards systems programming
is an extension of C;
supports data abstraction
supports object-oriented programming
![Page 5: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/5.jpg)
First program in C++
#include<iostream>
using namespace std;
int main()
{
cout<<”salem SDU! \n”;
return 0;
}
salem SDU!
![Page 6: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/6.jpg)
// first program in C++
This is a comment line. All lines beginning with two slash signs
(//) are considered comments and do not have any effect on the
behavior of the program. The programmer can use them to
include short explanations.
#include <iostream>
Lines beginning with a hash sign (#) are directives for the
preprocessor. #include <iostream> tells the preprocessor to
include the iostream standard file. This specific file (iostream)
and includes the declarations of the basic standard input-output
library in C++.
![Page 7: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/7.jpg)
main ()
This line corresponds to the beginning of the definition of the
main function. The main function is the point by where all C++
programs start their execution, independently of its location
within the source code. It does not matter whether there are
other functions with other names defined before or after it - the
instructions contained within this function's definition will
always be the first ones to be executed in any C++ program.
![Page 8: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/8.jpg)
Escape Sequence
\n - Newline. Position the screen cursor to the beginning of the
next line.
\t - Horizontal tab. Move the screen cursor to the next tab stop.
\r - Carriage return. Position the screen cursor to the beginning
of the current line.
![Page 9: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/9.jpg)
#include<iostream>
using namespace std;
int main()
{
// first program in C++
cout<<”Salem \n”<<“SDU !”;
return 0;
}
Salem
SDU!
![Page 10: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/10.jpg)
Memory concepts a = 5;
b = 2;
a = a + 1;
result = a - b;
Any variable names actually correspond to locations in the
computer's memory. Every variable has a name, a type, a size and
a value.
Notice, we can give any names for variables except reserved
words.
![Page 11: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/11.jpg)
The standard reserved keywords:
asm, auto, bool, break, case, catch, char, class, const, const_cast,
continue, default, delete, do, double, dynamic_cast, else, enum,
explicit, export, extern, false, float, for, friend, goto,if, inline,
int, long, mutable, namespace, new, operator, private, protected,
public, register,reinterpret_cast, return, short, signed, sizeof,
static, static_cast, struct, switch, template, this, throw, true, try,
typedef, typeid, typename, union, unsigned, using, virtual, void,
volatile, wchar_t, while.
![Page 12: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/12.jpg)
Fundamental data types Name Description Size Range
char Character. 1byte signed: -128 to 127 unsigned: 0 to 255
short int (short)
Short Integer. 2bytes signed: -32768 to 32767 unsigned: 0 to 65535
int Integer. 4bytes signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295
long int (long)
Long integer. 4bytes signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295
bool Boolean value. It can take one of two values: true or false.
1byte true or false
float Floating point number. 4bytes (~7 digits)
double Double precision floating point number.
8bytes (~15 digits)
long double Long double precision floating point number.
8bytes (~15 digits)
wchar_t Wide character. 2 or 4 bytes
1 wide character
![Page 13: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/13.jpg)
Scope of Variable A variable can be either of global or local scope.
![Page 14: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/14.jpg)
Taking an information For example:
int age;
cin >> age;
There is also usage of cin to request more than one data from the
user:
cin >> a >> b;
is equivalent to:
cin >> a;
cin >> b;
![Page 15: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/15.jpg)
Displaying an information For example:
int age=20;
cout <<age;
There is also usage of cout to display more than one data at a time:
int a=10, b=20;
cout << a << b;
is equivalent to:
cout << a;
cout<<b;
![Page 16: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/16.jpg)
Assignment (=) The assignment operator assigns a value to a variable.
a = 5;
a = 2 + (b = 5);
is equivalent to: b = 5; a = 2 + b;
![Page 17: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/17.jpg)
Arithmetic operators
C++ operation
C++ arithmetic
operator
Algebraic
expression
C++
expression
Addition + x + y x + y
Subtraction - x - y x – y
Multiplication * xy or x · y x * y
Division / x/ y
Modulus % x mod y x % y
![Page 18: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/18.jpg)
if & if-else
![Page 19: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/19.jpg)
Nested if-else
if ( x > 5 )
if ( y > 5 ) cout << "x and y are > 5";
else cout << "x is <= 5";
if ( x > 5 )
{
if ( y > 5 ) cout << "x and y are > 5";
}
else cout << "x is <= 5";
![Page 20: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/20.jpg)
(= and ==)?
![Page 21: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/21.jpg)
Bool data type Bool data types can take only 2 values true or false, false is equal to 0 and true is equa
l to 1.
![Page 22: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/22.jpg)
Char data type In C++ char data type is used for a single character. Collection of char
acters is ASCII.
Note: char c='a' works fine, but char c=“a”; is compilation error.
![Page 23: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/23.jpg)
Scope A variable can be either of global or local scope. If there is same name for global and l
ocal variable “::” keyword is used for global.
![Page 24: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/24.jpg)
Escape codes
In C++ there are escape codes, which are used to express more additional symbols
or actions. Its structure consists of “\” sign and letter.
![Page 25: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/25.jpg)
Type casting There are 2 ways:
answer = (double) (numerator) / denominator
answer = static_cast<double>(numerator) / denominator
![Page 26: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/26.jpg)
Conditional operator ? In C++ it can be used conditional operator “?” instead of if .. else structure.
Its structure is : condition ? result1 : result2;
![Page 27: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/27.jpg)
Short writing operators For example “x = x + 5;” can be written as “x += 5;”
Also for short writing code increase (++) or decrease (--) are used.
![Page 28: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/28.jpg)
pre – post
![Page 29: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/29.jpg)
Comparision
relational operator meaning_________
> x is greater than y
< x is less than y
>= x is greater than or equal to y
<= x is less than or equal to y
== x is equal to y
!= x is not equal to y
![Page 30: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/30.jpg)
![Page 31: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/31.jpg)
Logical operators
3 main logical operators in C++: “and” (&&), “or” (||) and “not” (!).
![Page 32: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/32.jpg)
Math library
![Page 33: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/33.jpg)
switch switch ( integer expression )
{
case constant 1 :
do this ;
case constant 2 :
do this ;
case constant 3 :
do this ;
default :
do this ;
}
![Page 34: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/34.jpg)
Ex: main( )
{
int i = 2 ;
switch ( i )
{
case 1 : cout<<"I am in case 1 \n" ; break ;
case 2 : cout<<"I am in case 2 \n" ; break ;
case 3 : cout<<"I am in case 3 \n" ; break ;
default : cout<<"I am in default \n";
}
}
![Page 35: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/35.jpg)
LOOPs
while
do … while
for
![Page 36: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/36.jpg)
WHILE The “while” loop's structure is : while (condition(s) is(are) true) { code }
In while loop there must be increment (decrement) operations or some checking to make condition false, otherwise it can infinite loop.
![Page 37: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/37.jpg)
DO … WHILE
The “do .. while” loop's structure is do { code } while (condition[s] is[are] true); The main difference between do/while and while loops is that sometimes in do/while it can be 1 mo
re step, because firstly it run code then checks the condition.
![Page 38: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/38.jpg)
FOR
The “for” loop's structure is: for (initialization; condition; increase)
![Page 39: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/39.jpg)
STRUCTURE
![Page 40: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/40.jpg)
FOR vs WHILE
![Page 41: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/41.jpg)
BREAK
Break command is used to finish loop and exit from loop even if the loop condition is true.
![Page 42: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/42.jpg)
CONTINUE The continue causes to pass rest of loop and go to next iteration.
![Page 43: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/43.jpg)
EXIT
The “exit” is used to terminate program even if it is not finished. It can be used when error happens in code.
![Page 44: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/44.jpg)
while (true) {
}
INFINITE LOOPS
![Page 45: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/45.jpg)
Statements with “for”
for (i=0, j=1; i<=10; i=i+2, j++) {
j = j+3; i++;
}
![Page 46: Introduction to C++ Lecture 1 - Suleyman Demirel Universityinstructor.sdu.edu.kz/~bakhyt/CPP/lectures/lecture 1.pdf · content Introduction: history of C and C++, and differences](https://reader034.vdocuments.net/reader034/viewer/2022052303/5f0abceb7e708231d42d1936/html5/thumbnails/46.jpg)
FOR in sdutents’ dining-hall
FOR (SDUdent i = 1; i<=7; i++)