introduction to c++ lecture 1 - suleyman demirel...

46
Instructor: Bakhyt Bakiyev Basic things

Upload: others

Post on 22-Jun-2020

15 views

Category:

Documents


1 download

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

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

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

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

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

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

// 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

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

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

#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

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

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

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

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

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

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

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

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

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

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

(= 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

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

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

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

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

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

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

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

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

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
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

FOR in sdutents’ dining-hall

FOR (SDUdent i = 1; i<=7; i++)