You are on page 1of 41

Introduction to C++

No pain, no gain. If you are not going to


put in the effort, you are not going to
get the results
Objektif
Di akhir pelajaran ini, pelajar sepatutnya dapat:
Memahami Jenis data dan pengisytiharan
Memahami fungsi output (cout) dan fungsi
input (cin)
Memahami fungsi-fungsi operator
Berupaya membina aturcara mudah.
Membina aturcara dengan
menghubungkaitkan fungsi-fungsi operator.
Introduction
C++ is basic for programming language
Need to understand syntax
To build a program you have to install
compiler (Borland, Visual C++)
Introduction
Introduction
The structure of program
comments
Preprocessor directive
Body of the program
Data types and declaration
Introduction
comments
Comments are explanation that are
included in a program for
documentation and clarification
purposes.
example
/* and */ - more then one line
// - one line
Introduction
Preprocessor Directive
#include<iostream.h>

Syntax of preprocessor directive


#include<file header>
Introduction

Kepala fail Penerangan

iostream.h Menyokong fungsi asas input dan output

string.h Menyokong penggunaan aksara

math.h Menyokong penggunaan matematik

fstream.h Menyokong penggunaan fail


Introduction
Data Types and Declarations

To add your skills and easier to build


the program, you have to understand
the following terminologies:
Identifiers (pengecam)
Identifiers are name to give to constants,
variables and functions.
Data Types and Declarations

Variables(Pembolehubah)
Variables are identifiers and the values its
hold can be change during the program is
run.
Reserved words (Kata kunci/simpanan)
Reserved words are define characters and
has a special purpose. For examples if, int,
const, else and ect..
Data Types and Declarations

Data Types Reserved words Range

Integer int -32768 to 32767

Long integer long -4294967296 to


4294967295

Short integer short -128 to 127

Unsigned integer unsigned 0 to 65535

Character char 0 to 255

Floating point float 3.4E+38 and 3.4E+38

Double Floating point double Around 12 digits


Data Types and Declarations

An example of declaration
Syntax to declare the variable.
Jenis_Data nama_pembolehubah;
Eg :
int nom1;
char gred;
float tinggi;
Data Types and Declarations

peraturan-peraturan yang perlu dipatuhi seperti:


Nama pembolehubah mesti dimulakan dengan huruf
atau underscore. Contoh int kadar, int _kadar
Nama pembolehubah bukan jenis perkataan simpanan.
Contoh int, if, float dan lain-lain lagi.
Nama pembolehubah boleh terdiri daripada gabungan
huruf, nombor dan underscore. Contoh float abc_123
Nama pembolehubah tidak boleh di jarakkan seperti int
kadar semasa.
Data Types and Declarations
Initializing variables
int x, y = 7; // pernyataan 1
float jumlah = 0.0; // pernyataan 2
char nama[4]=UPSI; // pernyataan 3
Constants
Constants are entities that appear in
the program code as fixed values.
const jenis_data nama_pemalar =
nilai_pemalar; // kaedah pertama
#define nama_pemalar nilai_pemalar
// kaedah kedua
Constants
Constants
Output Statement
What are the functions of cout? There are
three main function of cout that you have to
understand.
To display string constants (pemalar rentetan)
cout<< Selamat Datang;
To display variables or constants
cout<<luas;
To display aritmetic operations
cout<<a+b;
Input Statement
What are the function of cin? The main
function of input statement cin is to read data
from user entry through the keyboard.
Syntax for the input statement cin:
cin>>nama_pembolehubah;
Examples.
cin>>a;
cin>>a>>b;
Input Statement
Operators
There are three operators always
use in programming:
Arithmetic operators
Relational operators

Logical operators
Operators
There are seven arithmetic operators
in C++ such as
subtraction,
addition,
multiplication,
division,
modulus division,
decrement
increment.
Operators
a = 10, b = 2
Operator Tindakan Ungkapan Nilai
- Tolak ab 8
+ Tambah a+b 12
* Darab a*b 20
/ Bahagi a/b 5
% Pembahagian a%b 0
modulus
-- Pengurangan (prefix) --a 9

++ Penambahan(postfix) b++ 3
Operators
Precedence (Keutamaan)

1. Tertinggi ++, --
2. Pertengahan *, /, %
(dari kiri ke kanan)
3. Terendah +, -
(dari kiri ke kanan)
Operators
The arithmetic expression will be
evaluated based on the operator
precedence. The highest operator
precedence will be evaluated first.
Example:
2+7*5
= 2 + 35
= 37
If the level of operator precedence at
the same level, the expression will be
evaluated from left to right.
Example:
2*3/2
=6/2
=3
Parentheses can be used to force the order of
evaluation. So, the evaluation is not based on
the operator precedence.
Example:
(9 (3 + 2)) * 3
= (9 5) * 3
= 4*3
= 12
Operators
Lets consider the following example:
int a=5, b=2, c=3, d=4;
( a + c) / (d / b) * a;
Increment And Decrement
Operators
Dalam C++, ia mempunyai dua operator
istimewa iaitu penambahan dan
pengurangan. Selalunya pernyataan seperti
y=y+1
akan diringkaskan menjadi
y++
dan ia mempunyai makna yang sama. Di
samping itu kita juga boleh menulis dalam
bentuk
++ y
Increment And Decrement
Operators
Tetapi perlu diingat bahawa penambahan
hanya melibatkan satu nombor sahaja dan
tidak melibatkan nombor-nombor lain.
Manakala operator pengurangan seperti
pernyataan
y=y1
akan diringkaskan menjadi
y--
dan ia mempunyai makna yang sama. Di
samping itu kita juga boleh menulis dalam
bentuk
-- y.
Increment And Decrement
Operators
#include<iostream.h>
void main()
{
int c;
c = 5;
cout<< c << endl; //print 5
cout<< c++ << endl; //print 5 then postincrement
cout<< c << endl << endl; //print 6
c = 5;
cout<< c << endl; //print 5
cout<< ++c << endl; //preincrement then print 6
cout<< c << endl; //print 6
}
Increment And Decrement
Operators
Sebagai contoh pertimbangkan contoh
berikut:
int a,b, c;
Kita anggap nilai semasa bagi
pembolehubah a dan b adalah 10 dan
20,
c = 2 * -- a + b;
Cast function
The function cast may be used to force
an expression to be of a specific data
type.
Syntax:
(type) expression;
Example:
int b;
double a;
a = 3.4 , b = 12;
Expression Output
a = (int)(a + b) 15
a = a + (double)b 15.4
a = (int) a % b 3
a = b % (int)a 0
a = (int)a + b 15
Operator Persamaan
Kompound
Berikut adalah operator persamaan kompound.
Tambah , +=
Tolak, -=
Darab, *=
Bahagi, /=
Baki, %=
Operator persamaan kompound membolehkan
kita meringkaskan sesuatu formula. Sebagai
contoh persamaan ditulis sebagai,
x=x+5
Jadi persamaan ini boleh diringkaskan menjadi
x += 5
Relational Operators
Terdapat 6 operator hubungan dalam C++ dan sebagai contoh di beri
nilai x = 5 dan y =2.

Bil Pernyataan Operator Maksud Boolean


1 x<y < Lebih kecil Palsu(False)
2 x <= y <= Lebih kecil atau sama Palsu (False)
dengan
3 x>y > Lebih besar Benar (True)
4 x >= y >= Lebih besar atau sama Benar(True)
dengan
5 x == y == Sama dengan Palsu(False)
6 x !=y != Tidak Sama dengan Benar (True)
Logical Operators
Logical
Bil Pernyataan Operator Maksud Boolean

1 (x < y) && (y > 1) && AND Palsu


(False)
2 (x < y) && (y > 1) || OR Benar
(True)
3 !x ! NOT Palsu
(False)
Aksara Lepas (Escape sequence)
Berikut merupakan beberapa aksara lepas yang boleh digunakan
semasa menggunakan pernyataan output :

Aksara Lepas Kegunaan


\n Membawa kursor ke baris baru
\t Menggerakkan kursor ke tab mendatar
seterusnya
\r Menggerakkan kursor ke permulaan baris semasa
\a Membunyikan loceng sistem
\\ Mencetak satu aksara ( \ )
\ Mencetak satu aksara ( )

You might also like