Lecture 1
Introduction to Computer and
Programming Language
C++ programming Language
• Extension of C
• Support procedure-oriented and object-oriented programming
approach
• Invented by Bjarne Stroustrup in 1980
HOW COMPUTER RUN A PROGRAM
Edit : Source code ( type the program)
Compile : If no syntax errors -> Object Code
Link : Link to library function -> exe module
Run : Output ( Check for any semantic errors)
A Typical C++ Program
# include <header files> Preprocessor Directive
# define P 100
/ / A typical C++ Program Comment
using namespace std;
int main ( ) Begin
{
Variables declaration
…….. ; main function
…….. ;
Statements;
……... ;
return 0;
} End
Simple C++ Program
# include <iostream>
using namespace std;
// Aturcara ini akan mencetak sahaja (Penggunaan arahan cout)
int main ( )
{
cout << “Welcome to KUTKM \n”;
cout << “ MELAKA BANDARAYA BERSEJARAH”;
return 0;
}
Buat aturcara lengkap yang boleh mencetak output berikut
Output
Welcome to KUTKM
MELAKA BANDARAYA BERSEJARAH
Welcome to
KUTKM
MELAKA
BANDARAYA BERSEJARAH
#include <iostream>
#include <iomanip>
#define P 100
using namespace std;
int main()
{
cout << "WELCOME TO KUTKM" << setfill('$') << setw(40);
cout << "MELAKA BANDARAYA BERSEJARAH\n";
cout << " " <<P <<"\n";
cout << "SELAMAT\tMAJU\tJAYA" <<endl <<endl;
return 0;
}
Output
WELCOME TO KUTKM$$$$$$$$$$$$MELAKA BANDARAYA BERSEJARAH
100
SELAMAT MAJU JAYA
Latihan
Buat satu aturcara lengkap untuk mencetak output seperti di bawah :-
SARJANA MUDA KEJURUTERAAN ELEKTRONIK
KOLEJ UNIVERSITI TEKNIKAL
******************MELAKA*******************
Manipulator ( # include <iomanip>
1. “\n” : Advances the cursor to the beginning of the next line
e.g cout << “Mummy The Return\n\n”;
cout << “The Sixth Sense << endl;
Output: Mummy The Return
The Sixth Sense
2. endl : Advances the cursor to the beginning of the next line. Forces any
data remaining in the archive buffer to be written to the file.
Same as \n
3. “\t” : Horizontal tab
e.g. cout << “Mummy\tThe\tReturn\n\n”
Output: Mummy The Return
4. setw (n) : sets the stream’s internal field width variable to n. This setting remains
in effect only for the next insertion.
e.g cout << setw(10) << “567” << setw(30) << "Pearl Harbour";
Output ^^^^^^^567^^^^^^^^^^^^^^^^^Pearl Harbour
5. setfill (‘?’) : This parameterized manipulator sets the stream’s fill character. The
default is a space. This setting remains in effect until the next change.
e.g cout << setfill (‘*’) << setw(5) << “567” << setw(20) << “Pearl
Harbour”
Output **567*******Pearl Harbour
Contoh penyelesaian
1 #include <iostream>
using namespace std;
int main()
{
cout << "SARJANA MUDA KEJURUTERAAN ELEKTRIK" << endl;
cout << " KOLEJ UNIVERSITI TEKNIKAL\n\n";
cout << "*****************MELAKA****************";
return 0;
}
2 #include <iostream>
using namespace std;
int main()
{
cout << "SARJANA MUDA KEJURUTERAAN ELEKTRONIK\n" ;
cout << " KOLEJ UNIVERSITI TEKNIKAL";
cout << “\n\n****************MELAKA****************";
return 0;
}
3 #include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "SARJANA MUDA KEJURUTERAAN ELEKTRONIK\n";
cout << setw(30) <<"KOLEJ UNIVERSITI TEKNIKAL\n";
cout << "\n" << setfill('*') << setw(20) <<"MELAKA" << setfill('*') << setw(15) << endl;
return 0;
}
4 #include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "SARJANA MUDA KEJURUTERAAN ELEKTRIK\n";
cout << " KOLEJ UNIVERSITI TEKNIKAL\n\n";
cout << setfill('*') << setw(34) <<"MELAKA**************" << endl;
return 0;
}
Latihan
1. Buat aturcara untuk mencetak output berikut
**********
**INTRO**
**********
2. Buat satu aturcara yang akan mencetak nama dan matrik anda.
Gunakan arahan yang telah dipelajari untuk menghasilkan
output yang menarik