Ady Wicaksono Daily Activities

Learning C/C++ part 1

with 7 comments


#include <iostream>
#include <cstdlib> // Define system()
#include <string>  // Define string
#include <fstream> // File Operation
#include <vector>  // The same with java Vector implementation
using namespace std;

// Global Variable
int globalIntOK = 1000;

string hello(){
    return "Hello TAHU\n";
}

void swap(int *a, int *b){
    int c;
    c  = *a; // C now contains memory address of a
    *a = *b; // data pointed by address memory a is now contain data pointed by memory b
    *b = c;  // data pointed by address memory b is now = c
}

void func_withnostatic(){
    int y = 10;
    cout << "y: " << y++ << endl;
}

void func_withstatic(){
    static int y = 10;
    cout << "y: " << y++ << endl;
}

void swapCPP(int& a, int& b){
    cout << "C++ SWAP -> a = " << a << endl;
    cout << "C++ SWAP -> b = " << b << endl;
    cout << "C++ SWAP address -> a = " << &a << endl;
    cout << "C++ SWAP address -> b = " << &b << endl;

    int c;
    c = a; // karena &a adalah alamat memory,
    // maka a adalah isi dari alamat memori itu
    a = b;
    b = c;
}

int main_learn1(int argc, char **argv){
    int number;
    cout << "Hello World" << endl
    << "Hello too !" << endl;
    cout << "Number 15 in decimal: " << dec << 15 << endl;
    cout << "Number 15 in octal : " << oct << 15 << endl;
    cout << "Number 15 in hex : " << hex << 15 << endl;
    cout << "Floating Point : " << 15.12 << endl;
    cout << "Char(27) is : " << char(27) << endl;
    cout << "Char concat "
    " continue 1 "
    " continue 2 " << endl;
    cout << "Enter Number: " ;
    cin >> number;
    cout << "Number: " << dec << number << endl;

    // Calling SYSTEM Program
    system("echo 'Hallo ADY'");

    // String on C++
    string s1, s2; // Empty String
    string s3 = "hello World String"; // init string
    string s4("Hello World Another string"); // init string too

    cout << "String 3: " << s3 << endl;
    cout << "String 4: " << s4 << endl;

    cout << "Just like java...concat is simple-> " << s3 + " " + s4 << endl;

    s1 = "testaja.yt";
    ifstream inFile(s1.c_str()); // ifstream -> open for reading
    if(inFile.is_open()){
        cout << "File " << s1 << " is opened" << endl;
        string buff1;
        while(getline(inFile,buff1)){ // ReadFile .. but '\n' discarded
            cout << buff1 << endl;
        }
        inFile.close();
    }else{
        cout << "File " << s1 << " is NOT opened" << endl;
    }

    ofstream outFile(s1.c_str()); // ofstream -> open for writing
    if(outFile.is_open()){
        cout << "File " << s1 << " is opened to be written" << endl;
        outFile.write(s3.c_str(),s3.length());
        outFile.close(); // Close File
    }else{
        cout << "File " << s1 << " is NOT opened to be written" << endl;
    }

    cout << "Vector" << endl;
    string x1,x2,x3;
    vector<string> vStr;

    x1 = "Data 1";
    x2 = "Data 2";
    x3 = "Data 3";
    vStr.push_back(x1); // Masukin Elemen
    vStr.push_back(x2); // Masukin Elemen
    vStr.push_back(x3); // Masukin Elemen

    // Tampilkan Elemen
    int i;
    for(i=0; i < vStr.size(); i++){
        cout << "Elemen ke-" << i+i << ": " << vStr[i] << endl;
    }

    vector<int> vInt;
    vInt.push_back(1);
    vInt.push_back(2);
    vInt.push_back(3);
    for(i=0; i < vInt.size(); i++){
        cout << "Elemen ke-" << i+i << ": " << vInt[i] << endl;
    }

    // Fungsi juga bisa dipakai lho untuk cout
    cout << "Hasil fungsi hello(): " << hello() << endl;

    // Ada boolean di sana
    bool x;
    x = true;

    cout << "Boolean x = " << x << endl;

    // Pointer tetap ada
    int y = 1231;
    cout << "Alamat memory dari y yang bernilai " << y << ", adalah " << (long)&y << endl;
    cout << "Alamat memory dari fungsi hello(), adalah " << (long)&hello << endl;

    int abc = 1000;
    int *p_abc = &abc; // p_abc adalah pointer ke integer -> diacu ke alamat abc
    cout << "ABC = " << abc << endl;
    *p_abc = 2000;
    cout << "Skrg ABC = " << abc << endl;

    // Fungsi Pointer dengan C

    char ans;
    while(true){
        cout << "Ketik y untuk melanjutkan: ";
        cin >> ans;
        if(ans == 'y'){
            break;
        }
    }
    int tahu = 100;
    int tempe = 200;
    cout << "Tahu = " << tahu << endl;
    cout << "Tempe = " << tempe << endl;
    swap(&tahu,&tempe); // Alamat memory tahu & tempe dipass ke fungsi
    cout << "SWAP(), now...\nTahu = " << tahu << endl;
    cout << "Tempe = " << tempe << endl;

    // C++ reference
    // C++ punya model baru untuk pass by reference,
    // Yakni dengan menggunakan tanda & pada parameter fungsi, bukan *
    swapCPP(tahu,tempe);
    cout << "SWAP back with C++ Reference(), now...\nTahu = " << tahu << endl;
    cout << "Tempe = " << tempe << endl;

    cout << "Now about scoping" << endl;
    { int tahutempe; // variabel ini hanya dikenali di sini
    };

    // Disini jika diuncomment akan tampil
    // : error C2065: 'tahutempe' : undeclared identifier (vc++6)
    /* cout << "Tahu Tempe variabel? " << tahutempe; */

    // Global Variable
    cout << "Global Variable globalIntOK = " << globalIntOK << endl;

    // Register Variable, process it as fast as you can :D
    // Only valid in block, you may not have global register :D
    register int test01 = 1001;
    cout << "Test01 Var as Register = " << test01 << endl;

    // Static Variables
    // Variabel yang tidak didefinisikan static dalam sebuah fungsi
    // nilainya akan diinisialisasi ulang, tp jika static maka
    // saat "return" dari fungsi nilainya akan tetap disimpan
    // Static mirip dengan global, tapi variabel static hanya berlaku
    // dalam blok fungsi dimana dia diterapkan :)
    // dikenal juga file scope, dimana static variabel yang ditaruh
    // spt penempatan variabel global -> berarti variabel ini hanya dikenali
    // di file ini saja
    for(i = 0; i < 10; i++){
        func_withnostatic();
    }
    for(i = 0; i < 10; i++){
        func_withstatic();
    }

    // Variabel extern ... waktu dilakukan linking (compile -> link)
    // diasumsikan ada diantara file2 yang linking
    /*
    When the compiler encounters the declaration ‘extern int i’, it
    knows that the definition for i must exist somewhere as a global
    variable.
    */
    // Constanta di C++
    // di C biasanya digunakan macro
    #define MY_CONST 1000
    cout << "My Const = " << MY_CONST << endl;
    // di C++ bisa digunakan const
    const int h = 121;
    cout << "Const h = " << h << endl;

    return 0;
}

int main_learn2(int argc, char **argv){
    return 0;
}

int main(int argc, char **argv){
    main_learn1(argc,argv);
    return 0;
}

Written by adywicaksono

October 5, 2007 at 10:19 pm

Posted in C/C++

7 Responses

Subscribe to comments with RSS.

  1. Ayy ur blog rocks!Subscribed🙂

    Gov

    January 25, 2008 at 12:21 pm

  2. Wonderful blog, I know this computer programming’s blog when visiting to

    http://trevorpythag.wordpress.com/2009/01/28/derive-quadratic-formula/

    Happy with you @adywicaksono.
    From me Arek Suroboyo
    Putrinya Bos ROHEDI LAboratory.

    Denaya Lesa.

    Denaya Lesa

    February 5, 2009 at 3:01 pm

    • Sopo kuwi bosRohedi?

      adywicaksono

      February 6, 2009 at 3:12 am

  3. He he he..

    Mr.Rohedi is a lecturer of physics at ITS Surabaya who found smart solver of nonlinear first order differential equation so-called SMT. If you have spare time for detail explanation of the SMT please visit to this link

    http://eqworld.ipmnet.ru/forum/viewtopic.php?f=2&t=34&start=30

    Wassalam Cak.

    Denaya Lesa.

    Denaya Lesa

    February 6, 2009 at 12:05 pm

  4. Hehehe…ketahuan niy ye, kakak Ady ternyata sekampus ITS Surabaya dengan papa aku. Denaya kagum banget pada karya kakak. Sukses ya kak.

    Denaya Lesa.

    Denaya Lesa

    March 8, 2009 at 4:32 am

  5. kunjungi blog saya n kasi komen juga dunk….
    makasih….
    http://elektrikkomp.blogspot.com/

    fuad

    August 4, 2009 at 10:24 am

  6. Communication dengan system ya…

    Ngk ada yg lebih dasar seperti contoh penggunaan class ato struc ato semacam organisasi fungsi dan data…

    Basic saya firmware programmer…

    Jackson

    July 10, 2010 at 8:43 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: