DIKTAT MATAKULIAH

Logika Dan Algoritma / C++

Perguruan Tinggi Raharja

 

 

 

 

 

 

 

 

 

 

 

 

2011


DAFTAR ISI

DAFTAR ISI                                                                                                                              2

STRUKTUR DASAR PENULISAN PROGRAM C++                                                             3

VARIABEL & KONSTANTA                                                                                                   5

Memanfaatkan typedef untuk mendefinisikan type                                                         7

Char                                                                                                                                           7

Konstanta Enumerasi                                                                                                            8

STATEMENTS                                                                                                                           9

OPERATOR                                                                                                                               9

IF STATEMENT & RELATIONAL OPERATOR                                                                 11

Conditional Operator                                                                                                         14

FUNCTION                                                                                                                              15

Scope variable dalam Block                                                                                               18

Return Value                                                                                                                         20

Function Recursion                                                                                                              22

CLASS           23

Deklarasi private dan public                                                                                             24

Constructor dan Destructor                                                                                            25

Class sebagai File External                                                                                               27


STRUKTUR DASAR PENULISAN PROGRAM C++

File name: hello1.cpp

// Struktur penulisan dasar program C++

// Penggunaan \n sebagai linefeed

#include <iostream.h>  //untuk cout

 

int main() {

cout << “Hello World!\n”;

return 0;

}

File name: hello2.cpp

# Penggunaan endl sebagai linefeed

#include <iostream.h> //untuk cout

 

int main() {

cout << “Hello World” <<endl;

return 0;

}

File name: hello3.cpp

# Penanganan error

#include <iostream.h> //untuk cout

 

int main(){

cout << “Hello World!\n”;

return 0;

Catatan:

–          Perhatikan struktur dan kerangka penulisan program C++

–          Perhatikan bahwa pada C++ berlaku case sensitive (huruf besar dan huruf kecil adalah beda).

–          Perhatikan penggunaan \n dan endl

–          Perhatikan extension file source untuk C++ adalah   .cpp

–          Perhatikan langkah – langkah kompilasi program C++

–          Latih penangan error pada saat compile.

Coba pelajari syntax berikut, dan buat tampilan output dari program tersebut tanpa menjalankannya pada editor C++  !!!!

File name: Lat1.cpp

 

#include <iostream.h>

int main(){

int x = 5;

int y = 7;

cout “\n”;

cout << x + y << ” ” << x * y;

cout “\n”;

return 0;

}

Hasil atau tampilannya adalah:

 

 

 

 

 

 

 

 

 

 


VARIABEL & KONSTANTA

File name: var1.cpp

 

// Menampilkan kapasistas penyimpanan tiap tipe variable dalam memory

#include <iostream.h>

int main(){

cout << “The size of an int is:\t\t” << sizeof(int) << ” bytes.\n”;

cout << “The size of a short int is:\t” << sizeof(short)<< ” bytes.\n”;

cout << “The size of a long int is:\t” << sizeof(long) << ” bytes.\n”;

cout << “The size of a char is:\t\t” << sizeof(char) << ” bytes.\n”;

cout << “The size of a float is:\t\t” << sizeof(float) << ” bytes.\n”;

cout << “The size of a double is:\t” << sizeof(double) << ” bytes.\n”;

return 0;

}

File name: var2.cpp

 

// Mendeklarasikan variable

#include <iostream.h>

int main(){

// membuat var unsigned short and memberi nilai

unsigned short int Width = 5, Length; //Deklarasi variable

Length = 10;

// Area = Width x Length

unsigned short int Area  = Width * Length;

cout << “Width:” << Width << “\n”;

cout << “Length: ”  << Length << endl;

cout << “Area: ” << Area << endl;

return 0;

}

Catatan:

–          Perhatikan tipe – tipe untuk variable dalam program C++

–          Perhatikan juga ukuran penyimpanan dari tiap tipe variable dalam memory.

File name: var3.cpp

// Deklarasi variable dengan tipe unsigned short int

#include <iostream.h>

int main(){

unsigned short int smallNumber;

smallNumber = 65535;

cout << “small number:” << smallNumber << endl;

smallNumber++;

cout << “small number:” << smallNumber << endl;

smallNumber++;

cout << “small number:” << smallNumber << endl;

return 0;

}

File name: var4.cpp

// Deklarasi variable dengan tipe short int

#include <iostream.h>

int main(){

short int smallNumber;

smallNumber = 32767;

cout << “small number:” << smallNumber << endl;

smallNumber++;

cout << “small number:” << smallNumber << endl;

smallNumber++;

cout << “small number:” << smallNumber << endl;

return 0;

}

Catatan:

–          Perhatikan perbedaan tipe variable unsigned short int dalam program C++

–          Perhatikan juga ukuran penyimpanan dari tiap tipe variable dalam memory.

Memanfaatkan typedef untuk mendefinisikan type

Syntax : typedef unsigned short int USHORT

File name: var5.cpp

 

// typedef  untuk membuat tipe buatan sendiri

#include <iostream.h>

typedef unsigned short int USHORT;       //typedef defined

void main(){

USHORT  Width = 5;

USHORT Length;

Length = 10;

USHORT Area  = Width * Length;

cout << “Width:” << Width << “\n”;

cout << “Length: ”  << Length << endl;

cout << “Area: ” << Area <<endl;

}

Catatan:

–          Perhatikan pemanfaatan typedef dalam mendeklarasikan tipe buatan sendiri

Char

File name: var6.cpp

 

// variable char

#include <iostream.h>

int main(){

char chasil;

for (int i = 32; i<128; i++) //inline declaration

{

chasil = (char) i;

cout << chasil;

}

return 0;

}

Catatan:

–          Perhatikan pendeklarasian variable tipe char

–          Perhatikan deklarasi variabel inline

–          Perhatikan konversi nilai integer menjadi char

Konstanta Enumerasi

File name: var6.cpp

 

// deklarasi type enum

#include <iostream.h>

int main(){

enum Days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

Days DayOff;

int x;

cout << “What day would you like off (0-6)? “;

cin  >> x;

DayOff = Days(x);

if (DayOff == Sunday || DayOff == Saturday)

cout << “\nYou’re already off on weekends!\n”;

else

cout << “\nOkay, I’ll put in the vacation day.\n”;

return 0;

}

Catatan:

–          Perhatikan pendeklarasian variable tipe enum

–          Perhatikan dengan tipe enum kita bisa membuat tipe variable yang sudah memiliki nilai tertentu.

–          Perhatikan cara pengisian suatu nilai dalam variable tipe enum.


STATEMENTS

File name: Sta1.cpp

 

// statements

#include <iostream.h>

int main(){

int a=0, b=0, x=0, y=35;

cout << “a: ” << a << ” b: ” << b;

cout << ” x: ” << x << ” y: ” << y << endl;

a = 9;

b = 7;

y = x = a+b;

cout << “a: ” << a << ” b: ” << b;

cout << ” x: ” << x << ” y: ” << y << endl;

return 0;

}

OPERATOR

File name: Op1.cpp

 

// Penggunaan operator –

#include <iostream.h>

int main(){

unsigned int difference;

unsigned int bigNumber = 100;

unsigned int smallNumber = 50;

difference = bigNumber – smallNumber;

cout << “Difference is: ” << difference;

difference = smallNumber – bigNumber;

cout << “\nNow difference is: ” << difference <<endl;

return 0;

}

Catatan:

–          Statement setiap baris perintah dalam program

–          Pada C++ terdapat banyak operator, coba latihan dengan beberapa operator lainnya.

File name: Op2.cpp

 

// Penggunaan operator postfix (var++) dan prefix (++var)

#include <iostream.h>

int main(){

int myAge = 39;      // initialize two integers

int yourAge = 39;

cout << “I am: ” << myAge << ” years old.\n”;

cout << “You are: ” << yourAge << ” years old\n”;

myAge++;         // postfix increment

++yourAge;       // prefix increment

cout << “One year passes…\n”;

cout << “I am: ” << myAge << ” years old.\n”;

cout << “You are: ” << yourAge << ” years old\n”;

cout << “Another year passes\n”;

cout << “I am: ” << myAge++ << ” years old.\n”;

cout << “You are: ” << ++yourAge << ” years old\n”;

cout << “Let’s print it again.\n”;

cout << “I am: ” << myAge << ” years old.\n”;

cout << “You are: ” << yourAge << ” years old\n”;

return 0;

}

Catatan:

–          Perhatikan penulisan operator postfix dan prefix.

–          Buat kesimpulan, apa perbedaan antara operator postfix dan prefix

 


IF STATEMENT & RELATIONAL OPERATOR

File name: IF1.cpp

 

// Struktur penulisan flow control dengan IF statement

#include <iostream.h>

int main(){

int RedSoxScore, YankeesScore;

cout << “Enter the score for the Red Sox: “;

cin >> RedSoxScore;

cout << “\nEnter the score for the Yankees: “;

cin >> YankeesScore;

cout << “\n”;

if (RedSoxScore > YankeesScore)

cout << “Go Sox!\n”;

if (RedSoxScore < YankeesScore)

{

cout << “Go Yankees!\n”;

cout << “Happy days in New York!\n”;

}

if (RedSoxScore == YankeesScore)

{

cout << “A tie? Naah, can’t be.\n”;

cout << “Give me the real score for the Yanks: “;

cin >> YankeesScore;

if (RedSoxScore > YankeesScore)

cout << “Knew it! Go Sox!”;

if (YankeesScore > RedSoxScore)

cout << “Knew it! Go Yanks!”;

if (YankeesScore == RedSoxScore)

cout << “Wow, it really was a tie!”;

}

cout << “\nThanks for telling me.\n”;

return 0;

}

Catatan:

–          Perhatikan struktur if statement program di atas

–          Perhatikan operator relational yang digunakan pada if statement tersebut

–          Perhatikan tata penulisan if statement, hal ini berguna untuk debuging dan penelusuran pada program.

–          Perhatikan block ( {    } )dalam penulisan if statement.

File name: IF2.cpp

// Struktur penulisan flow control dengan if-else statement

#include <iostream.h>

int main(){

int firstNumber, secondNumber;

cout << “Please enter a big number: “;

cin >> firstNumber;

cout << “\nPlease enter a smaller number: “;

cin >> secondNumber;

if (firstNumber > secondNumber)

cout << “\nThanks!\n”;

else

cout << “\nOops. The second is bigger!”;

return 0;

}

File name: IF3.cpp

// Struktur penulisan flow control dengan nested if (if bersarang)

#include <iostream.h>

int main(){

int firstNumber, secondNumber;

cout << “Enter two numbers.\nFirst: “;

cin >> firstNumber;

cout << “\nSecond: “;

cin >> secondNumber;

cout << “\n\n”;

if (firstNumber >= secondNumber)

{

if ( (firstNumber % secondNumber) == 0)

{

if (firstNumber == secondNumber)

cout << “They are the same!\n”;

else

cout << “They are evenly divisible!\n”;

}

else

cout << “They are not evenly divisible!\n”;

}

else

cout << “Hey! The second one is larger!\n”;

return 0;

}

Catatan:

–          Perhatikan struktur if statement dari 2 program diatas.

–          Jalankan program dan isi kedua nilai dengan nilai yg sama, bandingkan hasil antara program IF2.cpp dan IF3.cpp

–          Mana yang lebih baik / lengkap struktur penulisannya?