C++ Fundamental

  • Uploaded by: hanur88
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View C++ Fundamental as PDF for free.

More details

  • Words: 809
  • Pages: 19
IXS1124-PROBLEM SOLVING ALGORITHM C++ FUNDAMENTALS

1

PROGRAM STRUCTURE • General format // Introductory comments // file name, programmer, when writtten or modified // what program does #include void main () { constant declarations variable declarations executable statements } 2

PROGRAM STRUCTURE • Example /* Filename Author Date Modified

: output.cpp : Morshidah Yazid : 12 August 2007 : 13 August 2007

This program merely prints some messages on the screen */ #include void main () { cout << “Start of program”<<endl; cout << “My first C++ program”<<endl; } 3

C++ FUNDAMENTALS • Character set – 92 characters: A – Z, a – z, 0 – 9, *, /, and etc

• Key words – Reserved words, can’t be used as identifier – Given meaning to compiler – Using lower case – Example: case, char, else, long, register, return, if, while, for, int, return, double, public

4

C++ FUNDAMENTALS • Punctuator/Separator − Example: [ ] ( ) { } , ; : • Operator Operator + * / %

Meaning Addition Subtraction Multiplication Division Modulus

5

C++ FUNDAMENTALS • Identifier – Used to define names to represent variables – Formed by combining letters (upper & lowercase), digits and underscore( _ ) – Rules – Consist only letters: A – Z, a – z, 0 – 9 or underscore symbol _ – Start with a letter – Not a reserved words – No space between words – Case sensitive

6

C++ FUNDAMENTALS – Valid identifier: matrix_no, Total, item10, length – Invalid identifier: matrix no, ringgit$, first.val, 20names – Chose identifier that reflect the significance of variable

7

C++ FUNDAMENTALS • Variable – Identifier that refers to memory location, hold values – Can only store one value at a time – The content of variable may change during program execution – General form data_type variable_name ; int

student_id 8

C++ FUNDAMENTALS • Variable – Example int num ; float cgpa; double total ; char grade ; char name [20]; long datnum ;

9

C++ FUNDAMENTALS • Variable Declaration – Example int num; //declare num as integer variable int num=10; //declare num as integer variable and sets the value to 10

– Variables having the same data type can be grouped together and declare as a single declaration statement int num1, num2, num3; int num1 = 10, num2=50, num3=7; 10

C++ FUNDAMENTALS • Variable Initialization – Once a variable is declared, contains no useful value – Must be given a value using assignment statement or input from user – Example num=10; //sets value of 10 into variable num using assignment statement cin>>number; //using input from user int num=10; //declare num as integer variable and sets the value into 10

11

C++ FUNDAMENTALS • Basic data type − int, long int − Can represent negative, zero and positive integer values − Limit of size of value that can be represented, depends on the number of bytes of storage allocated to an int variable by the computer system and compiler − Most compilers allocate two bytes for each integer: range -32768 to +32767 (long integers for outside range) − Represented exactly in computer − Example: 123 -56 0 5645

12

C++ FUNDAMENTALS • Basic data type − float, double − Can represent any real numeric value, both whole numbers and numbers that require digits after decimal point − float – ranges approximately 6 to 7 digits of precision − double – approximately 13 decimal digits of precision − Example: 16.315, -0.67, 31.567 13

C++ FUNDAMENTALS • Basic data type − bool

− Only hold the values true or false − Known as boolean variables

− char − Represent a single character, a letter, a digit or a punctuation character − Occupy one byte, giving 256 different possible character − Example: ‘+’, ‘A’, ‘a’, ‘*’, ‘7’ 14

C++ FUNDAMENTALS • C++ program • Example • Comments

/*This program is to calculate the nett salary for workers based on total hours per week and rate per hour.*/ //get input from user

15

C++ FUNDAMENTALS • C++ program • Preprocessing #include

• Data type declaration

Value unchang ed

const double KWSP = 0.05; //rate for KWSP int worker_no; //worker number float total_hour; //total hours per week float rate_hour; //rate per hour float weekly_salary; //weekly salary

16

C++ FUNDAMENTALS • C++ program • Equation weekly_salary = total_hour * rate_hour;

• Selection if (total_hour > 40) weekly_salary = (40 * rate_hour) + ( (total_hour - 40) * (rate_hour * 1.5)); else weekly_salary = total_hour * rate_hour; 17

C++ FUNDAMENTALS • Input and Output Operator − cout

cout<<"\nEnter Worker Number: "; cout<<"\nWorker Number: "<<worker_no;

− Escape Sequences Sequence \n \t \b \r \f \" \' \\

Meaning new line horizontal tab backspace carriage return form feed output a double quote character output a single quote character backslash

18

C++ FUNDAMENTALS • Input and Output Operator −cin cin>>worker_no;

19

Related Documents

C++ Fundamental
May 2020 2
Bahasa C Fundamental
November 2019 8
Fundamental
October 2019 39
Fundamental
November 2019 36
Fundamental
October 2019 43

More Documents from "(unknown)"