Programming With C++ - Lecture 2
Programming With C++ - Lecture 2
Basic requirements
There are two basic requirements for programing with C++ (see plus
plus)
A Text Edition: To write, save and edit programming
instructions e.g. two built-in Window’s editors are Notepad,
WordPad.
A compiler: To translate the set of instructions to machine
language (binary codes). This translation is called
compilation e.g.
• Borland: Borland C++ compilers
• Microsoft: Microsoft Visual C++ compiler
• Open source (free): MinGW system of compilers (MinGW GCC)
3
Basic requirements
Integrated Development Enviroment (IDE): is a
bundle with built in text editor, compilers and other
tools for programs development e.g.
• Borland: C++ builder and Turbo C++ ( with old Borland C++
compiler)
• Microsoft: Microsoft Visual C++ (2005/2008/2010/23), the
express edition is free to use.
• Open source: Code::Blocks (with built-in MinGW GCC compiler
system and can use other compilers such as from Borland and
microsoft visual C++). It our choice of IDE for this course
4
Using Code::Blocks
Can be downloaded using the link below.
http://www.codeblocks.org/downloads/binaries
5
First Program
#include <iostream>
int main()
{
std::cout << "Hello world!\n“;
}
• The first line is Preprocessor derivative giving information regarding
the std::cout. The iostream is the library file name. Every program
requires this first line it if there is standard input/output used.
• The second line is also required, it denotes the beginning of the main
program by creating a main () function. Where int is the data type
“integer” and denote the return value after end of this main function
• There can only be one main function. A function is enclosed in { }
6
First Program
std::cout << "Hello world!\n“;
Another Program
#include <iostream>
using namespace std;
int main()
{// Prints "Hello world";
cout << "Hello world!\n";
return 0;}
• The line using namespace std; tells the compiler that
prefixes such as std:: is applied in whole program. This make a
program easy to read/write.
• // Prints "Hello world"; is the comment line. This is
removed before compilation. It is good to add explanation in
human of understanding.
• return 0; is optional line. Some compilers expect it.
8
Characters
Contain standard 26 Latin alphabets in upper and lower
cases. 0-9 Hindi-Arabic numerals.
Character are stored in computer in integer with set
codes. Most common is the ASCII (Appendix A)
\n, \t, \a characters.
String literals
Numerical values can also be part of those string
literals rather than values.
cout << “Birth date 20-03-1960”endl;
10
int main()
{int m,n;
m=9; //assign value of 9 to variable m
n=m*14; //assign value of 9*14 to variable n
cout << "m="<<m << " and n="<<n<<endl;
}
11
Program Tokens
The elements of the program are called tokens
The tokens include key words such as main, int, <<
The program use token to check the errors in the
syntax.
int main()
{//This CODE has an error
int m=9
cout << "m="<<m << endl;
}
12
Program: Input
13
The End
Reference book:
Programming with C++, SCHAUM’s outlines, John Hubbard,
3rd Edition, McGraw Hill, New Delhi