#oop_d_its - 3rd - migration from c to c++

37
Migration: C to C++ 26/06/2022 1 Hadziq Fabroyir - Informatics ITS

Upload: hadziq-fabroyir

Post on 21-May-2015

511 views

Category:

Health & Medicine


4 download

TRANSCRIPT

Page 1: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 1

Migration: C to C++

Page 2: #OOP_D_ITS - 3rd - Migration From C To C++

C/C++ Program Structure

int main(){

function1();

function2();

function3();

return 0;}

void function1(){

//...return;

}

void function2(){

//...return;

}

void function3(){

//...return;

}

OperatingSystem

OperatingSystem

Page 3: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 3

Naming VariableMUST

Identifier / variable name can include letters (A-z), digits (0-9), and underscore (_)

Identifier starts with letter or underscore

Do NOT use keywords as identifier

Identifier in C++ is case-sensitive

CONSIDER

Use meaningfull name

Limit identifier length up to 31 characters, although it can have length up to 2048

Avoid using identifiers that start with an underscore

Page 4: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 4

Keywords C++ Keywords

Keywords common to the C and C++ programming languages

auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

C++-only keywords

and and_eq asm bitand bitor bool catch class compl const_cast

delete dynamic_cast explicit export false

friend inline mutable namespace new not not_eq operator or or_eq

private protected public reinterpret_cast static_cast

template this throw true try typeid typename using virtual wchar_t xor xor_eq

Page 5: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 5

Declaring Variable

int value;char[] firstName;Char[] address;int 9ball;long bigInt;System::String full_name;int count!;long class;float a234_djJ_685_abc___;

Page 6: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 6

Initializing Variable

int value = 0;char[] firstName = “Budi”;long bigInt(100L);System::String^ full_name = “Budi Lagi”;

Page 7: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 7

Fundamental Data Types

Type Size (Bytes) Range of Values

bool 1 true or false

char 1 -128 to +128

signed char 1 -128 to +128

unsigned char 1 0 to 255

wchar_t 2 0 to 65535

short 2 –32,768 to +32,767

unsigned short 2 0 to 65,535

int 4 –2,147,483,648 to 2,147,483,647

unsigned int 4 0 to 4,294,967,295

long 4 –2,147,483,648 to 2,147,483,647

unsigned long 4 0 to 4,294,967,295

float 4 ±3.4×10±38

double 8 ±1.7×10±308

long double 8 ±1.7×10±308

Page 8: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 8

LiteralsType Example of Literals

char, signed char,or unsigned char

‘A’, ‘Z’, ‘8’, ‘*’

wchar_t L’A’, L’Z’, L’8’, L’*’

int -77, 65, 12345, 0x9FE

unsigned int 10U, 64000u

long -77L, 65L, 12345l

unsigned long 5UL, 999999999UL, 25ul, 35Ul

float 3.14f, 34.506F

double 1.414, 2.71828

long double 1.414L, 2.71828l

bool true, false

Page 9: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 9

Example of Data Types

int main(){

char c = 'A';wchar_t wideChar = L'9';int i = 123;long l = 10240L;float f = 3.14f;double d = 3.14;bool b = true;return 0;

}

Page 10: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 10

EnumerationsVariable with specific sets of values

Enum Day {Mon, Tues, Wed, Thurs, Fri, Sat, Sun};

Day today = Mon;

Enum Day {Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun};

Day nextDay = Tues;

Page 11: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 11

Basic Input/Output Operations

int main(){

//declare and initialize variablesint num1 = 0;int num2 = 0;

//getting input from keyboardcin >> num1 >> num2;

//output the variables value to command linecout << endl;cout << "Num1 : " << num1 << endl;cout << "Num2 : " << num2;

return 0;}

Page 12: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 12

Escape SequenceEscape Sequence What It Does

\a Sounds a beep

\n Newline

\’ Single quote

\\ Backslash

\b Backspace

\t Tab

\” Double quote

\? Question mark

Page 13: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 13

Basic Operatorsint main(){

int a = 0;int b = 0;int c = 0;

c = a + b;c = a - b;c = a * b;c = a / b;c = a % b;

a = -b;

return 0;}

Page 14: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 14

Bitwise Operators& bitwise AND

~ bitwise NOT

| bitwise OR

^ bitwise XOR

>> shift right

<< shift left

Page 15: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 15

Increment and Decrement Operators

int main(){

int a = 0;int b = 0;

a++;b--;++a;++b;

return 0;}

Page 16: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 16

Shorthand Operators

int main(){

int a = 0;int b = 0;

a += 3;b -= a;a *= 2;b /= 32;a %= b;

return 0;}

Page 17: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 17

Explicit Castingstatic_cast<the_type_to_convert_to>(expression)

(the_type_to_convert_to)expression

Page 18: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 18

Constant Declaration

int main(){ const double rollwidth = 21.0;

const double rolllength = 12.0*33.0;

const double rollarea = rollwidth*rolllength;

return 0;}

Page 19: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 19

Declaring Namespace

namespace MyNamespace{

// code belongs to myNamespace}

namespace OtherNamespace{

// code belongs to otherNamespace}

Page 20: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 20

Using Namespace#include <iostream>namespace myStuff{

int value = 0;}int main(){

std::cout << “enter an integer: “;std::cin >> myStuff::value;std::cout << “\nYou entered “ << myStuff::value << std:: endl;return 0;

}

#include <iostream>namespace myStuff{ int value = 0;}

using namespace myStuff;int main(){std::cout << “enter an integer: “;std::cin >> value;std::cout << “\nYou entered “ << value << std:: endl;return 0;}

Page 21: #OOP_D_ITS - 3rd - Migration From C To C++

Hadziq Fabroyir - Informatics ITS

Visual C++ Programming Environment

12/04/202321

HHardware

Operating System

Native C++ MFCCommon Language

Runtime (CLR)

Native C++Framework

Classes

Managed C++

ISO/ANSI C++ (unmanaged)

C++/CLI.NET Framework

Page 22: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 22

C++/CLI Data TypesType Size (Bytes) CLI Value Class

bool 1 System::Boolean

char 1 System::SByte

signed char 1 System::SByte

unsigned char 1 System::Byte

wchar_t 2 System::Char

short 2 System::Int16

unsigned short 2 System::UInt16

int 4 System::Int32

unsigned int 4 System::UInt32

long 4 System::Int32

unsigned long 4 System::UInt32

float 4 System::Single

double 8 System::Double

long double 8 System::Double

long long 8 System::Int64

unsigned long long 8 System::UInt64

Page 23: #OOP_D_ITS - 3rd - Migration From C To C++

Chapter 3 23ITC1398 Introduction to

Programming

Control Structures

Three control structures Sequence structure

Programs executed sequentially by default

Selection structuresif, if…else, switch

Repetition structureswhile, do…while, for

Page 24: #OOP_D_ITS - 3rd - Migration From C To C++

Chapter 3 24ITC1398 Introduction to

Programming

if Selection Statement

Choose among alternative courses of action

Pseudocode exampleIf student’s grade is greater than or equal to 60 print “Passed”

If the condition is truePrint statement executes, program continues to next statement

If the condition is falsePrint statement ignored, program continues

Page 25: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 25

Activity Diagram

Page 26: #OOP_D_ITS - 3rd - Migration From C To C++

Chapter 3 26ITC1398 Introduction to

Programming

if Selection Statement

Translation into C++if ( grade >= 60 ) cout << "Passed";

Any expression can be used as the condition

If it evaluates to zero, it is treated as false

If it evaluates to non-zero, it is treated as true

Page 27: #OOP_D_ITS - 3rd - Migration From C To C++

Chapter 3 27ITC1398 Introduction to

Programming

if…else Double-Selection

StatementifPerforms action if condition true

if…elsePerforms one action if condition is true, a different action if it is false

PseudocodeIf student’s grade is greater than or equal to 60 print “Passed”Else print “Failed”

C++ codeif ( grade >= 60 ) cout << "Passed";else cout << "Failed";

Page 28: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 28

Activity Diagram

Page 29: #OOP_D_ITS - 3rd - Migration From C To C++

Chapter 3 29ITC1398 Introduction to

Programming

if…else Double-Selection

StatementTernary conditional operator (?:)Three arguments (condition, value if true, value if false)

Code could be written:cout << ( grade >= 60 ? “Passed” : “Failed” );

Condition Value if true Value if false

Page 30: #OOP_D_ITS - 3rd - Migration From C To C++

Chapter 3 30ITC1398 Introduction to

Programming

if…else Double-Selection

StatementNested if…else statementsOne inside another, test for multiple cases Once a condition met, other statements are skippedExample

If student’s grade is greater than or equal to 90 Print “A” Else

If student’s grade is greater than or equal to 80 Print “B”

Else If student’s grade is greater than or equal to 70

Print “C” Else If student’s grade is greater than or equal to

60 Print “D”

Else Print “F”

Page 31: #OOP_D_ITS - 3rd - Migration From C To C++

Chapter 3 31ITC1398 Introduction to

Programming

if…else Double-Selection

StatementNested if…else statements (Cont.)

Written In C++if ( studentGrade >= 90 ) cout << "A";else if (studentGrade >= 80 ) cout << "B"; else if (studentGrade >= 70 ) cout << "C"; else if ( studentGrade >= 60 ) cout << "D"; else cout << "F";

Page 32: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 32

while Repetition StatementA repetition statement (also called a looping statement or a loop) allows the programmer to specify that a program should repeat an

action while some condition remains true. The pseudocode statement

While there are more items on my shopping list Purchase next item and cross it off my list

Page 33: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 33

for Repetition Statement

Page 34: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 34

do …while Repetition Statementdo { statement } while ( condition );

Page 35: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 35

switch Multiple-Selection Statement

Page 36: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS 36

For your practice …

Lab Session I (Ahad, 19.00-21.00)4.14

5.20

6.27

Lab Session II (Senin, 19.00-21.00)4.35

5.12

6.30

Page 37: #OOP_D_ITS - 3rd - Migration From C To C++

12/04/2023Hadziq Fabroyir - Informatics ITS

☺~ Next: OOP using C++ ~☺

[ 37 ]